[gcc r16-9308] Make -Wuse-after-free alias for -Wuse-after-free=1 [PR124058]

Torbjorn Svensson via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:872f2200a1b78e8ecb6a2596444c48470dc904f3

commit r16-9308-g872f2200a1b78e8ecb6a2596444c48470dc904f3
Author: Torbjörn SVENSSON <[email protected]>
Date:   Sun Jul 12 20:42:55 2026 +0200

    Make -Wuse-after-free alias for -Wuse-after-free=1 [PR124058]
    
    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-diagnostic-spec.cc (nowarn_spec_t::nowarn_spec_t):
            Likewise.
    
    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]>
    (cherry picked from commit c5627f93a8259131da2cdebee6d9b0e1f2de1a60)

Diff:
---
 gcc/common.opt                                 |  3 +--
 gcc/cp/decl.cc                                 |  2 +-
 gcc/doc/invoke.texi                            |  1 +
 gcc/gcc-diagnostic-spec.cc                     |  1 -
 gcc/gimple-ssa-warn-access.cc                  | 10 +++++-----
 gcc/testsuite/c-c++-common/Wuse-after-free-8.c | 26 ++++++++++++++++++++++++++
 6 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index 5d0e255a40d9..204c8ae721bd 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -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)
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 9b10899f5c20..095cef2f2e50 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -20292,7 +20292,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 aa80ebd0ced1..1d69e434e824 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -8442,6 +8442,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/gcc-diagnostic-spec.cc b/gcc/gcc-diagnostic-spec.cc
index 813625e28773..dd631699539d 100644
--- a/gcc/gcc-diagnostic-spec.cc
+++ b/gcc/gcc-diagnostic-spec.cc
@@ -100,7 +100,6 @@ nowarn_spec_t::nowarn_spec_t (opt_code opt)
 
     case OPT_Wdangling_pointer_:
     case OPT_Wreturn_local_addr:
-    case OPT_Wuse_after_free:
     case OPT_Wuse_after_free_:
       m_bits = NW_DANGLING;
       break;
diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index 98de5df86b02..f1fb92f6be1c 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -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
index 000000000000..07e7348e801f
--- /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);
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.