public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-948] PR middle-end/100684 - spurious -Wnonnull with -O1 on a C++ lambda
@ 2021-05-20 19:16 Martin Sebor
0 siblings, 0 replies; only message in thread
From: Martin Sebor @ 2021-05-20 19:16 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:9480491a6447576e8e695b8ea3c4989cf72c9670
commit r12-948-g9480491a6447576e8e695b8ea3c4989cf72c9670
Author: Martin Sebor <msebor@redhat.com>
Date: Thu May 20 13:15:58 2021 -0600
PR middle-end/100684 - spurious -Wnonnull with -O1 on a C++ lambda
gcc/ChangeLog:
PR middle-end/100684
* tree-ssa-ccp.c (pass_post_ipa_warn::execute): Handle C++ lambda.
gcc/testsuite/ChangeLog:
PR middle-end/100684
* g++.dg/warn/Wnonnull13.C: New test.
* g++.dg/warn/Wnonnull14.C: New test.
* g++.dg/warn/Wnonnull15.C: New test.
Diff:
---
gcc/testsuite/g++.dg/warn/Wnonnull13.C | 28 ++++++++++++++++++++++++++++
gcc/testsuite/g++.dg/warn/Wnonnull14.C | 28 ++++++++++++++++++++++++++++
gcc/testsuite/g++.dg/warn/Wnonnull15.C | 28 ++++++++++++++++++++++++++++
gcc/tree-ssa-ccp.c | 4 ++++
4 files changed, 88 insertions(+)
diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull13.C b/gcc/testsuite/g++.dg/warn/Wnonnull13.C
new file mode 100644
index 00000000000..e3279764ac0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wnonnull13.C
@@ -0,0 +1,28 @@
+/* PR middle-end/100684 - spurious -Wnonnull with -O1 on a C++ lambda
+ { dg-do compile { target c++11 } }
+ { dg-options "-O0 -Wall -fsanitize=undefined" } */
+
+#define NONNULL __attribute__ ((nonnull))
+
+typedef int F (const char *);
+
+NONNULL int f (const char *);
+
+int nowarn_O0 ()
+{
+ return static_cast<F*>([](const char *s){ return f (s); })("O0");
+ // { dg-bogus "\\\[-Wnonnull" "" { target *-*-* } .-1 }
+}
+
+int warn_O0 ()
+{
+ return static_cast<F*>([] NONNULL (const char *){ return 0; })(0);
+ // { dg-warning "\\\[-Wnonnull" "" { target *-*-* } .-1 }
+}
+
+int warn_O0_inline ()
+{
+ return static_cast<F*>([](const char *s){ return f (s); })(0);
+ // { dg-warning "\\\[-Wnonnull" "lambda not inlined" { xfail *-*-* } .-1 }
+}
+
diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull14.C b/gcc/testsuite/g++.dg/warn/Wnonnull14.C
new file mode 100644
index 00000000000..16d7ec3f573
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wnonnull14.C
@@ -0,0 +1,28 @@
+/* PR middle-end/100684 - spurious -Wnonnull with -O1 on a C++ lambda
+ { dg-do compile { target c++11 } }
+ { dg-options "-Og -Wall -fsanitize=undefined" } */
+
+#define NONNULL __attribute__ ((nonnull))
+
+typedef int F (const char *);
+
+__attribute__ ((nonnull)) int f (const char *);
+
+int nowarn_Og ()
+{
+ return static_cast<F*>([](const char *s){ return f (s); })("Og");
+ // { dg-bogus "'this' pointer is null" "" { target *-*-* } .-1 }
+}
+
+int warn_Og ()
+{
+ return static_cast<F*>([] NONNULL (const char *){ return 0; })(0);
+ // { dg-warning "\\\[-Wnonnull" "" { target *-*-* } .-1 }
+}
+
+int warn_Og_inline ()
+{
+ const char *p = 0;
+ return static_cast<F*>([](const char *s){ return f (s); })(p);
+ // { dg-warning "\\\[-Wnonnull" "lambda not inlined" { xfail *-*-* } .-1 }
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull15.C b/gcc/testsuite/g++.dg/warn/Wnonnull15.C
new file mode 100644
index 00000000000..36a2ab48789
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wnonnull15.C
@@ -0,0 +1,28 @@
+/* PR middle-end/100684 - spurious -Wnonnull with -O1 on a C++ lambda
+ { dg-do compile { target c++11 } }
+ { dg-options "-O1 -Wall -fsanitize=undefined" } */
+
+#define NONNULL __attribute__ ((nonnull))
+
+typedef int F (const char *);
+
+NONNULL int f (const char *);
+
+int nowarn_O1 ()
+{
+ return static_cast<F*>([](const char *s){ return f (s); })("O1");
+ // { dg-bogus "\\\[-Wnonnull" "" { target *-*-* } .-1 }
+}
+
+int warn_O1 ()
+{
+ return static_cast<F*>([] NONNULL (const char *){ return 0; })(0);
+ // { dg-warning "\\\[-Wnonnull" "" { target *-*-* } .-1 }
+}
+
+int warn_O1_inline ()
+{
+ const char *p = 0;
+ return static_cast<F*>([](const char *s){ return f (s); })(p);
+ // { dg-warning "\\\[-Wnonnull" "lambda not inlined" { xfail *-*-* } .-1 }
+}
diff --git a/gcc/tree-ssa-ccp.c b/gcc/tree-ssa-ccp.c
index bf31f035153..3834212b867 100644
--- a/gcc/tree-ssa-ccp.c
+++ b/gcc/tree-ssa-ccp.c
@@ -3536,6 +3536,7 @@ pass_post_ipa_warn::execute (function *fun)
continue;
tree fndecl = gimple_call_fndecl (stmt);
+ const bool closure = fndecl && DECL_LAMBDA_FUNCTION_P (fndecl);
for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
{
@@ -3544,6 +3545,9 @@ pass_post_ipa_warn::execute (function *fun)
continue;
if (!integer_zerop (arg))
continue;
+ if (i == 0 && closure)
+ /* Avoid warning for the first argument to lambda functions. */
+ continue;
if (!bitmap_empty_p (nonnullargs)
&& !bitmap_bit_p (nonnullargs, i))
continue;
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-05-20 19:16 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-20 19:16 [gcc r12-948] PR middle-end/100684 - spurious -Wnonnull with -O1 on a C++ lambda Martin Sebor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).