blob: e8500224443bce5bf65ba5ce1fcd00d8c7044c8f [file] [log] [blame]
mistergc2e75482017-09-19 16:54:40 -04001// Copyright 2017 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
Abseil Team53c239d2017-09-19 17:15:26 -070015// This header file defines macros for declaring attributes for functions,
16// types, and variables.
17//
18// These macros are used within Abseil and allow the compiler to optimize, where
19// applicable, certain function calls.
20//
mistergc2e75482017-09-19 16:54:40 -040021// This file is used for both C and C++!
22//
23// Most macros here are exposing GCC or Clang features, and are stubbed out for
24// other compilers.
Abseil Team53c239d2017-09-19 17:15:26 -070025//
mistergc2e75482017-09-19 16:54:40 -040026// GCC attributes documentation:
Abseil Team53c239d2017-09-19 17:15:26 -070027// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html
28// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Variable-Attributes.html
29// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Type-Attributes.html
mistergc2e75482017-09-19 16:54:40 -040030//
Abseil Team53c239d2017-09-19 17:15:26 -070031// Most attributes in this file are already supported by GCC 4.7. However, some
32// of them are not supported in older version of Clang. Thus, we check
33// `__has_attribute()` first. If the check fails, we check if we are on GCC and
34// assume the attribute exists on GCC (which is verified on GCC 4.7).
mistergc2e75482017-09-19 16:54:40 -040035//
Abseil Team53c239d2017-09-19 17:15:26 -070036// -----------------------------------------------------------------------------
37// Sanitizer Attributes
38// -----------------------------------------------------------------------------
39//
40// Sanitizer-related attributes are not "defined" in this file (and indeed
41// are not defined as such in any file). To utilize the following
42// sanitizer-related attributes within your builds, define the following macros
43// within your build using a `-D` flag, along with the given value for
44// `-fsanitize`:
45//
46// * `ADDRESS_SANITIZER` + `-fsanitize=address` (Clang, GCC 4.8)
47// * `MEMORY_SANITIZER` + `-fsanitize=memory` (Clang-only)
48// * `THREAD_SANITIZER + `-fsanitize=thread` (Clang, GCC 4.8+)
49// * `UNDEFINED_BEHAVIOR_SANITIZER` + `-fsanitize=undefined` (Clang, GCC 4.9+)
50// * `CONTROL_FLOW_INTEGRITY` + -fsanitize=cfi (Clang-only)
51//
52// Example:
53//
54// // Enable branches in the Abseil code that are tagged for ASan:
Abseil Team30de2042018-05-17 09:05:57 -070055// $ bazel build --copt=-DADDRESS_SANITIZER --copt=-fsanitize=address
56// --linkopt=-fsanitize=address *target*
Abseil Team53c239d2017-09-19 17:15:26 -070057//
58// Since these macro names are only supported by GCC and Clang, we only check
59// for `__GNUC__` (GCC or Clang) and the above macros.
mistergc2e75482017-09-19 16:54:40 -040060#ifndef ABSL_BASE_ATTRIBUTES_H_
61#define ABSL_BASE_ATTRIBUTES_H_
62
Abseil Team53c239d2017-09-19 17:15:26 -070063// ABSL_HAVE_ATTRIBUTE
64//
65// A function-like feature checking macro that is a wrapper around
66// `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
67// nonzero constant integer if the attribute is supported or 0 if not.
68//
69// It evaluates to zero if `__has_attribute` is not defined by the compiler.
70//
mistergc2e75482017-09-19 16:54:40 -040071// GCC: https://gcc.gnu.org/gcc-5/changes.html
72// Clang: https://clang.llvm.org/docs/LanguageExtensions.html
73#ifdef __has_attribute
74#define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x)
75#else
76#define ABSL_HAVE_ATTRIBUTE(x) 0
77#endif
78
Abseil Team53c239d2017-09-19 17:15:26 -070079// ABSL_HAVE_CPP_ATTRIBUTE
80//
81// A function-like feature checking macro that accepts C++11 style attributes.
82// It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
mistergc2e75482017-09-19 16:54:40 -040083// (http://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
Abseil Team53c239d2017-09-19 17:15:26 -070084// find `__has_cpp_attribute`, will evaluate to 0.
mistergc2e75482017-09-19 16:54:40 -040085#if defined(__cplusplus) && defined(__has_cpp_attribute)
86// NOTE: requiring __cplusplus above should not be necessary, but
87// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
88#define ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
89#else
90#define ABSL_HAVE_CPP_ATTRIBUTE(x) 0
91#endif
92
93// -----------------------------------------------------------------------------
94// Function Attributes
95// -----------------------------------------------------------------------------
Abseil Team53c239d2017-09-19 17:15:26 -070096//
mistergc2e75482017-09-19 16:54:40 -040097// GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
98// Clang: https://clang.llvm.org/docs/AttributeReference.html
99
Abseil Team53c239d2017-09-19 17:15:26 -0700100// ABSL_PRINTF_ATTRIBUTE
101// ABSL_SCANF_ATTRIBUTE
102//
Abseil Teambed5bd62018-08-21 11:31:02 -0700103// Tells the compiler to perform `printf` format string checking if the
mistergc2e75482017-09-19 16:54:40 -0400104// compiler supports it; see the 'format' attribute in
105// <http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>.
106//
Abseil Team53c239d2017-09-19 17:15:26 -0700107// Note: As the GCC manual states, "[s]ince non-static C++ methods
mistergc2e75482017-09-19 16:54:40 -0400108// have an implicit 'this' argument, the arguments of such methods
109// should be counted from two, not one."
110#if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__))
111#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
112 __attribute__((__format__(__printf__, string_index, first_to_check)))
113#define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
114 __attribute__((__format__(__scanf__, string_index, first_to_check)))
115#else
116#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
117#define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
118#endif
119
Abseil Team53c239d2017-09-19 17:15:26 -0700120// ABSL_ATTRIBUTE_ALWAYS_INLINE
121// ABSL_ATTRIBUTE_NOINLINE
122//
123// Forces functions to either inline or not inline. Introduced in gcc 3.1.
mistergc2e75482017-09-19 16:54:40 -0400124#if ABSL_HAVE_ATTRIBUTE(always_inline) || \
125 (defined(__GNUC__) && !defined(__clang__))
126#define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
127#define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1
128#else
129#define ABSL_ATTRIBUTE_ALWAYS_INLINE
130#endif
131
132#if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
133#define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline))
134#define ABSL_HAVE_ATTRIBUTE_NOINLINE 1
135#else
136#define ABSL_ATTRIBUTE_NOINLINE
137#endif
138
139// ABSL_ATTRIBUTE_NO_TAIL_CALL
Abseil Team53c239d2017-09-19 17:15:26 -0700140//
141// Prevents the compiler from optimizing away stack frames for functions which
mistergc2e75482017-09-19 16:54:40 -0400142// end in a call to another function.
143#if ABSL_HAVE_ATTRIBUTE(disable_tail_calls)
144#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
145#define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls))
146#elif defined(__GNUC__) && !defined(__clang__)
147#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
148#define ABSL_ATTRIBUTE_NO_TAIL_CALL \
149 __attribute__((optimize("no-optimize-sibling-calls")))
150#else
151#define ABSL_ATTRIBUTE_NO_TAIL_CALL
152#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0
153#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700154
mistergc2e75482017-09-19 16:54:40 -0400155// ABSL_ATTRIBUTE_WEAK
Abseil Team53c239d2017-09-19 17:15:26 -0700156//
157// Tags a function as weak for the purposes of compilation and linking.
mistergc2e75482017-09-19 16:54:40 -0400158#if ABSL_HAVE_ATTRIBUTE(weak) || (defined(__GNUC__) && !defined(__clang__))
159#undef ABSL_ATTRIBUTE_WEAK
160#define ABSL_ATTRIBUTE_WEAK __attribute__((weak))
161#define ABSL_HAVE_ATTRIBUTE_WEAK 1
162#else
163#define ABSL_ATTRIBUTE_WEAK
164#define ABSL_HAVE_ATTRIBUTE_WEAK 0
165#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700166
mistergc2e75482017-09-19 16:54:40 -0400167// ABSL_ATTRIBUTE_NONNULL
Abseil Team53c239d2017-09-19 17:15:26 -0700168//
169// Tells the compiler either (a) that a particular function parameter
170// should be a non-null pointer, or (b) that all pointer arguments should
mistergc2e75482017-09-19 16:54:40 -0400171// be non-null.
172//
173// Note: As the GCC manual states, "[s]ince non-static C++ methods
174// have an implicit 'this' argument, the arguments of such methods
175// should be counted from two, not one."
176//
177// Args are indexed starting at 1.
Abseil Team53c239d2017-09-19 17:15:26 -0700178//
179// For non-static class member functions, the implicit `this` argument
180// is arg 1, and the first explicit argument is arg 2. For static class member
181// functions, there is no implicit `this`, and the first explicit argument is
182// arg 1.
183//
184// Example:
mistergc2e75482017-09-19 16:54:40 -0400185//
186// /* arg_a cannot be null, but arg_b can */
187// void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1);
188//
189// class C {
190// /* arg_a cannot be null, but arg_b can */
191// void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2);
192//
193// /* arg_a cannot be null, but arg_b can */
194// static void StaticMethod(void* arg_a, void* arg_b)
195// ABSL_ATTRIBUTE_NONNULL(1);
196// };
197//
198// If no arguments are provided, then all pointer arguments should be non-null.
199//
200// /* No pointer arguments may be null. */
201// void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL();
202//
203// NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
204// ABSL_ATTRIBUTE_NONNULL does not.
205#if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
206#define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
207#else
208#define ABSL_ATTRIBUTE_NONNULL(...)
209#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700210
mistergc2e75482017-09-19 16:54:40 -0400211// ABSL_ATTRIBUTE_NORETURN
Abseil Team53c239d2017-09-19 17:15:26 -0700212//
213// Tells the compiler that a given function never returns.
mistergc2e75482017-09-19 16:54:40 -0400214#if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
215#define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
216#elif defined(_MSC_VER)
217#define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
218#else
219#define ABSL_ATTRIBUTE_NORETURN
220#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700221
mistergc2e75482017-09-19 16:54:40 -0400222// ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
Abseil Team53c239d2017-09-19 17:15:26 -0700223//
224// Tells the AddressSanitizer (or other memory testing tools) to ignore a given
mistergc2e75482017-09-19 16:54:40 -0400225// function. Useful for cases when a function reads random locations on stack,
226// calls _exit from a cloned subprocess, deliberately accesses buffer
227// out of bounds or does other scary things with memory.
228// NOTE: GCC supports AddressSanitizer(asan) since 4.8.
229// https://gcc.gnu.org/gcc-4.8/changes.html
230#if defined(__GNUC__) && defined(ADDRESS_SANITIZER)
231#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
232#else
233#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
234#endif
235
236// ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
Abseil Team53c239d2017-09-19 17:15:26 -0700237//
238// Tells the MemorySanitizer to relax the handling of a given function. All
239// "Use of uninitialized value" warnings from such functions will be suppressed,
240// and all values loaded from memory will be considered fully initialized.
241// This attribute is similar to the ADDRESS_SANITIZER attribute above, but deals
242// with initialized-ness rather than addressability issues.
mistergc2e75482017-09-19 16:54:40 -0400243// NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
244#if defined(__GNUC__) && defined(MEMORY_SANITIZER)
245#define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
246#else
247#define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
248#endif
249
250// ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
Abseil Team53c239d2017-09-19 17:15:26 -0700251//
252// Tells the ThreadSanitizer to not instrument a given function.
mistergc2e75482017-09-19 16:54:40 -0400253// NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
254// https://gcc.gnu.org/gcc-4.8/changes.html
255#if defined(__GNUC__) && defined(THREAD_SANITIZER)
256#define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
257#else
258#define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
259#endif
260
261// ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
Abseil Team53c239d2017-09-19 17:15:26 -0700262//
263// Tells the UndefinedSanitizer to ignore a given function. Useful for cases
Abseil Team8db6cfd2017-10-30 15:55:37 -0700264// where certain behavior (eg. division by zero) is being used intentionally.
mistergc2e75482017-09-19 16:54:40 -0400265// NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
266// https://gcc.gnu.org/gcc-4.9/changes.html
267#if defined(__GNUC__) && \
268 (defined(UNDEFINED_BEHAVIOR_SANITIZER) || defined(ADDRESS_SANITIZER))
269#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
270 __attribute__((no_sanitize("undefined")))
271#else
272#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
273#endif
274
275// ABSL_ATTRIBUTE_NO_SANITIZE_CFI
Abseil Team53c239d2017-09-19 17:15:26 -0700276//
277// Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
mistergc2e75482017-09-19 16:54:40 -0400278// See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
279#if defined(__GNUC__) && defined(CONTROL_FLOW_INTEGRITY)
280#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
281#else
282#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
283#endif
284
Abseil Teambe40fdf2018-01-12 07:45:05 -0800285// ABSL_ATTRIBUTE_RETURNS_NONNULL
286//
287// Tells the compiler that a particular function never returns a null pointer.
288#if ABSL_HAVE_ATTRIBUTE(returns_nonnull) || \
289 (defined(__GNUC__) && \
290 (__GNUC__ > 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && \
291 !defined(__clang__))
292#define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
293#else
294#define ABSL_ATTRIBUTE_RETURNS_NONNULL
295#endif
296
Abseil Team53c239d2017-09-19 17:15:26 -0700297// ABSL_HAVE_ATTRIBUTE_SECTION
298//
299// Indicates whether labeled sections are supported. Labeled sections are not
300// supported on Darwin/iOS.
mistergc2e75482017-09-19 16:54:40 -0400301#ifdef ABSL_HAVE_ATTRIBUTE_SECTION
302#error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
303#elif (ABSL_HAVE_ATTRIBUTE(section) || \
304 (defined(__GNUC__) && !defined(__clang__))) && \
305 !defined(__APPLE__)
306#define ABSL_HAVE_ATTRIBUTE_SECTION 1
Abseil Team53c239d2017-09-19 17:15:26 -0700307
308// ABSL_ATTRIBUTE_SECTION
mistergc2e75482017-09-19 16:54:40 -0400309//
Abseil Team53c239d2017-09-19 17:15:26 -0700310// Tells the compiler/linker to put a given function into a section and define
311// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
312// This functionality is supported by GNU linker. Any function annotated with
313// `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
314// whatever section its caller is placed into.
mistergc2e75482017-09-19 16:54:40 -0400315//
316#ifndef ABSL_ATTRIBUTE_SECTION
317#define ABSL_ATTRIBUTE_SECTION(name) \
318 __attribute__((section(#name))) __attribute__((noinline))
319#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700320
Abseil Team6280bdd2017-12-14 12:36:12 -0800321
Abseil Team53c239d2017-09-19 17:15:26 -0700322// ABSL_ATTRIBUTE_SECTION_VARIABLE
323//
324// Tells the compiler/linker to put a given variable into a section and define
325// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
mistergc2e75482017-09-19 16:54:40 -0400326// This functionality is supported by GNU linker.
327#ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
328#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
329#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700330
331// ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
mistergc2e75482017-09-19 16:54:40 -0400332//
Abseil Team53c239d2017-09-19 17:15:26 -0700333// A weak section declaration to be used as a global declaration
mistergc2e75482017-09-19 16:54:40 -0400334// for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
335// even without functions with ABSL_ATTRIBUTE_SECTION(name).
336// ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
337// a no-op on ELF but not on Mach-O.
338//
339#ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
340#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
341 extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
342 extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
343#endif
344#ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
345#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
346#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
347#endif
348
Abseil Team53c239d2017-09-19 17:15:26 -0700349// ABSL_ATTRIBUTE_SECTION_START
350//
351// Returns `void*` pointers to start/end of a section of code with
mistergc2e75482017-09-19 16:54:40 -0400352// functions having ABSL_ATTRIBUTE_SECTION(name).
353// Returns 0 if no such functions exist.
354// One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
355// link.
356//
357#define ABSL_ATTRIBUTE_SECTION_START(name) \
358 (reinterpret_cast<void *>(__start_##name))
359#define ABSL_ATTRIBUTE_SECTION_STOP(name) \
360 (reinterpret_cast<void *>(__stop_##name))
Abseil Team6280bdd2017-12-14 12:36:12 -0800361
mistergc2e75482017-09-19 16:54:40 -0400362#else // !ABSL_HAVE_ATTRIBUTE_SECTION
363
364#define ABSL_HAVE_ATTRIBUTE_SECTION 0
365
366// provide dummy definitions
367#define ABSL_ATTRIBUTE_SECTION(name)
368#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
369#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
370#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
371#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
372#define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
373#define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
Abseil Team6280bdd2017-12-14 12:36:12 -0800374
mistergc2e75482017-09-19 16:54:40 -0400375#endif // ABSL_ATTRIBUTE_SECTION
376
377// ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
Abseil Team53c239d2017-09-19 17:15:26 -0700378//
mistergc2e75482017-09-19 16:54:40 -0400379// Support for aligning the stack on 32-bit x86.
380#if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
381 (defined(__GNUC__) && !defined(__clang__))
382#if defined(__i386__)
383#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
384 __attribute__((force_align_arg_pointer))
385#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
386#elif defined(__x86_64__)
387#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
388#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
389#else // !__i386__ && !__x86_64
390#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
391#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
392#endif // __i386__
393#else
394#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
395#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
396#endif
397
398// ABSL_MUST_USE_RESULT
Abseil Team53c239d2017-09-19 17:15:26 -0700399//
400// Tells the compiler to warn about unused return values for functions declared
mistergc2e75482017-09-19 16:54:40 -0400401// with this macro. The macro must appear as the very first part of a function
402// declaration or definition:
403//
Abseil Team53c239d2017-09-19 17:15:26 -0700404// Example:
405//
mistergc2e75482017-09-19 16:54:40 -0400406// ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
407//
408// This placement has the broadest compatibility with GCC, Clang, and MSVC, with
409// both defs and decls, and with GCC-style attributes, MSVC declspec, C++11
410// and C++17 attributes.
411//
412// ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
413// warning. For that, warn_unused_result is used only for clang but not for gcc.
414// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
415//
416// Note: past advice was to place the macro after the argument list.
417#if ABSL_HAVE_ATTRIBUTE(nodiscard)
418#define ABSL_MUST_USE_RESULT [[nodiscard]]
419#elif defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
420#define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
421#else
422#define ABSL_MUST_USE_RESULT
423#endif
424
425// ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
Abseil Team53c239d2017-09-19 17:15:26 -0700426//
427// Tells GCC that a function is hot or cold. GCC can use this information to
mistergc2e75482017-09-19 16:54:40 -0400428// improve static analysis, i.e. a conditional branch to a cold function
429// is likely to be not-taken.
Abseil Team53c239d2017-09-19 17:15:26 -0700430// This annotation is used for function declarations.
431//
432// Example:
433//
mistergc2e75482017-09-19 16:54:40 -0400434// int foo() ABSL_ATTRIBUTE_HOT;
435#if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
436#define ABSL_ATTRIBUTE_HOT __attribute__((hot))
437#else
438#define ABSL_ATTRIBUTE_HOT
439#endif
440
441#if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
442#define ABSL_ATTRIBUTE_COLD __attribute__((cold))
443#else
444#define ABSL_ATTRIBUTE_COLD
445#endif
446
447// ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
448//
449// We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
450// macro used as an attribute to mark functions that must always or never be
451// instrumented by XRay. Currently, this is only supported in Clang/LLVM.
452//
453// For reference on the LLVM XRay instrumentation, see
454// http://llvm.org/docs/XRay.html.
455//
456// A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
457// will always get the XRay instrumentation sleds. These sleds may introduce
458// some binary size and runtime overhead and must be used sparingly.
459//
460// These attributes only take effect when the following conditions are met:
461//
Abseil Team53c239d2017-09-19 17:15:26 -0700462// * The file/target is built in at least C++11 mode, with a Clang compiler
463// that supports XRay attributes.
464// * The file/target is built with the -fxray-instrument flag set for the
465// Clang/LLVM compiler.
466// * The function is defined in the translation unit (the compiler honors the
467// attribute in either the definition or the declaration, and must match).
mistergc2e75482017-09-19 16:54:40 -0400468//
469// There are cases when, even when building with XRay instrumentation, users
470// might want to control specifically which functions are instrumented for a
471// particular build using special-case lists provided to the compiler. These
472// special case lists are provided to Clang via the
473// -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
474// attributes in source take precedence over these special-case lists.
475//
476// To disable the XRay attributes at build-time, users may define
477// ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
478// packages/targets, as this may lead to conflicting definitions of functions at
479// link-time.
480//
481#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
482 !defined(ABSL_NO_XRAY_ATTRIBUTES)
483#define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
484#define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
485#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
486#define ABSL_XRAY_LOG_ARGS(N) \
487 [[clang::xray_always_instrument, clang::xray_log_args(N)]]
488#else
489#define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
490#endif
491#else
492#define ABSL_XRAY_ALWAYS_INSTRUMENT
493#define ABSL_XRAY_NEVER_INSTRUMENT
494#define ABSL_XRAY_LOG_ARGS(N)
495#endif
496
Abseil Teamd6df7692018-08-28 01:05:21 -0700497// ABSL_ATTRIBUTE_REINITIALIZES
498//
499// Indicates that a member function reinitializes the entire object to a known
500// state, independent of the previous state of the object.
501//
502// The clang-tidy check bugprone-use-after-move allows member functions marked
503// with this attribute to be called on objects that have been moved from;
504// without the attribute, this would result in a use-after-move warning.
505#if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
506#define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
507#else
508#define ABSL_ATTRIBUTE_REINITIALIZES
509#endif
510
mistergc2e75482017-09-19 16:54:40 -0400511// -----------------------------------------------------------------------------
512// Variable Attributes
513// -----------------------------------------------------------------------------
514
515// ABSL_ATTRIBUTE_UNUSED
Abseil Team53c239d2017-09-19 17:15:26 -0700516//
Abseil Teamf21d1872018-10-02 12:09:18 -0700517// Prevents the compiler from complaining about variables that appear unused.
mistergc2e75482017-09-19 16:54:40 -0400518#if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
519#undef ABSL_ATTRIBUTE_UNUSED
520#define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
521#else
522#define ABSL_ATTRIBUTE_UNUSED
523#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700524
mistergc2e75482017-09-19 16:54:40 -0400525// ABSL_ATTRIBUTE_INITIAL_EXEC
Abseil Team53c239d2017-09-19 17:15:26 -0700526//
527// Tells the compiler to use "initial-exec" mode for a thread-local variable.
mistergc2e75482017-09-19 16:54:40 -0400528// See http://people.redhat.com/drepper/tls.pdf for the gory details.
529#if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
530#define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
531#else
532#define ABSL_ATTRIBUTE_INITIAL_EXEC
533#endif
534
535// ABSL_ATTRIBUTE_PACKED
Abseil Team53c239d2017-09-19 17:15:26 -0700536//
537// Prevents the compiler from padding a structure to natural alignment
mistergc2e75482017-09-19 16:54:40 -0400538#if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
539#define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
540#else
541#define ABSL_ATTRIBUTE_PACKED
542#endif
543
Abseil Teame2d17842018-02-16 01:13:15 -0800544// ABSL_ATTRIBUTE_FUNC_ALIGN
545//
546// Tells the compiler to align the function start at least to certain
547// alignment boundary
548#if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
549#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
550#else
551#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
552#endif
553
mistergc2e75482017-09-19 16:54:40 -0400554// ABSL_CONST_INIT
Abseil Team53c239d2017-09-19 17:15:26 -0700555//
556// A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
mistergc2e75482017-09-19 16:54:40 -0400557// not compile (on supported platforms) unless the variable has a constant
558// initializer. This is useful for variables with static and thread storage
559// duration, because it guarantees that they will not suffer from the so-called
Abseil Team7fda0992018-02-27 13:38:47 -0800560// "static init order fiasco". Prefer to put this attribute on the most visible
561// declaration of the variable, if there's more than one, because code that
562// accesses the variable can then use the attribute for optimization.
mistergc2e75482017-09-19 16:54:40 -0400563//
Abseil Team53c239d2017-09-19 17:15:26 -0700564// Example:
mistergc2e75482017-09-19 16:54:40 -0400565//
Abseil Team7fda0992018-02-27 13:38:47 -0800566// class MyClass {
567// public:
568// ABSL_CONST_INIT static MyType my_var;
569// };
570//
571// MyType MyClass::my_var = MakeMyType(...);
mistergc2e75482017-09-19 16:54:40 -0400572//
573// Note that this attribute is redundant if the variable is declared constexpr.
574#if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
Abseil Teamcf6ab6b2017-09-24 08:20:48 -0700575// NOLINTNEXTLINE(whitespace/braces)
mistergc2e75482017-09-19 16:54:40 -0400576#define ABSL_CONST_INIT [[clang::require_constant_initialization]]
577#else
578#define ABSL_CONST_INIT
579#endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
580
581#endif // ABSL_BASE_ATTRIBUTES_H_