blob: a4ec7e7c97c425538cfdcb31c54e8610c03a258e [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:
55// $ bazel -D ADDRESS_SANITIZER -fsanitize=address *target*
56//
57// Since these macro names are only supported by GCC and Clang, we only check
58// for `__GNUC__` (GCC or Clang) and the above macros.
mistergc2e75482017-09-19 16:54:40 -040059#ifndef ABSL_BASE_ATTRIBUTES_H_
60#define ABSL_BASE_ATTRIBUTES_H_
61
Abseil Team53c239d2017-09-19 17:15:26 -070062// ABSL_HAVE_ATTRIBUTE
63//
64// A function-like feature checking macro that is a wrapper around
65// `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
66// nonzero constant integer if the attribute is supported or 0 if not.
67//
68// It evaluates to zero if `__has_attribute` is not defined by the compiler.
69//
mistergc2e75482017-09-19 16:54:40 -040070// GCC: https://gcc.gnu.org/gcc-5/changes.html
71// Clang: https://clang.llvm.org/docs/LanguageExtensions.html
72#ifdef __has_attribute
73#define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x)
74#else
75#define ABSL_HAVE_ATTRIBUTE(x) 0
76#endif
77
Abseil Team53c239d2017-09-19 17:15:26 -070078// ABSL_HAVE_CPP_ATTRIBUTE
79//
80// A function-like feature checking macro that accepts C++11 style attributes.
81// It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
mistergc2e75482017-09-19 16:54:40 -040082// (http://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
Abseil Team53c239d2017-09-19 17:15:26 -070083// find `__has_cpp_attribute`, will evaluate to 0.
mistergc2e75482017-09-19 16:54:40 -040084#if defined(__cplusplus) && defined(__has_cpp_attribute)
85// NOTE: requiring __cplusplus above should not be necessary, but
86// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
87#define ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
88#else
89#define ABSL_HAVE_CPP_ATTRIBUTE(x) 0
90#endif
91
92// -----------------------------------------------------------------------------
93// Function Attributes
94// -----------------------------------------------------------------------------
Abseil Team53c239d2017-09-19 17:15:26 -070095//
mistergc2e75482017-09-19 16:54:40 -040096// GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
97// Clang: https://clang.llvm.org/docs/AttributeReference.html
98
Abseil Team53c239d2017-09-19 17:15:26 -070099// ABSL_PRINTF_ATTRIBUTE
100// ABSL_SCANF_ATTRIBUTE
101//
102// Tells the compiler to perform `printf` format std::string checking if the
mistergc2e75482017-09-19 16:54:40 -0400103// compiler supports it; see the 'format' attribute in
104// <http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>.
105//
Abseil Team53c239d2017-09-19 17:15:26 -0700106// Note: As the GCC manual states, "[s]ince non-static C++ methods
mistergc2e75482017-09-19 16:54:40 -0400107// have an implicit 'this' argument, the arguments of such methods
108// should be counted from two, not one."
109#if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__))
110#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
111 __attribute__((__format__(__printf__, string_index, first_to_check)))
112#define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
113 __attribute__((__format__(__scanf__, string_index, first_to_check)))
114#else
115#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
116#define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
117#endif
118
Abseil Team53c239d2017-09-19 17:15:26 -0700119// ABSL_ATTRIBUTE_ALWAYS_INLINE
120// ABSL_ATTRIBUTE_NOINLINE
121//
122// Forces functions to either inline or not inline. Introduced in gcc 3.1.
mistergc2e75482017-09-19 16:54:40 -0400123#if ABSL_HAVE_ATTRIBUTE(always_inline) || \
124 (defined(__GNUC__) && !defined(__clang__))
125#define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
126#define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1
127#else
128#define ABSL_ATTRIBUTE_ALWAYS_INLINE
129#endif
130
131#if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
132#define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline))
133#define ABSL_HAVE_ATTRIBUTE_NOINLINE 1
134#else
135#define ABSL_ATTRIBUTE_NOINLINE
136#endif
137
138// ABSL_ATTRIBUTE_NO_TAIL_CALL
Abseil Team53c239d2017-09-19 17:15:26 -0700139//
140// Prevents the compiler from optimizing away stack frames for functions which
mistergc2e75482017-09-19 16:54:40 -0400141// end in a call to another function.
142#if ABSL_HAVE_ATTRIBUTE(disable_tail_calls)
143#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
144#define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls))
145#elif defined(__GNUC__) && !defined(__clang__)
146#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
147#define ABSL_ATTRIBUTE_NO_TAIL_CALL \
148 __attribute__((optimize("no-optimize-sibling-calls")))
149#else
150#define ABSL_ATTRIBUTE_NO_TAIL_CALL
151#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0
152#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700153
mistergc2e75482017-09-19 16:54:40 -0400154// ABSL_ATTRIBUTE_WEAK
Abseil Team53c239d2017-09-19 17:15:26 -0700155//
156// Tags a function as weak for the purposes of compilation and linking.
mistergc2e75482017-09-19 16:54:40 -0400157#if ABSL_HAVE_ATTRIBUTE(weak) || (defined(__GNUC__) && !defined(__clang__))
158#undef ABSL_ATTRIBUTE_WEAK
159#define ABSL_ATTRIBUTE_WEAK __attribute__((weak))
160#define ABSL_HAVE_ATTRIBUTE_WEAK 1
161#else
162#define ABSL_ATTRIBUTE_WEAK
163#define ABSL_HAVE_ATTRIBUTE_WEAK 0
164#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700165
mistergc2e75482017-09-19 16:54:40 -0400166// ABSL_ATTRIBUTE_NONNULL
Abseil Team53c239d2017-09-19 17:15:26 -0700167//
168// Tells the compiler either (a) that a particular function parameter
169// should be a non-null pointer, or (b) that all pointer arguments should
mistergc2e75482017-09-19 16:54:40 -0400170// be non-null.
171//
172// Note: As the GCC manual states, "[s]ince non-static C++ methods
173// have an implicit 'this' argument, the arguments of such methods
174// should be counted from two, not one."
175//
176// Args are indexed starting at 1.
Abseil Team53c239d2017-09-19 17:15:26 -0700177//
178// For non-static class member functions, the implicit `this` argument
179// is arg 1, and the first explicit argument is arg 2. For static class member
180// functions, there is no implicit `this`, and the first explicit argument is
181// arg 1.
182//
183// Example:
mistergc2e75482017-09-19 16:54:40 -0400184//
185// /* arg_a cannot be null, but arg_b can */
186// void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1);
187//
188// class C {
189// /* arg_a cannot be null, but arg_b can */
190// void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2);
191//
192// /* arg_a cannot be null, but arg_b can */
193// static void StaticMethod(void* arg_a, void* arg_b)
194// ABSL_ATTRIBUTE_NONNULL(1);
195// };
196//
197// If no arguments are provided, then all pointer arguments should be non-null.
198//
199// /* No pointer arguments may be null. */
200// void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL();
201//
202// NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
203// ABSL_ATTRIBUTE_NONNULL does not.
204#if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
205#define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
206#else
207#define ABSL_ATTRIBUTE_NONNULL(...)
208#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700209
mistergc2e75482017-09-19 16:54:40 -0400210// ABSL_ATTRIBUTE_NORETURN
Abseil Team53c239d2017-09-19 17:15:26 -0700211//
212// Tells the compiler that a given function never returns.
mistergc2e75482017-09-19 16:54:40 -0400213#if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
214#define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
215#elif defined(_MSC_VER)
216#define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
217#else
218#define ABSL_ATTRIBUTE_NORETURN
219#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700220
mistergc2e75482017-09-19 16:54:40 -0400221// ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
Abseil Team53c239d2017-09-19 17:15:26 -0700222//
223// Tells the AddressSanitizer (or other memory testing tools) to ignore a given
mistergc2e75482017-09-19 16:54:40 -0400224// function. Useful for cases when a function reads random locations on stack,
225// calls _exit from a cloned subprocess, deliberately accesses buffer
226// out of bounds or does other scary things with memory.
227// NOTE: GCC supports AddressSanitizer(asan) since 4.8.
228// https://gcc.gnu.org/gcc-4.8/changes.html
229#if defined(__GNUC__) && defined(ADDRESS_SANITIZER)
230#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
231#else
232#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
233#endif
234
235// ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
Abseil Team53c239d2017-09-19 17:15:26 -0700236//
237// Tells the MemorySanitizer to relax the handling of a given function. All
238// "Use of uninitialized value" warnings from such functions will be suppressed,
239// and all values loaded from memory will be considered fully initialized.
240// This attribute is similar to the ADDRESS_SANITIZER attribute above, but deals
241// with initialized-ness rather than addressability issues.
mistergc2e75482017-09-19 16:54:40 -0400242// NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
243#if defined(__GNUC__) && defined(MEMORY_SANITIZER)
244#define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
245#else
246#define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
247#endif
248
249// ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
Abseil Team53c239d2017-09-19 17:15:26 -0700250//
251// Tells the ThreadSanitizer to not instrument a given function.
mistergc2e75482017-09-19 16:54:40 -0400252// NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
253// https://gcc.gnu.org/gcc-4.8/changes.html
254#if defined(__GNUC__) && defined(THREAD_SANITIZER)
255#define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
256#else
257#define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
258#endif
259
260// ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
Abseil Team53c239d2017-09-19 17:15:26 -0700261//
262// Tells the UndefinedSanitizer to ignore a given function. Useful for cases
Abseil Team8db6cfd2017-10-30 15:55:37 -0700263// where certain behavior (eg. division by zero) is being used intentionally.
mistergc2e75482017-09-19 16:54:40 -0400264// NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
265// https://gcc.gnu.org/gcc-4.9/changes.html
266#if defined(__GNUC__) && \
267 (defined(UNDEFINED_BEHAVIOR_SANITIZER) || defined(ADDRESS_SANITIZER))
268#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
269 __attribute__((no_sanitize("undefined")))
270#else
271#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
272#endif
273
274// ABSL_ATTRIBUTE_NO_SANITIZE_CFI
Abseil Team53c239d2017-09-19 17:15:26 -0700275//
276// Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
mistergc2e75482017-09-19 16:54:40 -0400277// See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
278#if defined(__GNUC__) && defined(CONTROL_FLOW_INTEGRITY)
279#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
280#else
281#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
282#endif
283
Abseil Teambe40fdf2018-01-12 07:45:05 -0800284// ABSL_ATTRIBUTE_RETURNS_NONNULL
285//
286// Tells the compiler that a particular function never returns a null pointer.
287#if ABSL_HAVE_ATTRIBUTE(returns_nonnull) || \
288 (defined(__GNUC__) && \
289 (__GNUC__ > 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && \
290 !defined(__clang__))
291#define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
292#else
293#define ABSL_ATTRIBUTE_RETURNS_NONNULL
294#endif
295
Abseil Team53c239d2017-09-19 17:15:26 -0700296// ABSL_HAVE_ATTRIBUTE_SECTION
297//
298// Indicates whether labeled sections are supported. Labeled sections are not
299// supported on Darwin/iOS.
mistergc2e75482017-09-19 16:54:40 -0400300#ifdef ABSL_HAVE_ATTRIBUTE_SECTION
301#error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
302#elif (ABSL_HAVE_ATTRIBUTE(section) || \
303 (defined(__GNUC__) && !defined(__clang__))) && \
304 !defined(__APPLE__)
305#define ABSL_HAVE_ATTRIBUTE_SECTION 1
Abseil Team53c239d2017-09-19 17:15:26 -0700306
307// ABSL_ATTRIBUTE_SECTION
mistergc2e75482017-09-19 16:54:40 -0400308//
Abseil Team53c239d2017-09-19 17:15:26 -0700309// Tells the compiler/linker to put a given function into a section and define
310// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
311// This functionality is supported by GNU linker. Any function annotated with
312// `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
313// whatever section its caller is placed into.
mistergc2e75482017-09-19 16:54:40 -0400314//
315#ifndef ABSL_ATTRIBUTE_SECTION
316#define ABSL_ATTRIBUTE_SECTION(name) \
317 __attribute__((section(#name))) __attribute__((noinline))
318#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700319
Abseil Team6280bdd2017-12-14 12:36:12 -0800320
Abseil Team53c239d2017-09-19 17:15:26 -0700321// ABSL_ATTRIBUTE_SECTION_VARIABLE
322//
323// Tells the compiler/linker to put a given variable into a section and define
324// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
mistergc2e75482017-09-19 16:54:40 -0400325// This functionality is supported by GNU linker.
326#ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
327#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
328#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700329
330// ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
mistergc2e75482017-09-19 16:54:40 -0400331//
Abseil Team53c239d2017-09-19 17:15:26 -0700332// A weak section declaration to be used as a global declaration
mistergc2e75482017-09-19 16:54:40 -0400333// for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
334// even without functions with ABSL_ATTRIBUTE_SECTION(name).
335// ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
336// a no-op on ELF but not on Mach-O.
337//
338#ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
339#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
340 extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
341 extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
342#endif
343#ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
344#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
345#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
346#endif
347
Abseil Team53c239d2017-09-19 17:15:26 -0700348// ABSL_ATTRIBUTE_SECTION_START
349//
350// Returns `void*` pointers to start/end of a section of code with
mistergc2e75482017-09-19 16:54:40 -0400351// functions having ABSL_ATTRIBUTE_SECTION(name).
352// Returns 0 if no such functions exist.
353// One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
354// link.
355//
356#define ABSL_ATTRIBUTE_SECTION_START(name) \
357 (reinterpret_cast<void *>(__start_##name))
358#define ABSL_ATTRIBUTE_SECTION_STOP(name) \
359 (reinterpret_cast<void *>(__stop_##name))
Abseil Team6280bdd2017-12-14 12:36:12 -0800360
mistergc2e75482017-09-19 16:54:40 -0400361#else // !ABSL_HAVE_ATTRIBUTE_SECTION
362
363#define ABSL_HAVE_ATTRIBUTE_SECTION 0
364
365// provide dummy definitions
366#define ABSL_ATTRIBUTE_SECTION(name)
367#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
368#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
369#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
370#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
371#define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
372#define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
Abseil Team6280bdd2017-12-14 12:36:12 -0800373
mistergc2e75482017-09-19 16:54:40 -0400374#endif // ABSL_ATTRIBUTE_SECTION
375
376// ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
Abseil Team53c239d2017-09-19 17:15:26 -0700377//
mistergc2e75482017-09-19 16:54:40 -0400378// Support for aligning the stack on 32-bit x86.
379#if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
380 (defined(__GNUC__) && !defined(__clang__))
381#if defined(__i386__)
382#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
383 __attribute__((force_align_arg_pointer))
384#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
385#elif defined(__x86_64__)
386#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
387#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
388#else // !__i386__ && !__x86_64
389#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
390#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
391#endif // __i386__
392#else
393#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
394#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
395#endif
396
397// ABSL_MUST_USE_RESULT
Abseil Team53c239d2017-09-19 17:15:26 -0700398//
399// Tells the compiler to warn about unused return values for functions declared
mistergc2e75482017-09-19 16:54:40 -0400400// with this macro. The macro must appear as the very first part of a function
401// declaration or definition:
402//
Abseil Team53c239d2017-09-19 17:15:26 -0700403// Example:
404//
mistergc2e75482017-09-19 16:54:40 -0400405// ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
406//
407// This placement has the broadest compatibility with GCC, Clang, and MSVC, with
408// both defs and decls, and with GCC-style attributes, MSVC declspec, C++11
409// and C++17 attributes.
410//
411// ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
412// warning. For that, warn_unused_result is used only for clang but not for gcc.
413// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
414//
415// Note: past advice was to place the macro after the argument list.
416#if ABSL_HAVE_ATTRIBUTE(nodiscard)
417#define ABSL_MUST_USE_RESULT [[nodiscard]]
418#elif defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
419#define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
420#else
421#define ABSL_MUST_USE_RESULT
422#endif
423
424// ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
Abseil Team53c239d2017-09-19 17:15:26 -0700425//
426// Tells GCC that a function is hot or cold. GCC can use this information to
mistergc2e75482017-09-19 16:54:40 -0400427// improve static analysis, i.e. a conditional branch to a cold function
428// is likely to be not-taken.
Abseil Team53c239d2017-09-19 17:15:26 -0700429// This annotation is used for function declarations.
430//
431// Example:
432//
mistergc2e75482017-09-19 16:54:40 -0400433// int foo() ABSL_ATTRIBUTE_HOT;
434#if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
435#define ABSL_ATTRIBUTE_HOT __attribute__((hot))
436#else
437#define ABSL_ATTRIBUTE_HOT
438#endif
439
440#if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
441#define ABSL_ATTRIBUTE_COLD __attribute__((cold))
442#else
443#define ABSL_ATTRIBUTE_COLD
444#endif
445
446// ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
447//
448// We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
449// macro used as an attribute to mark functions that must always or never be
450// instrumented by XRay. Currently, this is only supported in Clang/LLVM.
451//
452// For reference on the LLVM XRay instrumentation, see
453// http://llvm.org/docs/XRay.html.
454//
455// A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
456// will always get the XRay instrumentation sleds. These sleds may introduce
457// some binary size and runtime overhead and must be used sparingly.
458//
459// These attributes only take effect when the following conditions are met:
460//
Abseil Team53c239d2017-09-19 17:15:26 -0700461// * The file/target is built in at least C++11 mode, with a Clang compiler
462// that supports XRay attributes.
463// * The file/target is built with the -fxray-instrument flag set for the
464// Clang/LLVM compiler.
465// * The function is defined in the translation unit (the compiler honors the
466// attribute in either the definition or the declaration, and must match).
mistergc2e75482017-09-19 16:54:40 -0400467//
468// There are cases when, even when building with XRay instrumentation, users
469// might want to control specifically which functions are instrumented for a
470// particular build using special-case lists provided to the compiler. These
471// special case lists are provided to Clang via the
472// -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
473// attributes in source take precedence over these special-case lists.
474//
475// To disable the XRay attributes at build-time, users may define
476// ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
477// packages/targets, as this may lead to conflicting definitions of functions at
478// link-time.
479//
480#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
481 !defined(ABSL_NO_XRAY_ATTRIBUTES)
482#define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
483#define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
484#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
485#define ABSL_XRAY_LOG_ARGS(N) \
486 [[clang::xray_always_instrument, clang::xray_log_args(N)]]
487#else
488#define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
489#endif
490#else
491#define ABSL_XRAY_ALWAYS_INSTRUMENT
492#define ABSL_XRAY_NEVER_INSTRUMENT
493#define ABSL_XRAY_LOG_ARGS(N)
494#endif
495
496// -----------------------------------------------------------------------------
497// Variable Attributes
498// -----------------------------------------------------------------------------
499
500// ABSL_ATTRIBUTE_UNUSED
Abseil Team53c239d2017-09-19 17:15:26 -0700501//
502// Prevents the compiler from complaining about or optimizing away variables
mistergc2e75482017-09-19 16:54:40 -0400503// that appear unused.
504#if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
505#undef ABSL_ATTRIBUTE_UNUSED
506#define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
507#else
508#define ABSL_ATTRIBUTE_UNUSED
509#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700510
mistergc2e75482017-09-19 16:54:40 -0400511// ABSL_ATTRIBUTE_INITIAL_EXEC
Abseil Team53c239d2017-09-19 17:15:26 -0700512//
513// Tells the compiler to use "initial-exec" mode for a thread-local variable.
mistergc2e75482017-09-19 16:54:40 -0400514// See http://people.redhat.com/drepper/tls.pdf for the gory details.
515#if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
516#define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
517#else
518#define ABSL_ATTRIBUTE_INITIAL_EXEC
519#endif
520
521// ABSL_ATTRIBUTE_PACKED
Abseil Team53c239d2017-09-19 17:15:26 -0700522//
523// Prevents the compiler from padding a structure to natural alignment
mistergc2e75482017-09-19 16:54:40 -0400524#if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
525#define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
526#else
527#define ABSL_ATTRIBUTE_PACKED
528#endif
529
Abseil Teame2d17842018-02-16 01:13:15 -0800530// ABSL_ATTRIBUTE_FUNC_ALIGN
531//
532// Tells the compiler to align the function start at least to certain
533// alignment boundary
534#if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
535#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
536#else
537#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
538#endif
539
mistergc2e75482017-09-19 16:54:40 -0400540// ABSL_CONST_INIT
Abseil Team53c239d2017-09-19 17:15:26 -0700541//
542// A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
mistergc2e75482017-09-19 16:54:40 -0400543// not compile (on supported platforms) unless the variable has a constant
544// initializer. This is useful for variables with static and thread storage
545// duration, because it guarantees that they will not suffer from the so-called
Abseil Team7fda0992018-02-27 13:38:47 -0800546// "static init order fiasco". Prefer to put this attribute on the most visible
547// declaration of the variable, if there's more than one, because code that
548// accesses the variable can then use the attribute for optimization.
mistergc2e75482017-09-19 16:54:40 -0400549//
Abseil Team53c239d2017-09-19 17:15:26 -0700550// Example:
mistergc2e75482017-09-19 16:54:40 -0400551//
Abseil Team7fda0992018-02-27 13:38:47 -0800552// class MyClass {
553// public:
554// ABSL_CONST_INIT static MyType my_var;
555// };
556//
557// MyType MyClass::my_var = MakeMyType(...);
mistergc2e75482017-09-19 16:54:40 -0400558//
559// Note that this attribute is redundant if the variable is declared constexpr.
560#if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
Abseil Teamcf6ab6b2017-09-24 08:20:48 -0700561// NOLINTNEXTLINE(whitespace/braces)
mistergc2e75482017-09-19 16:54:40 -0400562#define ABSL_CONST_INIT [[clang::require_constant_initialization]]
563#else
564#define ABSL_CONST_INIT
565#endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
566
567#endif // ABSL_BASE_ATTRIBUTES_H_