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// The definitions in this file are intended to be portable across Clang,
52// GCC, and MSVC. Function-like macros are usable without an #ifdef guard.
53// Syntax macros (for example, attributes) are always defined, although
54// they may be empty.
55//
56// Some definitions rely on the NDEBUG macro and/or (in MSVC) _DEBUG:
57// - https://en.cppreference.com/w/c/error/assert
58// - https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros#microsoft-specific-predefined-macros
59//
60// References for predefined macros:
61// - Standard: https://en.cppreference.com/w/cpp/preprocessor/replace
62// - Clang: https://clang.llvm.org/docs/LanguageExtensions.html
63// (see also GCC predefined macros)
64// - GCC: https://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html
65// - MSVC: https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
66// - Interactive (Clang/GCC only): https://www.compiler-explorer.com/z/hc6jKd3sj
67//
68// References for attributes (and extension attributes):
69// - Standard: https://en.cppreference.com/w/cpp/language/attributes
70// - Clang: https://clang.llvm.org/docs/AttributeReference.html
71// - GCC: https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
72// (see Clang attribute docs as well)
73//
74// References for standard C++ language conformance (and minimum versions):
75// - Clang: https://clang.llvm.org/cxx_status.html
76// - GCC: https://gcc.gnu.org/projects/cxx-status.html
77// - MSVC: https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance
78//
79// Historical release notes (which can help to determine minimum versions):
80// - Clang: https://releases.llvm.org/
81// - GCC: https://gcc.gnu.org/releases.html
82// - MSVC: https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes-history
83// https://docs.microsoft.com/en-us/visualstudio/releasenotes/vs2017-relnotes-history
84
85// Portable fallbacks for C++20 feature test macros:
86// https://en.cppreference.com/w/cpp/feature_test
87#ifndef __has_cpp_attribute
88#define __has_cpp_attribute(x) 0
89#define PROTOBUF_has_cpp_attribute_DEFINED_
90#endif
91
92// Portable fallback for Clang's __has_feature macro:
93// https://clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
94#ifndef __has_feature
95#define __has_feature(x) 0
96#define PROTOBUF_has_feature_DEFINED_
97#endif
98
99// Portable fallback for Clang's __has_warning macro:
100#ifndef __has_warning
101#define __has_warning(x) 0
102#define PROTOBUF_has_warning_DEFINED_
103#endif
104
105// Portable fallbacks for the __has_attribute macro (GCC and Clang):
106// https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
107// https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html
108#ifndef __has_attribute
109#define __has_attribute(x) 0
110#define PROTOBUF_has_attribute_DEFINED_
111#endif
112
113// Portable fallback for __has_builtin (GCC and Clang):
114// https://clang.llvm.org/docs/LanguageExtensions.html#has-builtin
115// https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fbuiltin.html
116#ifndef __has_builtin
117#define __has_builtin(x) 0
118#define PROTOBUF_has_builtin_DEFINED_
119#endif
120
121// Portable PROTOBUF_BUILTIN_BSWAPxx definitions
122// Code must check for availability, e.g.: `defined(PROTOBUF_BUILTIN_BSWAP32)`
123#ifdef PROTOBUF_BUILTIN_BSWAP16
124#error PROTOBUF_BUILTIN_BSWAP16 was previously defined
125#endif
126#ifdef PROTOBUF_BUILTIN_BSWAP32
127#error PROTOBUF_BUILTIN_BSWAP32 was previously defined
128#endif
129#ifdef PROTOBUF_BUILTIN_BSWAP64
130#error PROTOBUF_BUILTIN_BSWAP64 was previously defined
131#endif
132#if defined(__GNUC__) || __has_builtin(__builtin_bswap16)
133#define PROTOBUF_BUILTIN_BSWAP16(x) __builtin_bswap16(x)
134#endif
135#if defined(__GNUC__) || __has_builtin(__builtin_bswap32)
136#define PROTOBUF_BUILTIN_BSWAP32(x) __builtin_bswap32(x)
137#endif
138#if defined(__GNUC__) || __has_builtin(__builtin_bswap64)
139#define PROTOBUF_BUILTIN_BSWAP64(x) __builtin_bswap64(x)
140#endif
141
142// Portable check for GCC minimum version:
143// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
144#if defined(__GNUC__) && defined(__GNUC_MINOR__) \
145 && defined(__GNUC_PATCHLEVEL__)
146# define PROTOBUF_GNUC_MIN(x, y) \
147 (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y))
148#else
149# define PROTOBUF_GNUC_MIN(x, y) 0
150#endif
151
152// Portable check for MSVC minimum version:
153// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
154#if defined(_MSC_VER)
155#define PROTOBUF_MSC_VER_MIN(x) (_MSC_VER >= x)
156#else
157#define PROTOBUF_MSC_VER_MIN(x) 0
158#endif
159
160// Portable check for minimum C++ language version:
161// https://en.cppreference.com/w/cpp/preprocessor/replace
162// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
163#if !defined(_MSVC_LANG)
164#define PROTOBUF_CPLUSPLUS_MIN(x) (__cplusplus >= x)
165#else
166#define PROTOBUF_CPLUSPLUS_MIN(x) (_MSVC_LANG >= x)
167#endif
168
169// Future versions of protobuf will include breaking changes to some APIs.
170// This macro can be set to enable these API changes ahead of time, so that
171// user code can be updated before upgrading versions of protobuf.
172// PROTOBUF_FUTURE_FINAL is used on classes that are historically not marked as
173// final, but that may be marked final in future (breaking) releases.
174// #define PROTOBUF_FUTURE_BREAKING_CHANGES 1
175// #define PROTOBUF_FUTURE_FINAL final
176#define PROTOBUF_FUTURE_FINAL
177
178#ifdef PROTOBUF_VERSION
179#error PROTOBUF_VERSION was previously defined
180#endif
181#define PROTOBUF_VERSION 3021004
182
183#ifdef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC
184#error PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC was previously defined
185#endif
186#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 3021000
187
188#ifdef PROTOBUF_MIN_PROTOC_VERSION
189#error PROTOBUF_MIN_PROTOC_VERSION was previously defined
190#endif
191#define PROTOBUF_MIN_PROTOC_VERSION 3021000
192
193#ifdef PROTOBUF_VERSION_SUFFIX
194#error PROTOBUF_VERSION_SUFFIX was previously defined
195#endif
196#define PROTOBUF_VERSION_SUFFIX ""
197
198#if defined(PROTOBUF_NAMESPACE) || defined(PROTOBUF_NAMESPACE_ID)
199#error PROTOBUF_NAMESPACE or PROTOBUF_NAMESPACE_ID was previously defined
200#endif
201#define PROTOBUF_NAMESPACE "google::protobuf"
202#define PROTOBUF_NAMESPACE_ID google::protobuf
203#define PROTOBUF_NAMESPACE_OPEN \
204 namespace google { \
205 namespace protobuf {
206#define PROTOBUF_NAMESPACE_CLOSE \
207 } /* namespace protobuf */ \
208 } /* namespace google */
209
210#ifdef PROTOBUF_ALWAYS_INLINE
211#error PROTOBUF_ALWAYS_INLINE was previously defined
212#endif
213// For functions we want to force inline.
214#if defined(PROTOBUF_NO_INLINE)
215# define PROTOBUF_ALWAYS_INLINE
216#elif PROTOBUF_GNUC_MIN(3, 1)
217# define PROTOBUF_ALWAYS_INLINE __attribute__((always_inline))
218#elif defined(_MSC_VER)
219# define PROTOBUF_ALWAYS_INLINE __forceinline
220#else
221# define PROTOBUF_ALWAYS_INLINE
222#endif
223
224#ifdef PROTOBUF_NDEBUG_INLINE
225#error PROTOBUF_NDEBUG_INLINE was previously defined
226#endif
227// Avoid excessive inlining in non-optimized builds. Without other optimizations
228// the inlining is not going to provide benefits anyway and the huge resulting
229// functions, especially in the proto-generated serialization functions, produce
230// stack frames so large that many tests run into stack overflows (b/32192897).
231#if defined(NDEBUG) || (defined(_MSC_VER) && !defined(_DEBUG))
232# define PROTOBUF_NDEBUG_INLINE PROTOBUF_ALWAYS_INLINE
233#else
234# define PROTOBUF_NDEBUG_INLINE
235#endif
236
237// Note that PROTOBUF_NOINLINE is an attribute applied to functions, to prevent
238// them from being inlined by the compiler. This is different from
239// PROTOBUF_NO_INLINE, which is a user-supplied macro that disables forced
240// inlining by PROTOBUF_(ALWAYS|NDEBUG)_INLINE.
241#ifdef PROTOBUF_NOINLINE
242#error PROTOBUF_NOINLINE was previously defined
243#endif
244#if PROTOBUF_GNUC_MIN(3, 1)
245# define PROTOBUF_NOINLINE __attribute__((noinline))
246#elif defined(_MSC_VER)
247// Seems to have been around since at least Visual Studio 2005
248# define PROTOBUF_NOINLINE __declspec(noinline)
249#endif
250
251#ifdef PROTOBUF_MUSTTAIL
252#error PROTOBUF_MUSTTAIL was previously defined
253#endif
254#ifdef PROTOBUF_TAILCALL
255#error PROTOBUF_TAILCALL was previously defined
256#endif
257#if __has_cpp_attribute(clang::musttail) && !defined(__arm__) && \
258 !defined(_ARCH_PPC) && !defined(__wasm__) && \
259 !(defined(_MSC_VER) && defined(_M_IX86)) && \
260 !(defined(__NDK_MAJOR__) && __NDK_MAJOR <= 24)
261# ifndef PROTO2_OPENSOURCE
262// Compilation fails on ARM32: b/195943306
263// Compilation fails on powerpc64le: b/187985113
264// Compilation fails on X86 Windows:
265// https://github.com/llvm/llvm-project/issues/53271
266# endif
267#define PROTOBUF_MUSTTAIL [[clang::musttail]]
268#define PROTOBUF_TAILCALL true
269#else
270#define PROTOBUF_MUSTTAIL
271#define PROTOBUF_TAILCALL false
272#endif
273
274#ifdef PROTOBUF_EXCLUSIVE_LOCKS_REQUIRED
275#error PROTOBUF_EXCLUSIVE_LOCKS_REQUIRED was previously defined
276#endif
277#if __has_attribute(exclusive_locks_required)
278#define PROTOBUF_EXCLUSIVE_LOCKS_REQUIRED(...) \
279 __attribute__((exclusive_locks_required(__VA_ARGS__)))
280#else
281#define PROTOBUF_EXCLUSIVE_LOCKS_REQUIRED(...)
282#endif
283
284#ifdef PROTOBUF_NO_THREAD_SAFETY_ANALYSIS
285#error PROTOBUF_NO_THREAD_SAFETY_ANALYSIS was previously defined
286#endif
287#if __has_attribute(no_thread_safety_analysis)
288#define PROTOBUF_NO_THREAD_SAFETY_ANALYSIS \
289 __attribute__((no_thread_safety_analysis))
290#else
291#define PROTOBUF_NO_THREAD_SAFETY_ANALYSIS
292#endif
293
294#ifdef PROTOBUF_GUARDED_BY
295#error PROTOBUF_GUARDED_BY was previously defined
296#endif
297#if __has_attribute(guarded_by)
298#define PROTOBUF_GUARDED_BY(x) __attribute__((guarded_by(x)))
299#else
300#define PROTOBUF_GUARDED_BY(x)
301#endif
302
303#ifdef PROTOBUF_LOCKS_EXCLUDED
304#error PROTOBUF_LOCKS_EXCLUDED was previously defined
305#endif
306#if __has_attribute(locks_excluded)
307#define PROTOBUF_LOCKS_EXCLUDED(...) \
308 __attribute__((locks_excluded(__VA_ARGS__)))
309#else
310#define PROTOBUF_LOCKS_EXCLUDED(...)
311#endif
312
313#ifdef PROTOBUF_COLD
314#error PROTOBUF_COLD was previously defined
315#endif
316#if __has_attribute(cold) || PROTOBUF_GNUC_MIN(4, 3)
317# define PROTOBUF_COLD __attribute__((cold))
318#else
319# define PROTOBUF_COLD
320#endif
321
322#ifdef PROTOBUF_SECTION_VARIABLE
323#error PROTOBUF_SECTION_VARIABLE was previously defined
324#endif
325#if (__has_attribute(section) || defined(__GNUC__)) && defined(__ELF__)
326// Place a variable in the given ELF section.
327# define PROTOBUF_SECTION_VARIABLE(x) __attribute__((section(#x)))
328#else
329# define PROTOBUF_SECTION_VARIABLE(x)
330#endif
331
332#if defined(PROTOBUF_DEPRECATED)
333#error PROTOBUF_DEPRECATED was previously defined
334#endif
335#if defined(PROTOBUF_DEPRECATED_MSG)
336#error PROTOBUF_DEPRECATED_MSG was previously defined
337#endif
338#if __has_attribute(deprecated) || PROTOBUF_GNUC_MIN(3, 0)
339# define PROTOBUF_DEPRECATED __attribute__((deprecated))
340# define PROTOBUF_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
341#elif defined(_MSC_VER)
342# define PROTOBUF_DEPRECATED __declspec(deprecated)
343# define PROTOBUF_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
344#else
345# define PROTOBUF_DEPRECATED
346# define PROTOBUF_DEPRECATED_MSG(msg)
347#endif
348
349#if defined(PROTOBUF_DEPRECATED_ENUM)
350#error PROTOBUF_DEPRECATED_ENUM was previously defined
351#endif
352#if defined(__clang__) || PROTOBUF_GNUC_MIN(6, 0)
353// https://gcc.gnu.org/gcc-6/changes.html
354# define PROTOBUF_DEPRECATED_ENUM __attribute__((deprecated))
355#else
356# define PROTOBUF_DEPRECATED_ENUM
357#endif
358
359#ifdef PROTOBUF_FUNC_ALIGN
360#error PROTOBUF_FUNC_ALIGN was previously defined
361#endif
362#if __has_attribute(aligned) || PROTOBUF_GNUC_MIN(4, 3)
363#define PROTOBUF_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
364#else
365#define PROTOBUF_FUNC_ALIGN(bytes)
366#endif
367
368#ifdef PROTOBUF_RETURNS_NONNULL
369#error PROTOBUF_RETURNS_NONNULL was previously defined
370#endif
371#if __has_attribute(returns_nonnull) || PROTOBUF_GNUC_MIN(4, 9)
372#define PROTOBUF_RETURNS_NONNULL __attribute__((returns_nonnull))
373#else
374#define PROTOBUF_RETURNS_NONNULL
375#endif
376
377#ifdef PROTOBUF_ATTRIBUTE_REINITIALIZES
378#error PROTOBUF_ATTRIBUTE_REINITIALIZES was previously defined
379#endif
380#if __has_cpp_attribute(clang::reinitializes)
381#define PROTOBUF_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
382#else
383#define PROTOBUF_ATTRIBUTE_REINITIALIZES
384#endif
385
386// The minimum library version which works with the current version of the
387// headers.
388#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3021000
389
390#ifdef PROTOBUF_RTTI
391#error PROTOBUF_RTTI was previously defined
392#endif
393#if defined(GOOGLE_PROTOBUF_NO_RTTI) && GOOGLE_PROTOBUF_NO_RTTI
394// A user-provided definition GOOGLE_PROTOBUF_NO_RTTI=1 disables RTTI.
395#define PROTOBUF_RTTI 0
396#elif defined(__cpp_rtti)
397// https://en.cppreference.com/w/cpp/feature_test
398#define PROTOBUF_RTTI 1
399#elif __has_feature(cxx_rtti)
400// https://clang.llvm.org/docs/LanguageExtensions.html#c-rtti
401#define PROTOBUF_RTTI 1
402#elif defined(__GXX_RTTI)
403// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
404#define PROTOBUF_RTTI 1
405#elif defined(_CPPRTTI)
406// https://docs.microsoft.com/en-us/cpp/build/reference/gr-enable-run-time-type-information
407#define PROTOBUF_RTTI 1
408#else
409#define PROTOBUF_RTTI 0
410#endif
411
412// Returns the offset of the given field within the given aggregate type.
413// This is equivalent to the ANSI C offsetof() macro. However, according
414// to the C++ standard, offsetof() only works on POD types, and GCC
415// enforces this requirement with a warning. In practice, this rule is
416// unnecessarily strict; there is probably no compiler or platform on
417// which the offsets of the direct fields of a class are non-constant.
418// Fields inherited from superclasses *can* have non-constant offsets,
419// but that's not what this macro will be used for.
420#ifdef PROTOBUF_FIELD_OFFSET
421#error PROTOBUF_FIELD_OFFSET was previously defined
422#endif
423#if defined(__clang__)
424// For Clang we use __builtin_offsetof() and suppress the warning,
425// to avoid Control Flow Integrity and UBSan vptr sanitizers from
426// crashing while trying to validate the invalid reinterpret_casts.
427#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \
428 _Pragma("clang diagnostic push") \
429 _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \
430 __builtin_offsetof(TYPE, FIELD) \
431 _Pragma("clang diagnostic pop")
432#elif PROTOBUF_GNUC_MIN(4, 8)
433#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) __builtin_offsetof(TYPE, FIELD)
434#else // defined(__clang__)
435// Note that we calculate relative to the pointer value 16 here since if we
436// just use zero, GCC complains about dereferencing a NULL pointer. We
437// choose 16 rather than some other number just in case the compiler would
438// be confused by an unaligned pointer.
439#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \
440 static_cast< ::uint32_t>(reinterpret_cast<const char*>( \
441 &reinterpret_cast<const TYPE*>(16)->FIELD) - \
442 reinterpret_cast<const char*>(16))
443#endif
444
445#ifdef PROTOBUF_EXPORT
446#error PROTOBUF_EXPORT was previously defined
447#endif
448
449#if defined(PROTOBUF_USE_DLLS) && defined(_MSC_VER)
450# if defined(LIBPROTOBUF_EXPORTS)
451# define PROTOBUF_EXPORT __declspec(dllexport)
452# define PROTOBUF_EXPORT_TEMPLATE_DECLARE
453# define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllexport)
454# else
455# define PROTOBUF_EXPORT __declspec(dllimport)
456# define PROTOBUF_EXPORT_TEMPLATE_DECLARE
457# define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllimport)
458# endif // defined(LIBPROTOBUF_EXPORTS)
459#elif defined(PROTOBUF_USE_DLLS) && defined(LIBPROTOBUF_EXPORTS)
460# define PROTOBUF_EXPORT __attribute__((visibility("default")))
461# define PROTOBUF_EXPORT_TEMPLATE_DECLARE __attribute__((visibility("default")))
462# define PROTOBUF_EXPORT_TEMPLATE_DEFINE
463#else
464# define PROTOBUF_EXPORT
465# define PROTOBUF_EXPORT_TEMPLATE_DECLARE
466# define PROTOBUF_EXPORT_TEMPLATE_DEFINE
467#endif
468
469#ifdef PROTOC_EXPORT
470#error PROTOC_EXPORT was previously defined
471#endif
472
473#if defined(PROTOBUF_USE_DLLS) && defined(_MSC_VER)
474# if defined(LIBPROTOC_EXPORTS)
475# define PROTOC_EXPORT __declspec(dllexport)
476# else
477# define PROTOC_EXPORT __declspec(dllimport)
478# endif // defined(LIBPROTOC_EXPORTS)
479#elif defined(PROTOBUF_USE_DLLS) && defined(LIBPROTOC_EXPORTS)
480# define PROTOC_EXPORT __attribute__((visibility("default")))
481#else
482# define PROTOC_EXPORT
483#endif
484
485#if defined(PROTOBUF_PREDICT_TRUE) || defined(PROTOBUF_PREDICT_FALSE)
486#error PROTOBUF_PREDICT_(TRUE|FALSE) was previously defined
487#endif
488#if PROTOBUF_GNUC_MIN(3, 0)
489# define PROTOBUF_PREDICT_TRUE(x) (__builtin_expect(false || (x), true))
490# define PROTOBUF_PREDICT_FALSE(x) (__builtin_expect(false || (x), false))
491#else
492# define PROTOBUF_PREDICT_TRUE(x) (x)
493# define PROTOBUF_PREDICT_FALSE(x) (x)
494#endif
495
496#ifdef PROTOBUF_NODISCARD
497#error PROTOBUF_NODISCARD was previously defined
498#endif
499#if __has_cpp_attribute(nodiscard) && PROTOBUF_CPLUSPLUS_MIN(201703L)
500#define PROTOBUF_NODISCARD [[nodiscard]]
501#elif __has_attribute(warn_unused_result) || PROTOBUF_GNUC_MIN(4, 8)
502#define PROTOBUF_NODISCARD __attribute__((warn_unused_result))
503#else
504#define PROTOBUF_NODISCARD
505#endif
506
507// Enable all stable experiments if this flag is set. This allows us to group
508// all of these experiments under a single build flag, which can be enabled in
509// the protobuf.stable-experiments TAP project.
510#ifdef PROTOBUF_ENABLE_STABLE_EXPERIMENTS
511#define PROTOBUF_FORCE_MESSAGE_OWNED_ARENA
512#endif // !PROTOBUF_ENABLE_STABLE_EXPERIMENTS
513
514#ifdef PROTOBUF_FORCE_COPY_IN_RELEASE
515#error PROTOBUF_FORCE_COPY_IN_RELEASE was previously defined
516#endif
517
518#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
519#error PROTOBUF_FORCE_COPY_IN_SWAP was previously defined
520#endif
521
522#ifdef PROTOBUF_FORCE_COPY_IN_MOVE
523#error PROTOBUF_FORCE_COPY_IN_MOVE was previously defined
524#endif
525
526#ifdef PROTOBUF_FORCE_RESET_IN_CLEAR
527#error PROTOBUF_FORCE_RESET_IN_CLEAR was previously defined
528#endif
529
530// Force copy the default string to a string field so that non-optimized builds
531// have harder-to-rely-on address stability.
532#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
533#error PROTOBUF_FORCE_COPY_DEFAULT_STRING was previously defined
534#endif
535
536#ifdef PROTOBUF_FALLTHROUGH_INTENDED
537#error PROTOBUF_FALLTHROUGH_INTENDED was previously defined
538#endif
539#if __has_cpp_attribute(fallthrough)
540#define PROTOBUF_FALLTHROUGH_INTENDED [[fallthrough]]
541#elif __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
542#define PROTOBUF_FALLTHROUGH_INTENDED [[clang::fallthrough]]
543#elif PROTOBUF_GNUC_MIN(7, 0)
544#define PROTOBUF_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
545#else
546#define PROTOBUF_FALLTHROUGH_INTENDED
547#endif
548
549// PROTOBUF_ASSUME(pred) tells the compiler that it can assume pred is true. To
550// be safe, we also validate the assumption with a GOOGLE_DCHECK in unoptimized
551// builds. The macro does not do anything useful if the compiler does not
552// support __builtin_assume.
553#ifdef PROTOBUF_ASSUME
554#error PROTOBUF_ASSUME was previously defined
555#endif
556#if __has_builtin(__builtin_assume)
557#define PROTOBUF_ASSUME(pred) \
558 GOOGLE_DCHECK(pred); \
559 __builtin_assume(pred)
560#else
561#define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred)
562#endif
563
564// Specify memory alignment for structs, classes, etc.
565// Use like:
566// class PROTOBUF_ALIGNAS(16) MyClass { ... }
567// PROTOBUF_ALIGNAS(16) int array[4];
568//
569// In most places you can use the C++11 keyword "alignas", which is preferred.
570//
571// But compilers have trouble mixing __attribute__((...)) syntax with
572// alignas(...) syntax.
573//
574// Doesn't work in clang or gcc:
575// struct alignas(16) __attribute__((packed)) S { char c; };
576// Works in clang but not gcc:
577// struct __attribute__((packed)) alignas(16) S2 { char c; };
578// Works in clang and gcc:
579// struct alignas(16) S3 { char c; } __attribute__((packed));
580//
581// There are also some attributes that must be specified *before* a class
582// definition: visibility (used for exporting functions/classes) is one of
583// these attributes. This means that it is not possible to use alignas() with a
584// class that is marked as exported.
585#ifdef PROTOBUF_ALIGNAS
586#error PROTOBUF_ALIGNAS was previously defined
587#endif
588#if defined(_MSC_VER)
589#define PROTOBUF_ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
590#elif PROTOBUF_GNUC_MIN(3, 0)
591#define PROTOBUF_ALIGNAS(byte_alignment) \
592 __attribute__((aligned(byte_alignment)))
593#else
594#define PROTOBUF_ALIGNAS(byte_alignment) alignas(byte_alignment)
595#endif
596
597#ifdef PROTOBUF_FINAL
598#error PROTOBUF_FINAL was previously defined
599#endif
600#define PROTOBUF_FINAL final
601
602#ifdef PROTOBUF_THREAD_LOCAL
603#error PROTOBUF_THREAD_LOCAL was previously defined
604#endif
605#if defined(_MSC_VER)
606#define PROTOBUF_THREAD_LOCAL __declspec(thread)
607#else
608#define PROTOBUF_THREAD_LOCAL __thread
609#endif
610
611// TODO(b/228173843): cleanup PROTOBUF_LITTLE_ENDIAN in various 3p forks.
612#if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
613 __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
614#define PROTOBUF_LITTLE_ENDIAN 1
615#ifdef PROTOBUF_BIG_ENDIAN
616#error Conflicting PROTOBUF_BIG_ENDIAN was previously defined
617#endif
618#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
619 __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
620#define PROTOBUF_BIG_ENDIAN 1
621#elif defined(_WIN32) || defined(__x86_64__) || defined(__aarch64__)
622#define PROTOBUF_LITTLE_ENDIAN 1
623#else
624#error "endian detection failed for current compiler"
625#endif
626
627// For enabling message owned arena, one major blocker is semantic change from
628// moving to copying when there is ownership transfer (e.g., move ctor, swap,
629// set allocated, release). This change not only causes performance regression
630// but also breaks users code (e.g., dangling reference). For top-level
631// messages, since it owns the arena, we can mitigate the issue by transferring
632// ownership of arena. However, we cannot do that for nested messages. In order
633// to tell how many usages of nested messages affected by message owned arena,
634// we need to simulate the arena ownership.
635// This experiment is purely for the purpose of gathering data. All code guarded
636// by this flag is supposed to be removed after this experiment.
637#define PROTOBUF_MESSAGE_OWNED_ARENA_EXPERIMENT
638#ifdef PROTOBUF_CONSTINIT
639#error PROTOBUF_CONSTINIT was previously defined
640#endif
641#if defined(__cpp_constinit) && !defined(_MSC_VER)
642#define PROTOBUF_CONSTINIT constinit
643#define PROTOBUF_CONSTEXPR constexpr
644// Some older Clang versions incorrectly raise an error about
645// constant-initializing weak default instance pointers. Versions 12.0 and
646// higher seem to work, except that XCode 12.5.1 shows the error even though it
647// uses Clang 12.0.5.
648// Clang-cl on Windows raises error also.
649#elif !defined(_MSC_VER) && __has_cpp_attribute(clang::require_constant_initialization) && \
650 ((defined(__APPLE__) && __clang_major__ >= 13) || \
651 (!defined(__APPLE__) && __clang_major__ >= 12))
652#define PROTOBUF_CONSTINIT [[clang::require_constant_initialization]]
653#define PROTOBUF_CONSTEXPR constexpr
654#elif PROTOBUF_GNUC_MIN(12, 2)
655#define PROTOBUF_CONSTINIT __constinit
656#define PROTOBUF_CONSTEXPR constexpr
657// MSVC 17 currently seems to raise an error about constant-initialized pointers.
658#elif defined(_MSC_VER) && _MSC_VER >= 1930
659#define PROTOBUF_CONSTINIT
660#define PROTOBUF_CONSTEXPR constexpr
661#else
662#define PROTOBUF_CONSTINIT
663#define PROTOBUF_CONSTEXPR
664#endif
665
666// Some globals with an empty non-trivial destructor are annotated with
667// no_destroy for performance reasons. It reduces the cost of these globals in
668// non-opt mode and under sanitizers.
669#ifdef PROTOBUF_ATTRIBUTE_NO_DESTROY
670#error PROTOBUF_ATTRIBUTE_NO_DESTROY was previously defined
671#endif
672#if __has_cpp_attribute(clang::no_destroy)
673#define PROTOBUF_ATTRIBUTE_NO_DESTROY [[clang::no_destroy]]
674#else
675#define PROTOBUF_ATTRIBUTE_NO_DESTROY
676#endif
677
678// Force clang to always emit complete debug info for a type.
679// Clang uses constructor homing to determine when to emit debug info for a
680// type. If the constructor of a type is never used, which can happen in some
681// cases where member variables are constructed in place for optimization
682// purposes (see b/208803175 for an example), the type will have incomplete
683// debug info unless this attribute is used.
684#ifdef PROTOBUF_ATTRIBUTE_STANDALONE_DEBUG
685#error PROTOBUF_ATTRIBUTE_STANDALONE_DEBUG was previously defined
686#endif
687#if __has_cpp_attribute(clang::standalone_debug)
688#define PROTOBUF_ATTRIBUTE_STANDALONE_DEBUG [[clang::standalone_debug]]
689#else
690#define PROTOBUF_ATTRIBUTE_STANDALONE_DEBUG
691#endif
692
693// Protobuf extensions and reflection require registration of the protos linked
694// in the binary. Not until everything is registered does the runtime have a
695// complete view on all protos. When code is using reflection or extensions
696// in between registration calls this can lead to surprising behavior. By
697// having the registration run first we mitigate this scenario.
698// Highest priority is 101. We use 102 for registration, to allow code that
699// really wants to higher priority to still beat us. Some initialization happens
700// at higher priority, though, since it is needed before registration.
701#ifdef PROTOBUF_ATTRIBUTE_INIT_PRIORITY1
702#error PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 was previously defined
703#endif
704#ifdef PROTOBUF_ATTRIBUTE_INIT_PRIORITY2
705#error PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 was previously defined
706#endif
707#if PROTOBUF_GNUC_MIN(3, 0) && (!defined(__APPLE__) || defined(__clang__)) && \
708 !((defined(sun) || defined(__sun)) && \
709 (defined(__SVR4) || defined(__svr4__)))
710#define PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 __attribute__((init_priority((101))))
711#define PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 __attribute__((init_priority((102))))
712#else
713#define PROTOBUF_ATTRIBUTE_INIT_PRIORITY1
714#define PROTOBUF_ATTRIBUTE_INIT_PRIORITY2
715#endif
716
717#ifdef PROTOBUF_PRAGMA_INIT_SEG
718#error PROTOBUF_PRAGMA_INIT_SEG was previously defined
719#endif
720#ifdef _MSC_VER
721#define PROTOBUF_PRAGMA_INIT_SEG __pragma(init_seg(lib))
722#else
723#define PROTOBUF_PRAGMA_INIT_SEG
724#endif
725
726#ifdef PROTOBUF_ATTRIBUTE_WEAK
727#error PROTOBUF_ATTRIBUTE_WEAK was previously defined
728#endif
729#if __has_attribute(weak) && \
730 !defined(__APPLE__) && \
731 (!defined(_WIN32) || __clang_major__ < 9) && \
732 !defined(__MINGW32__)
733#define PROTOBUF_ATTRIBUTE_WEAK __attribute__((weak))
734#define PROTOBUF_HAVE_ATTRIBUTE_WEAK 1
735#else
736#define PROTOBUF_ATTRIBUTE_WEAK
737#define PROTOBUF_HAVE_ATTRIBUTE_WEAK 0
738#endif
739
740// Macros to detect sanitizers.
741#ifdef PROTOBUF_ASAN
742#error PROTOBUF_ASAN was previously defined
743#endif
744#ifdef PROTOBUF_MSAN
745#error PROTOBUF_MSAN was previously defined
746#endif
747#ifdef PROTOBUF_TSAN
748#error PROTOBUF_TSAN was previously defined
749#endif
750#if defined(__clang__)
751# if __has_feature(address_sanitizer)
752# define PROTOBUF_ASAN 1
753# endif
754# if __has_feature(thread_sanitizer)
755# define PROTOBUF_TSAN 1
756# endif
757# if __has_feature(memory_sanitizer)
758# define PROTOBUF_MSAN 1
759# endif
760#elif PROTOBUF_GNUC_MIN(3, 0)
761// Double-guard is needed for -Wundef:
762# ifdef __SANITIZE_ADDRESS__
763# if __SANITIZE_ADDRESS__
764# define PROTOBUF_ASAN 1
765# endif
766# endif
767# ifdef __SANITIZE_THREAD__
768# if __SANITIZE_THREAD__
769# define PROTOBUF_TSAN 1
770# endif
771# endif
772#endif
773
774// Tail call table-driven parsing can be enabled by defining
775// PROTOBUF_EXPERIMENTAL_USE_TAIL_CALL_TABLE_PARSER at compilation time. Note
776// that this macro is for small-scale testing only, and is not supported.
777#ifdef PROTOBUF_TAIL_CALL_TABLE_PARSER_ENABLED
778#error PROTOBUF_TAIL_CALL_TABLE_PARSER_ENABLED was previously declared
779#endif
780#if defined(PROTOBUF_EXPERIMENTAL_USE_TAIL_CALL_TABLE_PARSER)
781#define PROTOBUF_TAIL_CALL_TABLE_PARSER_ENABLED 1
782#endif
783
784#define PROTOBUF_TC_PARAM_DECL \
785 ::google::protobuf::MessageLite *msg, const char *ptr, \
786 ::google::protobuf::internal::ParseContext *ctx, \
787 const ::google::protobuf::internal::TcParseTableBase *table, \
788 uint64_t hasbits, ::google::protobuf::internal::TcFieldData data
789
790#ifdef PROTOBUF_UNUSED
791#error PROTOBUF_UNUSED was previously defined
792#endif
793#if __has_cpp_attribute(maybe_unused) || \
794 (PROTOBUF_MSC_VER_MIN(1911) && PROTOBUF_CPLUSPLUS_MIN(201703L))
795#define PROTOBUF_UNUSED [[maybe_unused]]
796#elif __has_attribute(unused) || PROTOBUF_GNUC_MIN(3, 0)
797#define PROTOBUF_UNUSED __attribute__((__unused__))
798#else
799#define PROTOBUF_UNUSED
800#endif
801
802// ThreadSafeArenaz is turned off completely in opensource builds.
803
804// Windows declares several inconvenient macro names. We #undef them and then
805// restore them in port_undef.inc.
806#ifdef _MSC_VER
807#pragma push_macro("CREATE_NEW")
808#undef CREATE_NEW
809#pragma push_macro("DELETE")
810#undef DELETE
811#pragma push_macro("DOUBLE_CLICK")
812#undef DOUBLE_CLICK
813#pragma push_macro("ERROR")
814#undef ERROR
815#pragma push_macro("ERROR_BUSY")
816#undef ERROR_BUSY
817#pragma push_macro("ERROR_INSTALL_FAILED")
818#undef ERROR_INSTALL_FAILED
819#pragma push_macro("ERROR_NOT_FOUND")
820#undef ERROR_NOT_FOUND
821#pragma push_macro("GetClassName")
822#undef GetClassName
823#pragma push_macro("GetMessage")
824#undef GetMessage
825#pragma push_macro("GetObject")
826#undef GetObject
827#pragma push_macro("IGNORE")
828#undef IGNORE
829#pragma push_macro("IN")
830#undef IN
831#pragma push_macro("INPUT_KEYBOARD")
832#undef INPUT_KEYBOARD
833#pragma push_macro("NO_ERROR")
834#undef NO_ERROR
835#pragma push_macro("OUT")
836#undef OUT
837#pragma push_macro("OPTIONAL")
838#undef OPTIONAL
839#pragma push_macro("min")
840#undef min
841#pragma push_macro("max")
842#undef max
843#pragma push_macro("NEAR")
844#undef NEAR
845#pragma push_macro("NO_DATA")
846#undef NO_DATA
847#pragma push_macro("REASON_UNKNOWN")
848#undef REASON_UNKNOWN
849#pragma push_macro("SERVICE_DISABLED")
850#undef SERVICE_DISABLED
851#pragma push_macro("SEVERITY_ERROR")
852#undef SEVERITY_ERROR
853#pragma push_macro("STATUS_PENDING")
854#undef STATUS_PENDING
855#pragma push_macro("STRICT")
856#undef STRICT
857#pragma push_macro("timezone")
858#undef timezone
859#endif // _MSC_VER
860
861#ifdef __APPLE__
862// Inconvenient macro names from usr/include/math.h in some macOS SDKs.
863#pragma push_macro("DOMAIN")
864#undef DOMAIN
865// Inconvenient macro names from /usr/include/mach/boolean.h in some macOS SDKs.
866#pragma push_macro("TRUE")
867#undef TRUE
868#pragma push_macro("FALSE")
869#undef FALSE
870// Inconvenient macro names from usr/include/sys/syslimits.h in some macOS SDKs.
871#pragma push_macro("UID_MAX")
872#undef UID_MAX
873#endif // __APPLE__
874
875#if defined(__clang__) || PROTOBUF_GNUC_MIN(3, 0) || defined(_MSC_VER)
876// Don't let Objective-C Macros interfere with proto identifiers with the same
877// name.
878#pragma push_macro("DEBUG")
879#undef DEBUG
880#endif // defined(__clang__) || PROTOBUF_GNUC_MIN(3, 0) || defined(_MSC_VER)
881
882#if PROTOBUF_GNUC_MIN(3, 0)
883// GCC does not allow disabling diagnostics within an expression:
884// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60875, so we disable this one
885// globally even though it's only used for PROTOBUF_FIELD_OFFSET.
886#pragma GCC diagnostic push
887#pragma GCC diagnostic ignored "-Winvalid-offsetof"
888#endif
889
890// Silence some MSVC warnings in all our code.
891#ifdef _MSC_VER
892#pragma warning(push)
893// For non-trivial unions
894#pragma warning(disable : 4582)
895#pragma warning(disable : 4583)
896// For init_seg(lib)
897#pragma warning(disable : 4073)
898// To silence the fact that we will pop this push from another file
899#pragma warning(disable : 5031)
900// Conditional expression is constant
901#pragma warning(disable: 4127)
902// decimal digit terminates octal escape sequence
903#pragma warning(disable: 4125)
904#endif
905
906// We don't want code outside port_def doing complex testing, so
907// remove our portable condition test macros to nudge folks away from
908// using it themselves.
909#ifdef PROTOBUF_has_cpp_attribute_DEFINED_
910# undef __has_cpp_attribute
911# undef PROTOBUF_has_cpp_attribute_DEFINED_
912#endif
913#ifdef PROTOBUF_has_feature_DEFINED_
914# undef __has_feature
915# undef PROTOBUF_has_feature_DEFINED_
916#endif
917#ifdef PROTOBUF_has_warning_DEFINED_
918# undef __has_warning
919# undef PROTOBUF_has_warning_DEFINED_
920#endif
921#ifdef PROTOBUF_has_attribute_DEFINED_
922# undef __has_attribute
923# undef PROTOBUF_has_attribute_DEFINED_
924#endif
925#ifdef PROTOBUF_has_builtin_DEFINED_
926# undef __has_builtin
927# undef PROTOBUF_has_builtin_DEFINED_
928#endif
929