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
150#if defined(__GNUC__) || defined(__clang__)
151#define PROTOBUF_DEPRECATED __attribute__((deprecated))
152#define PROTOBUF_DEPRECATED_ENUM __attribute__((deprecated))
153#define PROTOBUF_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
154#elif defined(_MSC_VER)
155#define PROTOBUF_DEPRECATED __declspec(deprecated)
156#define PROTOBUF_DEPRECATED_ENUM
157#define PROTOBUF_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
158#endif
159
160#define PROTOBUF_SECTION_VARIABLE(x)
161#define PROTOBUF_MUST_USE_RESULT
162
163// ----------------------------------------------------------------------------
164// Annotations: Some parts of the code have been annotated in ways that might
165// be useful to some compilers or tools, but are not supported universally.
166// You can #define these annotations yourself if the default implementation
167// is not right for you.
168
169#ifdef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
170#define PROTOBUF_ALWAYS_INLINE GOOGLE_ATTRIBUTE_ALWAYS_INLINE
171#else
172#if defined(__GNUC__) && \
173 (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
174// For functions we want to force inline.
175// Introduced in gcc 3.1.
176#define PROTOBUF_ALWAYS_INLINE __attribute__((always_inline))
177#else
178// Other compilers will have to figure it out for themselves.
179#define PROTOBUF_ALWAYS_INLINE
180#endif
181#endif
182
183#ifdef GOOGLE_ATTRIBUTE_NOINLINE
184#define PROTOBUF_NOINLINE GOOGLE_ATTRIBUTE_NOINLINE
185#else
186#if defined(__GNUC__) && \
187 (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
188// For functions we want to force not inline.
189// Introduced in gcc 3.1.
190#define PROTOBUF_NOINLINE __attribute__((noinline))
191#elif defined(_MSC_VER) && (_MSC_VER >= 1400)
192// Seems to have been around since at least Visual Studio 2005
193#define PROTOBUF_NOINLINE __declspec(noinline)
194#else
195// Other compilers will have to figure it out for themselves.
196#define PROTOBUF_NOINLINE
197#endif
198#endif
199
200#ifdef GOOGLE_ATTRIBUTE_FUNC_ALIGN
201#define PROTOBUF_FUNC_ALIGN GOOGLE_ATTRIBUTE_FUNC_ALIGN
202#else
203#if defined(__clang__) || \
204 defined(__GNUC__) && \
205 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
206// Function alignment attribute introduced in gcc 4.3
207#define PROTOBUF_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
208#else
209#define PROTOBUF_FUNC_ALIGN(bytes)
210#endif
211#endif
212
213#ifdef GOOGLE_PREDICT_TRUE
214#define PROTOBUF_PREDICT_TRUE GOOGLE_PREDICT_TRUE
215#else
216#ifdef __GNUC__
217// Provided at least since GCC 3.0.
218#define PROTOBUF_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
219#else
220#define PROTOBUF_PREDICT_TRUE(x) (x)
221#endif
222#endif
223
224#ifdef GOOGLE_PREDICT_FALSE
225#define PROTOBUF_PREDICT_FALSE GOOGLE_PREDICT_FALSE
226#else
227#ifdef __GNUC__
228// Provided at least since GCC 3.0.
229#define PROTOBUF_PREDICT_FALSE(x) (__builtin_expect(x, 0))
230#else
231#define PROTOBUF_PREDICT_FALSE(x) (x)
232#endif
233#endif
234
235#ifdef GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL
236#define PROTOBUF_RETURNS_NONNULL GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL
237#else
238#ifdef __GNUC__
239#define PROTOBUF_RETURNS_NONNULL __attribute__((returns_nonnull))
240#else
241#define PROTOBUF_RETURNS_NONNULL
242#endif
243#endif
244
245#if defined(__has_cpp_attribute)
246#if __has_cpp_attribute(clang::reinitializes)
247#define PROTOBUF_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
248#endif
249#endif
250#ifndef PROTOBUF_ATTRIBUTE_REINITIALIZES
251#define PROTOBUF_ATTRIBUTE_REINITIALIZES
252#endif
253
254#define PROTOBUF_GUARDED_BY(x)
255#define PROTOBUF_COLD
256
257// Copied from ABSL.
258#if defined(__clang__) && defined(__has_warning)
259#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
260#define PROTOBUF_FALLTHROUGH_INTENDED [[clang::fallthrough]]
261#endif
262#elif defined(__GNUC__) && __GNUC__ >= 7
263#define PROTOBUF_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
264#endif
265
266#ifndef PROTOBUF_FALLTHROUGH_INTENDED
267#define PROTOBUF_FALLTHROUGH_INTENDED
268#endif
269
270#if defined(__has_cpp_attribute)
271#define HAS_ATTRIBUTE(attr) __has_cpp_attribute(attr)
272#else
273#define HAS_ATTRIBUTE(attr) 0
274#endif
275
276#if HAS_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
277#define PROTOBUF_UNUSED __attribute__((__unused__))
278#else
279#define PROTOBUF_UNUSED
280#endif
281
282#undef HAS_ATTRIBUTE
283
284#ifdef _MSC_VER
285#define PROTOBUF_LONGLONG(x) x##I64
286#define PROTOBUF_ULONGLONG(x) x##UI64
287#define PROTOBUF_LL_FORMAT "I64" // As in printf("%I64d", ...)
288#else
289// By long long, we actually mean int64.
290#define PROTOBUF_LONGLONG(x) x##LL
291#define PROTOBUF_ULONGLONG(x) x##ULL
292// Used to format real long long integers.
293#define PROTOBUF_LL_FORMAT \
294 "ll" // As in "%lld". Note that "q" is poor form also.
295#endif
296
297
298// Shared google3/opensource definitions. //////////////////////////////////////
299
300#define PROTOBUF_VERSION 3011004
301#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 3011000
302#define PROTOBUF_MIN_PROTOC_VERSION 3011000
303#define PROTOBUF_VERSION_SUFFIX ""
304
305// The minimum library version which works with the current version of the
306// headers.
307#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3011000
308
309#if defined(GOOGLE_PROTOBUF_NO_RTTI) && GOOGLE_PROTOBUF_NO_RTTI
310#define PROTOBUF_RTTI 0
311#else
312#define PROTOBUF_RTTI 1
313#endif
314
315// Returns the offset of the given field within the given aggregate type.
316// This is equivalent to the ANSI C offsetof() macro. However, according
317// to the C++ standard, offsetof() only works on POD types, and GCC
318// enforces this requirement with a warning. In practice, this rule is
319// unnecessarily strict; there is probably no compiler or platform on
320// which the offsets of the direct fields of a class are non-constant.
321// Fields inherited from superclasses *can* have non-constant offsets,
322// but that's not what this macro will be used for.
323#if defined(__clang__)
324// For Clang we use __builtin_offsetof() and suppress the warning,
325// to avoid Control Flow Integrity and UBSan vptr sanitizers from
326// crashing while trying to validate the invalid reinterpet_casts.
327#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \
328 _Pragma("clang diagnostic push") \
329 _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \
330 __builtin_offsetof(TYPE, FIELD) \
331 _Pragma("clang diagnostic pop")
332#else
333// Note that we calculate relative to the pointer value 16 here since if we
334// just use zero, GCC complains about dereferencing a NULL pointer. We
335// choose 16 rather than some other number just in case the compiler would
336// be confused by an unaligned pointer.
337#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \
338 static_cast< ::google::protobuf::uint32>(reinterpret_cast<const char*>( \
339 &reinterpret_cast<const TYPE*>(16)->FIELD) - \
340 reinterpret_cast<const char*>(16))
341#endif
342
343#if defined(PROTOBUF_USE_DLLS)
344 #if defined(_MSC_VER)
345 #ifdef LIBPROTOBUF_EXPORTS
346 #define PROTOBUF_EXPORT __declspec(dllexport)
347 #define PROTOBUF_EXPORT_TEMPLATE_DECLARE
348 #define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllexport)
349 #else
350 #define PROTOBUF_EXPORT __declspec(dllimport)
351 #define PROTOBUF_EXPORT_TEMPLATE_DECLARE
352 #define PROTOBUF_EXPORT_TEMPLATE_DEFINE __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 // defined(_MSC_VER)
360 #ifdef LIBPROTOBUF_EXPORTS
361 #define PROTOBUF_EXPORT __attribute__((visibility("default")))
362 #define PROTOBUF_EXPORT_TEMPLATE_DECLARE __attribute__((visibility("default")))
363 #define PROTOBUF_EXPORT_TEMPLATE_DEFINE
364 #else
365 #define PROTOBUF_EXPORT
366 #define PROTOBUF_EXPORT_TEMPLATE_DECLARE
367 #define PROTOBUF_EXPORT_TEMPLATE_DEFINE
368 #endif
369 #ifdef LIBPROTOC_EXPORTS
370 #define PROTOC_EXPORT __attribute__((visibility("default")))
371 #else
372 #define PROTOC_EXPORT
373 #endif
374 #endif
375#else // defined(PROTOBUF_USE_DLLS)
376 #define PROTOBUF_EXPORT
377 #define PROTOC_EXPORT
378 #define PROTOBUF_EXPORT_TEMPLATE_DECLARE
379 #define PROTOBUF_EXPORT_TEMPLATE_DEFINE
380#endif
381
382// Windows declares several inconvenient macro names. We #undef them and then
383// restore them in port_undef.inc.
384#ifdef _MSC_VER
385#pragma push_macro("GetMessage")
386#undef GetMessage
387#pragma push_macro("IGNORE")
388#undef IGNORE
389#pragma push_macro("IN")
390#undef IN
391#pragma push_macro("OUT")
392#undef OUT
393#pragma push_macro("OPTIONAL")
394#undef OPTIONAL
395#pragma push_macro("min")
396#undef min
397#pragma push_macro("max")
398#undef max
399#endif // _MSC_VER
400
401#if defined(__clang__)
402#pragma clang diagnostic push
403// TODO(gerbens) ideally we cleanup the code. But a cursory try shows many
404// violations. So let's ignore for now.
405#pragma clang diagnostic ignored "-Wshorten-64-to-32"
406#endif
407
408// PROTOBUF_ASSUME(pred) tells the compiler that it can assume pred is true. To
409// be safe, we also validate the assumption with a GOOGLE_DCHECK in unoptimized
410// builds. The macro does not do anything useful if the compiler does not
411// support __builtin_assume.
412#ifdef __has_builtin
413#if __has_builtin(__builtin_assume)
414#define PROTOBUF_ASSUME(pred) \
415 GOOGLE_DCHECK(pred); \
416 __builtin_assume(pred)
417#else
418#define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred)
419#endif
420#else
421#define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred)
422#endif
423