@@ -1281,6 +1281,10 @@ fdiagnostics-show-option
Common Var(flag_diagnostics_show_option) Init(1)
Amend appropriate diagnostic messages with the command line option that controls them.
+fdiagnostics-minimum-margin-width=
+Common Joined UInteger Var(diagnostics_minimum_margin_width) Init(6)
+Set minimum width of left margin of source code when showing source
+
fdisable-
Common Joined RejectNegative Var(common_deferred_options) Defer
-fdisable-[tree|rtl|ipa]-<pass>=range1+range2 disables an optimization pass.
@@ -930,6 +930,9 @@ layout::layout (diagnostic_context * context,
/* If we're showing jumps in the line-numbering, allow at least 3 chars. */
if (m_line_spans.length () > 1)
m_linenum_width = MAX (m_linenum_width, 3);
+ /* If there's a minimum margin width, apply it (subtracting 1 for the space
+ after the line number. */
+ m_linenum_width = MAX (m_linenum_width, context->min_margin_width - 1);
/* Adjust m_x_offset.
Center the primary caret to fit in max_width; all columns
@@ -1386,7 +1389,12 @@ layout::start_annotation_line (char margin_char) const
{
if (m_show_line_numbers_p)
{
- for (int i = 0; i < m_linenum_width; i++)
+ /* Print the margin. If MARGIN_CHAR != ' ', then print up to 3
+ of it, right-aligned, padded with spaces. */
+ int i;
+ for (i = 0; i < m_linenum_width - 3; i++)
+ pp_space (m_pp);
+ for (; i < m_linenum_width; i++)
pp_character (m_pp, margin_char);
pp_string (m_pp, " |");
}
@@ -3027,12 +3035,12 @@ test_diagnostic_show_locus_fixit_lines (const line_table_case &case_)
dc.show_line_numbers_p = true;
diagnostic_show_locus (&dc, &richloc, DK_ERROR);
ASSERT_STREQ ("\n"
- " 3 | y\n"
- " | .\n"
- "....\n"
- " 6 | : 0.0};\n"
- " | ^\n"
- " | =\n",
+ " 3 | y\n"
+ " | .\n"
+ "......\n"
+ " 6 | : 0.0};\n"
+ " | ^\n"
+ " | =\n",
pp_formatted_text (dc.printer));
}
}
@@ -3523,10 +3531,10 @@ test_fixit_insert_containing_newline (const line_table_case &case_)
dc.show_line_numbers_p = true;
diagnostic_show_locus (&dc, &richloc, DK_ERROR);
ASSERT_STREQ ("\n"
- "2 | x = a;\n"
- "+ |+ break;\n"
- "3 | case 'b':\n"
- " | ^~~~~~~~~\n",
+ " 2 | x = a;\n"
+ " +++ |+ break;\n"
+ " 3 | case 'b':\n"
+ " | ^~~~~~~~~\n",
pp_formatted_text (dc.printer));
}
}
@@ -3605,11 +3613,11 @@ test_fixit_insert_containing_newline_2 (const line_table_case &case_)
dc.show_line_numbers_p = true;
diagnostic_show_locus (&dc, &richloc, DK_ERROR);
ASSERT_STREQ ("\n"
- "+ |+#include <stdio.h>\n"
- "1 | test (int ch)\n"
- "2 | {\n"
- "3 | putchar (ch);\n"
- " | ^~~~~~~\n",
+ " +++ |+#include <stdio.h>\n"
+ " 1 | test (int ch)\n"
+ " 2 | {\n"
+ " 3 | putchar (ch);\n"
+ " | ^~~~~~~\n",
pp_formatted_text (dc.printer));
}
}
@@ -3734,6 +3742,7 @@ test_line_numbers_multiline_range ()
test_diagnostic_context dc;
dc.show_line_numbers_p = true;
+ dc.min_margin_width = 0;
gcc_rich_location richloc (loc);
diagnostic_show_locus (&dc, &richloc, DK_ERROR);
ASSERT_STREQ ("\n"
@@ -177,6 +177,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts)
context->colorize_source_p = false;
context->show_labels_p = false;
context->show_line_numbers_p = false;
+ context->min_margin_width = 0;
context->show_ruler_p = false;
context->parseable_fixits_p = false;
context->edit_context_ptr = NULL;
@@ -211,6 +211,10 @@ struct diagnostic_context
showing line numbers? */
bool show_line_numbers_p;
+ /* If printing source code, what should the minimum width of the margin
+ be? Line numbers will be right-aligned, and padded to this width. */
+ int min_margin_width;
+
/* Usable by plugins; if true, print a debugging ruler above the
source output. */
bool show_ruler_p;
@@ -270,6 +270,7 @@ Objective-C and Objective-C++ Dialects}.
-fdiagnostics-color=@r{[}auto@r{|}never@r{|}always@r{]} @gol
-fno-diagnostics-show-option -fno-diagnostics-show-caret @gol
-fno-diagnostics-show-labels -fno-diagnostics-show-line-numbers @gol
+-fdiagnostics-minimum-margin-width=@var{width} @gol
-fdiagnostics-parseable-fixits -fdiagnostics-generate-patch @gol
-fdiagnostics-show-template-tree -fno-elide-type @gol
-fno-show-column}
@@ -3819,6 +3820,11 @@ By default, when printing source code (via @option{-fdiagnostics-show-caret}),
a left margin is printed, showing line numbers. This option suppresses this
left margin.
+@item -fdiagnostics-minimum-margin-width=@var{width}
+@opindex -fdiagnostics-minimum-margin-width
+This option controls the minimum width of the left margin printed by
+@option{-fdiagnostics-show-line-numbers}. It defaults to 6.
+
@item -fdiagnostics-parseable-fixits
@opindex fdiagnostics-parseable-fixits
Emit fix-it hints in a machine-parseable format, suitable for consumption
@@ -2228,6 +2228,10 @@ common_handle_option (struct gcc_options *opts,
dc->show_option_requested = value;
break;
+ case OPT_fdiagnostics_minimum_margin_width_:
+ dc->min_margin_width = value;
+ break;
+
case OPT_fdump_:
/* Deferred. */
break;
@@ -40,6 +40,7 @@ test_diagnostic_context::test_diagnostic_context ()
show_labels_p = true;
show_column = true;
start_span = start_span_cb;
+ min_margin_width = 6;
}
test_diagnostic_context::~test_diagnostic_context ()
@@ -10,12 +10,12 @@ void test (int i, int j)
/* { 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);
- | ^~~~~~
+ 9 | printf ("%i of %i\n", i, j);
+ | ^~~~~~
{ dg-end-multiline-output "" } */
/* { dg-begin-multiline-output "" }
-+ |+#include <stdio.h>
-1 | /* Example of a fix-it hint that adds a #include directive,
+ +++ |+#include <stdio.h>
+ 1 | /* Example of a fix-it hint that adds a #include directive,
{ dg-end-multiline-output "" } */
#endif
}
@@ -11,13 +11,13 @@ void test (int i, int j)
/* { dg-warning "implicit declaration of function" "" { target *-*-* } printf } */
/* { dg-warning "incompatible implicit declaration" "" { target *-*-* } printf } */
/* { dg-begin-multiline-output "" }
-10 | printf ("%i of %i\n", i, j);
- | ^~~~~~
+ 10 | printf ("%i of %i\n", i, j);
+ | ^~~~~~
{ dg-end-multiline-output "" } */
/* { dg-message "include '<stdio.h>' or provide a declaration of 'printf'" "" { target *-*-* } 4 } */
/* { dg-begin-multiline-output "" }
-3 | #include "empty.h"
-+ |+#include <stdio.h>
-4 | int the_next_line;
+ 3 | #include "empty.h"
+ +++ |+#include <stdio.h>
+ 4 | int the_next_line;
{ dg-end-multiline-output "" } */
}
new file mode 100644
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* Verify "-fdiagnostics-minimum-margin-width=0". */
+/* { dg-options "-O -fdiagnostics-show-caret -fdiagnostics-show-line-numbers -fdiagnostics-minimum-margin-width=0" } */
+
+/* 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 "" }
+15 | myvar = myvar.x;
+ | ~~~~~^~
+ { dg-end-multiline-output "" } */
+#endif
+}
@@ -14,8 +14,8 @@ void test_simple (void)
myvar = myvar.x; /* { dg-warning "test" } */
/* { dg-begin-multiline-output "" }
-14 | myvar = myvar.x;
- | ~~~~~^~
+ 14 | myvar = myvar.x;
+ | ~~~~~^~
{ dg-end-multiline-output "" } */
#endif
}
@@ -27,12 +27,12 @@ void test_multiline (void)
+ second_function ()); /* { dg-warning "test" } */
/* { dg-begin-multiline-output "" }
-26 | x = (first_function ()
- | ~~~~~~~~~~~~~~~~~
-27 | + second_function ());
- | ^ ~~~~~~~~~~~~~~~~~~
- | |
- | label
+ 26 | x = (first_function ()
+ | ~~~~~~~~~~~~~~~~~
+ 27 | + second_function ());
+ | ^ ~~~~~~~~~~~~~~~~~~
+ | |
+ | label
{ dg-end-multiline-output "" } */
#endif
}
@@ -42,14 +42,14 @@ 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
-43 | float f = foo * bar;
- | ~~~~^~~~~
- | |
- | label
- | bar * foo
+ | 0 0 0 0 0 1 1
+ | 5 6 7 8 9 0 1
+ | 3456789012345678901234567890123456789012345678901234567890123456789012
+ 43 | float f = foo * bar;
+ | ~~~~^~~~~
+ | |
+ | label 0
+ | bar * foo
{ dg-end-multiline-output "" } */
#endif
}
@@ -62,9 +62,9 @@ void test_fixit_insert (void)
#if 0
int a[2][2] = { 0, 1 , 2, 3 }; /* { dg-warning "insertion hints" } */
/* { dg-begin-multiline-output "" }
-63 | int a[2][2] = { 0, 1 , 2, 3 };
- | ^~~~
- | { }
+ 63 | int a[2][2] = { 0, 1 , 2, 3 };
+ | ^~~~
+ | { }
{ dg-end-multiline-output "" } */
#endif
}
@@ -76,9 +76,9 @@ void test_fixit_remove (void)
#if 0
int a;; /* { dg-warning "example of a removal hint" } */
/* { dg-begin-multiline-output "" }
-77 | int a;;
- | ^
- | -
+ 77 | int a;;
+ | ^
+ | -
{ dg-end-multiline-output "" } */
#endif
}
@@ -90,9 +90,9 @@ void test_fixit_replace (void)
#if 0
gtk_widget_showall (dlg); /* { dg-warning "example of a replacement hint" } */
/* { dg-begin-multiline-output "" }
-91 | gtk_widget_showall (dlg);
- | ^~~~~~~~~~~~~~~~~~
- | gtk_widget_show_all
+ 91 | gtk_widget_showall (dlg);
+ | ^~~~~~~~~~~~~~~~~~
+ | gtk_widget_show_all
{ dg-end-multiline-output "" } */
#endif
}
@@ -111,10 +111,10 @@ void test_fixit_insert_newline (void)
x = b;
}
/* { dg-begin-multiline-output "" }
-109 | x = a;
-+++ |+ break;
-110 | case 'b':
- | ^~~~~~~~
+ 109 | x = a;
+ +++ |+ break;
+ 110 | case 'b':
+ | ^~~~~~~~
{ dg-end-multiline-output "" } */
#endif
}
@@ -15,12 +15,12 @@ void test_multiline (void)
+ second_function ()); /* { dg-warning "test" } */
/* { dg-begin-multiline-output "" }
-14 | x = ([32m[Kfirst_function ()[m[K
- | [32m[K~~~~~~~~~~~~~~~~~[m[K
-15 | [01;35m[K+[m[K [34m[Ksecond_function ()[m[K);
- | [01;35m[K^[m[K [34m[K~~~~~~~~~~~~~~~~~~[m[K
- | [01;35m[K|[m[K
- | [01;35m[Klabel[m[K
+ 14 | x = ([32m[Kfirst_function ()[m[K
+ | [32m[K~~~~~~~~~~~~~~~~~[m[K
+ 15 | [01;35m[K+[m[K [34m[Ksecond_function ()[m[K);
+ | [01;35m[K^[m[K [34m[K~~~~~~~~~~~~~~~~~~[m[K
+ | [01;35m[K|[m[K
+ | [01;35m[Klabel[m[K
{ dg-end-multiline-output "" } */
#endif
}
@@ -76,6 +76,7 @@ set plugin_test_list [list \
diagnostic-test-show-locus-color.c \
diagnostic-test-show-locus-no-labels.c \
diagnostic-test-show-locus-bw-line-numbers.c \
+ diagnostic-test-show-locus-bw-line-numbers-2.c \
diagnostic-test-show-locus-color-line-numbers.c \
diagnostic-test-show-locus-parseable-fixits.c \
diagnostic-test-show-locus-generate-patch.c }\
@@ -1120,6 +1120,8 @@ general_init (const char *argv0, bool init_signals)
= global_options_init.x_flag_diagnostics_show_line_numbers;
global_dc->show_option_requested
= global_options_init.x_flag_diagnostics_show_option;
+ global_dc->min_margin_width
+ = global_options_init.x_diagnostics_minimum_margin_width;
global_dc->show_column
= global_options_init.x_flag_show_column;
global_dc->internal_error = internal_error_function;