c++: fix explicit/copy problem [PR109247]
[official-gcc.git] / gcc / edit-context.cc
blob6f5bc6b9d8f42e50b7e41ae797524299f10dc66f
1 /* Determining the results of applying fix-it hints.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "line-map.h"
24 #include "edit-context.h"
25 #include "pretty-print.h"
26 #include "diagnostic-color.h"
27 #include "selftest.h"
29 /* This file implements a way to track the effect of fix-its,
30 via a class edit_context; the other classes are support classes for
31 edit_context.
33 A complication here is that fix-its are expressed relative to coordinates
34 in the file when it was parsed, before any changes have been made, and
35 so if there's more that one fix-it to be applied, we have to adjust
36 later fix-its to allow for the changes made by earlier ones. This
37 is done by the various "get_effective_column" methods.
39 The "filename" params are required to outlive the edit_context (no
40 copy of the underlying str is taken, just the ptr). */
42 /* Forward decls. class edit_context is declared within edit-context.h.
43 The other types are declared here. */
44 class edit_context;
45 class edited_file;
46 class edited_line;
47 class line_event;
49 /* A struct to hold the params of a print_diff call. */
51 class diff
53 public:
54 diff (pretty_printer *pp, bool show_filenames)
55 : m_pp (pp), m_show_filenames (show_filenames) {}
57 pretty_printer *m_pp;
58 bool m_show_filenames;
61 /* The state of one named file within an edit_context: the filename,
62 and the lines that have been edited so far. */
64 class edited_file
66 public:
67 edited_file (const char *filename);
68 static void delete_cb (edited_file *file);
70 const char *get_filename () const { return m_filename; }
71 char *get_content ();
73 bool apply_fixit (int line, int start_column,
74 int next_column,
75 const char *replacement_str,
76 int replacement_len);
77 int get_effective_column (int line, int column);
79 static int call_print_diff (const char *, edited_file *file,
80 void *user_data)
82 diff *d = (diff *)user_data;
83 file->print_diff (d->m_pp, d->m_show_filenames);
84 return 0;
87 private:
88 bool print_content (pretty_printer *pp);
89 void print_diff (pretty_printer *pp, bool show_filenames);
90 int print_diff_hunk (pretty_printer *pp, int old_start_of_hunk,
91 int old_end_of_hunk, int new_start_of_hunk);
92 edited_line *get_line (int line);
93 edited_line *get_or_insert_line (int line);
94 int get_num_lines (bool *missing_trailing_newline);
96 int get_effective_line_count (int old_start_of_hunk,
97 int old_end_of_hunk);
99 void print_run_of_changed_lines (pretty_printer *pp,
100 int start_of_run,
101 int end_of_run);
103 const char *m_filename;
104 typed_splay_tree<int, edited_line *> m_edited_lines;
105 int m_num_lines;
108 /* A line added before an edited_line. */
110 class added_line
112 public:
113 added_line (const char *content, int len)
114 : m_content (xstrndup (content, len)), m_len (len) {}
115 ~added_line () { free (m_content); }
117 const char *get_content () const { return m_content; }
118 int get_len () const { return m_len; }
120 private:
121 char *m_content;
122 int m_len;
125 /* The state of one edited line within an edited_file.
126 As well as the current content of the line, it contains a record of
127 the changes, so that further changes can be applied in the correct
128 place.
130 When handling fix-it hints containing newlines, new lines are added
131 as added_line predecessors to an edited_line. Hence it's possible
132 for an "edited_line" to not actually have been changed, but to merely
133 be a placeholder for the lines added before it. This can be tested
134 for with actuall_edited_p, and has a slight effect on how diff hunks
135 are generated. */
137 class edited_line
139 public:
140 edited_line (const char *filename, int line_num);
141 ~edited_line ();
142 static void delete_cb (edited_line *el);
144 int get_line_num () const { return m_line_num; }
145 const char *get_content () const { return m_content; }
146 int get_len () const { return m_len; }
148 int get_effective_column (int orig_column) const;
149 bool apply_fixit (int start_column,
150 int next_column,
151 const char *replacement_str,
152 int replacement_len);
154 int get_effective_line_count () const;
156 /* Has the content of this line actually changed, or are we merely
157 recording predecessor added_lines? */
158 bool actually_edited_p () const { return m_line_events.length () > 0; }
160 void print_content (pretty_printer *pp) const;
161 void print_diff_lines (pretty_printer *pp) const;
163 private:
164 void ensure_capacity (int len);
165 void ensure_terminated ();
167 int m_line_num;
168 char *m_content;
169 int m_len;
170 int m_alloc_sz;
171 auto_vec <line_event> m_line_events;
172 auto_vec <added_line *> m_predecessors;
175 /* Class for representing edit events that have occurred on one line of
176 one file: the replacement of some text betweeen some columns
177 on the line.
179 Subsequent events will need their columns adjusting if they're
180 are on this line and their column is >= the start point. */
182 class line_event
184 public:
185 line_event (int start, int next, int len) : m_start (start),
186 m_delta (len - (next - start)) {}
188 int get_effective_column (int orig_column) const
190 if (orig_column >= m_start)
191 return orig_column += m_delta;
192 else
193 return orig_column;
196 private:
197 int m_start;
198 int m_delta;
201 /* Forward decls. */
203 static void
204 print_diff_line (pretty_printer *pp, char prefix_char,
205 const char *line, int line_size);
207 /* Implementation of class edit_context. */
209 /* edit_context's ctor. */
211 edit_context::edit_context ()
212 : m_valid (true),
213 m_files (strcmp, NULL, edited_file::delete_cb)
216 /* Add any fixits within RICHLOC to this context, recording the
217 changes that they make. */
219 void
220 edit_context::add_fixits (rich_location *richloc)
222 if (!m_valid)
223 return;
224 if (richloc->seen_impossible_fixit_p ())
226 m_valid = false;
227 return;
229 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
231 const fixit_hint *hint = richloc->get_fixit_hint (i);
232 if (!apply_fixit (hint))
233 m_valid = false;
237 /* Get the content of the given file, with fix-its applied.
238 If any errors occurred in this edit_context, return NULL.
239 The ptr should be freed by the caller. */
241 char *
242 edit_context::get_content (const char *filename)
244 if (!m_valid)
245 return NULL;
246 edited_file &file = get_or_insert_file (filename);
247 return file.get_content ();
250 /* Map a location before the edits to a column number after the edits.
251 This method is for the selftests. */
254 edit_context::get_effective_column (const char *filename, int line,
255 int column)
257 edited_file *file = get_file (filename);
258 if (!file)
259 return column;
260 return file->get_effective_column (line, column);
263 /* Generate a unified diff. The resulting string should be freed by the
264 caller. Primarily for selftests.
265 If any errors occurred in this edit_context, return NULL. */
267 char *
268 edit_context::generate_diff (bool show_filenames)
270 if (!m_valid)
271 return NULL;
273 pretty_printer pp;
274 print_diff (&pp, show_filenames);
275 return xstrdup (pp_formatted_text (&pp));
278 /* Print a unified diff to PP, showing the changes made within the
279 context. */
281 void
282 edit_context::print_diff (pretty_printer *pp, bool show_filenames)
284 if (!m_valid)
285 return;
286 diff d (pp, show_filenames);
287 m_files.foreach (edited_file::call_print_diff, &d);
290 /* Attempt to apply the given fixit. Return true if it can be
291 applied, or false otherwise. */
293 bool
294 edit_context::apply_fixit (const fixit_hint *hint)
296 expanded_location start = expand_location (hint->get_start_loc ());
297 expanded_location next_loc = expand_location (hint->get_next_loc ());
298 if (start.file != next_loc.file)
299 return false;
300 if (start.line != next_loc.line)
301 return false;
302 if (start.column == 0)
303 return false;
304 if (next_loc.column == 0)
305 return false;
307 edited_file &file = get_or_insert_file (start.file);
308 if (!m_valid)
309 return false;
310 return file.apply_fixit (start.line, start.column, next_loc.column,
311 hint->get_string (),
312 hint->get_length ());
315 /* Locate the edited_file * for FILENAME, if any
316 Return NULL if there isn't one. */
318 edited_file *
319 edit_context::get_file (const char *filename)
321 gcc_assert (filename);
322 return m_files.lookup (filename);
325 /* Locate the edited_file for FILENAME, adding one if there isn't one. */
327 edited_file &
328 edit_context::get_or_insert_file (const char *filename)
330 gcc_assert (filename);
332 edited_file *file = get_file (filename);
333 if (file)
334 return *file;
336 /* Not found. */
337 file = new edited_file (filename);
338 m_files.insert (filename, file);
339 return *file;
342 /* Implementation of class edited_file. */
344 /* Callback for m_edited_lines, for comparing line numbers. */
346 static int line_comparator (int a, int b)
348 return a - b;
351 /* edited_file's constructor. */
353 edited_file::edited_file (const char *filename)
354 : m_filename (filename),
355 m_edited_lines (line_comparator, NULL, edited_line::delete_cb),
356 m_num_lines (-1)
360 /* A callback for deleting edited_file *, for use as a
361 delete_value_fn for edit_context::m_files. */
363 void
364 edited_file::delete_cb (edited_file *file)
366 delete file;
369 /* Get the content of the file, with fix-its applied.
370 The ptr should be freed by the caller. */
372 char *
373 edited_file::get_content ()
375 pretty_printer pp;
376 if (!print_content (&pp))
377 return NULL;
378 return xstrdup (pp_formatted_text (&pp));
381 /* Attempt to replace columns START_COLUMN up to but not including NEXT_COLUMN
382 of LINE with the string REPLACEMENT_STR of length REPLACEMENT_LEN,
383 updating the in-memory copy of the line, and the record of edits to
384 the line. */
386 bool
387 edited_file::apply_fixit (int line, int start_column, int next_column,
388 const char *replacement_str,
389 int replacement_len)
391 edited_line *el = get_or_insert_line (line);
392 if (!el)
393 return false;
394 return el->apply_fixit (start_column, next_column, replacement_str,
395 replacement_len);
398 /* Given line LINE, map from COLUMN in the input file to its current
399 column after edits have been applied. */
402 edited_file::get_effective_column (int line, int column)
404 const edited_line *el = get_line (line);
405 if (!el)
406 return column;
407 return el->get_effective_column (column);
410 /* Attempt to print the content of the file to PP, with edits applied.
411 Return true if successful, false otherwise. */
413 bool
414 edited_file::print_content (pretty_printer *pp)
416 bool missing_trailing_newline;
417 int line_count = get_num_lines (&missing_trailing_newline);
418 for (int line_num = 1; line_num <= line_count; line_num++)
420 edited_line *el = get_line (line_num);
421 if (el)
422 el->print_content (pp);
423 else
425 char_span line = location_get_source_line (m_filename, line_num);
426 if (!line)
427 return false;
428 for (size_t i = 0; i < line.length (); i++)
429 pp_character (pp, line[i]);
431 if (line_num < line_count)
432 pp_character (pp, '\n');
435 if (!missing_trailing_newline)
436 pp_character (pp, '\n');
438 return true;
441 /* Print a unified diff to PP, showing any changes that have occurred
442 to this file. */
444 void
445 edited_file::print_diff (pretty_printer *pp, bool show_filenames)
447 if (show_filenames)
449 pp_string (pp, colorize_start (pp_show_color (pp), "diff-filename"));
450 /* Avoid -Wformat-diag in non-diagnostic output. */
451 pp_string (pp, "--- ");
452 pp_string (pp, m_filename);
453 pp_newline (pp);
454 pp_string (pp, "+++ ");
455 pp_string (pp, m_filename);
456 pp_newline (pp);
457 pp_string (pp, colorize_stop (pp_show_color (pp)));
460 edited_line *el = m_edited_lines.min ();
462 bool missing_trailing_newline;
463 int line_count = get_num_lines (&missing_trailing_newline);
465 const int context_lines = 3;
467 /* Track new line numbers minus old line numbers. */
469 int line_delta = 0;
471 while (el)
473 int start_of_hunk = el->get_line_num ();
474 start_of_hunk -= context_lines;
475 if (start_of_hunk < 1)
476 start_of_hunk = 1;
478 /* Locate end of hunk, merging in changed lines
479 that are sufficiently close. */
480 while (true)
482 edited_line *next_el
483 = m_edited_lines.successor (el->get_line_num ());
484 if (!next_el)
485 break;
487 int end_of_printed_hunk = el->get_line_num () + context_lines;
488 if (!el->actually_edited_p ())
489 end_of_printed_hunk--;
491 if (end_of_printed_hunk
492 >= next_el->get_line_num () - context_lines)
493 el = next_el;
494 else
495 break;
498 int end_of_hunk = el->get_line_num ();
499 end_of_hunk += context_lines;
500 if (!el->actually_edited_p ())
501 end_of_hunk--;
502 if (end_of_hunk > line_count)
503 end_of_hunk = line_count;
505 int new_start_of_hunk = start_of_hunk + line_delta;
506 line_delta += print_diff_hunk (pp, start_of_hunk, end_of_hunk,
507 new_start_of_hunk);
508 el = m_edited_lines.successor (el->get_line_num ());
512 /* Print one hunk within a unified diff to PP, covering the
513 given range of lines. OLD_START_OF_HUNK and OLD_END_OF_HUNK are
514 line numbers in the unedited version of the file.
515 NEW_START_OF_HUNK is a line number in the edited version of the file.
516 Return the change in the line count within the hunk. */
519 edited_file::print_diff_hunk (pretty_printer *pp, int old_start_of_hunk,
520 int old_end_of_hunk, int new_start_of_hunk)
522 int old_num_lines = old_end_of_hunk - old_start_of_hunk + 1;
523 int new_num_lines
524 = get_effective_line_count (old_start_of_hunk, old_end_of_hunk);
526 pp_string (pp, colorize_start (pp_show_color (pp), "diff-hunk"));
527 pp_printf (pp, "%s -%i,%i +%i,%i %s",
528 "@@", old_start_of_hunk, old_num_lines,
529 new_start_of_hunk, new_num_lines, "@@\n");
530 pp_string (pp, colorize_stop (pp_show_color (pp)));
532 int line_num = old_start_of_hunk;
533 while (line_num <= old_end_of_hunk)
535 edited_line *el = get_line (line_num);
536 if (el)
538 /* We have an edited line.
539 Consolidate into runs of changed lines. */
540 const int first_changed_line_in_run = line_num;
541 while (get_line (line_num))
542 line_num++;
543 const int last_changed_line_in_run = line_num - 1;
544 print_run_of_changed_lines (pp, first_changed_line_in_run,
545 last_changed_line_in_run);
547 else
549 /* Unchanged line. */
550 char_span old_line = location_get_source_line (m_filename, line_num);
551 print_diff_line (pp, ' ', old_line.get_buffer (), old_line.length ());
552 line_num++;
556 return new_num_lines - old_num_lines;
559 /* Subroutine of edited_file::print_diff_hunk: given a run of lines
560 from START_OF_RUN to END_OF_RUN that all have edited_line instances,
561 print the diff to PP. */
563 void
564 edited_file::print_run_of_changed_lines (pretty_printer *pp,
565 int start_of_run,
566 int end_of_run)
568 /* Show old version of lines. */
569 pp_string (pp, colorize_start (pp_show_color (pp),
570 "diff-delete"));
571 for (int line_num = start_of_run;
572 line_num <= end_of_run;
573 line_num++)
575 edited_line *el_in_run = get_line (line_num);
576 gcc_assert (el_in_run);
577 if (el_in_run->actually_edited_p ())
579 char_span old_line = location_get_source_line (m_filename, line_num);
580 print_diff_line (pp, '-', old_line.get_buffer (),
581 old_line.length ());
584 pp_string (pp, colorize_stop (pp_show_color (pp)));
586 /* Show new version of lines. */
587 pp_string (pp, colorize_start (pp_show_color (pp),
588 "diff-insert"));
589 for (int line_num = start_of_run;
590 line_num <= end_of_run;
591 line_num++)
593 edited_line *el_in_run = get_line (line_num);
594 gcc_assert (el_in_run);
595 el_in_run->print_diff_lines (pp);
597 pp_string (pp, colorize_stop (pp_show_color (pp)));
600 /* Print one line within a diff, starting with PREFIX_CHAR,
601 followed by the LINE of content, of length LEN. LINE is
602 not necessarily 0-terminated. Print a trailing newline. */
604 static void
605 print_diff_line (pretty_printer *pp, char prefix_char,
606 const char *line, int len)
608 pp_character (pp, prefix_char);
609 for (int i = 0; i < len; i++)
610 pp_character (pp, line[i]);
611 pp_character (pp, '\n');
614 /* Determine the number of lines that will be present after
615 editing for the range of lines from OLD_START_OF_HUNK to
616 OLD_END_OF_HUNK inclusive. */
619 edited_file::get_effective_line_count (int old_start_of_hunk,
620 int old_end_of_hunk)
622 int line_count = 0;
623 for (int old_line_num = old_start_of_hunk; old_line_num <= old_end_of_hunk;
624 old_line_num++)
626 edited_line *el = get_line (old_line_num);
627 if (el)
628 line_count += el->get_effective_line_count ();
629 else
630 line_count++;
632 return line_count;
635 /* Get the state of LINE within the file, or NULL if it is untouched. */
637 edited_line *
638 edited_file::get_line (int line)
640 return m_edited_lines.lookup (line);
643 /* Get the state of LINE within the file, creating a state for it
644 if necessary. Return NULL if an error occurs. */
646 edited_line *
647 edited_file::get_or_insert_line (int line)
649 edited_line *el = get_line (line);
650 if (el)
651 return el;
652 el = new edited_line (m_filename, line);
653 if (el->get_content () == NULL)
655 delete el;
656 return NULL;
658 m_edited_lines.insert (line, el);
659 return el;
662 /* Get the total number of lines in m_content, writing
663 true to *MISSING_TRAILING_NEWLINE if the final line
664 if missing a newline, false otherwise. */
667 edited_file::get_num_lines (bool *missing_trailing_newline)
669 gcc_assert (missing_trailing_newline);
670 if (m_num_lines == -1)
672 m_num_lines = 0;
673 while (true)
675 char_span line
676 = location_get_source_line (m_filename, m_num_lines + 1);
677 if (line)
678 m_num_lines++;
679 else
680 break;
683 *missing_trailing_newline = location_missing_trailing_newline (m_filename);
684 return m_num_lines;
687 /* Implementation of class edited_line. */
689 /* edited_line's ctor. */
691 edited_line::edited_line (const char *filename, int line_num)
692 : m_line_num (line_num),
693 m_content (NULL), m_len (0), m_alloc_sz (0),
694 m_line_events (),
695 m_predecessors ()
697 char_span line = location_get_source_line (filename, line_num);
698 if (!line)
699 return;
700 m_len = line.length ();
701 ensure_capacity (m_len);
702 memcpy (m_content, line.get_buffer (), m_len);
703 ensure_terminated ();
706 /* edited_line's dtor. */
708 edited_line::~edited_line ()
710 unsigned i;
711 added_line *pred;
713 free (m_content);
714 FOR_EACH_VEC_ELT (m_predecessors, i, pred)
715 delete pred;
718 /* A callback for deleting edited_line *, for use as a
719 delete_value_fn for edited_file::m_edited_lines. */
721 void
722 edited_line::delete_cb (edited_line *el)
724 delete el;
727 /* Map a location before the edits to a column number after the edits,
728 within a specific line. */
731 edited_line::get_effective_column (int orig_column) const
733 int i;
734 line_event *event;
735 FOR_EACH_VEC_ELT (m_line_events, i, event)
736 orig_column = event->get_effective_column (orig_column);
737 return orig_column;
740 /* Attempt to replace columns START_COLUMN up to but not including
741 NEXT_COLUMN of the line with the string REPLACEMENT_STR of
742 length REPLACEMENT_LEN, updating the in-memory copy of the line,
743 and the record of edits to the line.
744 Return true if successful; false if an error occurred. */
746 bool
747 edited_line::apply_fixit (int start_column,
748 int next_column,
749 const char *replacement_str,
750 int replacement_len)
752 /* Handle newlines. They will only ever be at the end of the
753 replacement text, thanks to the filtering in rich_location. */
754 if (replacement_len > 1)
755 if (replacement_str[replacement_len - 1] == '\n')
757 /* Stash in m_predecessors, stripping off newline. */
758 m_predecessors.safe_push (new added_line (replacement_str,
759 replacement_len - 1));
760 return true;
763 start_column = get_effective_column (start_column);
764 next_column = get_effective_column (next_column);
766 int start_offset = start_column - 1;
767 int next_offset = next_column - 1;
769 gcc_assert (start_offset >= 0);
770 gcc_assert (next_offset >= 0);
772 if (start_column > next_column)
773 return false;
774 if (start_offset >= (m_len + 1))
775 return false;
776 if (next_offset >= (m_len + 1))
777 return false;
779 size_t victim_len = next_offset - start_offset;
781 /* Ensure buffer is big enough. */
782 size_t new_len = m_len + replacement_len - victim_len;
783 ensure_capacity (new_len);
785 char *suffix = m_content + next_offset;
786 gcc_assert (suffix <= m_content + m_len);
787 size_t len_suffix = (m_content + m_len) - suffix;
789 /* Move successor content into position. They overlap, so use memmove. */
790 memmove (m_content + start_offset + replacement_len,
791 suffix, len_suffix);
793 /* Replace target content. They don't overlap, so use memcpy. */
794 memcpy (m_content + start_offset,
795 replacement_str,
796 replacement_len);
798 m_len = new_len;
800 ensure_terminated ();
802 /* Record the replacement, so that future changes to the line can have
803 their column information adjusted accordingly. */
804 m_line_events.safe_push (line_event (start_column, next_column,
805 replacement_len));
806 return true;
809 /* Determine the number of lines that will be present after
810 editing for this line. Typically this is just 1, but
811 if newlines have been added before this line, they will
812 also be counted. */
815 edited_line::get_effective_line_count () const
817 return m_predecessors.length () + 1;
820 /* Subroutine of edited_file::print_content.
821 Print this line and any new lines added before it, to PP. */
823 void
824 edited_line::print_content (pretty_printer *pp) const
826 unsigned i;
827 added_line *pred;
828 FOR_EACH_VEC_ELT (m_predecessors, i, pred)
830 pp_string (pp, pred->get_content ());
831 pp_newline (pp);
833 pp_string (pp, m_content);
836 /* Subroutine of edited_file::print_run_of_changed_lines for
837 printing diff hunks to PP.
838 Print the '+' line for this line, and any newlines added
839 before it.
840 Note that if this edited_line was actually edited, the '-'
841 line has already been printed. If it wasn't, then we merely
842 have a placeholder edited_line for adding newlines to, and
843 we need to print a ' ' line for the edited_line as we haven't
844 printed it yet. */
846 void
847 edited_line::print_diff_lines (pretty_printer *pp) const
849 unsigned i;
850 added_line *pred;
851 FOR_EACH_VEC_ELT (m_predecessors, i, pred)
852 print_diff_line (pp, '+', pred->get_content (),
853 pred->get_len ());
854 if (actually_edited_p ())
855 print_diff_line (pp, '+', m_content, m_len);
856 else
857 print_diff_line (pp, ' ', m_content, m_len);
860 /* Ensure that the buffer for m_content is at least large enough to hold
861 a string of length LEN and its 0-terminator, doubling on repeated
862 allocations. */
864 void
865 edited_line::ensure_capacity (int len)
867 /* Allow 1 extra byte for 0-termination. */
868 if (m_alloc_sz < (len + 1))
870 size_t new_alloc_sz = (len + 1) * 2;
871 m_content = (char *)xrealloc (m_content, new_alloc_sz);
872 m_alloc_sz = new_alloc_sz;
876 /* Ensure that m_content is 0-terminated. */
878 void
879 edited_line::ensure_terminated ()
881 /* 0-terminate the buffer. */
882 gcc_assert (m_len < m_alloc_sz);
883 m_content[m_len] = '\0';
886 #if CHECKING_P
888 /* Selftests of code-editing. */
890 namespace selftest {
892 /* A wrapper class for ensuring that the underlying pointer is freed. */
894 template <typename POINTER_T>
895 class auto_free
897 public:
898 auto_free (POINTER_T p) : m_ptr (p) {}
899 ~auto_free () { free (m_ptr); }
901 operator POINTER_T () { return m_ptr; }
903 private:
904 POINTER_T m_ptr;
907 /* Verify that edit_context::get_content works for unedited files. */
909 static void
910 test_get_content ()
912 /* Test of empty file. */
914 const char *content = ("");
915 temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
916 edit_context edit;
917 auto_free <char *> result = edit.get_content (tmp.get_filename ());
918 ASSERT_STREQ ("", result);
921 /* Test of simple content. */
923 const char *content = ("/* before */\n"
924 "foo = bar.field;\n"
925 "/* after */\n");
926 temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
927 edit_context edit;
928 auto_free <char *> result = edit.get_content (tmp.get_filename ());
929 ASSERT_STREQ ("/* before */\n"
930 "foo = bar.field;\n"
931 "/* after */\n", result);
934 /* Test of omitting the trailing newline on the final line. */
936 const char *content = ("/* before */\n"
937 "foo = bar.field;\n"
938 "/* after */");
939 temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
940 edit_context edit;
941 auto_free <char *> result = edit.get_content (tmp.get_filename ());
942 /* We should respect the omitted trailing newline. */
943 ASSERT_STREQ ("/* before */\n"
944 "foo = bar.field;\n"
945 "/* after */", result);
949 /* Test applying an "insert" fixit, using insert_before. */
951 static void
952 test_applying_fixits_insert_before (const line_table_case &case_)
954 /* Create a tempfile and write some text to it.
955 .........................0000000001111111.
956 .........................1234567890123456. */
957 const char *old_content = ("/* before */\n"
958 "foo = bar.field;\n"
959 "/* after */\n");
960 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
961 const char *filename = tmp.get_filename ();
962 line_table_test ltt (case_);
963 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 2);
965 /* Add a comment in front of "bar.field". */
966 location_t start = linemap_position_for_column (line_table, 7);
967 rich_location richloc (line_table, start);
968 richloc.add_fixit_insert_before ("/* inserted */");
970 if (start > LINE_MAP_MAX_LOCATION_WITH_COLS)
971 return;
973 edit_context edit;
974 edit.add_fixits (&richloc);
975 auto_free <char *> new_content = edit.get_content (filename);
976 if (start <= LINE_MAP_MAX_LOCATION_WITH_COLS)
977 ASSERT_STREQ ("/* before */\n"
978 "foo = /* inserted */bar.field;\n"
979 "/* after */\n", new_content);
981 /* Verify that locations on other lines aren't affected by the change. */
982 ASSERT_EQ (100, edit.get_effective_column (filename, 1, 100));
983 ASSERT_EQ (100, edit.get_effective_column (filename, 3, 100));
985 /* Verify locations on the line before the change. */
986 ASSERT_EQ (1, edit.get_effective_column (filename, 2, 1));
987 ASSERT_EQ (6, edit.get_effective_column (filename, 2, 6));
989 /* Verify locations on the line at and after the change. */
990 ASSERT_EQ (21, edit.get_effective_column (filename, 2, 7));
991 ASSERT_EQ (22, edit.get_effective_column (filename, 2, 8));
993 /* Verify diff. */
994 auto_free <char *> diff = edit.generate_diff (false);
995 ASSERT_STREQ ("@@ -1,3 +1,3 @@\n"
996 " /* before */\n"
997 "-foo = bar.field;\n"
998 "+foo = /* inserted */bar.field;\n"
999 " /* after */\n", diff);
1002 /* Test applying an "insert" fixit, using insert_after, with
1003 a range of length > 1 (to ensure that the end-point of
1004 the input range is used). */
1006 static void
1007 test_applying_fixits_insert_after (const line_table_case &case_)
1009 /* Create a tempfile and write some text to it.
1010 .........................0000000001111111.
1011 .........................1234567890123456. */
1012 const char *old_content = ("/* before */\n"
1013 "foo = bar.field;\n"
1014 "/* after */\n");
1015 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1016 const char *filename = tmp.get_filename ();
1017 line_table_test ltt (case_);
1018 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 2);
1020 /* Add a comment after "field". */
1021 location_t start = linemap_position_for_column (line_table, 11);
1022 location_t finish = linemap_position_for_column (line_table, 15);
1023 location_t field = make_location (start, start, finish);
1024 rich_location richloc (line_table, field);
1025 richloc.add_fixit_insert_after ("/* inserted */");
1027 if (finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
1028 return;
1030 /* Verify that the text was inserted after the end of "field". */
1031 edit_context edit;
1032 edit.add_fixits (&richloc);
1033 auto_free <char *> new_content = edit.get_content (filename);
1034 ASSERT_STREQ ("/* before */\n"
1035 "foo = bar.field/* inserted */;\n"
1036 "/* after */\n", new_content);
1038 /* Verify diff. */
1039 auto_free <char *> diff = edit.generate_diff (false);
1040 ASSERT_STREQ ("@@ -1,3 +1,3 @@\n"
1041 " /* before */\n"
1042 "-foo = bar.field;\n"
1043 "+foo = bar.field/* inserted */;\n"
1044 " /* after */\n", diff);
1047 /* Test applying an "insert" fixit, using insert_after at the end of
1048 a line (contrast with test_applying_fixits_insert_after_failure
1049 below). */
1051 static void
1052 test_applying_fixits_insert_after_at_line_end (const line_table_case &case_)
1054 /* Create a tempfile and write some text to it.
1055 .........................0000000001111111.
1056 .........................1234567890123456. */
1057 const char *old_content = ("/* before */\n"
1058 "foo = bar.field;\n"
1059 "/* after */\n");
1060 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1061 const char *filename = tmp.get_filename ();
1062 line_table_test ltt (case_);
1063 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 2);
1065 /* Add a comment after the semicolon. */
1066 location_t loc = linemap_position_for_column (line_table, 16);
1067 rich_location richloc (line_table, loc);
1068 richloc.add_fixit_insert_after ("/* inserted */");
1070 if (loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
1071 return;
1073 edit_context edit;
1074 edit.add_fixits (&richloc);
1075 auto_free <char *> new_content = edit.get_content (filename);
1076 ASSERT_STREQ ("/* before */\n"
1077 "foo = bar.field;/* inserted */\n"
1078 "/* after */\n", new_content);
1080 /* Verify diff. */
1081 auto_free <char *> diff = edit.generate_diff (false);
1082 ASSERT_STREQ ("@@ -1,3 +1,3 @@\n"
1083 " /* before */\n"
1084 "-foo = bar.field;\n"
1085 "+foo = bar.field;/* inserted */\n"
1086 " /* after */\n", diff);
1089 /* Test of a failed attempt to apply an "insert" fixit, using insert_after,
1090 due to the relevant linemap ending. Contrast with
1091 test_applying_fixits_insert_after_at_line_end above. */
1093 static void
1094 test_applying_fixits_insert_after_failure (const line_table_case &case_)
1096 /* Create a tempfile and write some text to it.
1097 .........................0000000001111111.
1098 .........................1234567890123456. */
1099 const char *old_content = ("/* before */\n"
1100 "foo = bar.field;\n"
1101 "/* after */\n");
1102 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1103 const char *filename = tmp.get_filename ();
1104 line_table_test ltt (case_);
1105 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 2);
1107 /* Add a comment after the semicolon. */
1108 location_t loc = linemap_position_for_column (line_table, 16);
1109 rich_location richloc (line_table, loc);
1111 /* We want a failure of linemap_position_for_loc_and_offset.
1112 We can do this by starting a new linemap at line 3, so that
1113 there is no appropriate location value for the insertion point
1114 within the linemap for line 2. */
1115 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 3);
1117 /* The failure fails to happen at the transition point from
1118 packed ranges to unpacked ranges (where there are some "spare"
1119 location_t values). Skip the test there. */
1120 if (loc >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
1121 return;
1123 /* Offsetting "loc" should now fail (by returning the input loc. */
1124 ASSERT_EQ (loc, linemap_position_for_loc_and_offset (line_table, loc, 1));
1126 /* Hence attempting to use add_fixit_insert_after at the end of the line
1127 should now fail. */
1128 richloc.add_fixit_insert_after ("/* inserted */");
1129 ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
1131 edit_context edit;
1132 edit.add_fixits (&richloc);
1133 ASSERT_FALSE (edit.valid_p ());
1134 ASSERT_EQ (NULL, edit.get_content (filename));
1135 ASSERT_EQ (NULL, edit.generate_diff (false));
1138 /* Test applying an "insert" fixit that adds a newline. */
1140 static void
1141 test_applying_fixits_insert_containing_newline (const line_table_case &case_)
1143 /* Create a tempfile and write some text to it.
1144 .........................0000000001111111.
1145 .........................1234567890123456. */
1146 const char *old_content = (" case 'a':\n" /* line 1. */
1147 " x = a;\n" /* line 2. */
1148 " case 'b':\n" /* line 3. */
1149 " x = b;\n");/* line 4. */
1151 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1152 const char *filename = tmp.get_filename ();
1153 line_table_test ltt (case_);
1154 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 3);
1156 /* Add a "break;" on a line by itself before line 3 i.e. before
1157 column 1 of line 3. */
1158 location_t case_start = linemap_position_for_column (line_table, 5);
1159 location_t case_finish = linemap_position_for_column (line_table, 13);
1160 location_t case_loc = make_location (case_start, case_start, case_finish);
1161 rich_location richloc (line_table, case_loc);
1162 location_t line_start = linemap_position_for_column (line_table, 1);
1163 richloc.add_fixit_insert_before (line_start, " break;\n");
1165 if (case_finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
1166 return;
1168 edit_context edit;
1169 edit.add_fixits (&richloc);
1170 auto_free <char *> new_content = edit.get_content (filename);
1171 ASSERT_STREQ ((" case 'a':\n"
1172 " x = a;\n"
1173 " break;\n"
1174 " case 'b':\n"
1175 " x = b;\n"),
1176 new_content);
1178 /* Verify diff. */
1179 auto_free <char *> diff = edit.generate_diff (false);
1180 ASSERT_STREQ (("@@ -1,4 +1,5 @@\n"
1181 " case 'a':\n"
1182 " x = a;\n"
1183 "+ break;\n"
1184 " case 'b':\n"
1185 " x = b;\n"),
1186 diff);
1189 /* Test applying a "replace" fixit that grows the affected line. */
1191 static void
1192 test_applying_fixits_growing_replace (const line_table_case &case_)
1194 /* Create a tempfile and write some text to it.
1195 .........................0000000001111111.
1196 .........................1234567890123456. */
1197 const char *old_content = ("/* before */\n"
1198 "foo = bar.field;\n"
1199 "/* after */\n");
1200 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1201 const char *filename = tmp.get_filename ();
1202 line_table_test ltt (case_);
1203 linemap_add (line_table, LC_ENTER, false, filename, 2);
1205 /* Replace "field" with "m_field". */
1206 location_t start = linemap_position_for_column (line_table, 11);
1207 location_t finish = linemap_position_for_column (line_table, 15);
1208 location_t field = make_location (start, start, finish);
1209 rich_location richloc (line_table, field);
1210 richloc.add_fixit_replace ("m_field");
1212 edit_context edit;
1213 edit.add_fixits (&richloc);
1214 auto_free <char *> new_content = edit.get_content (filename);
1215 if (finish <= LINE_MAP_MAX_LOCATION_WITH_COLS)
1217 ASSERT_STREQ ("/* before */\n"
1218 "foo = bar.m_field;\n"
1219 "/* after */\n", new_content);
1221 /* Verify location of ";" after the change. */
1222 ASSERT_EQ (18, edit.get_effective_column (filename, 2, 16));
1224 /* Verify diff. */
1225 auto_free <char *> diff = edit.generate_diff (false);
1226 ASSERT_STREQ ("@@ -1,3 +1,3 @@\n"
1227 " /* before */\n"
1228 "-foo = bar.field;\n"
1229 "+foo = bar.m_field;\n"
1230 " /* after */\n", diff);
1234 /* Test applying a "replace" fixit that shrinks the affected line. */
1236 static void
1237 test_applying_fixits_shrinking_replace (const line_table_case &case_)
1239 /* Create a tempfile and write some text to it.
1240 .........................000000000111111111.
1241 .........................123456789012345678. */
1242 const char *old_content = ("/* before */\n"
1243 "foo = bar.m_field;\n"
1244 "/* after */\n");
1245 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1246 const char *filename = tmp.get_filename ();
1247 line_table_test ltt (case_);
1248 linemap_add (line_table, LC_ENTER, false, filename, 2);
1250 /* Replace "field" with "m_field". */
1251 location_t start = linemap_position_for_column (line_table, 11);
1252 location_t finish = linemap_position_for_column (line_table, 17);
1253 location_t m_field = make_location (start, start, finish);
1254 rich_location richloc (line_table, m_field);
1255 richloc.add_fixit_replace ("field");
1257 edit_context edit;
1258 edit.add_fixits (&richloc);
1259 auto_free <char *> new_content = edit.get_content (filename);
1260 if (finish <= LINE_MAP_MAX_LOCATION_WITH_COLS)
1262 ASSERT_STREQ ("/* before */\n"
1263 "foo = bar.field;\n"
1264 "/* after */\n", new_content);
1266 /* Verify location of ";" after the change. */
1267 ASSERT_EQ (16, edit.get_effective_column (filename, 2, 18));
1269 /* Verify diff. */
1270 auto_free <char *> diff = edit.generate_diff (false);
1271 ASSERT_STREQ ("@@ -1,3 +1,3 @@\n"
1272 " /* before */\n"
1273 "-foo = bar.m_field;\n"
1274 "+foo = bar.field;\n"
1275 " /* after */\n", diff);
1279 /* Replacement fix-it hint containing a newline. */
1281 static void
1282 test_applying_fixits_replace_containing_newline (const line_table_case &case_)
1284 /* Create a tempfile and write some text to it.
1285 .........................0000000001111.
1286 .........................1234567890123. */
1287 const char *old_content = "foo = bar ();\n";
1289 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1290 const char *filename = tmp.get_filename ();
1291 line_table_test ltt (case_);
1292 linemap_add (line_table, LC_ENTER, false, filename, 1);
1294 /* Replace the " = " with "\n = ", as if we were reformatting an
1295 overly long line. */
1296 location_t start = linemap_position_for_column (line_table, 4);
1297 location_t finish = linemap_position_for_column (line_table, 6);
1298 location_t loc = linemap_position_for_column (line_table, 13);
1299 rich_location richloc (line_table, loc);
1300 source_range range = source_range::from_locations (start, finish);
1301 richloc.add_fixit_replace (range, "\n = ");
1303 /* Newlines are only supported within fix-it hints that
1304 are at the start of lines (for entirely new lines), hence
1305 this fix-it should not be displayed. */
1306 ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
1308 if (finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
1309 return;
1311 edit_context edit;
1312 edit.add_fixits (&richloc);
1313 auto_free <char *> new_content = edit.get_content (filename);
1314 //ASSERT_STREQ ("foo\n = bar ();\n", new_content);
1317 /* Test applying a "remove" fixit. */
1319 static void
1320 test_applying_fixits_remove (const line_table_case &case_)
1322 /* Create a tempfile and write some text to it.
1323 .........................000000000111111111.
1324 .........................123456789012345678. */
1325 const char *old_content = ("/* before */\n"
1326 "foo = bar.m_field;\n"
1327 "/* after */\n");
1328 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1329 const char *filename = tmp.get_filename ();
1330 line_table_test ltt (case_);
1331 linemap_add (line_table, LC_ENTER, false, filename, 2);
1333 /* Remove ".m_field". */
1334 location_t start = linemap_position_for_column (line_table, 10);
1335 location_t finish = linemap_position_for_column (line_table, 17);
1336 rich_location richloc (line_table, start);
1337 source_range range;
1338 range.m_start = start;
1339 range.m_finish = finish;
1340 richloc.add_fixit_remove (range);
1342 edit_context edit;
1343 edit.add_fixits (&richloc);
1344 auto_free <char *> new_content = edit.get_content (filename);
1345 if (finish <= LINE_MAP_MAX_LOCATION_WITH_COLS)
1347 ASSERT_STREQ ("/* before */\n"
1348 "foo = bar;\n"
1349 "/* after */\n", new_content);
1351 /* Verify location of ";" after the change. */
1352 ASSERT_EQ (10, edit.get_effective_column (filename, 2, 18));
1354 /* Verify diff. */
1355 auto_free <char *> diff = edit.generate_diff (false);
1356 ASSERT_STREQ ("@@ -1,3 +1,3 @@\n"
1357 " /* before */\n"
1358 "-foo = bar.m_field;\n"
1359 "+foo = bar;\n"
1360 " /* after */\n", diff);
1364 /* Test applying multiple fixits to one line. */
1366 static void
1367 test_applying_fixits_multiple (const line_table_case &case_)
1369 /* Create a tempfile and write some text to it.
1370 .........................00000000011111111.
1371 .........................12345678901234567. */
1372 const char *old_content = ("/* before */\n"
1373 "foo = bar.field;\n"
1374 "/* after */\n");
1375 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1376 const char *filename = tmp.get_filename ();
1377 line_table_test ltt (case_);
1378 linemap_add (line_table, LC_ENTER, false, filename, 2);
1380 location_t c7 = linemap_position_for_column (line_table, 7);
1381 location_t c9 = linemap_position_for_column (line_table, 9);
1382 location_t c11 = linemap_position_for_column (line_table, 11);
1383 location_t c15 = linemap_position_for_column (line_table, 15);
1384 location_t c17 = linemap_position_for_column (line_table, 17);
1386 if (c17 > LINE_MAP_MAX_LOCATION_WITH_COLS)
1387 return;
1389 /* Add a comment in front of "bar.field". */
1390 rich_location insert_a (line_table, c7);
1391 insert_a.add_fixit_insert_before (c7, "/* alpha */");
1393 /* Add a comment after "bar.field;". */
1394 rich_location insert_b (line_table, c17);
1395 insert_b.add_fixit_insert_before (c17, "/* beta */");
1397 /* Replace "bar" with "pub". */
1398 rich_location replace_a (line_table, c7);
1399 replace_a.add_fixit_replace (source_range::from_locations (c7, c9),
1400 "pub");
1402 /* Replace "field" with "meadow". */
1403 rich_location replace_b (line_table, c7);
1404 replace_b.add_fixit_replace (source_range::from_locations (c11, c15),
1405 "meadow");
1407 edit_context edit;
1408 edit.add_fixits (&insert_a);
1409 ASSERT_EQ (100, edit.get_effective_column (filename, 1, 100));
1410 ASSERT_EQ (1, edit.get_effective_column (filename, 2, 1));
1411 ASSERT_EQ (6, edit.get_effective_column (filename, 2, 6));
1412 ASSERT_EQ (18, edit.get_effective_column (filename, 2, 7));
1413 ASSERT_EQ (27, edit.get_effective_column (filename, 2, 16));
1414 ASSERT_EQ (100, edit.get_effective_column (filename, 3, 100));
1416 edit.add_fixits (&insert_b);
1417 edit.add_fixits (&replace_a);
1418 edit.add_fixits (&replace_b);
1420 if (c17 <= LINE_MAP_MAX_LOCATION_WITH_COLS)
1422 auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
1423 ASSERT_STREQ ("/* before */\n"
1424 "foo = /* alpha */pub.meadow;/* beta */\n"
1425 "/* after */\n",
1426 new_content);
1428 /* Verify diff. */
1429 auto_free <char *> diff = edit.generate_diff (false);
1430 ASSERT_STREQ ("@@ -1,3 +1,3 @@\n"
1431 " /* before */\n"
1432 "-foo = bar.field;\n"
1433 "+foo = /* alpha */pub.meadow;/* beta */\n"
1434 " /* after */\n", diff);
1438 /* Subroutine of test_applying_fixits_multiple_lines.
1439 Add the text "CHANGED: " to the front of the given line. */
1441 static location_t
1442 change_line (edit_context &edit, int line_num)
1444 const line_map_ordinary *ord_map
1445 = LINEMAPS_LAST_ORDINARY_MAP (line_table);
1446 const int column = 1;
1447 location_t loc =
1448 linemap_position_for_line_and_column (line_table, ord_map,
1449 line_num, column);
1451 expanded_location exploc = expand_location (loc);
1452 if (loc <= LINE_MAP_MAX_LOCATION_WITH_COLS)
1454 ASSERT_EQ (line_num, exploc.line);
1455 ASSERT_EQ (column, exploc.column);
1458 rich_location insert (line_table, loc);
1459 insert.add_fixit_insert_before ("CHANGED: ");
1460 edit.add_fixits (&insert);
1461 return loc;
1464 /* Subroutine of test_applying_fixits_multiple_lines.
1465 Add the text "INSERTED\n" in front of the given line. */
1467 static location_t
1468 insert_line (edit_context &edit, int line_num)
1470 const line_map_ordinary *ord_map
1471 = LINEMAPS_LAST_ORDINARY_MAP (line_table);
1472 const int column = 1;
1473 location_t loc =
1474 linemap_position_for_line_and_column (line_table, ord_map,
1475 line_num, column);
1477 expanded_location exploc = expand_location (loc);
1478 if (loc <= LINE_MAP_MAX_LOCATION_WITH_COLS)
1480 ASSERT_EQ (line_num, exploc.line);
1481 ASSERT_EQ (column, exploc.column);
1484 rich_location insert (line_table, loc);
1485 insert.add_fixit_insert_before ("INSERTED\n");
1486 edit.add_fixits (&insert);
1487 return loc;
1490 /* Test of editing multiple lines within a long file,
1491 to ensure that diffs are generated as expected. */
1493 static void
1494 test_applying_fixits_multiple_lines (const line_table_case &case_)
1496 /* Create a tempfile and write many lines of text to it. */
1497 named_temp_file tmp (".txt");
1498 const char *filename = tmp.get_filename ();
1499 FILE *f = fopen (filename, "w");
1500 ASSERT_NE (f, NULL);
1501 for (int i = 1; i <= 1000; i++)
1502 fprintf (f, "line %i\n", i);
1503 fclose (f);
1505 line_table_test ltt (case_);
1506 linemap_add (line_table, LC_ENTER, false, filename, 1);
1507 linemap_position_for_column (line_table, 127);
1509 edit_context edit;
1511 /* A run of consecutive lines. */
1512 change_line (edit, 2);
1513 change_line (edit, 3);
1514 change_line (edit, 4);
1515 insert_line (edit, 5);
1517 /* A run of nearby lines, within the contextual limit. */
1518 change_line (edit, 150);
1519 change_line (edit, 151);
1520 location_t last_loc = change_line (edit, 153);
1522 if (last_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
1523 return;
1525 /* Verify diff. */
1526 auto_free <char *> diff = edit.generate_diff (false);
1527 ASSERT_STREQ ("@@ -1,7 +1,8 @@\n"
1528 " line 1\n"
1529 "-line 2\n"
1530 "-line 3\n"
1531 "-line 4\n"
1532 "+CHANGED: line 2\n"
1533 "+CHANGED: line 3\n"
1534 "+CHANGED: line 4\n"
1535 "+INSERTED\n"
1536 " line 5\n"
1537 " line 6\n"
1538 " line 7\n"
1539 "@@ -147,10 +148,10 @@\n"
1540 " line 147\n"
1541 " line 148\n"
1542 " line 149\n"
1543 "-line 150\n"
1544 "-line 151\n"
1545 "+CHANGED: line 150\n"
1546 "+CHANGED: line 151\n"
1547 " line 152\n"
1548 "-line 153\n"
1549 "+CHANGED: line 153\n"
1550 " line 154\n"
1551 " line 155\n"
1552 " line 156\n", diff);
1554 /* Ensure tmp stays alive until this point, so that the tempfile
1555 persists until after the generate_diff call. */
1556 tmp.get_filename ();
1559 /* Test of converting an initializer for a named field from
1560 the old GCC extension to C99 syntax.
1561 Exercises a shrinking replacement followed by a growing
1562 replacement on the same line. */
1564 static void
1565 test_applying_fixits_modernize_named_init (const line_table_case &case_)
1567 /* Create a tempfile and write some text to it.
1568 .........................00000000011111111.
1569 .........................12345678901234567. */
1570 const char *old_content = ("/* before */\n"
1571 "bar : 1,\n"
1572 "/* after */\n");
1573 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
1574 const char *filename = tmp.get_filename ();
1575 line_table_test ltt (case_);
1576 linemap_add (line_table, LC_ENTER, false, filename, 2);
1578 location_t c1 = linemap_position_for_column (line_table, 1);
1579 location_t c3 = linemap_position_for_column (line_table, 3);
1580 location_t c8 = linemap_position_for_column (line_table, 8);
1582 if (c8 > LINE_MAP_MAX_LOCATION_WITH_COLS)
1583 return;
1585 /* Replace "bar" with ".". */
1586 rich_location r1 (line_table, c8);
1587 r1.add_fixit_replace (source_range::from_locations (c1, c3),
1588 ".");
1590 /* Replace ":" with "bar =". */
1591 rich_location r2 (line_table, c8);
1592 r2.add_fixit_replace (source_range::from_locations (c8, c8),
1593 "bar =");
1595 /* The order should not matter. Do r1 then r2. */
1597 edit_context edit;
1598 edit.add_fixits (&r1);
1600 /* Verify state after first replacement. */
1602 auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
1603 /* We should now have:
1604 ............00000000011.
1605 ............12345678901. */
1606 ASSERT_STREQ ("/* before */\n"
1607 ". : 1,\n"
1608 "/* after */\n",
1609 new_content);
1610 /* Location of the "1". */
1611 ASSERT_EQ (6, edit.get_effective_column (filename, 2, 8));
1612 /* Location of the ",". */
1613 ASSERT_EQ (9, edit.get_effective_column (filename, 2, 11));
1616 edit.add_fixits (&r2);
1618 auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
1619 /* Verify state after second replacement.
1620 ............00000000011111111.
1621 ............12345678901234567. */
1622 ASSERT_STREQ ("/* before */\n"
1623 ". bar = 1,\n"
1624 "/* after */\n",
1625 new_content);
1628 /* Try again, doing r2 then r1; the new_content should be the same. */
1630 edit_context edit;
1631 edit.add_fixits (&r2);
1632 edit.add_fixits (&r1);
1633 auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
1634 /*.............00000000011111111.
1635 .............12345678901234567. */
1636 ASSERT_STREQ ("/* before */\n"
1637 ". bar = 1,\n"
1638 "/* after */\n",
1639 new_content);
1643 /* Test of a fixit affecting a file that can't be read. */
1645 static void
1646 test_applying_fixits_unreadable_file ()
1648 const char *filename = "this-does-not-exist.txt";
1649 line_table_test ltt;
1650 linemap_add (line_table, LC_ENTER, false, filename, 1);
1652 location_t loc = linemap_position_for_column (line_table, 1);
1654 rich_location insert (line_table, loc);
1655 insert.add_fixit_insert_before ("change 1");
1656 insert.add_fixit_insert_before ("change 2");
1658 edit_context edit;
1659 /* Attempting to add the fixits affecting the unreadable file
1660 should transition the edit from valid to invalid. */
1661 ASSERT_TRUE (edit.valid_p ());
1662 edit.add_fixits (&insert);
1663 ASSERT_FALSE (edit.valid_p ());
1664 ASSERT_EQ (NULL, edit.get_content (filename));
1665 ASSERT_EQ (NULL, edit.generate_diff (false));
1668 /* Verify that we gracefully handle an attempt to edit a line
1669 that's beyond the end of the file. */
1671 static void
1672 test_applying_fixits_line_out_of_range ()
1674 /* Create a tempfile and write some text to it.
1675 ........................00000000011111111.
1676 ........................12345678901234567. */
1677 const char *old_content = "One-liner file\n";
1678 temp_source_file tmp (SELFTEST_LOCATION, ".txt", old_content);
1679 const char *filename = tmp.get_filename ();
1680 line_table_test ltt;
1681 linemap_add (line_table, LC_ENTER, false, filename, 2);
1683 /* Try to insert a string in line 2. */
1684 location_t loc = linemap_position_for_column (line_table, 1);
1686 rich_location insert (line_table, loc);
1687 insert.add_fixit_insert_before ("change");
1689 /* Verify that attempting the insertion puts an edit_context
1690 into an invalid state. */
1691 edit_context edit;
1692 ASSERT_TRUE (edit.valid_p ());
1693 edit.add_fixits (&insert);
1694 ASSERT_FALSE (edit.valid_p ());
1695 ASSERT_EQ (NULL, edit.get_content (filename));
1696 ASSERT_EQ (NULL, edit.generate_diff (false));
1699 /* Verify the boundary conditions of column values in fix-it
1700 hints applied to edit_context instances. */
1702 static void
1703 test_applying_fixits_column_validation (const line_table_case &case_)
1705 /* Create a tempfile and write some text to it.
1706 ........................00000000011111111.
1707 ........................12345678901234567. */
1708 const char *old_content = "One-liner file\n";
1709 temp_source_file tmp (SELFTEST_LOCATION, ".txt", old_content);
1710 const char *filename = tmp.get_filename ();
1711 line_table_test ltt (case_);
1712 linemap_add (line_table, LC_ENTER, false, filename, 1);
1714 location_t c11 = linemap_position_for_column (line_table, 11);
1715 location_t c14 = linemap_position_for_column (line_table, 14);
1716 location_t c15 = linemap_position_for_column (line_table, 15);
1717 location_t c16 = linemap_position_for_column (line_table, 16);
1719 /* Verify limits of valid columns in insertion fixits. */
1721 /* Verify inserting at the end of the line. */
1723 rich_location richloc (line_table, c11);
1724 richloc.add_fixit_insert_before (c15, " change");
1726 /* Col 15 is at the end of the line, so the insertion
1727 should succeed. */
1728 edit_context edit;
1729 edit.add_fixits (&richloc);
1730 auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
1731 if (c15 <= LINE_MAP_MAX_LOCATION_WITH_COLS)
1732 ASSERT_STREQ ("One-liner file change\n", new_content);
1733 else
1734 ASSERT_EQ (NULL, new_content);
1737 /* Verify inserting beyond the end of the line. */
1739 rich_location richloc (line_table, c11);
1740 richloc.add_fixit_insert_before (c16, " change");
1742 /* Col 16 is beyond the end of the line, so the insertion
1743 should fail gracefully. */
1744 edit_context edit;
1745 ASSERT_TRUE (edit.valid_p ());
1746 edit.add_fixits (&richloc);
1747 ASSERT_FALSE (edit.valid_p ());
1748 ASSERT_EQ (NULL, edit.get_content (filename));
1749 ASSERT_EQ (NULL, edit.generate_diff (false));
1752 /* Verify limits of valid columns in replacement fixits. */
1754 /* Verify replacing the end of the line. */
1756 rich_location richloc (line_table, c11);
1757 source_range range = source_range::from_locations (c11, c14);
1758 richloc.add_fixit_replace (range, "change");
1760 /* Col 14 is at the end of the line, so the replacement
1761 should succeed. */
1762 edit_context edit;
1763 edit.add_fixits (&richloc);
1764 auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
1765 if (c14 <= LINE_MAP_MAX_LOCATION_WITH_COLS)
1766 ASSERT_STREQ ("One-liner change\n", new_content);
1767 else
1768 ASSERT_EQ (NULL, new_content);
1771 /* Verify going beyond the end of the line. */
1773 rich_location richloc (line_table, c11);
1774 source_range range = source_range::from_locations (c11, c15);
1775 richloc.add_fixit_replace (range, "change");
1777 /* Col 15 is after the end of the line, so the replacement
1778 should fail; verify that the attempt fails gracefully. */
1779 edit_context edit;
1780 ASSERT_TRUE (edit.valid_p ());
1781 edit.add_fixits (&richloc);
1782 ASSERT_FALSE (edit.valid_p ());
1783 ASSERT_EQ (NULL, edit.get_content (filename));
1784 ASSERT_EQ (NULL, edit.generate_diff (false));
1788 /* Run all of the selftests within this file. */
1790 void
1791 edit_context_cc_tests ()
1793 test_get_content ();
1794 for_each_line_table_case (test_applying_fixits_insert_before);
1795 for_each_line_table_case (test_applying_fixits_insert_after);
1796 for_each_line_table_case (test_applying_fixits_insert_after_at_line_end);
1797 for_each_line_table_case (test_applying_fixits_insert_after_failure);
1798 for_each_line_table_case (test_applying_fixits_insert_containing_newline);
1799 for_each_line_table_case (test_applying_fixits_growing_replace);
1800 for_each_line_table_case (test_applying_fixits_shrinking_replace);
1801 for_each_line_table_case (test_applying_fixits_replace_containing_newline);
1802 for_each_line_table_case (test_applying_fixits_remove);
1803 for_each_line_table_case (test_applying_fixits_multiple);
1804 for_each_line_table_case (test_applying_fixits_multiple_lines);
1805 for_each_line_table_case (test_applying_fixits_modernize_named_init);
1806 test_applying_fixits_unreadable_file ();
1807 test_applying_fixits_line_out_of_range ();
1808 for_each_line_table_case (test_applying_fixits_column_validation);
1811 } // namespace selftest
1813 #endif /* CHECKING_P */