--
GCC currently treats -Wuse-after-free and -Wuse-after-free= as separate
options internally, with OPT_Wuse_after_free for no argument and
OPT_Wuse_after_free_ with argument. Make -Wuse-after-free an alias for
-Wuse-after-free=1 so both forms go through the same option code.
Switch the warning and suppression sites to OPT_Wuse_after_free_ so
pragmas, diagnostic classification, and suppression all refer to the same
option.
Document that -Wuse-after-free is equivalent to -Wuse-after-free=1.
On Arm AAPCS targets, constructors and destructors return this. In
maybe_prepare_return_this, suppressing OPT_Wuse_after_free for this records
the suppression under NW_OTHER, since OPT_Wuse_after_free is not explicitly
mapped to a diagnostic group. That can suppress unrelated NW_OTHER
warnings, such as -Wdeprecated-declarations. OPT_Wuse_after_free_ is
mapped to NW_DANGLING, so using it keeps the suppression scoped to the
use-after-free warning.
PR driver/124058
gcc/ChangeLog:
* common.opt (Wuse-after-free): Make an alias for
-Wuse-after-free=1.
* doc/invoke.texi: Document alias.
* gimple-ssa-warn-access.cc (pass_waccess::warn_invalid_pointer):
Use OPT_Wuse_after_free_.
gcc/cp/ChangeLog:
* decl.cc (maybe_prepare_return_this): Use OPT_Wuse_after_free_.
gcc/testsuite/ChangeLog:
* c-c++-common/Wuse-after-free-8.c: New test.
Signed-off-by: Torbjörn SVENSSON <[email protected]>
---
gcc/common.opt | 3 +--
gcc/cp/decl.cc | 2 +-
gcc/doc/invoke.texi | 1 +
gcc/gimple-ssa-warn-access.cc | 10 +++----
.../c-c++-common/Wuse-after-free-8.c | 26 +++++++++++++++++++
5 files changed, 34 insertions(+), 8 deletions(-)
create mode 100644 gcc/testsuite/c-c++-common/Wuse-after-free-8.c
diff --git a/gcc/common.opt b/gcc/common.opt
index e3fa0dacec4..98f06619455 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -552,8 +552,7 @@ Common Joined RejectNegative UInteger
Var(warn_array_bounds) Warning IntegerRang
Warn if an array is accessed out of bounds.
Wuse-after-free
-Common Var(warn_use_after_free) Warning
-Warn for uses of pointers to deallocated storage.
+Common Alias(Wuse-after-free=, 1, 0) Warning
Wuse-after-free=
Common Joined RejectNegative UInteger Var(warn_use_after_free) Warning
IntegerRange(0, 3)
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 9d67a25ec90..276c793c137 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -18948,7 +18948,7 @@ maybe_prepare_return_this (tree cdtor)
if (targetm.cxx.cdtor_returns_this ())
if (tree val = DECL_ARGUMENTS (cdtor))
{
- suppress_warning (val, OPT_Wuse_after_free);
+ suppress_warning (val, OPT_Wuse_after_free_);
return val;
}
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index b560ed41ca3..c4c14ccd590 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -7918,6 +7918,7 @@ Warn about uses of pointers to dynamically allocated
objects that have
been rendered indeterminate by a call to a deallocation function.
The warning is enabled at all optimization levels but may yield different
results with optimization than without.
+@option{-Wuse-after-free} is equivalent to @option{-Wuse-after-free=1}.
@table @gcctabopt
@item -Wuse-after-free=1
diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index 305b63567fe..d5d30770e25 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -3911,7 +3911,7 @@ pass_waccess::warn_invalid_pointer (tree ref, gimple
*use_stmt,
if (!var)
ref = NULL_TREE;
/* Don't warn for cases like when a cdtor returns 'this' on ARM. */
- else if (warning_suppressed_p (var, OPT_Wuse_after_free))
+ else if (warning_suppressed_p (var, OPT_Wuse_after_free_))
return;
else if (DECL_ARTIFICIAL (var))
ref = NULL_TREE;
@@ -3933,18 +3933,18 @@ pass_waccess::warn_invalid_pointer (tree ref, gimple
*use_stmt,
if (!m_early_checks_p
|| (equality && warn_use_after_free < 3)
|| (maybe && warn_use_after_free < 2)
- || warning_suppressed_p (use_stmt, OPT_Wuse_after_free))
+ || warning_suppressed_p (use_stmt, OPT_Wuse_after_free_))
return;
const tree inval_decl = gimple_call_fndecl (inval_stmt);
auto_diagnostic_group d;
- if ((ref && warning_at (use_loc, OPT_Wuse_after_free,
+ if ((ref && warning_at (use_loc, OPT_Wuse_after_free_,
(maybe
? G_("pointer %qE may be used after %qD")
: G_("pointer %qE used after %qD")),
ref, inval_decl))
- || (!ref && warning_at (use_loc, OPT_Wuse_after_free,
+ || (!ref && warning_at (use_loc, OPT_Wuse_after_free_,
(maybe
? G_("pointer may be used after %qD")
: G_("pointer used after %qD")),
@@ -3952,7 +3952,7 @@ pass_waccess::warn_invalid_pointer (tree ref, gimple
*use_stmt,
{
location_t loc = gimple_location (inval_stmt);
inform (loc, "call to %qD here", inval_decl);
- suppress_warning (use_stmt, OPT_Wuse_after_free);
+ suppress_warning (use_stmt, OPT_Wuse_after_free_);
}
return;
}
diff --git a/gcc/testsuite/c-c++-common/Wuse-after-free-8.c
b/gcc/testsuite/c-c++-common/Wuse-after-free-8.c
new file mode 100644
index 00000000000..07e7348e801
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wuse-after-free-8.c
@@ -0,0 +1,26 @@
+/* Verify -Wuse-after-free is an alias for -Wuse-after-free=1. */
+/* { dg-do compile } */
+/* { dg-options "-O0 -Wuse-after-free" } */
+
+#if __cplusplus
+# define EXTERN_C extern "C"
+#else
+# define EXTERN_C extern
+#endif
+
+EXTERN_C void free (void *);
+
+void sink (void *);
+
+void warn_call_after_free (void *p)
+{
+ free (p);
+ sink (p); // { dg-warning "pointer 'p' used" }
+}
+
+void warn_cond_call_after_free (void *p, int c)
+{
+ free (p);
+ if (c)
+ sink (p);
+}