Commit 628a23ba authored by Robert S. Edmonds's avatar Robert S. Edmonds

Expose generic atomicops on Clang

The generic atomicops implementation is only exposed if GCC >= 4.7 is
available, but Clang, where the underlying __atomic built-ins are also
available, typically only claims to be GCC 4.2. This causes build
failures when compiling protobuf or the output of protoc's C++ code
generator on an architecture that needs the generic atomicops
implementation with Clang.

Clang has a "c_atomic" extension which can be tested for which almost
does what we want:

    C11 atomic operations

    Use __has_feature(c_atomic) or __has_extension(c_atomic) to
    determine if support for atomic types using _Atomic is enabled.
    Clang also provides a set of builtins which can be used to implement
    the <stdatomic.h> operations on _Atomic types.

I'm not sure if this guarantees that the GNU atomic builtins (the ones
with the __atomic prefix) are also available, but in practice this
should guarantee that Clang is new enough.

With this change in place, Clang generates several diagnostics when
compiling the generic atomicops implementation. These appear to be bugs
in the generic atomicops implementation and are not Clang-specific.
parent d24b987c
...@@ -192,7 +192,8 @@ GOOGLE_PROTOBUF_ATOMICOPS_ERROR ...@@ -192,7 +192,8 @@ GOOGLE_PROTOBUF_ATOMICOPS_ERROR
#include <google/protobuf/stubs/atomicops_internals_mips_gcc.h> #include <google/protobuf/stubs/atomicops_internals_mips_gcc.h>
#elif defined(__native_client__) #elif defined(__native_client__)
#include <google/protobuf/stubs/atomicops_internals_pnacl.h> #include <google/protobuf/stubs/atomicops_internals_pnacl.h>
#elif (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)) #elif (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)) || \
(defined(__clang__) && __has_extension(c_atomic))
#include <google/protobuf/stubs/atomicops_internals_generic_gcc.h> #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h>
#else #else
GOOGLE_PROTOBUF_ATOMICOPS_ERROR GOOGLE_PROTOBUF_ATOMICOPS_ERROR
......
...@@ -63,7 +63,8 @@ ...@@ -63,7 +63,8 @@
#elif defined(__pnacl__) #elif defined(__pnacl__)
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 #define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#elif defined(__GNUC__) && \ #elif defined(__GNUC__) && \
(((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)) ((((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)) || \
(defined(__clang__) && __has_extension(c_atomic)))
// We fallback to the generic GCC >= 4.7 implementation in atomicops.h // We fallback to the generic GCC >= 4.7 implementation in atomicops.h
# if __LP64__ # if __LP64__
# define GOOGLE_PROTOBUF_ARCH_64_BIT 1 # define GOOGLE_PROTOBUF_ARCH_64_BIT 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment