--- /dev/null
+/* 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 }
+}
+
--- /dev/null
+/* 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 }
+}
--- /dev/null
+/* 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 }
+}
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++)
{
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;