]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Fix handling of register atomic compound literals
authorJoseph Myers <josmyers@redhat.com>
Wed, 24 Sep 2025 19:53:01 +0000 (19:53 +0000)
committerJoseph Myers <josmyers@redhat.com>
Wed, 24 Sep 2025 19:53:01 +0000 (19:53 +0000)
The logic for loads and stores of _Atomic objects in the C front end
involves taking the address of such objects, with really_atomic_lvalue
detecting cases where this cannot be done (and also no special
handling is needed for atomicity), in particular register _Atomic
objects.  This logic failed to deal with the case of register _Atomic
compound literals, so resulting in spurious errors "error: address of
register compound literal requested" followed by "error: argument 1 of
'__atomic_load' must be a non-void pointer type".  (This is a C23 bug
that I found while changing really_atomic_lvalue as part of previous
C2y changes.)  Add a use of COMPOUND_LITERAL_EXPR_DECL in that case.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.

gcc/c/
* c-typeck.cc (really_atomic_lvalue): For a COMPOUND_LITERAL_EXPR,
check C_DECL_REGISTER on the COMPOUND_LITERAL_EXPR_DECL.

gcc/testsuite/
* gcc.dg/c23-complit-9.c: New test.

gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/c23-complit-9.c [new file with mode: 0644]

index 624e7a3fa3599de1dc04c527697935407b124329..6c807a2a7927aa9c923699a9b4e7955299406e90 100644 (file)
@@ -2531,6 +2531,8 @@ really_atomic_lvalue (tree expr)
        return false;
       expr = TREE_OPERAND (expr, 0);
     }
+  if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
+    expr = COMPOUND_LITERAL_EXPR_DECL (expr);
   if (DECL_P (expr) && C_DECL_REGISTER (expr))
     return false;
   return true;
diff --git a/gcc/testsuite/gcc.dg/c23-complit-9.c b/gcc/testsuite/gcc.dg/c23-complit-9.c
new file mode 100644 (file)
index 0000000..738f1a6
--- /dev/null
@@ -0,0 +1,9 @@
+/* Test register _Atomic compound literals.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+
+void
+f ()
+{
+  (register _Atomic int) { 1 };
+}