blob: 6f3cfe4cbdaeae4ef2db1c25645177bdfb234a92 [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 Team53c239d2017-09-19 17:15:26 -0700284// ABSL_HAVE_ATTRIBUTE_SECTION
285//
286// Indicates whether labeled sections are supported. Labeled sections are not
287// supported on Darwin/iOS.
mistergc2e75482017-09-19 16:54:40 -0400288#ifdef ABSL_HAVE_ATTRIBUTE_SECTION
289#error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
290#elif (ABSL_HAVE_ATTRIBUTE(section) || \
291 (defined(__GNUC__) && !defined(__clang__))) && \
292 !defined(__APPLE__)
293#define ABSL_HAVE_ATTRIBUTE_SECTION 1
Abseil Team53c239d2017-09-19 17:15:26 -0700294
295// ABSL_ATTRIBUTE_SECTION
mistergc2e75482017-09-19 16:54:40 -0400296//
Abseil Team53c239d2017-09-19 17:15:26 -0700297// Tells the compiler/linker to put a given function into a section and define
298// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
299// This functionality is supported by GNU linker. Any function annotated with
300// `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
301// whatever section its caller is placed into.
mistergc2e75482017-09-19 16:54:40 -0400302//
303#ifndef ABSL_ATTRIBUTE_SECTION
304#define ABSL_ATTRIBUTE_SECTION(name) \
305 __attribute__((section(#name))) __attribute__((noinline))
306#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700307
308// ABSL_ATTRIBUTE_SECTION_VARIABLE
309//
310// Tells the compiler/linker to put a given variable into a section and define
311// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
mistergc2e75482017-09-19 16:54:40 -0400312// This functionality is supported by GNU linker.
313#ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
314#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
315#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700316
317// ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
mistergc2e75482017-09-19 16:54:40 -0400318//
Abseil Team53c239d2017-09-19 17:15:26 -0700319// A weak section declaration to be used as a global declaration
mistergc2e75482017-09-19 16:54:40 -0400320// for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
321// even without functions with ABSL_ATTRIBUTE_SECTION(name).
322// ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
323// a no-op on ELF but not on Mach-O.
324//
325#ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
326#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
327 extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
328 extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
329#endif
330#ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
331#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
332#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
333#endif
334
Abseil Team53c239d2017-09-19 17:15:26 -0700335// ABSL_ATTRIBUTE_SECTION_START
336//
337// Returns `void*` pointers to start/end of a section of code with
mistergc2e75482017-09-19 16:54:40 -0400338// functions having ABSL_ATTRIBUTE_SECTION(name).
339// Returns 0 if no such functions exist.
340// One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
341// link.
342//
343#define ABSL_ATTRIBUTE_SECTION_START(name) \
344 (reinterpret_cast<void *>(__start_##name))
345#define ABSL_ATTRIBUTE_SECTION_STOP(name) \
346 (reinterpret_cast<void *>(__stop_##name))
347#else // !ABSL_HAVE_ATTRIBUTE_SECTION
348
349#define ABSL_HAVE_ATTRIBUTE_SECTION 0
350
351// provide dummy definitions
352#define ABSL_ATTRIBUTE_SECTION(name)
353#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
354#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
355#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
356#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
357#define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
358#define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
359#endif // ABSL_ATTRIBUTE_SECTION
360
361// ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
Abseil Team53c239d2017-09-19 17:15:26 -0700362//
mistergc2e75482017-09-19 16:54:40 -0400363// Support for aligning the stack on 32-bit x86.
364#if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
365 (defined(__GNUC__) && !defined(__clang__))
366#if defined(__i386__)
367#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
368 __attribute__((force_align_arg_pointer))
369#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
370#elif defined(__x86_64__)
371#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
372#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
373#else // !__i386__ && !__x86_64
374#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
375#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
376#endif // __i386__
377#else
378#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
379#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
380#endif
381
382// ABSL_MUST_USE_RESULT
Abseil Team53c239d2017-09-19 17:15:26 -0700383//
384// Tells the compiler to warn about unused return values for functions declared
mistergc2e75482017-09-19 16:54:40 -0400385// with this macro. The macro must appear as the very first part of a function
386// declaration or definition:
387//
Abseil Team53c239d2017-09-19 17:15:26 -0700388// Example:
389//
mistergc2e75482017-09-19 16:54:40 -0400390// ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
391//
392// This placement has the broadest compatibility with GCC, Clang, and MSVC, with
393// both defs and decls, and with GCC-style attributes, MSVC declspec, C++11
394// and C++17 attributes.
395//
396// ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
397// warning. For that, warn_unused_result is used only for clang but not for gcc.
398// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
399//
400// Note: past advice was to place the macro after the argument list.
401#if ABSL_HAVE_ATTRIBUTE(nodiscard)
402#define ABSL_MUST_USE_RESULT [[nodiscard]]
403#elif defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
404#define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
405#else
406#define ABSL_MUST_USE_RESULT
407#endif
408
409// ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
Abseil Team53c239d2017-09-19 17:15:26 -0700410//
411// Tells GCC that a function is hot or cold. GCC can use this information to
mistergc2e75482017-09-19 16:54:40 -0400412// improve static analysis, i.e. a conditional branch to a cold function
413// is likely to be not-taken.
Abseil Team53c239d2017-09-19 17:15:26 -0700414// This annotation is used for function declarations.
415//
416// Example:
417//
mistergc2e75482017-09-19 16:54:40 -0400418// int foo() ABSL_ATTRIBUTE_HOT;
419#if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
420#define ABSL_ATTRIBUTE_HOT __attribute__((hot))
421#else
422#define ABSL_ATTRIBUTE_HOT
423#endif
424
425#if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
426#define ABSL_ATTRIBUTE_COLD __attribute__((cold))
427#else
428#define ABSL_ATTRIBUTE_COLD
429#endif
430
431// ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
432//
433// We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
434// macro used as an attribute to mark functions that must always or never be
435// instrumented by XRay. Currently, this is only supported in Clang/LLVM.
436//
437// For reference on the LLVM XRay instrumentation, see
438// http://llvm.org/docs/XRay.html.
439//
440// A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
441// will always get the XRay instrumentation sleds. These sleds may introduce
442// some binary size and runtime overhead and must be used sparingly.
443//
444// These attributes only take effect when the following conditions are met:
445//
Abseil Team53c239d2017-09-19 17:15:26 -0700446// * The file/target is built in at least C++11 mode, with a Clang compiler
447// that supports XRay attributes.
448// * The file/target is built with the -fxray-instrument flag set for the
449// Clang/LLVM compiler.
450// * The function is defined in the translation unit (the compiler honors the
451// attribute in either the definition or the declaration, and must match).
mistergc2e75482017-09-19 16:54:40 -0400452//
453// There are cases when, even when building with XRay instrumentation, users
454// might want to control specifically which functions are instrumented for a
455// particular build using special-case lists provided to the compiler. These
456// special case lists are provided to Clang via the
457// -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
458// attributes in source take precedence over these special-case lists.
459//
460// To disable the XRay attributes at build-time, users may define
461// ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
462// packages/targets, as this may lead to conflicting definitions of functions at
463// link-time.
464//
465#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
466 !defined(ABSL_NO_XRAY_ATTRIBUTES)
467#define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
468#define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
469#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
470#define ABSL_XRAY_LOG_ARGS(N) \
471 [[clang::xray_always_instrument, clang::xray_log_args(N)]]
472#else
473#define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
474#endif
475#else
476#define ABSL_XRAY_ALWAYS_INSTRUMENT
477#define ABSL_XRAY_NEVER_INSTRUMENT
478#define ABSL_XRAY_LOG_ARGS(N)
479#endif
480
481// -----------------------------------------------------------------------------
482// Variable Attributes
483// -----------------------------------------------------------------------------
484
485// ABSL_ATTRIBUTE_UNUSED
Abseil Team53c239d2017-09-19 17:15:26 -0700486//
487// Prevents the compiler from complaining about or optimizing away variables
mistergc2e75482017-09-19 16:54:40 -0400488// that appear unused.
489#if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
490#undef ABSL_ATTRIBUTE_UNUSED
491#define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
492#else
493#define ABSL_ATTRIBUTE_UNUSED
494#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700495
mistergc2e75482017-09-19 16:54:40 -0400496// ABSL_ATTRIBUTE_INITIAL_EXEC
Abseil Team53c239d2017-09-19 17:15:26 -0700497//
498// Tells the compiler to use "initial-exec" mode for a thread-local variable.
mistergc2e75482017-09-19 16:54:40 -0400499// See http://people.redhat.com/drepper/tls.pdf for the gory details.
500#if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
501#define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
502#else
503#define ABSL_ATTRIBUTE_INITIAL_EXEC
504#endif
505
506// ABSL_ATTRIBUTE_PACKED
Abseil Team53c239d2017-09-19 17:15:26 -0700507//
508// Prevents the compiler from padding a structure to natural alignment
mistergc2e75482017-09-19 16:54:40 -0400509#if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
510#define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
511#else
512#define ABSL_ATTRIBUTE_PACKED
513#endif
514
515// ABSL_CONST_INIT
Abseil Team53c239d2017-09-19 17:15:26 -0700516//
517// A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
mistergc2e75482017-09-19 16:54:40 -0400518// not compile (on supported platforms) unless the variable has a constant
519// initializer. This is useful for variables with static and thread storage
520// duration, because it guarantees that they will not suffer from the so-called
521// "static init order fiasco".
522//
Abseil Team53c239d2017-09-19 17:15:26 -0700523// Example:
mistergc2e75482017-09-19 16:54:40 -0400524//
Abseil Team53c239d2017-09-19 17:15:26 -0700525// ABSL_CONST_INIT static MyType my_var = MakeMyType(...);
mistergc2e75482017-09-19 16:54:40 -0400526//
527// Note that this attribute is redundant if the variable is declared constexpr.
528#if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
Abseil Teamcf6ab6b2017-09-24 08:20:48 -0700529// NOLINTNEXTLINE(whitespace/braces)
mistergc2e75482017-09-19 16:54:40 -0400530#define ABSL_CONST_INIT [[clang::require_constant_initialization]]
531#else
532#define ABSL_CONST_INIT
533#endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
534
535#endif // ABSL_BASE_ATTRIBUTES_H_