diagnostics: fix bad interaction between line spans and line numbers
authorDavid Malcolm <dmalcolm@redhat.com>
Thu, 16 Aug 2018 22:33:00 +0000 (22:33 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Thu, 16 Aug 2018 22:33:00 +0000 (22:33 +0000)
Without this patch, the "line span" markers and the line numbering
interacted badly, leading to stray copies of the line-span markers
appearing as prefixes on the first source line in a span:

missing-header-fixit-3.c: In function 'test':
missing-header-fixit-3.c:9:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
9 |   printf ("%i of %i\n", i, j);
  |   ^~~~~~
missing-header-fixit-3.c:9:3: warning: incompatible implicit declaration of built-in function 'printf'
missing-header-fixit-3.c:9:3: note: include '<stdio.h>' or provide a declaration of 'printf'
missing-header-fixit-3.c:1:1:
  |+#include <stdio.h>
missing-header-fixit-3.c:1:1:1 | /* Example of a fix-it hint that adds a #include directive,
missing-header-fixit-3.c:9:3:
missing-header-fixit-3.c:9:3:9 |   printf ("%i of %i\n", i, j);
  |   ^~~~~~

With this patch, we now correctly print:

missing-header-fixit-3.c: In function 'test':
missing-header-fixit-3.c:9:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
9 |   printf ("%i of %i\n", i, j);
  |   ^~~~~~
missing-header-fixit-3.c:9:3: warning: incompatible implicit declaration of built-in function 'printf'
missing-header-fixit-3.c:9:3: note: include '<stdio.h>' or provide a declaration of 'printf'
missing-header-fixit-3.c:1:1:
+ |+#include <stdio.h>
1 | /* Example of a fix-it hint that adds a #include directive,
missing-header-fixit-3.c:9:3:
9 |   printf ("%i of %i\n", i, j);
  |   ^~~~~~

gcc/ChangeLog:
* diagnostic.c (default_diagnostic_start_span_fn): Call pp_string
to emit the span, rather than setting it as the prefix.

gcc/testsuite/ChangeLog:
* gcc.dg/missing-header-fixit-3.c: New test.

From-SVN: r263606

gcc/ChangeLog
gcc/diagnostic.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/missing-header-fixit-3.c [new file with mode: 0644]

index c776f6dc952c83910eec27f20a9b9b3a552326aa..146e4e37c3a63b525e089724fbcf43d093a8cad9 100644 (file)
@@ -1,3 +1,8 @@
+2018-08-16  David Malcolm  <dmalcolm@redhat.com>
+
+       * diagnostic.c (default_diagnostic_start_span_fn): Call pp_string
+       to emit the span, rather than setting it as the prefix.
+
 2018-08-16  David Malcolm  <dmalcolm@redhat.com>
 
        * diagnostic-show-locus.c (layout::start_annotation_line): Add
index 59477ce93f607d124b6c00109fb41da019ddd1ed..7e8bcf52dc1e533a62b853cd438afcb65fff2c06 100644 (file)
@@ -629,9 +629,9 @@ void
 default_diagnostic_start_span_fn (diagnostic_context *context,
                                  expanded_location exploc)
 {
-  pp_set_prefix (context->printer,
-                diagnostic_get_location_text (context, exploc));
-  pp_string (context->printer, "");
+  char *text = diagnostic_get_location_text (context, exploc);
+  pp_string (context->printer, text);
+  free (text);
   pp_newline (context->printer);
 }
 
index 978bd8f822376a62a29ad142f1303f9e204a7086..ad2de8468ec1736acd541ed47f70fc65457c5cbe 100644 (file)
@@ -1,3 +1,7 @@
+2018-08-16  David Malcolm  <dmalcolm@redhat.com>
+
+       * gcc.dg/missing-header-fixit-3.c: New test.
+
 2018-08-16  David Malcolm  <dmalcolm@redhat.com>
 
        * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c
diff --git a/gcc/testsuite/gcc.dg/missing-header-fixit-3.c b/gcc/testsuite/gcc.dg/missing-header-fixit-3.c
new file mode 100644 (file)
index 0000000..8f2fb5b
--- /dev/null
@@ -0,0 +1,27 @@
+/* Example of a fix-it hint that adds a #include directive,
+   adding them to the top of the file, given that there is no
+   pre-existing #include.  */
+
+/* { dg-options "-fdiagnostics-show-caret -fdiagnostics-show-line-numbers" } */
+
+void test (int i, int j)
+{
+  printf ("%i of %i\n", i, j); /* { dg-warning "implicit declaration" } */
+  /* { dg-message "include '<stdio.h>' or provide a declaration of 'printf'" "" { target *-*-* } .-1 } */
+#if 0
+/* { dg-begin-multiline-output "" }
+9 |   printf ("%i of %i\n", i, j);
+  |   ^~~~~~
+   { dg-end-multiline-output "" } */
+/* { dg-regexp ".*missing-header-fixit-3.c:1:1:" } */
+/* { dg-begin-multiline-output "" }
++ |+#include <stdio.h>
+1 | /* Example of a fix-it hint that adds a #include directive,
+   { dg-end-multiline-output "" } */
+/* { dg-regexp ".*missing-header-fixit-3.c:9:3:" } */
+/* { dg-begin-multiline-output "" }
+9 |   printf ("%i of %i\n", i, j);
+  |   ^~~~~~
+   { dg-end-multiline-output "" } */
+#endif
+}