https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106762

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Qing Zhao <qinz...@gcc.gnu.org>:

https://gcc.gnu.org/g:6faa3cfe60ff9769d1bebfffdd2c7325217d7389

commit r16-3303-g6faa3cfe60ff9769d1bebfffdd2c7325217d7389
Author: Qing Zhao <qing.z...@oracle.com>
Date:   Wed Aug 20 15:56:06 2025 +0000

    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.
  • [Bug tree-optimization/106762] ... cvs-commit at gcc dot gnu.org via Gcc-bugs

Reply via email to