]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: avoid ARM -Wunused-value [PR114970]
authorJason Merrill <jason@redhat.com>
Thu, 10 Apr 2025 22:16:37 +0000 (18:16 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 11 Apr 2025 14:37:21 +0000 (10:37 -0400)
Because of the __builtin_is_constant_evaluated, maybe_constant_init in
expand_default_init fails, so the constexpr constructor isn't folded until
cp_fold, which builds a COMPOUND_EXPR in case the enclosing expression is
relying on the ARM behavior of returning 'this'.

As in other places, avoid -Wunused-value on artificial COMPOUND_EXPR.

PR c++/114970

gcc/cp/ChangeLog:

* cp-gimplify.cc (cp_fold): Suppress warnings on
return_this COMPOUND_EXPR.

gcc/testsuite/ChangeLog:

* g++.dg/opt/is_constant_evaluated4.C: New test.

(cherry picked from commit 4acdfb71d4fdaa43c2707ad7b2fb7b2b7bddfc42)

gcc/cp/cp-gimplify.cc
gcc/testsuite/g++.dg/opt/is_constant_evaluated4.C [new file with mode: 0644]

index 7b52fc301d61a829602b1563a9d609515c3dbcbf..37dc4a286a0a42b683ca77d4178a3cfb573df615 100644 (file)
@@ -3437,8 +3437,11 @@ cp_fold (tree x, fold_flags_t flags)
                tree s = build_fold_indirect_ref_loc (loc, a);
                r = cp_build_init_expr (s, r);
                if (return_this)
-                 r = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (x), r,
-                                 fold_convert_loc (loc, TREE_TYPE (x), a));
+                 {
+                   r = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (x), r,
+                                   fold_convert_loc (loc, TREE_TYPE (x), a));
+                   suppress_warning (r);
+                 }
              }
            x = r;
            break;
diff --git a/gcc/testsuite/g++.dg/opt/is_constant_evaluated4.C b/gcc/testsuite/g++.dg/opt/is_constant_evaluated4.C
new file mode 100644 (file)
index 0000000..9650004
--- /dev/null
@@ -0,0 +1,20 @@
+// PR c++/114970
+// { dg-do compile { target c++17 } }
+// { dg-additional-options "-O -Wunused-value" }
+
+struct sv
+{
+  const char* str;
+  unsigned len;
+
+  constexpr sv(const char *p): str(p), len(0)
+  {
+    if (__builtin_is_constant_evaluated ()) { len = 42; }
+  }
+};
+
+int main()
+{
+  sv s ("foo");
+  return s.len;
+}