From 7ad8690c5d8a875881ba00c3551595a2676538ef Mon Sep 17 00:00:00 2001 From: George Redivo Date: Mon, 6 Jul 2015 16:56:41 -0300 Subject: Fix GOOGLE_PROTOBUF_ATOMICOPS_ERROR syntax error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's not possible to define "#error" inside a define. It causes 'error: stray ‘#’ in program' compilation error. Now the define GOOGLE_PROTOBUF_ATOMICOPS_ERROR is the error message and it's used along the code together "#error". --- src/google/protobuf/stubs/atomicops.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/stubs/atomicops.h b/src/google/protobuf/stubs/atomicops.h index cd20caac..5fa31b0a 100644 --- a/src/google/protobuf/stubs/atomicops.h +++ b/src/google/protobuf/stubs/atomicops.h @@ -173,7 +173,7 @@ Atomic64 Release_Load(volatile const Atomic64* ptr); // Include our platform specific implementation. #define GOOGLE_PROTOBUF_ATOMICOPS_ERROR \ -#error "Atomic operations are not supported on your platform" +"Atomic operations are not supported on your platform" // ThreadSanitizer, http://clang.llvm.org/docs/ThreadSanitizer.html. #if defined(THREAD_SANITIZER) @@ -183,7 +183,7 @@ Atomic64 Release_Load(volatile const Atomic64* ptr); #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64) #include #else -GOOGLE_PROTOBUF_ATOMICOPS_ERROR +#error GOOGLE_PROTOBUF_ATOMICOPS_ERROR #endif // Solaris @@ -218,15 +218,15 @@ GOOGLE_PROTOBUF_ATOMICOPS_ERROR #if __has_extension(c_atomic) #include #else -GOOGLE_PROTOBUF_ATOMICOPS_ERROR +#error GOOGLE_PROTOBUF_ATOMICOPS_ERROR #endif #else -GOOGLE_PROTOBUF_ATOMICOPS_ERROR +#error GOOGLE_PROTOBUF_ATOMICOPS_ERROR #endif // Unknown. #else -GOOGLE_PROTOBUF_ATOMICOPS_ERROR +#error GOOGLE_PROTOBUF_ATOMICOPS_ERROR #endif // On some platforms we need additional declarations to make AtomicWord -- cgit v1.2.3 From fcf1b575799f93d10d030069ba9857af924c31a8 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Wed, 26 Aug 2015 11:24:41 -0700 Subject: Fix the no-op definitions of GOOGLE_PREDICT_{TRUE,FALSE} Updating to the current protobuf version caused the following build errors in Chromium when using Clang on Windows: ..\..\third_party\protobuf\src\google/protobuf/stubs/fastmem.h(67,43) : error: equality comparison with extraneous parentheses [-Werror,-Wparentheses-equality] if (GOOGLE_PREDICT_FALSE(n_rounded_down == 0)) { // n <= 7 ~~~~~~~~~~~~~~~^~~~ The problem is that on Windows, GOOGLE_PREDICT_FALSE is #defined to nothing, so the code expands to 'if ((n_rounded_down == 0))', which Clang warns about. Clang would not have warned if the extra parentheses came from the macro, but in this case they don't because the macro is just dropped. Fix this by making the macros behave as an identity function instead of just getting dropped. This is closer to what these macros look like in stubs/port.h internally. --- src/google/protobuf/stubs/port.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/stubs/port.h b/src/google/protobuf/stubs/port.h index a3c53dd9..52c89005 100644 --- a/src/google/protobuf/stubs/port.h +++ b/src/google/protobuf/stubs/port.h @@ -184,7 +184,7 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); // Provided at least since GCC 3.0. #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) #else -#define GOOGLE_PREDICT_TRUE +#define GOOGLE_PREDICT_TRUE(x) (x) #endif #endif @@ -193,7 +193,7 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); // Provided at least since GCC 3.0. #define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(x, 0)) #else -#define GOOGLE_PREDICT_FALSE +#define GOOGLE_PREDICT_FALSE(x) (x) #endif #endif -- cgit v1.2.3 From 2e789bc246144a1b8a3fba5e4de3105089498674 Mon Sep 17 00:00:00 2001 From: Peter Kasting Date: Thu, 27 Aug 2015 14:16:10 -0700 Subject: Avoid #including system headers from inside a namespace. port.h #includes various headers in order to define byteswap functions, but it currently does so from inside the google::protobuf namespace. This can cause bizarre symbol conflicts and other build errors as these headers' contents are then included inside this namespace. Instead, #include the relevant headers above the namespace declarations. --- src/google/protobuf/stubs/port.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/stubs/port.h b/src/google/protobuf/stubs/port.h index a3c53dd9..6b94bc3a 100644 --- a/src/google/protobuf/stubs/port.h +++ b/src/google/protobuf/stubs/port.h @@ -79,6 +79,15 @@ #define LIBPROTOC_EXPORT #endif +// These #includes are for the byte swap functions declared later on. +#ifdef _MSC_VER +#include // NOLINT(build/include) +#elif defined(__APPLE__) +#include +#elif defined(__GLIBC__) || defined(__CYGWIN__) +#include // IWYU pragma: export +#endif + // =================================================================== // from google3/base/port.h namespace google { @@ -275,7 +284,6 @@ inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) { // The following guarantees declaration of the byte swap functions, and // defines __BYTE_ORDER for MSVC #ifdef _MSC_VER -#include // NOLINT(build/include) #define __BYTE_ORDER __LITTLE_ENDIAN #define bswap_16(x) _byteswap_ushort(x) #define bswap_32(x) _byteswap_ulong(x) @@ -283,15 +291,11 @@ inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) { #elif defined(__APPLE__) // Mac OS X / Darwin features -#include #define bswap_16(x) OSSwapInt16(x) #define bswap_32(x) OSSwapInt32(x) #define bswap_64(x) OSSwapInt64(x) -#elif defined(__GLIBC__) || defined(__CYGWIN__) -#include // IWYU pragma: export - -#else +#elif !defined(__GLIBC__) && !defined(__CYGWIN__) static inline uint16 bswap_16(uint16 x) { return static_cast(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8)); -- cgit v1.2.3 From d6801595277a194745ec65797e0c8447247066f9 Mon Sep 17 00:00:00 2001 From: Peter Kasting Date: Thu, 27 Aug 2015 14:19:51 -0700 Subject: Delete kNanosPerSecond from time.cc. This variable is unused, and thus triggers a build warning on MSVC. --- src/google/protobuf/stubs/time.cc | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/google/protobuf/stubs/time.cc b/src/google/protobuf/stubs/time.cc index 3319a244..49c0412c 100644 --- a/src/google/protobuf/stubs/time.cc +++ b/src/google/protobuf/stubs/time.cc @@ -21,7 +21,6 @@ static const int64 kSecondsFromEraToEpoch = 62135596800LL; static const int64 kMinTime = -62135596800LL; // 0001-01-01T00:00:00 static const int64 kMaxTime = 253402300799LL; // 9999-12-31T23:59:59 -static const int kNanosPerSecond = 1000000000; static const int kNanosPerMillisecond = 1000000; static const int kNanosPerMicrosecond = 1000; -- cgit v1.2.3 From b913cbd3073826ea113efe917d4d3ce71b92ab2d Mon Sep 17 00:00:00 2001 From: Peter Kasting Date: Thu, 27 Aug 2015 14:37:18 -0700 Subject: Move StringSpaceUsedIncludingSelf to lite library. This came up because Chromium downstream modifies the lite library in a way that requires this function, but I'm upstreaming it because based on the comments in repeated_field.h, this ought to allow resolution of an existing hack. I don't know enough about the protobuf code to feel confident trying to resolve this hack myself, so I've merely updated the TODO comments. --- src/google/protobuf/generated_message_reflection.cc | 12 ------------ src/google/protobuf/generated_message_util.cc | 12 ++++++++++++ src/google/protobuf/generated_message_util.h | 6 ------ src/google/protobuf/repeated_field.h | 3 ++- 4 files changed, 14 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index 412c48a1..0b01e73a 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -57,18 +57,6 @@ bool IsMapFieldInApi(const FieldDescriptor* field) { } } // anonymous namespace -int StringSpaceUsedExcludingSelf(const string& str) { - const void* start = &str; - const void* end = &str + 1; - - if (start <= str.data() && str.data() < end) { - // The string's data is stored inside the string object itself. - return 0; - } else { - return str.capacity(); - } -} - bool ParseNamedEnum(const EnumDescriptor* descriptor, const string& name, int* value) { diff --git a/src/google/protobuf/generated_message_util.cc b/src/google/protobuf/generated_message_util.cc index 53cae8b5..afaca2ee 100644 --- a/src/google/protobuf/generated_message_util.cc +++ b/src/google/protobuf/generated_message_util.cc @@ -60,6 +60,18 @@ void InitEmptyString() { OnShutdown(&DeleteEmptyString); } +int StringSpaceUsedExcludingSelf(const string& str) { + const void* start = &str; + const void* end = &str + 1; + + if (start <= str.data() && str.data() < end) { + // The string's data is stored inside the string object itself. + return 0; + } else { + return str.capacity(); + } +} + } // namespace internal } // namespace protobuf diff --git a/src/google/protobuf/generated_message_util.h b/src/google/protobuf/generated_message_util.h index 6357e27d..78c8d7ff 100644 --- a/src/google/protobuf/generated_message_util.h +++ b/src/google/protobuf/generated_message_util.h @@ -89,12 +89,6 @@ LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyString() { return GetEmptyStringAlreadyInited(); } -// Defined in generated_message_reflection.cc -- not actually part of the lite -// library. -// -// TODO(jasonh): The various callers get this declaration from a variety of -// places: probably in most cases repeated_field.h. Clean these up so they all -// get the declaration from this file. LIBPROTOBUF_EXPORT int StringSpaceUsedExcludingSelf(const string& str); diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h index 9c84ed98..2ba5dfd5 100644 --- a/src/google/protobuf/repeated_field.h +++ b/src/google/protobuf/repeated_field.h @@ -647,7 +647,8 @@ inline const Message& GenericTypeHandler::default_instance() { // StringTypeHandler is exported. So, we factor out StringTypeHandlerBase, // export that, then make StringTypeHandler be a subclass which is NOT // exported. -// TODO(kenton): There has to be a better way. +// TODO(kenton): Now that StringSpaceUsedExcludingSelf() is in the lite +// library, this can be cleaned up. class LIBPROTOBUF_EXPORT StringTypeHandlerBase { public: typedef string Type; -- cgit v1.2.3 From 4f3bead5373decc3750f65ff35ca8785fa97ba38 Mon Sep 17 00:00:00 2001 From: Peter Kasting Date: Thu, 27 Aug 2015 20:16:33 -0700 Subject: Remove a static initializer by removing a global of non-POD type. These are banned by the Google style guide, and Chromium has a hard no-new-static-initializers policy preventing updating to a new version of libprotobuf unless this is resolved. This is the first such change, I'll need to make at least one more in the future. Luckily, the protobuf source tree already has an alternative to static initializers in once.h; use that machinery instead. I defined everything in the .cc file in a blob to replace the old implementation rather than matching the .h layout precisely; let me know if a different ordering is preferred. I also eliminated the macro that used to be used here as spelling everything out only takes one additional line, and the macro didn't actually handle all details of using a particular member variable, just the declaration, so it felt a bit error-prone. --- src/google/protobuf/extension_set.cc | 109 ++++++++++++++++++----------------- src/google/protobuf/extension_set.h | 32 +++++++--- 2 files changed, 78 insertions(+), 63 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/extension_set.cc b/src/google/protobuf/extension_set.cc index 649ae184..919bd83b 100644 --- a/src/google/protobuf/extension_set.cc +++ b/src/google/protobuf/extension_set.cc @@ -34,7 +34,6 @@ #include #include -#include #include #include #include @@ -1747,66 +1746,68 @@ void ExtensionSet::Extension::Free() { // ================================================================== // Default repeated field instances for iterator-compatible accessors -const RepeatedStringTypeTraits::RepeatedFieldType* -RepeatedStringTypeTraits::default_repeated_field_ = NULL; +GOOGLE_PROTOBUF_DECLARE_ONCE(repeated_primitive_generic_type_traits_once_init_); +GOOGLE_PROTOBUF_DECLARE_ONCE(repeated_string_type_traits_once_init_); +GOOGLE_PROTOBUF_DECLARE_ONCE(repeated_message_generic_type_traits_once_init_); -const RepeatedMessageGenericTypeTraits::RepeatedFieldType* -RepeatedMessageGenericTypeTraits::default_repeated_field_ = NULL; +void RepeatedPrimitiveGenericTypeTraits::InitializeDefaultRepeatedFields() { + default_repeated_field_int32_ = new RepeatedField; + default_repeated_field_int64_ = new RepeatedField; + default_repeated_field_uint32_ = new RepeatedField; + default_repeated_field_uint64_ = new RepeatedField; + default_repeated_field_double_ = new RepeatedField; + default_repeated_field_float_ = new RepeatedField; + default_repeated_field_bool_ = new RepeatedField; + OnShutdown(&DestroyDefaultRepeatedFields); +} + +void RepeatedPrimitiveGenericTypeTraits::DestroyDefaultRepeatedFields() { + delete default_repeated_field_int32_; + delete default_repeated_field_int64_; + delete default_repeated_field_uint32_; + delete default_repeated_field_uint64_; + delete default_repeated_field_double_; + delete default_repeated_field_float_; + delete default_repeated_field_bool_; +} -#define PROTOBUF_DEFINE_DEFAULT_REPEATED(TYPE) \ - const RepeatedField* \ - RepeatedPrimitiveGenericTypeTraits::default_repeated_field_##TYPE##_ = NULL; +void RepeatedStringTypeTraits::InitializeDefaultRepeatedFields() { + default_repeated_field_ = new RepeatedFieldType; + OnShutdown(&DestroyDefaultRepeatedFields); +} -PROTOBUF_DEFINE_DEFAULT_REPEATED(int32) -PROTOBUF_DEFINE_DEFAULT_REPEATED(int64) -PROTOBUF_DEFINE_DEFAULT_REPEATED(uint32) -PROTOBUF_DEFINE_DEFAULT_REPEATED(uint64) -PROTOBUF_DEFINE_DEFAULT_REPEATED(double) -PROTOBUF_DEFINE_DEFAULT_REPEATED(float) -PROTOBUF_DEFINE_DEFAULT_REPEATED(bool) +void RepeatedStringTypeTraits::DestroyDefaultRepeatedFields() { + delete default_repeated_field_; +} -#undef PROTOBUF_DEFINE_DEFAULT_REPEATED +void RepeatedMessageGenericTypeTraits::InitializeDefaultRepeatedFields() { + default_repeated_field_ = new RepeatedFieldType; + OnShutdown(&DestroyDefaultRepeatedFields); +} -struct StaticDefaultRepeatedFieldsInitializer { - StaticDefaultRepeatedFieldsInitializer() { - InitializeDefaultRepeatedFields(); - OnShutdown(&DestroyDefaultRepeatedFields); - } -} static_repeated_fields_initializer; - -void InitializeDefaultRepeatedFields() { - RepeatedStringTypeTraits::default_repeated_field_ = - new RepeatedStringTypeTraits::RepeatedFieldType; - RepeatedMessageGenericTypeTraits::default_repeated_field_ = - new RepeatedMessageGenericTypeTraits::RepeatedFieldType; - RepeatedPrimitiveGenericTypeTraits::default_repeated_field_int32_ = - new RepeatedField; - RepeatedPrimitiveGenericTypeTraits::default_repeated_field_int64_ = - new RepeatedField; - RepeatedPrimitiveGenericTypeTraits::default_repeated_field_uint32_ = - new RepeatedField; - RepeatedPrimitiveGenericTypeTraits::default_repeated_field_uint64_ = - new RepeatedField; - RepeatedPrimitiveGenericTypeTraits::default_repeated_field_double_ = - new RepeatedField; - RepeatedPrimitiveGenericTypeTraits::default_repeated_field_float_ = - new RepeatedField; - RepeatedPrimitiveGenericTypeTraits::default_repeated_field_bool_ = - new RepeatedField; -} - -void DestroyDefaultRepeatedFields() { - delete RepeatedStringTypeTraits::default_repeated_field_; - delete RepeatedMessageGenericTypeTraits::default_repeated_field_; - delete RepeatedPrimitiveGenericTypeTraits::default_repeated_field_int32_; - delete RepeatedPrimitiveGenericTypeTraits::default_repeated_field_int64_; - delete RepeatedPrimitiveGenericTypeTraits::default_repeated_field_uint32_; - delete RepeatedPrimitiveGenericTypeTraits::default_repeated_field_uint64_; - delete RepeatedPrimitiveGenericTypeTraits::default_repeated_field_double_; - delete RepeatedPrimitiveGenericTypeTraits::default_repeated_field_float_; - delete RepeatedPrimitiveGenericTypeTraits::default_repeated_field_bool_; +void RepeatedMessageGenericTypeTraits::DestroyDefaultRepeatedFields() { + delete default_repeated_field_; } +const RepeatedField* +RepeatedPrimitiveGenericTypeTraits::default_repeated_field_int32_ = NULL; +const RepeatedField* +RepeatedPrimitiveGenericTypeTraits::default_repeated_field_int64_ = NULL; +const RepeatedField* +RepeatedPrimitiveGenericTypeTraits::default_repeated_field_uint32_ = NULL; +const RepeatedField* +RepeatedPrimitiveGenericTypeTraits::default_repeated_field_uint64_ = NULL; +const RepeatedField* +RepeatedPrimitiveGenericTypeTraits::default_repeated_field_double_ = NULL; +const RepeatedField* +RepeatedPrimitiveGenericTypeTraits::default_repeated_field_float_ = NULL; +const RepeatedField* +RepeatedPrimitiveGenericTypeTraits::default_repeated_field_bool_ = NULL; +const RepeatedStringTypeTraits::RepeatedFieldType* +RepeatedStringTypeTraits::default_repeated_field_ = NULL; +const RepeatedMessageGenericTypeTraits::RepeatedFieldType* +RepeatedMessageGenericTypeTraits::default_repeated_field_ = NULL; + } // namespace internal } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/extension_set.h b/src/google/protobuf/extension_set.h index c371e011..35f14b9c 100644 --- a/src/google/protobuf/extension_set.h +++ b/src/google/protobuf/extension_set.h @@ -45,6 +45,7 @@ #include +#include #include @@ -716,15 +717,14 @@ class RepeatedPrimitiveTypeTraits { static const RepeatedFieldType* GetDefaultRepeatedField(); }; -// Declared here so that this can be friended below. -void InitializeDefaultRepeatedFields(); -void DestroyDefaultRepeatedFields(); +LIBPROTOBUF_EXPORT extern ProtobufOnceType +repeated_primitive_generic_type_traits_once_init_; class LIBPROTOBUF_EXPORT RepeatedPrimitiveGenericTypeTraits { private: template friend class RepeatedPrimitiveTypeTraits; - friend void InitializeDefaultRepeatedFields(); - friend void DestroyDefaultRepeatedFields(); + static void InitializeDefaultRepeatedFields(); + static void DestroyDefaultRepeatedFields(); static const RepeatedField* default_repeated_field_int32_; static const RepeatedField* default_repeated_field_int64_; static const RepeatedField* default_repeated_field_uint32_; @@ -759,6 +759,9 @@ template<> inline void RepeatedPrimitiveTypeTraits::Add( \ } \ template<> inline const RepeatedField* \ RepeatedPrimitiveTypeTraits::GetDefaultRepeatedField() { \ + GoogleOnceInit( \ + &repeated_primitive_generic_type_traits_once_init_, \ + &RepeatedPrimitiveGenericTypeTraits::InitializeDefaultRepeatedFields); \ return RepeatedPrimitiveGenericTypeTraits:: \ default_repeated_field_##TYPE##_; \ } \ @@ -812,6 +815,9 @@ class LIBPROTOBUF_EXPORT StringTypeTraits { } }; +LIBPROTOBUF_EXPORT extern ProtobufOnceType +repeated_string_type_traits_once_init_; + class LIBPROTOBUF_EXPORT RepeatedStringTypeTraits { public: typedef const string& ConstType; @@ -855,12 +861,14 @@ class LIBPROTOBUF_EXPORT RepeatedStringTypeTraits { } static const RepeatedFieldType* GetDefaultRepeatedField() { + GoogleOnceInit(&repeated_string_type_traits_once_init_, + &InitializeDefaultRepeatedFields); return default_repeated_field_; } private: - friend void InitializeDefaultRepeatedFields(); - friend void DestroyDefaultRepeatedFields(); + static void InitializeDefaultRepeatedFields(); + static void DestroyDefaultRepeatedFields(); static const RepeatedFieldType *default_repeated_field_; }; @@ -1019,6 +1027,9 @@ class RepeatedMessageTypeTraits { static const RepeatedFieldType* GetDefaultRepeatedField(); }; +LIBPROTOBUF_EXPORT extern ProtobufOnceType +repeated_message_generic_type_traits_once_init_; + // This class exists only to hold a generic default empty repeated field for all // message-type repeated field extensions. class LIBPROTOBUF_EXPORT RepeatedMessageGenericTypeTraits { @@ -1026,14 +1037,17 @@ class LIBPROTOBUF_EXPORT RepeatedMessageGenericTypeTraits { typedef RepeatedPtrField< ::google::protobuf::MessageLite*> RepeatedFieldType; private: template friend class RepeatedMessageTypeTraits; - friend void InitializeDefaultRepeatedFields(); - friend void DestroyDefaultRepeatedFields(); + static void InitializeDefaultRepeatedFields(); + static void DestroyDefaultRepeatedFields(); static const RepeatedFieldType* default_repeated_field_; }; template inline const typename RepeatedMessageTypeTraits::RepeatedFieldType* RepeatedMessageTypeTraits::GetDefaultRepeatedField() { + GoogleOnceInit( + &repeated_message_generic_type_traits_once_init_, + &RepeatedMessageGenericTypeTraits::InitializeDefaultRepeatedFields); return reinterpret_cast( RepeatedMessageGenericTypeTraits::default_repeated_field_); } -- cgit v1.2.3