Go to file
David Malcolm 56b61d7fc4 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
2018-08-09 15:32:13 +00:00
config Add missing '|' as wrong patch was applied. 2018-07-05 14:28:01 -06:00
contrib [contrib] Add contrib/maintainers-verify.sh 2018-07-13 09:33:40 +00:00
fixincludes inclhack.def (aix_unistd): New. 2018-07-23 16:51:50 -04:00
gcc diagnostics: add line numbers to source (PR other/84889) 2018-08-09 15:32:13 +00:00
gnattools
gotools go/build, cmd/go: update to match recent changes to gc 2018-05-09 21:49:47 +00:00
include Add linker_output as prefix for LTO temps (PR lto/86548). 2018-07-26 12:13:14 +00:00
INSTALL
intl
libada
libatomic [ARM] Use __ARM_ARCH and __ARM_FEATURE_LDREX instead of __ARM_ARCH__ 2018-06-21 13:05:36 +02:00
libbacktrace libbacktrace: Suppress the default action-if-found for AC_CHECK_LIBS. 2018-08-05 20:24:59 +00:00
libcc1
libcpp [PATCH] line-map include-from representation 2018-08-08 18:13:00 +00:00
libdecnumber Add missing '|' as wrong patch was applied. 2018-07-05 14:28:01 -06:00
libffi Backport of RISC-V support for libffi 2018-05-08 10:29:16 +00:00
libgcc arm - correctly handle denormal results during softfp subtraction 2018-08-02 16:50:07 +00:00
libgfortran Reverting 'AsyncI/O patch committed' as it is breaking bare-metal builds. 2018-07-31 08:42:21 +00:00
libgo runtime: use poll rather than pollset for netpoll on AIX 2018-08-07 17:29:50 +00:00
libgomp [libgomp, nvptx] Fall back to cuLinkAddData/cuLinkCreate if _v2 not found 2018-08-08 14:26:37 +00:00
libhsail-rt [BRIGFE] phsa-specific optimizations 2018-05-04 19:43:57 +00:00
libiberty cplus-dem.c (remember_Btype): Don't call memcpy with LEN==0. 2018-07-30 13:47:01 +00:00
libitm Remove nfs bogon 2018-08-03 14:53:48 -06:00
libobjc Regenerate configure of target libraries 2018-04-24 09:45:26 -07:00
liboffloadmic
libquadmath Regenerate configure of target libraries 2018-04-24 09:45:26 -07:00
libsanitizer Cherry-pick compiler-rt revision 338606 (PR sanitizer/86022). 2018-08-02 09:32:58 +00:00
libssp Regenerate configure of target libraries 2018-04-24 09:45:26 -07:00
libstdc++-v3 2018-08-08 François Dumont <fdumont@gcc.gnu.org> 2018-08-08 20:28:21 +00:00
libvtv [testsuite] Add scan-ltrans-tree-dump 2018-05-02 12:16:32 +00:00
lto-plugin lto-plugin.c: (non_claimed_files): New static var. 2018-05-30 16:34:54 +00:00
maintainer-scripts re PR other/85622 (gcc-8.1.0/NEWS says it's not released yet) 2018-05-03 11:29:39 +02:00
zlib Allow building of the zlib component when the building takes place in the source directory. 2018-06-19 11:49:08 +00:00
.dir-locals.el
.gitattributes
.gitignore
ABOUT-NLS
ChangeLog MAINTAINERS: Update my email address. 2018-08-06 07:22:57 +00:00
ChangeLog.jit
ChangeLog.tree-ssa
compile
config-ml.in
config.guess Update config.guess and config.sub 2018-07-06 05:57:35 +00:00
config.rpath
config.sub Update config.sub 2018-07-06 06:22:08 +00:00
configure Introduce @unless/@endunless and postbootstrap Makefile targets 2018-06-30 02:47:29 +00:00
configure.ac Introduce @unless/@endunless and postbootstrap Makefile targets 2018-06-30 02:47:29 +00:00
COPYING
COPYING3
COPYING3.LIB
COPYING.LIB
COPYING.RUNTIME
depcomp
install-sh
libtool-ldflags
libtool.m4 libtool.m4: Sort output of 'find' to enable deterministic builds. 2018-07-05 13:13:45 -06:00
lt~obsolete.m4
ltgcc.m4
ltmain.sh libtool.m4: Sort output of 'find' to enable deterministic builds. 2018-07-05 13:13:45 -06:00
ltoptions.m4
ltsugar.m4
ltversion.m4
MAINTAINERS MAINTAINERS: Update my email address. 2018-08-06 07:22:57 +00:00
Makefile.def Makefile.def (fortran): Add check-target-libgomp-fortran. 2018-06-18 22:01:58 +00:00
Makefile.in Introduce @unless/@endunless and postbootstrap Makefile targets 2018-06-30 02:47:29 +00:00
Makefile.tpl Introduce @unless/@endunless and postbootstrap Makefile targets 2018-06-30 02:47:29 +00:00
missing
mkdep
mkinstalldirs
move-if-change
README
symlink-tree
ylwrap

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.