Hello-

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

This is a short patch to fix the PR. Bootstrap + regtest (with and without
bootstrap-lto config) on x86-64 looks good. Does it look OK please? Thanks!

-Lewis

-- >8 --

The implementation of -Walloc-size-larger-than has logic to avoid issuing
the warning for ::operator new[] calls emitted by the C++ front end, which
otherwise produce known false positives.  The logic for suppressing the
warning only activates for the C++ front end, and so it does not prevent the
LTO front end from issuing the warning.  Fix by checking the language name
recorded in the trans_unit_decl associated with the function for LTO.

gcc/ChangeLog:

        PR tree-optimization/106409
        * gimple-ssa-warn-access.cc (maybe_warn_alloc_args_overflow): Adjust
        comment for clarity, and augment check for C++ to work in LTO as
        well.

gcc/testsuite/ChangeLog:

        PR tree-optimization/106409
        * g++.dg/lto/pr106409_0.C: New test.
---
 gcc/gimple-ssa-warn-access.cc         | 19 +++++++++++++------
 gcc/testsuite/g++.dg/lto/pr106409_0.C | 23 +++++++++++++++++++++++
 2 files changed, 36 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/lto/pr106409_0.C

diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index d12f797f36b..aa94a5ee5eb 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -2334,16 +2334,23 @@ maybe_warn_alloc_args_overflow (gimple *stmt, const 
tree args[2],
            }
          else if (tree_int_cst_lt (maxobjsize, args[i]))
            {
-             /* G++ emits calls to ::operator new[](SIZE_MAX) in C++98
-                mode and with -fno-exceptions as a way to indicate array
-                size overflow.  There's no good way to detect C++98 here
-                so avoid diagnosing these calls for all C++ modes.  */
+             /* G++ emits calls to ::operator new[](SIZE_MAX) in C++98 mode or
+                with -fno-exceptions as a way to indicate array size overflow.
+                Avoid diagnosing these calls.  Additionally, see e.g. PR99934,
+                G++ also potentially generates such calls in C++11 and later as
+                well, so suppress the diagnostic in all C++ modes.  */
+             const_tree context;
+             const char *lang;
              if (i == 0
                  && fn
                  && !args[1]
-                 && lang_GNU_CXX ()
                  && DECL_IS_OPERATOR_NEW_P (fn)
-                 && integer_all_onesp (args[i]))
+                 && integer_all_onesp (args[i])
+                 && (lang_GNU_CXX ()
+                     || (in_lto_p
+                         && (context = get_ultimate_context (fn))
+                         && (lang = TRANSLATION_UNIT_LANGUAGE (context))
+                         && startswith (lang, "GNU C++"))))
                continue;
 
              warned = warning_at (loc, OPT_Walloc_size_larger_than_,
diff --git a/gcc/testsuite/g++.dg/lto/pr106409_0.C 
b/gcc/testsuite/g++.dg/lto/pr106409_0.C
new file mode 100644
index 00000000000..464d13a7947
--- /dev/null
+++ b/gcc/testsuite/g++.dg/lto/pr106409_0.C
@@ -0,0 +1,23 @@
+/* PR tree-optimization/106409 */
+/* { dg-lto-do link } */
+/* { dg-lto-options { { -flto -W -Wall -O2 -fno-exceptions } { -flto -W -Wall 
-O2 -std=c++98 } { -flto -W -Wall -O2 -std=gnu++20 } } } */
+struct bb
+{
+  int t;
+  int t1;
+  int t2;
+  int t3;
+};
+
+[[gnu::noipa]]
+void *f(unsigned long paramCount)
+{
+    if (paramCount == 0)
+      return 0;
+    return new bb[paramCount]();
+}
+
+int main(void)
+{
+  f(100);
+}

Reply via email to