In the C front end, arrays used as expressions immediately decay to
pointers. In C++, they don't decay until we know that the expression is
being used as an rvalue, as we might want to bind a reference to the
array as a whole. The atomics code in c-common expects pointers, so
let's force any array arguments to decay.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 36cb4ee17f1d0e9fd14210fda1eb43e21637e981
Author: Jason Merrill <ja...@redhat.com>
Date: Mon Apr 7 12:57:34 2014 -0400
PR c++/57926
* c-common.c (sync_resolve_size, get_atomic_generic_size): Call
default_conversion for an array argument.
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 03731b4..1d56bc0 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -10202,6 +10202,13 @@ sync_resolve_size (tree function, vec<tree, va_gc> *params)
}
type = TREE_TYPE ((*params)[0]);
+ if (TREE_CODE (type) == ARRAY_TYPE)
+ {
+ /* Force array-to-pointer decay for C++. */
+ gcc_assert (c_dialect_cxx());
+ (*params)[0] = default_conversion ((*params)[0]);
+ type = TREE_TYPE ((*params)[0]);
+ }
if (TREE_CODE (type) != POINTER_TYPE)
goto incompatible;
@@ -10353,6 +10360,13 @@ get_atomic_generic_size (location_t loc, tree function,
/* Get type of first parameter, and determine its size. */
type_0 = TREE_TYPE ((*params)[0]);
+ if (TREE_CODE (type_0) == ARRAY_TYPE)
+ {
+ /* Force array-to-pointer decay for C++. */
+ gcc_assert (c_dialect_cxx());
+ (*params)[0] = default_conversion ((*params)[0]);
+ type_0 = TREE_TYPE ((*params)[0]);
+ }
if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
{
error_at (loc, "argument 1 of %qE must be a non-void pointer type",
diff --git a/gcc/testsuite/g++.dg/ext/atomic-2.C b/gcc/testsuite/g++.dg/ext/atomic-2.C
new file mode 100644
index 0000000..ac363eb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/atomic-2.C
@@ -0,0 +1,14 @@
+// PR c++/57926
+
+long Mutex[1];
+
+int AcquireLogMutex(void)
+{
+ return __atomic_exchange_n(Mutex, 1, __ATOMIC_SEQ_CST);
+}
+
+void ReleaseLogMutex(void)
+{
+ long i = 0;
+ __atomic_store(Mutex, &i, __ATOMIC_SEQ_CST);
+}