• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#ifdef PROTOBUF_FINAL
140#error PROTOBUF_FINAL was previously defined
141#endif
142
143
144#define PROTOBUF_NAMESPACE "google::protobuf"
145#define PROTOBUF_NAMESPACE_ID google::protobuf
146#define PROTOBUF_NAMESPACE_OPEN \
147  namespace google {            \
148  namespace protobuf {
149#define PROTOBUF_NAMESPACE_CLOSE \
150  } /* namespace protobuf */     \
151  } /* namespace google */
152
153#if defined(__GNUC__) || defined(__clang__)
154#define PROTOBUF_DEPRECATED __attribute__((deprecated))
155#define PROTOBUF_DEPRECATED_ENUM __attribute__((deprecated))
156#define PROTOBUF_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
157#elif defined(_MSC_VER)
158#define PROTOBUF_DEPRECATED __declspec(deprecated)
159#define PROTOBUF_DEPRECATED_ENUM
160#define PROTOBUF_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
161#endif
162
163#define PROTOBUF_SECTION_VARIABLE(x)
164#define PROTOBUF_MUST_USE_RESULT
165
166// ----------------------------------------------------------------------------
167// Annotations:  Some parts of the code have been annotated in ways that might
168//   be useful to some compilers or tools, but are not supported universally.
169//   You can #define these annotations yourself if the default implementation
170//   is not right for you.
171
172#ifdef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
173#define PROTOBUF_ALWAYS_INLINE GOOGLE_ATTRIBUTE_ALWAYS_INLINE
174#else
175#if defined(__GNUC__) && \
176    (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
177// For functions we want to force inline.
178// Introduced in gcc 3.1.
179#define PROTOBUF_ALWAYS_INLINE __attribute__((always_inline))
180#else
181// Other compilers will have to figure it out for themselves.
182#define PROTOBUF_ALWAYS_INLINE
183#endif
184#endif
185
186#ifdef GOOGLE_ATTRIBUTE_NOINLINE
187#define PROTOBUF_NOINLINE GOOGLE_ATTRIBUTE_NOINLINE
188#else
189#if defined(__GNUC__) && \
190    (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
191// For functions we want to force not inline.
192// Introduced in gcc 3.1.
193#define PROTOBUF_NOINLINE __attribute__((noinline))
194#elif defined(_MSC_VER) && (_MSC_VER >= 1400)
195// Seems to have been around since at least Visual Studio 2005
196#define PROTOBUF_NOINLINE __declspec(noinline)
197#else
198// Other compilers will have to figure it out for themselves.
199#define PROTOBUF_NOINLINE
200#endif
201#endif
202
203#ifdef GOOGLE_ATTRIBUTE_FUNC_ALIGN
204#define PROTOBUF_FUNC_ALIGN GOOGLE_ATTRIBUTE_FUNC_ALIGN
205#else
206#if defined(__clang__) || \
207    defined(__GNUC__) &&  \
208        (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
209// Function alignment attribute introduced in gcc 4.3
210#define PROTOBUF_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
211#else
212#define PROTOBUF_FUNC_ALIGN(bytes)
213#endif
214#endif
215
216#ifdef GOOGLE_PREDICT_TRUE
217#define PROTOBUF_PREDICT_TRUE GOOGLE_PREDICT_TRUE
218#else
219#ifdef __GNUC__
220// Provided at least since GCC 3.0.
221#define PROTOBUF_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
222#else
223#define PROTOBUF_PREDICT_TRUE(x) (x)
224#endif
225#endif
226
227#ifdef GOOGLE_PREDICT_FALSE
228#define PROTOBUF_PREDICT_FALSE GOOGLE_PREDICT_FALSE
229#else
230#ifdef __GNUC__
231// Provided at least since GCC 3.0.
232#define PROTOBUF_PREDICT_FALSE(x) (__builtin_expect(x, 0))
233#else
234#define PROTOBUF_PREDICT_FALSE(x) (x)
235#endif
236#endif
237
238#ifdef GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL
239#define PROTOBUF_RETURNS_NONNULL GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL
240#else
241#ifdef __GNUC__
242#define PROTOBUF_RETURNS_NONNULL __attribute__((returns_nonnull))
243#else
244#define PROTOBUF_RETURNS_NONNULL
245#endif
246#endif
247
248#if defined(__has_cpp_attribute)
249#if __has_cpp_attribute(clang::reinitializes)
250#define PROTOBUF_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
251#endif
252#endif
253#ifndef PROTOBUF_ATTRIBUTE_REINITIALIZES
254#define PROTOBUF_ATTRIBUTE_REINITIALIZES
255#endif
256
257#define PROTOBUF_GUARDED_BY(x)
258#define PROTOBUF_COLD
259
260// Copied from ABSL.
261#if defined(__clang__) && defined(__has_warning)
262#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
263#define PROTOBUF_FALLTHROUGH_INTENDED [[clang::fallthrough]]
264#endif
265#elif defined(__GNUC__) && __GNUC__ >= 7
266#define PROTOBUF_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
267#endif
268
269#ifndef PROTOBUF_FALLTHROUGH_INTENDED
270#define PROTOBUF_FALLTHROUGH_INTENDED
271#endif
272
273#if defined(__has_cpp_attribute)
274#define HAS_ATTRIBUTE(attr) __has_cpp_attribute(attr)
275#else
276#define HAS_ATTRIBUTE(attr) 0
277#endif
278
279#if HAS_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
280#define PROTOBUF_UNUSED __attribute__((__unused__))
281#else
282#define PROTOBUF_UNUSED
283#endif
284
285#undef HAS_ATTRIBUTE
286
287#ifdef _MSC_VER
288#define PROTOBUF_LONGLONG(x) x##I64
289#define PROTOBUF_ULONGLONG(x) x##UI64
290#define PROTOBUF_LL_FORMAT "I64"  // As in printf("%I64d", ...)
291#else
292// By long long, we actually mean int64.
293#define PROTOBUF_LONGLONG(x) x##LL
294#define PROTOBUF_ULONGLONG(x) x##ULL
295// Used to format real long long integers.
296#define PROTOBUF_LL_FORMAT \
297  "ll"  // As in "%lld". Note that "q" is poor form also.
298#endif
299
300
301// Shared google3/opensource definitions. //////////////////////////////////////
302
303#define PROTOBUF_VERSION 3013000
304#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 3013000
305#define PROTOBUF_MIN_PROTOC_VERSION 3013000
306#define PROTOBUF_VERSION_SUFFIX ""
307
308// The minimum library version which works with the current version of the
309// headers.
310#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3013000
311
312#if defined(GOOGLE_PROTOBUF_NO_RTTI) && GOOGLE_PROTOBUF_NO_RTTI
313#define PROTOBUF_RTTI 0
314#elif defined(__has_feature)
315// https://clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
316#define PROTOBUF_RTTI __has_feature(cxx_rtti)
317#elif !defined(__cxx_rtti)
318// https://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros#C.2B.2B98
319#define PROTOBUF_RTTI 0
320#elif defined(__GNUC__) && !defined(__GXX_RTTI)
321#https:  // gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
322#define PROTOBUF_RTTI 0
323#else
324#define PROTOBUF_RTTI 1
325#endif
326
327// Returns the offset of the given field within the given aggregate type.
328// This is equivalent to the ANSI C offsetof() macro.  However, according
329// to the C++ standard, offsetof() only works on POD types, and GCC
330// enforces this requirement with a warning.  In practice, this rule is
331// unnecessarily strict; there is probably no compiler or platform on
332// which the offsets of the direct fields of a class are non-constant.
333// Fields inherited from superclasses *can* have non-constant offsets,
334// but that's not what this macro will be used for.
335#if defined(__clang__)
336// For Clang we use __builtin_offsetof() and suppress the warning,
337// to avoid Control Flow Integrity and UBSan vptr sanitizers from
338// crashing while trying to validate the invalid reinterpet_casts.
339#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD)                   \
340  _Pragma("clang diagnostic push")                           \
341  _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \
342  __builtin_offsetof(TYPE, FIELD)                            \
343  _Pragma("clang diagnostic pop")
344#elif defined(__GNUC__) && \
345          (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
346#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) __builtin_offsetof(TYPE, FIELD)
347#else  // defined(__clang__)
348// Note that we calculate relative to the pointer value 16 here since if we
349// just use zero, GCC complains about dereferencing a NULL pointer.  We
350// choose 16 rather than some other number just in case the compiler would
351// be confused by an unaligned pointer.
352#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD)                                \
353  static_cast< ::google::protobuf::uint32>(reinterpret_cast<const char*>(                   \
354                             &reinterpret_cast<const TYPE*>(16)->FIELD) - \
355                         reinterpret_cast<const char*>(16))
356#endif
357
358#if defined(PROTOBUF_USE_DLLS)
359#if defined(_MSC_VER)
360#ifdef LIBPROTOBUF_EXPORTS
361#define PROTOBUF_EXPORT __declspec(dllexport)
362#define PROTOBUF_EXPORT_TEMPLATE_DECLARE
363#define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllexport)
364#else
365#define PROTOBUF_EXPORT __declspec(dllimport)
366#define PROTOBUF_EXPORT_TEMPLATE_DECLARE
367#define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllimport)
368#endif
369#ifdef LIBPROTOC_EXPORTS
370#define PROTOC_EXPORT __declspec(dllexport)
371#else
372#define PROTOC_EXPORT __declspec(dllimport)
373#endif
374#else  // defined(_MSC_VER)
375#ifdef LIBPROTOBUF_EXPORTS
376#define PROTOBUF_EXPORT __attribute__((visibility("default")))
377#define PROTOBUF_EXPORT_TEMPLATE_DECLARE __attribute__((visibility("default")))
378#define PROTOBUF_EXPORT_TEMPLATE_DEFINE
379#else
380#define PROTOBUF_EXPORT
381#define PROTOBUF_EXPORT_TEMPLATE_DECLARE
382#define PROTOBUF_EXPORT_TEMPLATE_DEFINE
383#endif
384#ifdef LIBPROTOC_EXPORTS
385#define PROTOC_EXPORT __attribute__((visibility("default")))
386#else
387#define PROTOC_EXPORT
388#endif
389#endif
390#else  // defined(PROTOBUF_USE_DLLS)
391#define PROTOBUF_EXPORT
392#define PROTOC_EXPORT
393#define PROTOBUF_EXPORT_TEMPLATE_DECLARE
394#define PROTOBUF_EXPORT_TEMPLATE_DEFINE
395#endif
396
397// Windows declares several inconvenient macro names.  We #undef them and then
398// restore them in port_undef.inc.
399#ifdef _MSC_VER
400#pragma push_macro("CREATE_NEW")
401#undef CREATE_NEW
402#pragma push_macro("DOUBLE_CLICK")
403#undef DOUBLE_CLICK
404#pragma push_macro("ERROR")
405#undef ERROR
406#pragma push_macro("ERROR_BUSY")
407#undef ERROR_BUSY
408#pragma push_macro("ERROR_NOT_FOUND")
409#undef ERROR_NOT_FOUND
410#pragma push_macro("GetMessage")
411#undef GetMessage
412#pragma push_macro("IGNORE")
413#undef IGNORE
414#pragma push_macro("IN")
415#undef IN
416#pragma push_macro("INPUT_KEYBOARD")
417#undef INPUT_KEYBOARD
418#pragma push_macro("NO_ERROR")
419#undef NO_ERROR
420#pragma push_macro("OUT")
421#undef OUT
422#pragma push_macro("OPTIONAL")
423#undef OPTIONAL
424#pragma push_macro("min")
425#undef min
426#pragma push_macro("max")
427#undef max
428#pragma push_macro("REASON_UNKNOWN")
429#undef REASON_UNKNOWN
430#pragma push_macro("SERVICE_DISABLED")
431#undef SERVICE_DISABLED
432#pragma push_macro("SEVERITY_ERROR")
433#undef SEVERITY_ERROR
434#pragma push_macro("STRICT")
435#undef STRICT
436#endif  // _MSC_VER
437
438#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
439// Don't let Objective-C Macros interfere with proto identifiers with the same
440// name.
441#pragma push_macro("YES")
442#undef YES
443#pragma push_macro("NO")
444#undef NO
445#pragma push_macro("DEBUG")
446#undef DEBUG
447#endif // defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
448
449#if defined(__clang__)
450#pragma clang diagnostic push
451// TODO(gerbens) ideally we cleanup the code. But a cursory try shows many
452// violations. So let's ignore for now.
453#pragma clang diagnostic ignored "-Wshorten-64-to-32"
454#elif defined(__GNUC__)
455// GCC does not allow disabling diagnostics within an expression:
456// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60875, so we disable this one
457// globally even though it's only used for PROTOBUF_FIELD_OFFSET.
458#pragma GCC diagnostic push
459#pragma GCC diagnostic ignored "-Winvalid-offsetof"
460#endif
461
462// PROTOBUF_ASSUME(pred) tells the compiler that it can assume pred is true. To
463// be safe, we also validate the assumption with a GOOGLE_DCHECK in unoptimized
464// builds. The macro does not do anything useful if the compiler does not
465// support __builtin_assume.
466#ifdef __has_builtin
467#if __has_builtin(__builtin_assume)
468#define PROTOBUF_ASSUME(pred) \
469  GOOGLE_DCHECK(pred);               \
470  __builtin_assume(pred)
471#else
472#define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred)
473#endif
474#else
475#define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred)
476#endif
477
478// Specify memory alignment for structs, classes, etc.
479// Use like:
480//   class PROTOBUF_ALIGNAS(16) MyClass { ... }
481//   PROTOBUF_ALIGNAS(16) int array[4];
482//
483// In most places you can use the C++11 keyword "alignas", which is preferred.
484//
485// But compilers have trouble mixing __attribute__((...)) syntax with
486// alignas(...) syntax.
487//
488// Doesn't work in clang or gcc:
489//   struct alignas(16) __attribute__((packed)) S { char c; };
490// Works in clang but not gcc:
491//   struct __attribute__((packed)) alignas(16) S2 { char c; };
492// Works in clang and gcc:
493//   struct alignas(16) S3 { char c; } __attribute__((packed));
494//
495// There are also some attributes that must be specified *before* a class
496// definition: visibility (used for exporting functions/classes) is one of
497// these attributes. This means that it is not possible to use alignas() with a
498// class that is marked as exported.
499#if defined(_MSC_VER)
500#define PROTOBUF_ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
501#elif defined(__GNUC__)
502#define PROTOBUF_ALIGNAS(byte_alignment) \
503  __attribute__((aligned(byte_alignment)))
504#else
505#define PROTOBUF_ALIGNAS(byte_alignment) alignas(byte_alignment)
506#endif
507
508#define PROTOBUF_FINAL final
509
510#if defined(_MSC_VER)
511#define PROTOBUF_THREAD_LOCAL __declspec(thread)
512#else
513#define PROTOBUF_THREAD_LOCAL __thread
514#endif
515
516// For enabling message owned arena, one major blocker is semantic change from
517// moving to copying when there is ownership transfer (e.g., move ctor, swap,
518// set allocated, release). This change not only causes performance regression
519// but also breaks users code (e.g., dangling reference). For top-level
520// messages, since it owns the arena, we can mitigate the issue by transferring
521// ownership of arena. However, we cannot do that for nested messages. In order
522// to tell how many usages of nested messages affected by message owned arena,
523// we need to simulate the arena ownership.
524// This experiment is purely for the purpose of gathering data. All code guarded
525// by this flag is supposed to be removed after this experiment.
526// #define PROTOBUF_MESSAGE_OWNED_ARENA_EXPERIMENT
527