]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Make -Wuse-after-free alias for -Wuse-after-free=1 [PR124058]
authorTorbjörn SVENSSON <torbjorn.svensson@foss.st.com>
Sun, 12 Jul 2026 18:42:55 +0000 (20:42 +0200)
committerTorbjörn SVENSSON <torbjorn.svensson@foss.st.com>
Mon, 13 Jul 2026 19:21:38 +0000 (21:21 +0200)
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 <torbjorn.svensson@foss.st.com>
gcc/common.opt
gcc/cp/decl.cc
gcc/doc/invoke.texi
gcc/gimple-ssa-warn-access.cc
gcc/testsuite/c-c++-common/Wuse-after-free-8.c [new file with mode: 0644]

index ecebc186af774318a69902fb8bf91b3de2fb89e3..1c6ad3aa4e5b84e1bf304975d901be21d70bf5c6 100644 (file)
@@ -561,8 +561,7 @@ Common Var(warn_auto_profile) Warning
 Warn about problems with auto-profile data.
 
 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)
index 472f81ea9e1f39315b9b6bb3e17a3ca974a165f7..6c5bb77e603178320a011c8fe6732d00982cf207 100644 (file)
@@ -20307,7 +20307,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;
       }
 
index 2e21053dae96c2197a215b10f2658b1823001107..78052b229a5890563cfc252d9dddb0367d36fdc2 100644 (file)
@@ -8492,6 +8492,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
index dd128802f9d9de19c2daeeff3bca228771aa4b8e..2906ee7320fa957f81954a422216f1a5579022f3 100644 (file)
@@ -3992,7 +3992,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;
@@ -4014,18 +4014,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")),
@@ -4033,7 +4033,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 (file)
index 0000000..07e7348
--- /dev/null
@@ -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);
+}