aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2018-08-09 15:32:13 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2018-08-09 15:32:13 +0000
commit56b61d7fc4f38c1a3fd51bf3315b0338513a2d2d (patch)
tree3a901a8ad0ba76dceac452206c01cd7eb3541b5a /gcc/testsuite
parentf4b905f0107b2a701c630787e378752cc2a3b181 (diff)
downloadgcc-56b61d7fc4f38c1a3fd51bf3315b0338513a2d2d.zip
gcc-56b61d7fc4f38c1a3fd51bf3315b0338513a2d2d.tar.gz
gcc-56b61d7fc4f38c1a3fd51bf3315b0338513a2d2d.tar.bz2
diagnostics: add line numbers to source (PR other/84889)
This patch adds a left margin to the lines of source (and annotations) printed by diagnostic_show_locus, so that e.g. rather than: test.c: In function 'test': test.c:12:15: error: 'struct foo' has no member named 'm_bar'; did you mean 'bar'? return ptr->m_bar; ^~~~~ bar we print: test.c: In function 'test': test.c:12:15: error: 'struct foo' has no member named 'm_bar'; did you mean 'bar'? 12 | return ptr->m_bar; | ^~~~~ | bar Similarly, for a multiline case (in C++ this time), this: bad-binary-ops.C: In function 'int test_2()': bad-binary-ops.C:26:4: error: no match for 'operator+' (operand types are 's' and 't') return (some_function () ~~~~~~~~~~~~~~~~ + some_other_function ()); ^~~~~~~~~~~~~~~~~~~~~~~~ becomes: bad-binary-ops.C: In function 'int test_2()': bad-binary-ops.C:26:4: error: no match for 'operator+' (operand types are 's' and 't') 25 | return (some_function () | ~~~~~~~~~~~~~~~~ 26 | + some_other_function ()); | ^~~~~~~~~~~~~~~~~~~~~~~~ I believe this slightly improves the readability of the output, in that it: - distinguishes between the user's source code vs the annotation lines that we're adding (the underlinings and fix-it hints here) - shows the line numbers in another place (potentially helpful for multiline diagnostics, where the user can see the line numbers directly, rather than have to figure them out relative to the caret: in the 2nd example, note how the diagnostic is reported at line 26, but the first line printed is actually line 25) I'm not sure that this is the precise format we want to go with [1], but I think it's an improvement over the status quo, and we're in stage 1 of gcc 9, so there's plenty of time to shake out issues. I've turned it on by default; it can be disabled via -fno-diagnostics-show-line-numbers (it's also turned off in the testsuite, to avoid breaking numerous existing test cases). [1] Some possible variants: - maybe just "LL|" rather than "LL | " - maybe ':' rather than '|' - maybe we should have some leading indentation, to better split up the diagnostics visually via the left-hand column - etc gcc/ChangeLog: PR other/84889 * common.opt (fdiagnostics-show-line-numbers): New option. * diagnostic-show-locus.c (class layout): Add fields "m_show_line_numbers_p" and "m_linenum_width"; (num_digits): New function. (test_num_digits): New function. (layout::layout): Initialize new fields. Update m_x_offset logic to handle any left margin. (layout::print_source_line): Print line number when requested. (layout::start_annotation_line): New member function. (layout::print_annotation_line): Call it. (layout::print_leading_fixits): Likewise. (layout::print_trailing_fixits): Likewise. Update calls to move_to_column for new parameter. (layout::get_x_bound_for_row): Add "add_left_margin" param and use it to potentially call start_annotation_line. (layout::show_ruler): Call start_annotation_line. (selftest::test_line_numbers_multiline_range): New selftest. (selftest::diagnostic_show_locus_c_tests): Call test_num_digits and selftest::test_line_numbers_multiline_range. * diagnostic.c (diagnostic_initialize): Initialize show_line_numbers_p. * diagnostic.h (struct diagnostic_context): Add field "show_line_numbers_p". * doc/invoke.texi (Diagnostic Message Formatting Options): Add -fno-diagnostics-show-line-numbers. * dwarf2out.c (gen_producer_string): Add OPT_fdiagnostics_show_line_numbers to the ignored options. * lto-wrapper.c (merge_and_complain): Likewise to the "pick one setting" options. (append_compiler_options): Likewise to the dropped options. (append_diag_options): Likewise to the passed-on options. * opts.c (common_handle_option): Handle the new option. * toplev.c (general_init): Set up global_dc->show_line_numbers_p. gcc/testsuite/ChangeLog: PR other/84889 * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: New test. * gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c: New test. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add the new tests. * lib/prune.exp: Add -fno-diagnostics-show-line-numbers to TEST_ALWAYS_FLAGS. From-SVN: r263450
Diffstat (limited to 'gcc/testsuite')
-rw-r--r--gcc/testsuite/ChangeLog11
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c115
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c24
-rw-r--r--gcc/testsuite/gcc.dg/plugin/plugin.exp4
-rw-r--r--gcc/testsuite/lib/prune.exp2
5 files changed, 154 insertions, 2 deletions
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index b59b4bb..3da9f3b 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,14 @@
+2018-08-09 David Malcolm <dmalcolm@redhat.com>
+
+ PR other/84889
+ * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: New
+ test.
+ * gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c:
+ New test.
+ * gcc.dg/plugin/plugin.exp (plugin_test_list): Add the new tests.
+ * lib/prune.exp: Add -fno-diagnostics-show-line-numbers to
+ TEST_ALWAYS_FLAGS.
+
2018-08-09 Richard Sandiford <richard.sandiford@arm.com>
PR tree-optimization/86858
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c
new file mode 100644
index 0000000..66a2faa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c
@@ -0,0 +1,115 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdiagnostics-show-caret -fdiagnostics-show-line-numbers" } */
+
+/* This is a collection of unittests for diagnostic_show_locus;
+ see the overview in diagnostic_plugin_test_show_locus.c.
+
+ In particular, note the discussion of why we need a very long line here:
+01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
+ and that we can't use macros in this file. */
+
+void test_simple (void)
+{
+#if 0
+ myvar = myvar.x; /* { dg-warning "test" } */
+
+/* { dg-begin-multiline-output "" }
+14 | myvar = myvar.x;
+ | ~~~~~^~
+ { dg-end-multiline-output "" } */
+#endif
+}
+
+void test_multiline (void)
+{
+#if 0
+ x = (first_function ()
+ + second_function ()); /* { dg-warning "test" } */
+
+/* { dg-begin-multiline-output "" }
+26 | x = (first_function ()
+ | ~~~~~~~~~~~~~~~~~
+27 | + second_function ());
+ | ^ ~~~~~~~~~~~~~~~~~~
+ { dg-end-multiline-output "" } */
+#endif
+}
+
+void test_very_wide_line (void)
+{
+#if 0
+ float f = foo * bar; /* { dg-warning "95: test" } */
+/* { dg-begin-multiline-output "" }
+ | 0 0 0 0 0 0 1
+ | 4 5 6 7 8 9 0
+ | 0123456789012345678901234567890123456789012345678901234567890123456789
+41 | float f = foo * bar;
+ | ~~~~^~~~~
+ | bar * foo
+ { dg-end-multiline-output "" } */
+#endif
+}
+
+/* Unit test for rendering of insertion fixit hints
+ (example taken from PR 62316). */
+
+void test_fixit_insert (void)
+{
+#if 0
+ int a[2][2] = { 0, 1 , 2, 3 }; /* { dg-warning "insertion hints" } */
+/* { dg-begin-multiline-output "" }
+59 | int a[2][2] = { 0, 1 , 2, 3 };
+ | ^~~~
+ | { }
+ { dg-end-multiline-output "" } */
+#endif
+}
+
+/* Unit test for rendering of "remove" fixit hints. */
+
+void test_fixit_remove (void)
+{
+#if 0
+ int a;; /* { dg-warning "example of a removal hint" } */
+/* { dg-begin-multiline-output "" }
+73 | int a;;
+ | ^
+ | -
+ { dg-end-multiline-output "" } */
+#endif
+}
+
+/* Unit test for rendering of "replace" fixit hints. */
+
+void test_fixit_replace (void)
+{
+#if 0
+ gtk_widget_showall (dlg); /* { dg-warning "example of a replacement hint" } */
+/* { dg-begin-multiline-output "" }
+87 | gtk_widget_showall (dlg);
+ | ^~~~~~~~~~~~~~~~~~
+ | gtk_widget_show_all
+ { dg-end-multiline-output "" } */
+#endif
+}
+
+
+/* Unit test for rendering of fix-it hints that add new lines. */
+
+void test_fixit_insert_newline (void)
+{
+#if 0
+ switch (op)
+ {
+ case 'a':
+ x = a;
+ case 'b': /* { dg-warning "newline insertion" } */
+ x = b;
+ }
+/* { dg-begin-multiline-output "" }
+ |+ break;
+106 | case 'b':
+ | ^~~~~~~~
+ { dg-end-multiline-output "" } */
+#endif
+}
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c
new file mode 100644
index 0000000..a80b6de
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdiagnostics-show-caret -fplugin-arg-diagnostic_plugin_test_show_locus-color -fdiagnostics-show-line-numbers" } */
+
+/* This is a collection of unittests for diagnostic_show_locus;
+ see the overview in diagnostic_plugin_test_show_locus.c.
+
+ In particular, note the discussion of why we need a very long line here:
+01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
+ and that we can't use macros in this file. */
+
+void test_multiline (void)
+{
+#if 0
+ x = (first_function ()
+ + second_function ()); /* { dg-warning "test" } */
+
+/* { dg-begin-multiline-output "" }
+14 | x = (first_function ()
+ | ~~~~~~~~~~~~~~~~~
+15 | + second_function ());
+ | ^ ~~~~~~~~~~~~~~~~~~
+ { dg-end-multiline-output "" } */
+#endif
+}
diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp
index 5a19fc9..b2f8507 100644
--- a/gcc/testsuite/gcc.dg/plugin/plugin.exp
+++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp
@@ -72,8 +72,10 @@ set plugin_test_list [list \
{ diagnostic_plugin_test_show_locus.c \
diagnostic-test-show-locus-bw.c \
diagnostic-test-show-locus-color.c \
+ diagnostic-test-show-locus-bw-line-numbers.c \
+ diagnostic-test-show-locus-color-line-numbers.c \
diagnostic-test-show-locus-parseable-fixits.c \
- diagnostic-test-show-locus-generate-patch.c } \
+ diagnostic-test-show-locus-generate-patch.c }\
{ diagnostic_plugin_test_tree_expression_range.c \
diagnostic-test-expressions-1.c } \
{ diagnostic_plugin_show_trees.c \
diff --git a/gcc/testsuite/lib/prune.exp b/gcc/testsuite/lib/prune.exp
index 1e11dc9..df36c34 100644
--- a/gcc/testsuite/lib/prune.exp
+++ b/gcc/testsuite/lib/prune.exp
@@ -21,7 +21,7 @@ load_lib multiline.exp
if ![info exists TEST_ALWAYS_FLAGS] {
set TEST_ALWAYS_FLAGS ""
}
-set TEST_ALWAYS_FLAGS "-fno-diagnostics-show-caret -fdiagnostics-color=never $TEST_ALWAYS_FLAGS"
+set TEST_ALWAYS_FLAGS "-fno-diagnostics-show-caret -fno-diagnostics-show-line-numbers -fdiagnostics-color=never $TEST_ALWAYS_FLAGS"
proc prune_gcc_output { text } {
global srcdir