port_def.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. // This file defines common macros that are used in protobuf.
  31. //
  32. // To hide these definitions from the outside world (and to prevent collisions
  33. // if more than one version of protobuf is #included in the same project) you
  34. // must follow this pattern when #including port_def.inc in a header file:
  35. //
  36. // #include "other_header.h"
  37. // #include "message.h"
  38. // // etc.
  39. //
  40. // #include "port_def.inc" // MUST be last header included
  41. //
  42. // // Definitions for this header.
  43. //
  44. // #include "port_undef.inc"
  45. //
  46. // This is a textual header with no include guard, because we want to
  47. // detect/prohibit anytime it is #included twice without a corresponding
  48. // #undef.
  49. // The definitions in this file are intended to be portable across Clang,
  50. // GCC, and MSVC. Function-like macros are usable without an #ifdef guard.
  51. // Syntax macros (for example, attributes) are always defined, although
  52. // they may be empty.
  53. // Portable fallbacks for C++20 feature test macros:
  54. // https://en.cppreference.com/w/cpp/feature_test
  55. #ifndef __has_cpp_attribute
  56. #define __has_cpp_attribute(x) 0
  57. #define PROTOBUF_has_cpp_attribute_DEFINED_
  58. #endif
  59. // Portable fallback for Clang's __has_feature macro:
  60. // https://clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
  61. #ifndef __has_feature
  62. #define __has_feature(x) 0
  63. #define PROTOBUF_has_feature_DEFINED_
  64. #endif
  65. // Portable fallback for Clang's __has_warning macro:
  66. #ifndef __has_warning
  67. #define __has_warning(x) 0
  68. #define PROTOBUF_has_warning_DEFINED_
  69. #endif
  70. // Portable fallbacks for the __has_attribute macro (GCC and Clang):
  71. // https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
  72. // https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html
  73. #ifndef __has_attribute
  74. #define __has_attribute(x) 0
  75. #define PROTOBUF_has_attribute_DEFINED_
  76. #endif
  77. // Portable fallback for __has_builtin (GCC and Clang):
  78. // https://clang.llvm.org/docs/LanguageExtensions.html#has-builtin
  79. // https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fbuiltin.html
  80. #ifndef __has_builtin
  81. #define __has_builtin(x) 0
  82. #define PROTOBUF_has_builtin_DEFINED_
  83. #endif
  84. // Portable check for GCC minimum version:
  85. // https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
  86. #if defined(__GNUC__) && defined(__GNUC_MINOR__) \
  87. && defined(__GNUC_PATCHLEVEL__)
  88. # define PROTOBUF_GNUC_MIN(x, y) \
  89. (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y))
  90. #else
  91. # define PROTOBUF_GNUC_MIN(x, y) 0
  92. #endif
  93. // Future versions of protobuf will include breaking changes to some APIs.
  94. // This macro can be set to enable these API changes ahead of time, so that
  95. // user code can be updated before upgrading versions of protobuf.
  96. // #define PROTOBUF_FUTURE_BREAKING_CHANGES 1
  97. #ifdef PROTOBUF_VERSION
  98. #error PROTOBUF_VERSION was previously defined
  99. #endif
  100. #define PROTOBUF_VERSION 3017003
  101. #ifdef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC
  102. #error PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC was previously defined
  103. #endif
  104. #define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 3017000
  105. #ifdef PROTOBUF_MIN_PROTOC_VERSION
  106. #error PROTOBUF_MIN_PROTOC_VERSION was previously defined
  107. #endif
  108. #define PROTOBUF_MIN_PROTOC_VERSION 3017000
  109. #ifdef PROTOBUF_VERSION_SUFFIX
  110. #error PROTOBUF_VERSION_SUFFIX was previously defined
  111. #endif
  112. #define PROTOBUF_VERSION_SUFFIX ""
  113. #if defined(PROTOBUF_NAMESPACE) || defined(PROTOBUF_NAMESPACE_ID)
  114. #error PROTOBUF_NAMESPACE or PROTOBUF_NAMESPACE_ID was previously defined
  115. #endif
  116. #define PROTOBUF_NAMESPACE "google::protobuf"
  117. #define PROTOBUF_NAMESPACE_ID google::protobuf
  118. #define PROTOBUF_NAMESPACE_OPEN \
  119. namespace google { \
  120. namespace protobuf {
  121. #define PROTOBUF_NAMESPACE_CLOSE \
  122. } /* namespace protobuf */ \
  123. } /* namespace google */
  124. #ifdef PROTOBUF_ALWAYS_INLINE
  125. #error PROTOBUF_ALWAYS_INLINE was previously defined
  126. #endif
  127. // For functions we want to force inline.
  128. #if defined(PROTOBUF_NO_INLINE)
  129. # define PROTOBUF_ALWAYS_INLINE
  130. #elif PROTOBUF_GNUC_MIN(3, 1)
  131. # define PROTOBUF_ALWAYS_INLINE __attribute__((always_inline))
  132. #elif defined(_MSC_VER)
  133. # define PROTOBUF_ALWAYS_INLINE __forceinline
  134. #else
  135. # define PROTOBUF_ALWAYS_INLINE
  136. #endif
  137. #ifdef PROTOBUF_NDEBUG_INLINE
  138. #error PROTOBUF_NDEBUG_INLINE was previously defined
  139. #endif
  140. // Avoid excessive inlining in non-optimized builds. Without other optimizations
  141. // the inlining is not going to provide benefits anyway and the huge resulting
  142. // functions, especially in the proto-generated serialization functions, produce
  143. // stack frames so large that many tests run into stack overflows (b/32192897).
  144. #if defined(NDEBUG) || (defined(_MSC_VER) && !defined(_DEBUG))
  145. # define PROTOBUF_NDEBUG_INLINE PROTOBUF_ALWAYS_INLINE
  146. #else
  147. # define PROTOBUF_NDEBUG_INLINE
  148. #endif
  149. // Note that PROTOBUF_NOINLINE is an attribute applied to functions, to prevent
  150. // them from being inlined by the compiler. This is different from
  151. // PROTOBUF_NO_INLINE, which is a user-supplied macro that disables forced
  152. // inlining by PROTOBUF_(ALWAYS|NDEBUG)_INLINE.
  153. #ifdef PROTOBUF_NOINLINE
  154. #error PROTOBUF_NOINLINE was previously defined
  155. #endif
  156. #if PROTOBUF_GNUC_MIN(3, 1)
  157. # define PROTOBUF_NOINLINE __attribute__((noinline))
  158. #elif defined(_MSC_VER)
  159. // Seems to have been around since at least Visual Studio 2005
  160. # define PROTOBUF_NOINLINE __declspec(noinline)
  161. #endif
  162. #ifdef PROTOBUF_MUSTTAIL
  163. #error PROTOBUF_MUSTTAIL was previously defined
  164. #endif
  165. #ifdef PROTOBUF_TAILCALL
  166. #error PROTOBUF_TAILCALL was previously defined
  167. #endif
  168. #if __has_cpp_attribute(clang::musttail) && \
  169. !defined(_ARCH_PPC) && !defined(__wasm__)
  170. # ifndef PROTO2_OPENSOURCE
  171. // Compilation fails on powerpc64le: b/187985113
  172. # endif
  173. #define PROTOBUF_MUSTTAIL [[clang::musttail]]
  174. #define PROTOBUF_TAILCALL true
  175. #else
  176. #define PROTOBUF_MUSTTAIL
  177. #define PROTOBUF_TAILCALL false
  178. #endif
  179. #ifdef PROTOBUF_EXCLUSIVE_LOCKS_REQUIRED
  180. #error PROTOBUF_EXCLUSIVE_LOCKS_REQUIRED was previously defined
  181. #endif
  182. #if __has_attribute(exclusive_locks_required)
  183. #define PROTOBUF_EXCLUSIVE_LOCKS_REQUIRED(...) \
  184. __attribute__((exclusive_locks_required(__VA_ARGS__)))
  185. #else
  186. #define PROTOBUF_EXCLUSIVE_LOCKS_REQUIRED(...)
  187. #endif
  188. #ifdef PROTOBUF_NO_THREAD_SAFETY_ANALYSIS
  189. #error PROTOBUF_NO_THREAD_SAFETY_ANALYSIS was previously defined
  190. #endif
  191. #if __has_attribute(no_thread_safety_analysis)
  192. #define PROTOBUF_NO_THREAD_SAFETY_ANALYSIS \
  193. __attribute__((no_thread_safety_analysis))
  194. #else
  195. #define PROTOBUF_NO_THREAD_SAFETY_ANALYSIS
  196. #endif
  197. #ifdef PROTOBUF_GUARDED_BY
  198. #error PROTOBUF_GUARDED_BY was previously defined
  199. #endif
  200. #if __has_attribute(guarded_by)
  201. #define PROTOBUF_GUARDED_BY(x) __attribute__((guarded_by(x)))
  202. #else
  203. #define PROTOBUF_GUARDED_BY(x)
  204. #endif
  205. #ifdef PROTOBUF_LOCKS_EXCLUDED
  206. #error PROTOBUF_LOCKS_EXCLUDED was previously defined
  207. #endif
  208. #if __has_attribute(locks_excluded)
  209. #define PROTOBUF_LOCKS_EXCLUDED(...) \
  210. __attribute__((locks_excluded(__VA_ARGS__)))
  211. #else
  212. #define PROTOBUF_LOCKS_EXCLUDED(...)
  213. #endif
  214. #ifdef PROTOBUF_COLD
  215. #error PROTOBUF_COLD was previously defined
  216. #endif
  217. #if __has_attribute(cold) || PROTOBUF_GNUC_MIN(4, 3)
  218. # define PROTOBUF_COLD __attribute__((cold))
  219. #else
  220. # define PROTOBUF_COLD
  221. #endif
  222. #ifdef PROTOBUF_SECTION_VARIABLE
  223. #error PROTOBUF_SECTION_VARIABLE was previously defined
  224. #endif
  225. #define PROTOBUF_SECTION_VARIABLE(x)
  226. #if defined(PROTOBUF_DEPRECATED)
  227. #error PROTOBUF_DEPRECATED was previously defined
  228. #endif
  229. #if defined(PROTOBUF_DEPRECATED_MSG)
  230. #error PROTOBUF_DEPRECATED_MSG was previously defined
  231. #endif
  232. #if __has_attribute(deprecated) || PROTOBUF_GNUC_MIN(3, 0)
  233. # define PROTOBUF_DEPRECATED __attribute__((deprecated))
  234. # define PROTOBUF_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
  235. #elif defined(_MSC_VER)
  236. # define PROTOBUF_DEPRECATED __declspec(deprecated)
  237. # define PROTOBUF_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
  238. #else
  239. # define PROTOBUF_DEPRECATED
  240. # define PROTOBUF_DEPRECATED_MSG(msg)
  241. #endif
  242. #if defined(PROTOBUF_DEPRECATED_ENUM)
  243. #error PROTOBUF_DEPRECATED_ENUM was previously defined
  244. #endif
  245. #if defined(__clang__) || PROTOBUF_GNUC_MIN(6, 0)
  246. // https://gcc.gnu.org/gcc-6/changes.html
  247. # define PROTOBUF_DEPRECATED_ENUM __attribute__((deprecated))
  248. #else
  249. # define PROTOBUF_DEPRECATED_ENUM
  250. #endif
  251. #ifdef PROTOBUF_FUNC_ALIGN
  252. #error PROTOBUF_FUNC_ALIGN was previously defined
  253. #endif
  254. #if __has_attribute(aligned) || PROTOBUF_GNUC_MIN(4, 3)
  255. #define PROTOBUF_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
  256. #else
  257. #define PROTOBUF_FUNC_ALIGN(bytes)
  258. #endif
  259. #ifdef PROTOBUF_RETURNS_NONNULL
  260. #error PROTOBUF_RETURNS_NONNULL was previously defined
  261. #endif
  262. #if __has_attribute(returns_nonnull) || PROTOBUF_GNUC_MIN(4, 9)
  263. #define PROTOBUF_RETURNS_NONNULL __attribute__((returns_nonnull))
  264. #else
  265. #define PROTOBUF_RETURNS_NONNULL
  266. #endif
  267. #ifdef PROTOBUF_ATTRIBUTE_REINITIALIZES
  268. #error PROTOBUF_ATTRIBUTE_REINITIALIZES was previously defined
  269. #endif
  270. #if __has_cpp_attribute(clang::reinitializes)
  271. #define PROTOBUF_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
  272. #else
  273. #define PROTOBUF_ATTRIBUTE_REINITIALIZES
  274. #endif
  275. // The minimum library version which works with the current version of the
  276. // headers.
  277. #define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3017000
  278. #ifdef PROTOBUF_RTTI
  279. #error PROTOBUF_RTTI was previously defined
  280. #endif
  281. #if defined(GOOGLE_PROTOBUF_NO_RTTI) && GOOGLE_PROTOBUF_NO_RTTI
  282. #define PROTOBUF_RTTI 0
  283. #elif __has_feature(cxx_rtti)
  284. #define PROTOBUF_RTTI 1
  285. #elif defined(__cxx_rtti)
  286. // https://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros#C.2B.2B98
  287. #define PROTOBUF_RTTI 1
  288. #else
  289. #define PROTOBUF_RTTI 0
  290. #endif
  291. // Returns the offset of the given field within the given aggregate type.
  292. // This is equivalent to the ANSI C offsetof() macro. However, according
  293. // to the C++ standard, offsetof() only works on POD types, and GCC
  294. // enforces this requirement with a warning. In practice, this rule is
  295. // unnecessarily strict; there is probably no compiler or platform on
  296. // which the offsets of the direct fields of a class are non-constant.
  297. // Fields inherited from superclasses *can* have non-constant offsets,
  298. // but that's not what this macro will be used for.
  299. #ifdef PROTOBUF_FIELD_OFFSET
  300. #error PROTOBUF_FIELD_OFFSET was previously defined
  301. #endif
  302. #if defined(__clang__)
  303. // For Clang we use __builtin_offsetof() and suppress the warning,
  304. // to avoid Control Flow Integrity and UBSan vptr sanitizers from
  305. // crashing while trying to validate the invalid reinterpret_casts.
  306. #define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \
  307. _Pragma("clang diagnostic push") \
  308. _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \
  309. __builtin_offsetof(TYPE, FIELD) \
  310. _Pragma("clang diagnostic pop")
  311. #elif PROTOBUF_GNUC_MIN(4, 8)
  312. #define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) __builtin_offsetof(TYPE, FIELD)
  313. #else // defined(__clang__)
  314. // Note that we calculate relative to the pointer value 16 here since if we
  315. // just use zero, GCC complains about dereferencing a NULL pointer. We
  316. // choose 16 rather than some other number just in case the compiler would
  317. // be confused by an unaligned pointer.
  318. #define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \
  319. static_cast< ::google::protobuf::uint32>(reinterpret_cast<const char*>( \
  320. &reinterpret_cast<const TYPE*>(16)->FIELD) - \
  321. reinterpret_cast<const char*>(16))
  322. #endif
  323. #ifdef PROTOBUF_EXPORT
  324. #error PROTOBUF_EXPORT was previously defined
  325. #endif
  326. #if defined(PROTOBUF_USE_DLLS) && defined(_MSC_VER)
  327. # if defined(LIBPROTOBUF_EXPORTS)
  328. # define PROTOBUF_EXPORT __declspec(dllexport)
  329. # define PROTOBUF_EXPORT_TEMPLATE_DECLARE
  330. # define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllexport)
  331. # else
  332. # define PROTOBUF_EXPORT __declspec(dllimport)
  333. # define PROTOBUF_EXPORT_TEMPLATE_DECLARE
  334. # define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllimport)
  335. # endif // defined(LIBPROTOBUF_EXPORTS)
  336. #elif defined(PROTOBUF_USE_DLLS) && defined(LIBPROTOBUF_EXPORTS)
  337. # define PROTOBUF_EXPORT __attribute__((visibility("default")))
  338. # define PROTOBUF_EXPORT_TEMPLATE_DECLARE __attribute__((visibility("default")))
  339. # define PROTOBUF_EXPORT_TEMPLATE_DEFINE
  340. #else
  341. # define PROTOBUF_EXPORT
  342. # define PROTOBUF_EXPORT_TEMPLATE_DECLARE
  343. # define PROTOBUF_EXPORT_TEMPLATE_DEFINE
  344. #endif
  345. #ifdef PROTOC_EXPORT
  346. #error PROTOC_EXPORT was previously defined
  347. #endif
  348. #if defined(PROTOBUF_USE_DLLS) && defined(_MSC_VER)
  349. # if defined(LIBPROTOC_EXPORTS)
  350. # define PROTOC_EXPORT __declspec(dllexport)
  351. # else
  352. # define PROTOC_EXPORT __declspec(dllimport)
  353. # endif // defined(LIBPROTOC_EXPORTS)
  354. #elif defined(PROTOBUF_USE_DLLS) && defined(LIBPROTOBUF_EXPORTS)
  355. # define PROTOC_EXPORT __attribute__((visibility("default")))
  356. #else
  357. # define PROTOC_EXPORT
  358. #endif
  359. #if defined(PROTOBUF_PREDICT_TRUE) || defined(PROTOBUF_PREDICT_FALSE)
  360. #error PROTOBUF_PREDICT_(TRUE|FALSE) was previously defined
  361. #endif
  362. #if PROTOBUF_GNUC_MIN(3, 0)
  363. # define PROTOBUF_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
  364. # define PROTOBUF_PREDICT_FALSE(x) (__builtin_expect((x), 0))
  365. #else
  366. # define PROTOBUF_PREDICT_TRUE(x) (x)
  367. # define PROTOBUF_PREDICT_FALSE(x) (x)
  368. #endif
  369. #ifdef PROTOBUF_MUST_USE_RESULT
  370. #error PROTOBUF_MUST_USE_RESULT was previously defined
  371. #endif
  372. # define PROTOBUF_MUST_USE_RESULT
  373. #ifdef PROTOBUF_MUST_USE_EXTRACT_RESULT
  374. #error PROTOBUF_MUST_USE_EXTRACT_RESULT was previously defined
  375. #endif
  376. #ifdef PROTOBUF_FORCE_COPY_IN_RELEASE
  377. #error PROTOBUF_FORCE_COPY_IN_RELEASE was previously defined
  378. #endif
  379. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  380. #error PROTOBUF_FORCE_COPY_IN_SWAP was previously defined
  381. #endif
  382. #ifdef PROTOBUF_FALLTHROUGH_INTENDED
  383. #error PROTOBUF_FALLTHROUGH_INTENDED was previously defined
  384. #endif
  385. #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
  386. #define PROTOBUF_FALLTHROUGH_INTENDED [[clang::fallthrough]]
  387. #elif PROTOBUF_GNUC_MIN(7, 0)
  388. #define PROTOBUF_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
  389. #else
  390. #define PROTOBUF_FALLTHROUGH_INTENDED
  391. #endif
  392. // PROTOBUF_ASSUME(pred) tells the compiler that it can assume pred is true. To
  393. // be safe, we also validate the assumption with a GOOGLE_DCHECK in unoptimized
  394. // builds. The macro does not do anything useful if the compiler does not
  395. // support __builtin_assume.
  396. #ifdef PROTOBUF_ASSUME
  397. #error PROTOBUF_ASSUME was previously defined
  398. #endif
  399. #if __has_builtin(__builtin_assume)
  400. #define PROTOBUF_ASSUME(pred) \
  401. GOOGLE_DCHECK(pred); \
  402. __builtin_assume(pred)
  403. #else
  404. #define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred)
  405. #endif
  406. // Specify memory alignment for structs, classes, etc.
  407. // Use like:
  408. // class PROTOBUF_ALIGNAS(16) MyClass { ... }
  409. // PROTOBUF_ALIGNAS(16) int array[4];
  410. //
  411. // In most places you can use the C++11 keyword "alignas", which is preferred.
  412. //
  413. // But compilers have trouble mixing __attribute__((...)) syntax with
  414. // alignas(...) syntax.
  415. //
  416. // Doesn't work in clang or gcc:
  417. // struct alignas(16) __attribute__((packed)) S { char c; };
  418. // Works in clang but not gcc:
  419. // struct __attribute__((packed)) alignas(16) S2 { char c; };
  420. // Works in clang and gcc:
  421. // struct alignas(16) S3 { char c; } __attribute__((packed));
  422. //
  423. // There are also some attributes that must be specified *before* a class
  424. // definition: visibility (used for exporting functions/classes) is one of
  425. // these attributes. This means that it is not possible to use alignas() with a
  426. // class that is marked as exported.
  427. #ifdef PROTOBUF_ALIGNAS
  428. #error PROTOBUF_ALIGNAS was previously defined
  429. #endif
  430. #if defined(_MSC_VER)
  431. #define PROTOBUF_ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
  432. #elif PROTOBUF_GNUC_MIN(3, 0)
  433. #define PROTOBUF_ALIGNAS(byte_alignment) \
  434. __attribute__((aligned(byte_alignment)))
  435. #else
  436. #define PROTOBUF_ALIGNAS(byte_alignment) alignas(byte_alignment)
  437. #endif
  438. #ifdef PROTOBUF_FINAL
  439. #error PROTOBUF_FINAL was previously defined
  440. #endif
  441. #define PROTOBUF_FINAL final
  442. #ifdef PROTOBUF_THREAD_LOCAL
  443. #error PROTOBUF_THREAD_LOCAL was previously defined
  444. #endif
  445. #if defined(_MSC_VER)
  446. #define PROTOBUF_THREAD_LOCAL __declspec(thread)
  447. #else
  448. #define PROTOBUF_THREAD_LOCAL __thread
  449. #endif
  450. // For enabling message owned arena, one major blocker is semantic change from
  451. // moving to copying when there is ownership transfer (e.g., move ctor, swap,
  452. // set allocated, release). This change not only causes performance regression
  453. // but also breaks users code (e.g., dangling reference). For top-level
  454. // messages, since it owns the arena, we can mitigate the issue by transferring
  455. // ownership of arena. However, we cannot do that for nested messages. In order
  456. // to tell how many usages of nested messages affected by message owned arena,
  457. // we need to simulate the arena ownership.
  458. // This experiment is purely for the purpose of gathering data. All code guarded
  459. // by this flag is supposed to be removed after this experiment.
  460. #define PROTOBUF_MESSAGE_OWNED_ARENA_EXPERIMENT
  461. #ifdef PROTOBUF_CONSTINIT
  462. #error PROTOBUF_CONSTINIT was previously defined
  463. #endif
  464. #if defined(__cpp_constinit) && !PROTOBUF_GNUC_MIN(3, 0) && !defined(_MSC_VER)
  465. // Our use of constinit does not yet work with GCC:
  466. // https://github.com/protocolbuffers/protobuf/issues/8310
  467. // Does not work yet with Visual Studio 2019 Update 16.10
  468. #define PROTOBUF_CONSTINIT constinit
  469. #elif __has_cpp_attribute(clang::require_constant_initialization)
  470. #define PROTOBUF_CONSTINIT [[clang::require_constant_initialization]]
  471. #else
  472. #define PROTOBUF_CONSTINIT
  473. #endif
  474. // Some globals with an empty non-trivial destructor are annotated with
  475. // no_destroy for performance reasons. It reduces the cost of these globals in
  476. // non-opt mode and under sanitizers.
  477. #ifdef PROTOBUF_ATTRIBUTE_NO_DESTROY
  478. #error PROTOBUF_ATTRIBUTE_NO_DESTROY was previously defined
  479. #endif
  480. #if __has_cpp_attribute(clang::no_destroy)
  481. #define PROTOBUF_ATTRIBUTE_NO_DESTROY [[clang::no_destroy]]
  482. #else
  483. #define PROTOBUF_ATTRIBUTE_NO_DESTROY
  484. #endif
  485. // Protobuf extensions and reflection require registration of the protos linked
  486. // in the binary. Not until everything is registered does the runtime have a
  487. // complete view on all protos. When code is using reflection or extensions
  488. // in between registration calls this can lead to surprising behavior. By
  489. // having the registration run first we mitigate this scenario.
  490. // Highest priority is 101. We use 102 to allow code that really wants to
  491. // higher priority to still beat us.
  492. #ifdef PROTOBUF_ATTRIBUTE_INIT_PRIORITY
  493. #error PROTOBUF_ATTRIBUTE_INIT_PRIORITY was previously defined
  494. #endif
  495. #if PROTOBUF_GNUC_MIN(3, 0) && (!defined(__APPLE__) || defined(__clang__))
  496. #define PROTOBUF_ATTRIBUTE_INIT_PRIORITY __attribute__((init_priority((102))))
  497. #else
  498. #define PROTOBUF_ATTRIBUTE_INIT_PRIORITY
  499. #endif
  500. #ifdef PROTOBUF_PRAGMA_INIT_SEG
  501. #error PROTOBUF_PRAGMA_INIT_SEG was previously defined
  502. #endif
  503. #if _MSC_VER
  504. #define PROTOBUF_PRAGMA_INIT_SEG __pragma(init_seg(lib))
  505. #else
  506. #define PROTOBUF_PRAGMA_INIT_SEG
  507. #endif
  508. #ifdef PROTOBUF_ATTRIBUTE_WEAK
  509. #error PROTOBUF_ATTRIBUTE_WEAK was previously defined
  510. #endif
  511. #if __has_attribute(weak) && !defined(__MINGW32__)
  512. #define PROTOBUF_ATTRIBUTE_WEAK __attribute__((weak))
  513. #else
  514. #define PROTOBUF_ATTRIBUTE_WEAK
  515. #endif
  516. // Macros to detect sanitizers.
  517. #ifdef PROTOBUF_ASAN
  518. #error PROTOBUF_ASAN was previously defined
  519. #endif
  520. #ifdef PROTOBUF_MSAN
  521. #error PROTOBUF_MSAN was previously defined
  522. #endif
  523. #ifdef PROTOBUF_TSAN
  524. #error PROTOBUF_TSAN was previously defined
  525. #endif
  526. #if defined(__clang__)
  527. # if __has_feature(address_sanitizer)
  528. # define PROTOBUF_ASAN 1
  529. # endif
  530. # if __has_feature(thread_sanitizer)
  531. # define PROTOBUF_TSAN 1
  532. # endif
  533. # if __has_feature(memory_sanitizer)
  534. # define PROTOBUF_MSAN 1
  535. # endif
  536. #elif PROTOBUF_GNUC_MIN(3, 0)
  537. # define PROTOBUF_ASAN __SANITIZE_ADDRESS__
  538. # define PROTOBUF_TSAN __SANITIZE_THREAD__
  539. #endif
  540. #ifdef PROTOBUF_UNUSED
  541. #error PROTOBUF_UNUSED was previously defined
  542. #endif
  543. #if __has_cpp_attribute(unused) || \
  544. (PROTOBUF_GNUC_MIN(3, 0) && !defined(__clang__))
  545. #define PROTOBUF_UNUSED __attribute__((__unused__))
  546. #else
  547. #define PROTOBUF_UNUSED
  548. #endif
  549. // Windows declares several inconvenient macro names. We #undef them and then
  550. // restore them in port_undef.inc.
  551. #ifdef _MSC_VER
  552. #pragma push_macro("CREATE_NEW")
  553. #undef CREATE_NEW
  554. #pragma push_macro("DELETE")
  555. #undef DELETE
  556. #pragma push_macro("DOUBLE_CLICK")
  557. #undef DOUBLE_CLICK
  558. #pragma push_macro("ERROR")
  559. #undef ERROR
  560. #pragma push_macro("ERROR_BUSY")
  561. #undef ERROR_BUSY
  562. #pragma push_macro("ERROR_INSTALL_FAILED")
  563. #undef ERROR_INSTALL_FAILED
  564. #pragma push_macro("ERROR_NOT_FOUND")
  565. #undef ERROR_NOT_FOUND
  566. #pragma push_macro("GetMessage")
  567. #undef GetMessage
  568. #pragma push_macro("IGNORE")
  569. #undef IGNORE
  570. #pragma push_macro("IN")
  571. #undef IN
  572. #pragma push_macro("INPUT_KEYBOARD")
  573. #undef INPUT_KEYBOARD
  574. #pragma push_macro("NO_ERROR")
  575. #undef NO_ERROR
  576. #pragma push_macro("OUT")
  577. #undef OUT
  578. #pragma push_macro("OPTIONAL")
  579. #undef OPTIONAL
  580. #pragma push_macro("min")
  581. #undef min
  582. #pragma push_macro("max")
  583. #undef max
  584. #pragma push_macro("NEAR")
  585. #undef NEAR
  586. #pragma push_macro("NO_DATA")
  587. #undef NO_DATA
  588. #pragma push_macro("REASON_UNKNOWN")
  589. #undef REASON_UNKNOWN
  590. #pragma push_macro("SERVICE_DISABLED")
  591. #undef SERVICE_DISABLED
  592. #pragma push_macro("SEVERITY_ERROR")
  593. #undef SEVERITY_ERROR
  594. #pragma push_macro("STRICT")
  595. #undef STRICT
  596. #pragma push_macro("timezone")
  597. #undef timezone
  598. #endif // _MSC_VER
  599. #if defined(__clang__) || PROTOBUF_GNUC_MIN(3, 0) || defined(_MSC_VER)
  600. // Don't let Objective-C Macros interfere with proto identifiers with the same
  601. // name.
  602. #pragma push_macro("DEBUG")
  603. #undef DEBUG
  604. #endif // defined(__clang__) || PROTOBUF_GNUC_MIN(3, 0) || defined(_MSC_VER)
  605. #if defined(__clang__)
  606. #pragma clang diagnostic push
  607. // TODO(gerbens) ideally we cleanup the code. But a cursory try shows many
  608. // violations. So let's ignore for now.
  609. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  610. #elif PROTOBUF_GNUC_MIN(3, 0)
  611. // GCC does not allow disabling diagnostics within an expression:
  612. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60875, so we disable this one
  613. // globally even though it's only used for PROTOBUF_FIELD_OFFSET.
  614. #pragma GCC diagnostic push
  615. #pragma GCC diagnostic ignored "-Winvalid-offsetof"
  616. #endif
  617. // Silence some MSVC warnings in all our code.
  618. #if _MSC_VER
  619. #pragma warning(push)
  620. // For non-trivial unions
  621. #pragma warning(disable : 4582)
  622. #pragma warning(disable : 4583)
  623. // For init_seg(lib)
  624. #pragma warning(disable : 4073)
  625. // To silence the fact that we will pop this push from another file
  626. #pragma warning(disable : 5031)
  627. #endif
  628. // We don't want code outside port_def doing complex testing, so
  629. // remove our portable condition test macros to nudge folks away from
  630. // using it themselves.
  631. #ifdef PROTOBUF_has_cpp_attribute_DEFINED_
  632. # undef __has_cpp_attribute
  633. # undef PROTOBUF_has_cpp_attribute_DEFINED_
  634. #endif
  635. #ifdef PROTOBUF_has_feature_DEFINED_
  636. # undef __has_feature
  637. # undef PROTOBUF_has_feature_DEFINED_
  638. #endif
  639. #ifdef PROTOBUF_has_warning_DEFINED_
  640. # undef __has_warning
  641. # undef PROTOBUF_has_warning_DEFINED_
  642. #endif
  643. #ifdef PROTOBUF_has_attribute_DEFINED_
  644. # undef __has_attribute
  645. # undef PROTOBUF_has_attribute_DEFINED_
  646. #endif
  647. #ifdef PROTOBUF_has_builtin_DEFINED_
  648. # undef __has_builtin
  649. # undef PROTOBUF_has_builtin_DEFINED_
  650. #endif
  651. #undef PROTOBUF_GNUC_MIN