]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end/110055 - avoid CLOBBERing static variables
authorRichard Biener <rguenther@suse.de>
Mon, 5 Jun 2023 06:56:53 +0000 (08:56 +0200)
committerRichard Biener <rguenther@suse.de>
Tue, 6 Jun 2023 07:19:35 +0000 (09:19 +0200)
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.

gcc/gimplify.cc
gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C [new file with mode: 0644]

index bd5d0bf2bd8ffb48a7e1fda444e5e95e631a8cd0..4aa6229fc7451abc011c845d41afca9e0f1da04f 100644 (file)
@@ -7174,8 +7174,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 (file)
index 0000000..77dbbf3
--- /dev/null
@@ -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;
+}