diagnostics: add minimum width to left margin for line numbers

Programming / Compilers / GCC - dmalcolm [138bc75d-0d04-0410-961f-82ee72b054a4] - 15 October 2018 22:16 EDT

This patch adds a minimum width to the left margin used for printing line numbers. I set the default to 6. Hence rather than:

some-filename:9:1: some message 9 | some source text | ^~~~~~~~~~~~~~~~ some-filename:10:1: another message 10 | more source text | ^~~~~~~~~~~~~~~~

we now print:

some-filename:9:42: some message 9 | some source text | ^~~~~~~~~~~~~~~~ some-filename:10:42: another message 10 | more source text | ^~~~~~~~~~~~~~~~

This implicitly fixes issues with margins failing to line up due to different lengths of the number when we haven't read the full file yet and so don't know the highest possible line number, for line numbers up to 99999.

Doing so adds some whitespace on the left-hand side, for non-huge files, at least. I believe that this makes it easier to see where each diagnostic starts, by visually breaking things up at the leftmost column; my hope is to make it easier for the eye to see the different diagnostics as if they were different "paragraphs".

gcc/ChangeLog:
- common.opt (fdiagnostics-minimum-margin-width=): New option.
- diagnostic-show-locus.c (layout::layout): Apply the minimum margin width. (layout::start_annotation_line): Only print up to 3 of the margin character, to avoid touching the left-hand side. (selftest::test_diagnostic_show_locus_fixit_lines): Update for minimum margin width, as set by test_diagnostic_context's ctor. (selftest::test_fixit_insert_containing_newline): Likewise. (selftest::test_fixit_insert_containing_newline_2): Likewise. (selftest::test_line_numbers_multiline_range): Clear dc.min_margin_width.
- diagnostic.c (diagnostic_initialize): Initialize min_margin_width.
- diagnostic.h (struct diagnostic_context): Add field "min_margin_width".
- doc/invoke.texi: Add -fdiagnostics-minimum-margin-width=.
- opts.c (common_handle_option): Handle OPT_fdiagnostics_minimum_margin_width_.
- selftest-diagnostic.c (selftest::test_diagnostic_context::test_diagnostic_context): Initialize min_margin_width to 6.
- toplev.c (general_init): Initialize global_dc->min_margin_width.

gcc/testsuite/ChangeLog:
- gcc.dg/missing-header-fixit-3.c: Update expected indentation to reflect minimum margin width.
- gcc.dg/missing-header-fixit-4.c: Likewise.
- gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: Likewise.
- gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c: Likewise.
- gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers-2.c: New test.
- gcc.dg/plugin/plugin.exp (plugin_test_list): Add it.

31087b7e830 diagnostics: add minimum width to left margin for line numbers
gcc/ChangeLog | 25 ++++++++++
gcc/common.opt | 4 ++
gcc/diagnostic-show-locus.c | 41 +++++++++------
gcc/diagnostic.c | 1 +
gcc/diagnostic.h | 4 ++
gcc/doc/invoke.texi | 6 +++
gcc/opts.c | 4 ++
gcc/selftest-diagnostic.c | 1 +
gcc/testsuite/ChangeLog | 13 +++++
gcc/testsuite/gcc.dg/missing-header-fixit-3.c | 8 +--
gcc/testsuite/gcc.dg/missing-header-fixit-4.c | 10 ++--
.../diagnostic-test-show-locus-bw-line-numbers-2.c | 22 ++++++++
.../diagnostic-test-show-locus-bw-line-numbers.c | 58 +++++++++++-----------
...diagnostic-test-show-locus-color-line-numbers.c | 12 ++---
gcc/testsuite/gcc.dg/plugin/plugin.exp | 1 +
gcc/toplev.c | 2 +
16 files changed, 152 insertions(+), 60 deletions(-)

Upstream: gcc.gnu.org


  • Share