]> gcc.gnu.org Git - gcc.git/commitdiff
c: Stray inform note with -Waddress [PR106947]
authorMarek Polacek <polacek@redhat.com>
Mon, 19 Sep 2022 18:12:55 +0000 (14:12 -0400)
committerMarek Polacek <polacek@redhat.com>
Mon, 19 Sep 2022 18:42:58 +0000 (14:42 -0400)
A trivial fix for maybe_warn_for_null_address where we print an
inform note without first checking the return value of a warning
call.

PR c/106947

gcc/c/ChangeLog:

* c-typeck.cc (maybe_warn_for_null_address): Don't emit stray
notes.

gcc/testsuite/ChangeLog:

* c-c++-common/Waddress-7.c: New test.

(cherry picked from commit 2d9429d5c0f86f588bdfd85bb9e236d2be367d3f)

gcc/c/c-typeck.cc
gcc/testsuite/c-c++-common/Waddress-7.c [new file with mode: 0644]

index e130196a3a7271064b617504f0fd1a74d1e18ffa..0e55fd1db4ba586624b323103aa883db7be68d9d 100644 (file)
@@ -11681,18 +11681,19 @@ maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
       || from_macro_expansion_at (loc))
     return;
 
+  bool w;
   if (code == EQ_EXPR)
-    warning_at (loc, OPT_Waddress,
-               "the comparison will always evaluate as %<false%> "
-               "for the address of %qE will never be NULL",
-               op);
+    w = warning_at (loc, OPT_Waddress,
+                   "the comparison will always evaluate as %<false%> "
+                   "for the address of %qE will never be NULL",
+                   op);
   else
-    warning_at (loc, OPT_Waddress,
-               "the comparison will always evaluate as %<true%> "
-               "for the address of %qE will never be NULL",
-               op);
+    w = warning_at (loc, OPT_Waddress,
+                   "the comparison will always evaluate as %<true%> "
+                   "for the address of %qE will never be NULL",
+                   op);
 
-  if (DECL_P (op))
+  if (w && DECL_P (op))
     inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
 }
 
diff --git a/gcc/testsuite/c-c++-common/Waddress-7.c b/gcc/testsuite/c-c++-common/Waddress-7.c
new file mode 100644 (file)
index 0000000..1799485
--- /dev/null
@@ -0,0 +1,22 @@
+/* PR c/106947 */
+/* { dg-do compile } */
+/* { dg-options "-Waddress" } */
+
+#ifndef __cplusplus
+# define bool _Bool
+#endif
+
+#pragma GCC diagnostic ignored "-Waddress"
+int s; /* { dg-bogus "declared" } */
+bool e = &s;
+int
+main ()
+{
+  int error = 0;
+  {
+    bool e1 = &s;
+    if (!e1)
+      error = 1;
+  }
+  return error;
+}
This page took 0.076399 seconds and 5 git commands to generate.