Fix GCC build with PROTOBUF_USE_DLLS enabled

GCC does not allow mixing __attribute__(()) syntax alignas()
syntax. Re-use approach from chromium base/compiler_specific.h
This commit is contained in:
Stephan Hartmann 2019-12-25 13:34:34 +01:00 committed by Adam Cozzette
parent f2c5ee5117
commit 32caf511c8
3 changed files with 31 additions and 1 deletions

View File

@ -248,7 +248,7 @@ struct ArenaOptions {
// well as protobuf container types like RepeatedPtrField and Map. The protocol
// is internal to protobuf and is not guaranteed to be stable. Non-proto types
// should not rely on this protocol.
class PROTOBUF_EXPORT alignas(8) Arena final {
class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
public:
// Arena constructor taking custom options. See ArenaOptions below for
// descriptions of the options available.

View File

@ -420,3 +420,32 @@
#else
#define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred)
#endif
// Specify memory alignment for structs, classes, etc.
// Use like:
// class PROTOBUF_ALIGNAS(16) MyClass { ... }
// PROTOBUF_ALIGNAS(16) int array[4];
//
// In most places you can use the C++11 keyword "alignas", which is preferred.
//
// But compilers have trouble mixing __attribute__((...)) syntax with
// alignas(...) syntax.
//
// Doesn't work in clang or gcc:
// struct alignas(16) __attribute__((packed)) S { char c; };
// Works in clang but not gcc:
// struct __attribute__((packed)) alignas(16) S2 { char c; };
// Works in clang and gcc:
// struct alignas(16) S3 { char c; } __attribute__((packed));
//
// There are also some attributes that must be specified *before* a class
// definition: visibility (used for exporting functions/classes) is one of
// these attributes. This means that it is not possible to use alignas() with a
// class that is marked as exported.
#if defined(_MSC_VER)
#define PROTOBUF_ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
#elif defined(__GNUC__)
#define PROTOBUF_ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
#else
#define PROTOBUF_ALIGNAS(byte_alignment) alignas(byte_alignment)
#endif

View File

@ -68,6 +68,7 @@
#undef PROTOBUF_ASSUME
#undef PROTOBUF_EXPORT_TEMPLATE_DECLARE
#undef PROTOBUF_EXPORT_TEMPLATE_DEFINE
#undef PROTOBUF_ALIGNAS
// Restore macro that may have been #undef'd in port_def.inc.
#ifdef _MSC_VER