Qing Zhao 6faa3cfe60 Provide new option -fdiagnostics-show-context=N for -Warray-bounds, -Wstringop-* warnings [PR109071,PR85788,PR88771,PR106762,PR108770,PR115274,PR117179]
'-fdiagnostics-show-context[=DEPTH]'
'-fno-diagnostics-show-context'
     With this option, the compiler might print the interesting control
     flow chain that guards the basic block of the statement which has
     the warning.  DEPTH is the maximum depth of the control flow chain.
     Currently, The list of the impacted warning options includes:
     '-Warray-bounds', '-Wstringop-overflow', '-Wstringop-overread',
     '-Wstringop-truncation'.  and '-Wrestrict'.  More warning options
     might be added to this list in future releases.  The forms
     '-fdiagnostics-show-context' and '-fno-diagnostics-show-context'
     are aliases for '-fdiagnostics-show-context=1' and
     '-fdiagnostics-show-context=0', respectively.

For example:

$ cat t.c
extern void warn(void);
static inline void assign(int val, int *regs, int *index)
{
  if (*index >= 4)
    warn();
  *regs = val;
}
struct nums {int vals[4];};

void sparx5_set (int *ptr, struct nums *sg, int index)
{
  int *val = &sg->vals[index];

  assign(0,    ptr, &index);
  assign(*val, ptr, &index);
}

$ gcc -Wall -O2  -c -o t.o t.c
t.c: In function ‘sparx5_set’:
t.c:12:23: warning: array subscript 4 is above array bounds of ‘int[4]’ [-Warray-bounds=]
   12 |   int *val = &sg->vals[index];
      |	       ~~~~~~~~^~~~~~~
t.c:8:18: note: while referencing ‘vals’
    8 | struct nums {int vals[4];};
      |		  ^~~~

In the above, Although the warning is correct in theory, the warning message
itself is confusing to the end-user since there is information that cannot
be connected to the source code directly.

It will be a nice improvement to add more information in the warning message
to report where such index value come from.

With the new option -fdiagnostics-show-context=1, the warning message for
the above testing case is now:

$ gcc -Wall -O2 -fdiagnostics-show-context=1 -c -o t.o t.c
t.c: In function ‘sparx5_set’:
t.c:12:23: warning: array subscript 4 is above array bounds of ‘int[4]’ [-Warray-bounds=]
   12 |   int *val = &sg->vals[index];
      |	       ~~~~~~~~^~~~~~~
  ‘sparx5_set’: events 1-2
    4 |   if (*index >= 4)
      |      ^
      |      |
      |      (1) when the condition is evaluated to true
......
   12 |   int *val = &sg->vals[index];
      |	       ~~~~~~~~~~~~~~~
      |		       |
      |		       (2) warning happens here
t.c:8:18: note: while referencing ‘vals’
    8 | struct nums {int vals[4];};
      |		  ^~~~

	PR tree-optimization/109071
	PR tree-optimization/85788
	PR tree-optimization/88771
	PR tree-optimization/106762
	PR tree-optimization/108770
	PR tree-optimization/115274
	PR tree-optimization/117179

gcc/ChangeLog:

	* Makefile.in (OBJS): Add diagnostic-context-rich-location.o.
	* common.opt (fdiagnostics-show-context): New option.
	(fdiagnostics-show-context=): New option.
	* diagnostic-context-rich-location.cc: New file.
	* diagnostic-context-rich-location.h: New file.
	* doc/invoke.texi (fdiagnostics-details): Add
	documentation for the new options.
	* gimple-array-bounds.cc (check_out_of_bounds_and_warn): Add
	one new parameter. Use rich location with details for warning_at.
	(array_bounds_checker::check_array_ref): Use rich location with
	ditails for warning_at.
	(array_bounds_checker::check_mem_ref): Add one new parameter.
	Use rich location with details for warning_at.
	(array_bounds_checker::check_addr_expr): Use rich location with
	move_history_diagnostic_path for warning_at.
	(array_bounds_checker::check_array_bounds): Call check_mem_ref with
	one more parameter.
	* gimple-array-bounds.h: Update prototype for check_mem_ref.
	* gimple-ssa-warn-access.cc (warn_string_no_nul): Use rich location
	with details for warning_at.
	(maybe_warn_nonstring_arg): Likewise.
	(maybe_warn_for_bound): Likewise.
	(warn_for_access): Likewise.
	(check_access): Likewise.
	(pass_waccess::check_strncat): Likewise.
	(pass_waccess::maybe_check_access_sizes): Likewise.
	* gimple-ssa-warn-restrict.cc (pass_wrestrict::execute): Calculate
	dominance info for diagnostics show context.
	(maybe_diag_overlap): Use rich location with details for warning_at.
	(maybe_diag_access_bounds): Use rich location with details for
	warning_at.

gcc/testsuite/ChangeLog:

	* gcc.dg/pr109071.c: New test.
	* gcc.dg/pr109071_1.c: New test.
	* gcc.dg/pr109071_10.c: New test.
	* gcc.dg/pr109071_11.c: New test.
	* gcc.dg/pr109071_12.c: New test.
	* gcc.dg/pr109071_2.c: New test.
	* gcc.dg/pr109071_3.c: New test.
	* gcc.dg/pr109071_4.c: New test.
	* gcc.dg/pr109071_5.c: New test.
	* gcc.dg/pr109071_6.c: New test.
	* gcc.dg/pr109071_7.c: New test.
	* gcc.dg/pr109071_8.c: New test.
	* gcc.dg/pr109071_9.c: New test.
	* gcc.dg/pr117375.c: New test.
2025-08-20 15:57:10 +00:00
2025-08-11 00:16:52 +00:00
2025-08-18 00:18:52 +00:00
2025-08-16 00:19:12 +00:00
2025-08-17 00:19:17 +00:00
2025-08-16 00:19:12 +00:00
2025-08-14 00:20:55 +00:00
2025-08-07 00:20:46 +00:00
2025-08-07 00:20:46 +00:00
2025-08-06 00:21:26 +00:00
2025-08-17 00:19:17 +00:00
2025-08-20 00:19:40 +00:00
2025-08-19 00:20:02 +00:00

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.
Description
No description provided
Readme 4.2 GiB
Languages
C++ 30.8%
C 30.2%
Ada 14.4%
D 6.1%
Go 5.7%
Other 12.3%