c: Stray inform note with -Waddress [PR106947]
Commit Message
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.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/12?
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.
---
gcc/c/c-typeck.cc | 19 ++++++++++---------
gcc/testsuite/c-c++-common/Waddress-7.c | 22 ++++++++++++++++++++++
2 files changed, 32 insertions(+), 9 deletions(-)
create mode 100644 gcc/testsuite/c-c++-common/Waddress-7.c
base-commit: de40fab2f32b03c3d8f69f72c7f1e38694f93d35
Comments
On Mon, Sep 19, 2022 at 02:25:59PM -0400, Marek Polacek via Gcc-patches wrote:
> 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.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/12?
>
> 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.
Ok, thanks.
Jakub
@@ -11738,18 +11738,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);
}
new file mode 100644
@@ -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;
+}