public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-7467] middle-end/110055 - avoid CLOBBERing static variables
Date: Fri, 23 Jun 2023 10:30:00 +0000 (GMT)	[thread overview]
Message-ID: <20230623103000.B85C63858421@sourceware.org> (raw)

https://gcc.gnu.org/g:b0856bb588c76e16a518b459a8c9a012dc2b4e21

commit r13-7467-gb0856bb588c76e16a518b459a8c9a012dc2b4e21
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Jun 5 08:56:53 2023 +0200

    middle-end/110055 - avoid CLOBBERing static variables
    
    The gimplifier can elide initialized constant automatic variables
    to static storage in which case TARGET_EXPR gimplification needs
    to avoid emitting a CLOBBER for them since their lifetime is no
    longer limited.  Failing to do so causes spurious dangling-pointer
    diagnostics on the added testcase for some targets.
    
            PR middle-end/110055
            * gimplify.cc (gimplify_target_expr): Do not emit
            CLOBBERs for variables which have static storage duration
            after gimplifying their initializers.
    
            * g++.dg/warn/Wdangling-pointer-pr110055.C: New testcase.
    
    (cherry picked from commit 84eec2916fa68cd2e2b3a2cf764f2ba595cce843)

Diff:
---
 gcc/gimplify.cc                                        |  4 +++-
 gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index ade6e335da7..aece89a3e9b 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -7154,8 +7154,10 @@ gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
 	gimplify_and_add (init, &init_pre_p);
 
       /* Add a clobber for the temporary going out of scope, like
-	 gimplify_bind_expr.  */
+	 gimplify_bind_expr.  But only if we did not promote the
+	 temporary to static storage.  */
       if (gimplify_ctxp->in_cleanup_point_expr
+	  && !TREE_STATIC (temp)
 	  && needs_to_live_in_memory (temp))
 	{
 	  if (flag_stack_reuse == SR_ALL)
diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C b/gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C
new file mode 100644
index 00000000000..77dbbf380b6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C
@@ -0,0 +1,16 @@
+// { dg-do compile }
+// { dg-require-effective-target c++11 }
+// { dg-options "-O3 -fno-exceptions -Wdangling-pointer" }
+
+#include <cstdint>
+#include <vector>
+
+struct Data {
+  std::vector<uint16_t> v = {1, 1};
+};
+
+int main()
+{
+  Data a;
+  Data b;
+}

                 reply	other threads:[~2023-06-23 10:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230623103000.B85C63858421@sourceware.org \
    --to=rguenth@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).