1// Protocol Buffers - Google's data interchange format 2// Copyright 2008 Google Inc. All rights reserved. 3// https://developers.google.com/protocol-buffers/ 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are 7// met: 8// 9// * Redistributions of source code must retain the above copyright 10// notice, this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above 12// copyright notice, this list of conditions and the following disclaimer 13// in the documentation and/or other materials provided with the 14// distribution. 15// * Neither the name of Google Inc. nor the names of its 16// contributors may be used to endorse or promote products derived from 17// this software without specific prior written permission. 18// 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31// This file defines common macros that are used in protobuf. 32// 33// To hide these definitions from the outside world (and to prevent collisions 34// if more than one version of protobuf is #included in the same project) you 35// must follow this pattern when #including port_def.inc in a header file: 36// 37// #include "other_header.h" 38// #include "message.h" 39// // etc. 40// 41// #include "port_def.inc" // MUST be last header included 42// 43// // Definitions for this header. 44// 45// #include "port_undef.inc" 46// 47// This is a textual header with no include guard, because we want to 48// detect/prohibit anytime it is #included twice without a corresponding 49// #undef. 50 51// These macros are private and should always be 52// ::util::RetrieveErrorSpace(*this) headers. If any of these errors fire, you 53// should either properly #include port_undef.h at the end of your header that 54// #includes port.h, or don't #include port.h twice in a .cc file. 55#ifdef PROTOBUF_NAMESPACE 56#error PROTOBUF_NAMESPACE was previously defined 57#endif 58#ifdef PROTOBUF_NAMESPACE_ID 59#error PROTOBUF_NAMESPACE_ID was previously defined 60#endif 61#ifdef PROTOBUF_ALWAYS_INLINE 62#error PROTOBUF_ALWAYS_INLINE was previously defined 63#endif 64#ifdef PROTOBUF_COLD 65#error PROTOBUF_COLD was previously defined 66#endif 67#ifdef PROTOBUF_NOINLINE 68#error PROTOBUF_NOINLINE was previously defined 69#endif 70#ifdef PROTOBUF_SECTION_VARIABLE 71#error PROTOBUF_SECTION_VARIABLE was previously defined 72#endif 73#ifdef PROTOBUF_DEPRECATED 74#error PROTOBUF_DEPRECATED was previously defined 75#endif 76#ifdef PROTOBUF_DEPRECATED_MSG 77#error PROTOBUF_DEPRECATED_MSG was previously defined 78#endif 79#ifdef PROTOBUF_FUNC_ALIGN 80#error PROTOBUF_FUNC_ALIGN was previously defined 81#endif 82#ifdef PROTOBUF_RETURNS_NONNULL 83#error PROTOBUF_RETURNS_NONNULL was previously defined 84#endif 85#ifdef PROTOBUF_ATTRIBUTE_REINITIALIZES 86#error PROTOBUF_ATTRIBUTE_REINITIALIZES was previously defined 87#endif 88#ifdef PROTOBUF_RTTI 89#error PROTOBUF_RTTI was previously defined 90#endif 91#ifdef PROTOBUF_VERSION 92#error PROTOBUF_VERSION was previously defined 93#endif 94#ifdef PROTOBUF_VERSION_SUFFIX 95#error PROTOBUF_VERSION_SUFFIX was previously defined 96#endif 97#ifdef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 98#error PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC was previously defined 99#endif 100#ifdef PROTOBUF_MIN_PROTOC_VERSION 101#error PROTOBUF_MIN_PROTOC_VERSION was previously defined 102#endif 103#ifdef PROTOBUF_PREDICT_TRUE 104#error PROTOBUF_PREDICT_TRUE was previously defined 105#endif 106#ifdef PROTOBUF_PREDICT_FALSE 107#error PROTOBUF_PREDICT_FALSE was previously defined 108#endif 109#ifdef PROTOBUF_FIELD_OFFSET 110#error PROTOBUF_FIELD_OFFSET was previously defined 111#endif 112#ifdef PROTOBUF_LL_FORMAT 113#error PROTOBUF_LL_FORMAT was previously defined 114#endif 115#ifdef PROTOBUF_GUARDED_BY 116#error PROTOBUF_GUARDED_BY was previously defined 117#endif 118#ifdef PROTOBUF_LONGLONG 119#error PROTOBUF_LONGLONG was previously defined 120#endif 121#ifdef PROTOBUF_ULONGLONG 122#error PROTOBUF_ULONGLONG was previously defined 123#endif 124#ifdef PROTOBUF_FALLTHROUGH_INTENDED 125#error PROTOBUF_FALLTHROUGH_INTENDED was previously defined 126#endif 127#ifdef PROTOBUF_EXPORT 128#error PROTOBUF_EXPORT was previously defined 129#endif 130#ifdef PROTOC_EXPORT 131#error PROTOC_EXPORT was previously defined 132#endif 133#ifdef PROTOBUF_MUST_USE_RESULT 134#error PROTOBUF_MUST_USE_RESULT was previously defined 135#endif 136#ifdef PROTOBUF_UNUSED 137#error PROTOBUF_UNUSED was previously defined 138#endif 139 140 141#define PROTOBUF_NAMESPACE "google::protobuf" 142#define PROTOBUF_NAMESPACE_ID google::protobuf 143#define PROTOBUF_NAMESPACE_OPEN \ 144 namespace google { \ 145 namespace protobuf { 146#define PROTOBUF_NAMESPACE_CLOSE \ 147 } /* namespace protobuf */ \ 148 } /* namespace google */ 149#define PROTOBUF_DEPRECATED 150#define PROTOBUF_DEPRECATED_MSG(x) 151#define PROTOBUF_SECTION_VARIABLE(x) 152#define PROTOBUF_MUST_USE_RESULT 153 154// ---------------------------------------------------------------------------- 155// Annotations: Some parts of the code have been annotated in ways that might 156// be useful to some compilers or tools, but are not supported universally. 157// You can #define these annotations yourself if the default implementation 158// is not right for you. 159 160#ifdef GOOGLE_ATTRIBUTE_ALWAYS_INLINE 161#define PROTOBUF_ALWAYS_INLINE GOOGLE_ATTRIBUTE_ALWAYS_INLINE 162#else 163#if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) 164// For functions we want to force inline. 165// Introduced in gcc 3.1. 166#define PROTOBUF_ALWAYS_INLINE __attribute__ ((always_inline)) 167#else 168// Other compilers will have to figure it out for themselves. 169#define PROTOBUF_ALWAYS_INLINE 170#endif 171#endif 172 173#ifdef GOOGLE_ATTRIBUTE_NOINLINE 174#define PROTOBUF_NOINLINE GOOGLE_ATTRIBUTE_NOINLINE 175#else 176#if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) 177// For functions we want to force not inline. 178// Introduced in gcc 3.1. 179#define PROTOBUF_NOINLINE __attribute__ ((noinline)) 180#elif defined(_MSC_VER) && (_MSC_VER >= 1400) 181// Seems to have been around since at least Visual Studio 2005 182#define PROTOBUF_NOINLINE __declspec(noinline) 183#else 184// Other compilers will have to figure it out for themselves. 185#define PROTOBUF_NOINLINE 186#endif 187#endif 188 189#ifdef GOOGLE_ATTRIBUTE_FUNC_ALIGN 190#define PROTOBUF_FUNC_ALIGN GOOGLE_ATTRIBUTE_FUNC_ALIGN 191#else 192#if defined(__clang__) || \ 193 defined(__GNUC__) && (__GNUC__ > 4 ||(__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) 194// Function alignment attribute introduced in gcc 4.3 195#define PROTOBUF_FUNC_ALIGN(bytes) __attribute__ ((aligned(bytes))) 196#else 197#define PROTOBUF_FUNC_ALIGN(bytes) 198#endif 199#endif 200 201#ifdef GOOGLE_PREDICT_TRUE 202#define PROTOBUF_PREDICT_TRUE GOOGLE_PREDICT_TRUE 203#else 204#ifdef __GNUC__ 205// Provided at least since GCC 3.0. 206#define PROTOBUF_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) 207#else 208#define PROTOBUF_PREDICT_TRUE(x) (x) 209#endif 210#endif 211 212#ifdef GOOGLE_PREDICT_FALSE 213#define PROTOBUF_PREDICT_FALSE GOOGLE_PREDICT_FALSE 214#else 215#ifdef __GNUC__ 216// Provided at least since GCC 3.0. 217#define PROTOBUF_PREDICT_FALSE(x) (__builtin_expect(x, 0)) 218#else 219#define PROTOBUF_PREDICT_FALSE(x) (x) 220#endif 221#endif 222 223#ifdef GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL 224#define PROTOBUF_RETURNS_NONNULL GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL 225#else 226#ifdef __GNUC__ 227#define PROTOBUF_RETURNS_NONNULL __attribute__((returns_nonnull)) 228#else 229#define PROTOBUF_RETURNS_NONNULL 230#endif 231#endif 232 233#if defined(__has_cpp_attribute) 234#if __has_cpp_attribute(clang::reinitializes) 235#define PROTOBUF_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]] 236#endif 237#endif 238#ifndef PROTOBUF_ATTRIBUTE_REINITIALIZES 239#define PROTOBUF_ATTRIBUTE_REINITIALIZES 240#endif 241 242#define PROTOBUF_GUARDED_BY(x) 243#define PROTOBUF_COLD 244 245// Copied from ABSL. 246#if defined(__clang__) && defined(__has_warning) 247#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") 248#define PROTOBUF_FALLTHROUGH_INTENDED [[clang::fallthrough]] 249#endif 250#elif defined(__GNUC__) && __GNUC__ >= 7 251#define PROTOBUF_FALLTHROUGH_INTENDED [[gnu::fallthrough]] 252#endif 253 254#ifndef PROTOBUF_FALLTHROUGH_INTENDED 255#define PROTOBUF_FALLTHROUGH_INTENDED 256#endif 257 258#if defined(__has_cpp_attribute) 259#define HAS_ATTRIBUTE(attr) __has_cpp_attribute(attr) 260#else 261#define HAS_ATTRIBUTE(attr) 0 262#endif 263 264#if HAS_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__)) 265#define PROTOBUF_UNUSED __attribute__((__unused__)) 266#else 267#define PROTOBUF_UNUSED 268#endif 269 270#undef HAS_ATTRIBUTE 271 272#ifdef _MSC_VER 273#define PROTOBUF_LONGLONG(x) x##I64 274#define PROTOBUF_ULONGLONG(x) x##UI64 275#define PROTOBUF_LL_FORMAT "I64" // As in printf("%I64d", ...) 276#else 277// By long long, we actually mean int64. 278#define PROTOBUF_LONGLONG(x) x##LL 279#define PROTOBUF_ULONGLONG(x) x##ULL 280// Used to format real long long integers. 281#define PROTOBUF_LL_FORMAT \ 282 "ll" // As in "%lld". Note that "q" is poor form also. 283#endif 284 285 286// Shared google3/opensource definitions. ////////////////////////////////////// 287 288#define PROTOBUF_VERSION 3009001 289#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 3009000 290#define PROTOBUF_MIN_PROTOC_VERSION 3009000 291#define PROTOBUF_VERSION_SUFFIX "" 292 293// The minimum library version which works with the current version of the 294// headers. 295#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3009000 296 297// ANDROID: autotdetect RTTI. Managing RTTI through command line -D 298// parameters is hard across modules, just autodetect it. 299#undef GOOGLE_PROTOBUF_NO_RTTI 300 301#if defined(__clang__) 302#if !__has_feature(cxx_rtti) 303#define GOOGLE_PROTOBUF_NO_RTTI 1 304#endif 305#elif defined(__GNUC__) 306#if !defined(__GXX_RTTI) 307#define GOOGLE_PROTOBUF_NO_RTTI 1 308#endif 309#else 310#define GOOGLE_PROTOBUF_NO_RTTI 1 311#endif 312 313#if defined(GOOGLE_PROTOBUF_NO_RTTI) && GOOGLE_PROTOBUF_NO_RTTI 314#define PROTOBUF_RTTI 0 315#else 316#define PROTOBUF_RTTI 1 317#endif 318 319// Returns the offset of the given field within the given aggregate type. 320// This is equivalent to the ANSI C offsetof() macro. However, according 321// to the C++ standard, offsetof() only works on POD types, and GCC 322// enforces this requirement with a warning. In practice, this rule is 323// unnecessarily strict; there is probably no compiler or platform on 324// which the offsets of the direct fields of a class are non-constant. 325// Fields inherited from superclasses *can* have non-constant offsets, 326// but that's not what this macro will be used for. 327#if defined(__clang__) 328// For Clang we use __builtin_offsetof() and suppress the warning, 329// to avoid Control Flow Integrity and UBSan vptr sanitizers from 330// crashing while trying to validate the invalid reinterpet_casts. 331#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \ 332 _Pragma("clang diagnostic push") \ 333 _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \ 334 __builtin_offsetof(TYPE, FIELD) \ 335 _Pragma("clang diagnostic pop") 336#else 337// Note that we calculate relative to the pointer value 16 here since if we 338// just use zero, GCC complains about dereferencing a NULL pointer. We 339// choose 16 rather than some other number just in case the compiler would 340// be confused by an unaligned pointer. 341#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \ 342 static_cast< ::google::protobuf::uint32>(reinterpret_cast<const char*>( \ 343 &reinterpret_cast<const TYPE*>(16)->FIELD) - \ 344 reinterpret_cast<const char*>(16)) 345#endif 346 347 348#if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS) 349#ifdef LIBPROTOBUF_EXPORTS 350#define PROTOBUF_EXPORT __declspec(dllexport) 351#else 352#define PROTOBUF_EXPORT __declspec(dllimport) 353#endif 354#ifdef LIBPROTOC_EXPORTS 355#define PROTOC_EXPORT __declspec(dllexport) 356#else 357#define PROTOC_EXPORT __declspec(dllimport) 358#endif 359#else 360#define PROTOBUF_EXPORT 361#define PROTOC_EXPORT 362#endif 363 364// Windows declares several inconvenient macro names. We #undef them and then 365// restore them in port_undef.inc. 366#ifdef _MSC_VER 367#pragma push_macro("GetMessage") 368#undef GetMessage 369#pragma push_macro("IGNORE") 370#undef IGNORE 371#pragma push_macro("IN") 372#undef IN 373#endif // _MSC_VER 374 375#if defined(__clang__) 376#pragma clang diagnostic push 377// TODO(gerbens) ideally we cleanup the code. But a cursory try shows many 378// violations. So let's ignore for now. 379#pragma clang diagnostic ignored "-Wshorten-64-to-32" 380#endif 381