From 299dbdba87a0a12e11500938551044f50251253d Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 14 Sep 2016 16:43:10 -0400
Subject: [PATCH 19/44] FIXME: not-yet-functioning rewrite of
 edited_file::print_run_of_changed_lines to use vec of effective_line

old_lines seems to be suffering from input.c lifetime issues
also, need a way of running levenshtein on lines as a whole, I think...
---
 gcc/edit-context.c | 90 ++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 71 insertions(+), 19 deletions(-)

diff --git a/gcc/edit-context.c b/gcc/edit-context.c
index 3d60dfd..bb1ad8e 100644
--- a/gcc/edit-context.c
+++ b/gcc/edit-context.c
@@ -513,32 +513,41 @@ edited_file::print_run_of_changed_lines (pretty_printer *pp,
 					 int start_of_run,
 					 int end_of_run)
 {
-  /* Show old version of lines.  */
-  pp_string (pp, colorize_start (pp_show_color (pp),
-				 "diff-delete"));
-  for (int line_num = start_of_run; line_num <= end_of_run; line_num++)
+  int old_num_lines = end_of_run + 1 - start_of_run;
+  auto_vec<effective_line> old_lines (old_num_lines);
+  auto_vec<effective_line> new_lines (old_num_lines);
+  for (int old_line_num = start_of_run; old_line_num <= end_of_run;
+       old_line_num++)
     {
       int line_len;
       const char *old_line
-	= location_get_source_line (m_filename, line_num, &line_len);
-      print_diff_line (pp, '-', old_line, line_len);
-    }
-  pp_string (pp, colorize_stop (pp_show_color (pp)));
+	= location_get_source_line (m_filename, old_line_num, &line_len);
+      old_lines.safe_push (effective_line (old_line, line_len));
 
-  /* Show new version of lines.  */
-  pp_string (pp, colorize_start (pp_show_color (pp),
-				 "diff-insert"));
-  for (int old_line_num = start_of_run; old_line_num <= end_of_run;
-       old_line_num++)
-    {
       edited_line *el = get_line (old_line_num);
       gcc_assert (el);
-      auto_vec<effective_line> new_lines = el->get_effective_lines ();
+      auto_vec<effective_line> new_lines_for_line = el->get_effective_lines ();
       int new_line_num;
       effective_line *new_line;
-      FOR_EACH_VEC_ELT (new_lines, new_line_num, new_line)
-	print_diff_line (pp, '+', new_line->m_start, new_line->m_len);
+      FOR_EACH_VEC_ELT (new_lines_for_line, new_line_num, new_line)
+	new_lines.safe_push (*new_line);
     }
+
+  int line_num;
+  effective_line *line;
+
+  /* Show old version of lines.  */
+  pp_string (pp, colorize_start (pp_show_color (pp),
+				 "diff-delete"));
+  FOR_EACH_VEC_ELT (old_lines, line_num, line)
+    print_diff_line (pp, '-', line->m_start, line->m_len);
+  pp_string (pp, colorize_stop (pp_show_color (pp)));
+
+  /* Show new version of lines.  */
+  pp_string (pp, colorize_start (pp_show_color (pp),
+				 "diff-insert"));
+  FOR_EACH_VEC_ELT (new_lines, line_num, line)
+    print_diff_line (pp, '+', line->m_start, line->m_len);
   pp_string (pp, colorize_stop (pp_show_color (pp)));
 }
 
@@ -1122,6 +1131,19 @@ test_applying_fixits_insert_containing_newline (const line_table_case &case_)
 		 "      x = b;\n"),
 		new_content);
 
+
+  /* Verify edited_line::get_effective_lines ().  */
+  edited_file *file = edit.get_file (filename);
+  ASSERT_NE (NULL, file);
+  edited_line *line_3 = file->get_line (3);
+  ASSERT_NE (NULL, line_3);
+  auto_vec<effective_line> lines = line_3->get_effective_lines ();
+  ASSERT_EQ (2, lines.length ());
+  ASSERT_EQ (12, lines[0].m_len);
+  ASSERT_EQ (0, strncmp (lines[0].m_start, "      break;", 12));
+  ASSERT_EQ (13, lines[1].m_len);
+  ASSERT_EQ (0, strncmp (lines[1].m_start, "    case 'b':", 13));
+
   /* Verify diff.  */
 #if 0
   auto_free <char *> diff = edit.generate_diff (false);
@@ -1412,6 +1434,32 @@ change_line (edit_context &edit, int line_num)
   return loc;
 }
 
+/* Subroutine of test_applying_fixits_multiple_lines.
+   Add the text "INSERTED\n" in front of the given line.  */
+
+static location_t
+insert_line (edit_context &edit, int line_num)
+{
+  const line_map_ordinary *ord_map
+    = LINEMAPS_LAST_ORDINARY_MAP (line_table);
+  const int column = 1;
+  location_t loc =
+    linemap_position_for_line_and_column (line_table, ord_map,
+					  line_num, column);
+
+  expanded_location exploc = expand_location (loc);
+  if (loc <= LINE_MAP_MAX_LOCATION_WITH_COLS)
+    {
+      ASSERT_EQ (line_num, exploc.line);
+      ASSERT_EQ (column, exploc.column);
+    }
+
+  rich_location insert (line_table, loc);
+  insert.add_fixit_insert_before ("INSERTED\n");
+  edit.add_fixits (&insert);
+  return loc;
+}
+
 /* Test of editing multiple lines within a long file,
    to ensure that diffs are generated as expected.  */
 
@@ -1437,6 +1485,7 @@ test_applying_fixits_multiple_lines (const line_table_case &case_)
   change_line (edit, 2);
   change_line (edit, 3);
   change_line (edit, 4);
+  insert_line (edit, 5);
 
   /* A run of nearby lines, within the contextual limit.  */
   change_line (edit, 150);
@@ -1446,9 +1495,10 @@ test_applying_fixits_multiple_lines (const line_table_case &case_)
   if (last_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
     return;
 
+#if 0
   /* Verify diff.  */
   auto_free <char *> diff = edit.generate_diff (false);
-  ASSERT_STREQ ("@@ -1,7 +1,7 @@\n"
+  ASSERT_STREQ ("@@ -1,7 +1,8 @@\n"
 		" line 1\n"
 		"-line 2\n"
 		"-line 3\n"
@@ -1456,10 +1506,11 @@ test_applying_fixits_multiple_lines (const line_table_case &case_)
 		"+CHANGED: line 2\n"
 		"+CHANGED: line 3\n"
 		"+CHANGED: line 4\n"
+		"+INSERTED\n"
 		" line 5\n"
 		" line 6\n"
 		" line 7\n"
-		"@@ -147,10 +147,10 @@\n"
+		"@@ -147,10 +148,10 @@\n"
 		" line 147\n"
 		" line 148\n"
 		" line 149\n"
@@ -1473,6 +1524,7 @@ test_applying_fixits_multiple_lines (const line_table_case &case_)
 		" line 154\n"
 		" line 155\n"
 		" line 156\n", diff);
+#endif
 
   /* Ensure tmp stays alive until this point, so that the tempfile
      persists until after the generate_diff call.  */
-- 
1.8.5.3

