From 4e3cc21bb87b6a96f381f7ab99c6d74479ff9465 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 9 Aug 2018 15:21:29 -0400
Subject: [PATCH 09/18] FIXME: WIP on tab vs spaces support

---
 gcc/diagnostic-show-locus.c                        | 80 ++++++++++++++++++++++
 .../gcc.dg/plugin/diagnostic-test-show-locus-bw.c  | 28 ++++++++
 .../plugin/diagnostic_plugin_test_show_locus.c     | 14 ++++
 3 files changed, 122 insertions(+)

diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index 79b76d2..1aa2a13 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -3752,6 +3752,85 @@ test_line_numbers_multiline_range ()
 		pp_formatted_text (dc.printer));
 }
 
+/* FIXME.  */
+
+static void
+test_tabs ()
+{
+  /* Create a tempfile and write some text to it.  */
+  pretty_printer pp;
+  for (int indent = 0; indent < 18; indent++)
+    {
+      /* ..............0000000001111111.
+	 ..............1234567890123456.  */
+      int tabs = indent / 8;
+      int spaces = indent % 8;
+      for (int i = 0; i < tabs; i++)
+	pp_character (&pp, '\t');
+      for (int i = 0; i < spaces; i++)
+	pp_character (&pp, ' ');
+      pp_printf (&pp,
+		 "indent: %i (tabs: %i, spaces: %i)\n",
+		 indent, tabs, spaces);
+    }
+  // TODO: add some erroneous leading spaces
+  temp_source_file tmp (SELFTEST_LOCATION, ".c", pp_formatted_text (&pp));
+  line_table_test ltt;
+
+  const line_map_ordinary *ord_map = linemap_check_ordinary
+    (linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 0));
+  linemap_line_start (line_table, 1, 100);
+
+  /* Create a multi-line location, covering the whole file.  */
+  location_t start
+    = linemap_position_for_line_and_column (line_table, ord_map, 1, 0);
+  location_t caret
+    = linemap_position_for_line_and_column (line_table, ord_map, 1, 0);
+  location_t finish
+    = linemap_position_for_line_and_column (line_table, ord_map, 17, 0);
+  location_t loc = make_location (caret, start, finish);
+
+  test_diagnostic_context dc;
+  gcc_rich_location richloc (loc);
+  diagnostic_show_locus (&dc, &richloc, DK_ERROR);
+  ASSERT_STREQ ("\n"
+		" indent: 0 (tabs: 0, spaces: 0)\n"
+		" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"  indent: 1 (tabs: 0, spaces: 1)\n"
+		"  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"   indent: 2 (tabs: 0, spaces: 2)\n"
+		"   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"    indent: 3 (tabs: 0, spaces: 3)\n"
+		"    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"     indent: 4 (tabs: 0, spaces: 4)\n"
+		"     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"      indent: 5 (tabs: 0, spaces: 5)\n"
+		"      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"       indent: 6 (tabs: 0, spaces: 6)\n"
+		"       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"        indent: 7 (tabs: 0, spaces: 7)\n"
+		"        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"  indent: 8 (tabs: 1, spaces: 0)\n"
+		"  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"   indent: 9 (tabs: 1, spaces: 1)\n"
+		"   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"    indent: 10 (tabs: 1, spaces: 2)\n"
+		"    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"     indent: 11 (tabs: 1, spaces: 3)\n"
+		"     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"      indent: 12 (tabs: 1, spaces: 4)\n"
+		"      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"       indent: 13 (tabs: 1, spaces: 5)\n"
+		"       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"        indent: 14 (tabs: 1, spaces: 6)\n"
+		"        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"         indent: 15 (tabs: 1, spaces: 7)\n"
+		"         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+		"   indent: 16 (tabs: 2, spaces: 0)\n"
+		" \n",
+		pp_formatted_text (dc.printer));
+}
+
 /* Run all of the selftests within this file.  */
 
 void
@@ -3780,6 +3859,7 @@ diagnostic_show_locus_c_tests ()
   for_each_line_table_case (test_fixit_deletion_affecting_newline);
 
   test_line_numbers_multiline_range ();
+  test_tabs ();
 }
 
 } // namespace selftest
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c
index bdfa420..fb577e0 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c
@@ -358,3 +358,31 @@ void test_mutually_exclusive_suggestions (void)
    { dg-end-multiline-output "" } */
 #endif
 }
+
+/* Unit test for mixtures of spaces and tabs.  */
+
+void test_spaces_vs_tabs (void)
+{
+#if 0
+indent: 0 (tabs: 0, spaces: 0)
+ indent: 1 (tabs: 0, spaces: 1)
+  indent: 2 (tabs: 0, spaces: 2)
+   indent: 3 (tabs: 0, spaces: 3)
+    indent: 4 (tabs: 0, spaces: 4)
+     indent: 5 (tabs: 0, spaces: 5)
+      indent: 6 (tabs: 0, spaces: 6)
+       indent: 7 (tabs: 0, spaces: 7)
+	indent: 8 (tabs: 1, spaces: 0)
+  	indent: 8 (leading spaces 2: tabs: 1, spaces: 0)
+	 indent: 9 (tabs: 1, spaces: 1)
+	  indent: 10 (tabs: 1, spaces: 2)
+	   indent: 11 (tabs: 1, spaces: 3)
+	    indent: 12 (tabs: 1, spaces: 4)
+	     indent: 13 (tabs: 1, spaces: 5)
+	      indent: 14 (tabs: 1, spaces: 6)
+   	      indent: 14 (leading spaces 3: tabs: 1, spaces: 6)
+	       indent: 15 (tabs: 1, spaces: 7)
+		indent: 16 (tabs: 2, spaces: 0)
+		 indent: 17 (tabs: 2, spaces: 1)
+#endif
+}
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
index 3d78538..c9cd0d2 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
@@ -165,6 +165,8 @@ test_show_locus (function *fun)
   const char *fnname = IDENTIFIER_POINTER (identifier);
   location_t fnstart = fun->function_start_locus;
   int fnstart_line = LOCATION_LINE (fnstart);
+  location_t fnend = fun->function_end_locus;
+  int fnend_line = LOCATION_LINE (fnend);
 
   diagnostic_finalizer (global_dc) = custom_diagnostic_finalizer;
 
@@ -470,6 +472,18 @@ test_show_locus (function *fun)
       warning_at (&richloc, 0, "test of %i locations",
 		  richloc.get_num_locations ());
     }
+
+  /* Example of spaces and tabs for indentation.  */
+  if (0 == strcmp (fnname, "test_spaces_vs_tabs"))
+    {
+      for (int line = fnstart_line + 1; line < fnend_line + 1; line++)
+	{
+	  location_t loc = get_loc (line, 0);
+	  global_dc->show_ruler_p = true;
+	  warning_at (loc, 0, "test");
+	  global_dc->show_ruler_p = false;
+	}
+    }
 }
 
 unsigned int
-- 
1.8.5.3

