On Fri, May 16, 2025 at 01:34:14PM +0000, Qing Zhao wrote:
> Adding -fdiagnotics-details into GCC to provide more hints to the
> end users on how the warnings come from, in order to help the user
> to locate the exact location in source code on the specific warnings
> due to compiler optimizations.

I just needed to examine an unexpected -Wrestrict warning, and
discovered that this patch didn't help with it, but in looking at the
implementation details, it turned out to be trivial to expand coverage
to include -Wrestrict, which worked for me, and got me the
diagnostics I needed[1].

Could you include this patch in the next version of the series too? I'll
put it to use! :)

-Kees

[1] https://lore.kernel.org/all/202505191117.C094A90F88@keescook/


diff --git a/gcc/gimple-ssa-warn-restrict.cc b/gcc/gimple-ssa-warn-restrict.cc
index a52307866cc4..0f6eddf01e4e 100644
--- a/gcc/gimple-ssa-warn-restrict.cc
+++ b/gcc/gimple-ssa-warn-restrict.cc
@@ -1448,6 +1448,8 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
 
   tree func = gimple_call_fndecl (call);
 
+  rich_location_with_details richloc (loc, call);
+
   /* To avoid a combinatorial explosion of diagnostics format the offsets
      or their ranges as strings and use them in the warning calls below.  */
   char offstr[3][64];
@@ -1493,7 +1495,7 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
       if (sizrange[0] == sizrange[1])
        {
          if (ovlsiz[0] == ovlsiz[1])
-           warning_at (loc, OPT_Wrestrict,
+           warning_at (&richloc, OPT_Wrestrict,
                        sizrange[0] == 1
                        ? (ovlsiz[0] == 1
                           ? G_("%qD accessing %wu byte at offsets %s "
@@ -1510,7 +1512,7 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
                        func, sizrange[0],
                        offstr[0], offstr[1], ovlsiz[0], offstr[2]);
          else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
-           warning_n (loc, OPT_Wrestrict, sizrange[0],
+           warning_n (&richloc, OPT_Wrestrict, sizrange[0],
                       "%qD accessing %wu byte at offsets %s "
                       "and %s overlaps between %wu and %wu bytes "
                       "at offset %s",
@@ -1520,7 +1522,7 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
                       func, sizrange[0], offstr[0], offstr[1],
                       ovlsiz[0], ovlsiz[1], offstr[2]);
          else
-           warning_n (loc, OPT_Wrestrict, sizrange[0],
+           warning_n (&richloc, OPT_Wrestrict, sizrange[0],
                       "%qD accessing %wu byte at offsets %s and "
                       "%s overlaps %wu or more bytes at offset %s",
                       "%qD accessing %wu bytes at offsets %s and "
@@ -1533,7 +1535,7 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
       if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
        {
          if (ovlsiz[0] == ovlsiz[1])
-           warning_n (loc, OPT_Wrestrict, ovlsiz[0],
+           warning_n (&richloc, OPT_Wrestrict, ovlsiz[0],
                       "%qD accessing between %wu and %wu bytes "
                       "at offsets %s and %s overlaps %wu byte at "
                       "offset %s",
@@ -1543,7 +1545,7 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
                       func, sizrange[0], sizrange[1],
                       offstr[0], offstr[1], ovlsiz[0], offstr[2]);
          else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
-           warning_at (loc, OPT_Wrestrict,
+           warning_at (&richloc, OPT_Wrestrict,
                        "%qD accessing between %wu and %wu bytes at "
                        "offsets %s and %s overlaps between %wu and %wu "
                        "bytes at offset %s",
@@ -1551,7 +1553,7 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
                        offstr[0], offstr[1], ovlsiz[0], ovlsiz[1],
                        offstr[2]);
          else
-           warning_at (loc, OPT_Wrestrict,
+           warning_at (&richloc, OPT_Wrestrict,
                        "%qD accessing between %wu and %wu bytes at "
                        "offsets %s and %s overlaps %wu or more bytes "
                        "at offset %s",
@@ -1564,7 +1566,7 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
        ovlsiz[1] = maxobjsize.to_shwi ();
 
       if (ovlsiz[0] == ovlsiz[1])
-       warning_n (loc, OPT_Wrestrict, ovlsiz[0],
+       warning_n (&richloc, OPT_Wrestrict, ovlsiz[0],
                   "%qD accessing %wu or more bytes at offsets "
                   "%s and %s overlaps %wu byte at offset %s",
                   "%qD accessing %wu or more bytes at offsets "
@@ -1572,14 +1574,14 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
                   func, sizrange[0], offstr[0], offstr[1],
                   ovlsiz[0], offstr[2]);
       else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
-       warning_at (loc, OPT_Wrestrict,
+       warning_at (&richloc, OPT_Wrestrict,
                    "%qD accessing %wu or more bytes at offsets %s "
                    "and %s overlaps between %wu and %wu bytes "
                    "at offset %s",
                    func, sizrange[0], offstr[0], offstr[1],
                    ovlsiz[0], ovlsiz[1], offstr[2]);
       else
-       warning_at (loc, OPT_Wrestrict,
+       warning_at (&richloc, OPT_Wrestrict,
                    "%qD accessing %wu or more bytes at offsets %s "
                    "and %s overlaps %wu or more bytes at offset %s",
                    func, sizrange[0], offstr[0], offstr[1],
@@ -1607,14 +1609,14 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
       if (ovlsiz[1] == 1)
        {
          if (open_range)
-           warning_n (loc, OPT_Wrestrict, sizrange[1],
+           warning_n (&richloc, OPT_Wrestrict, sizrange[1],
                       "%qD accessing %wu byte may overlap "
                       "%wu byte",
                       "%qD accessing %wu bytes may overlap "
                       "%wu byte",
                       func, sizrange[1], ovlsiz[1]);
          else
-           warning_n (loc, OPT_Wrestrict, sizrange[1],
+           warning_n (&richloc, OPT_Wrestrict, sizrange[1],
                       "%qD accessing %wu byte at offsets %s "
                       "and %s may overlap %wu byte at offset %s",
                       "%qD accessing %wu bytes at offsets %s "
@@ -1625,14 +1627,14 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
        }
 
       if (open_range)
-       warning_n (loc, OPT_Wrestrict, sizrange[1],
+       warning_n (&richloc, OPT_Wrestrict, sizrange[1],
                   "%qD accessing %wu byte may overlap "
                   "up to %wu bytes",
                   "%qD accessing %wu bytes may overlap "
                   "up to %wu bytes",
                   func, sizrange[1], ovlsiz[1]);
       else
-       warning_n (loc, OPT_Wrestrict, sizrange[1],
+       warning_n (&richloc, OPT_Wrestrict, sizrange[1],
                   "%qD accessing %wu byte at offsets %s and "
                   "%s may overlap up to %wu bytes at offset %s",
                   "%qD accessing %wu bytes at offsets %s and "
@@ -1645,14 +1647,14 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
   if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
     {
       if (open_range)
-       warning_n (loc, OPT_Wrestrict, ovlsiz[1],
+       warning_n (&richloc, OPT_Wrestrict, ovlsiz[1],
                   "%qD accessing between %wu and %wu bytes "
                   "may overlap %wu byte",
                   "%qD accessing between %wu and %wu bytes "
                   "may overlap up to %wu bytes",
                   func, sizrange[0], sizrange[1], ovlsiz[1]);
       else
-       warning_n (loc, OPT_Wrestrict, ovlsiz[1],
+       warning_n (&richloc, OPT_Wrestrict, ovlsiz[1],
                   "%qD accessing between %wu and %wu bytes "
                   "at offsets %s and %s may overlap %wu byte "
                   "at offset %s",
@@ -1664,7 +1666,7 @@ maybe_diag_overlap (location_t loc, gimple *call, 
builtin_access &acs)
       return true;
     }
 
-  warning_n (loc, OPT_Wrestrict, ovlsiz[1],
+  warning_n (&richloc, OPT_Wrestrict, ovlsiz[1],
             "%qD accessing %wu or more bytes at offsets %s "
             "and %s may overlap %wu byte at offset %s",
             "%qD accessing %wu or more bytes at offsets %s "



-- 
Kees Cook

Reply via email to