blob: 02bb030f5e5ac5e7e83d80ad9cf96122fc8a56e6 [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
Abseil Team6280bdd2017-12-14 12:36:12 -0800308
Abseil Team53c239d2017-09-19 17:15:26 -0700309// ABSL_ATTRIBUTE_SECTION_VARIABLE
310//
311// Tells the compiler/linker to put a given variable into a section and define
312// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
mistergc2e75482017-09-19 16:54:40 -0400313// This functionality is supported by GNU linker.
314#ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
315#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
316#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700317
318// ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
mistergc2e75482017-09-19 16:54:40 -0400319//
Abseil Team53c239d2017-09-19 17:15:26 -0700320// A weak section declaration to be used as a global declaration
mistergc2e75482017-09-19 16:54:40 -0400321// for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
322// even without functions with ABSL_ATTRIBUTE_SECTION(name).
323// ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
324// a no-op on ELF but not on Mach-O.
325//
326#ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
327#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
328 extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
329 extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
330#endif
331#ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
332#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
333#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
334#endif
335
Abseil Team53c239d2017-09-19 17:15:26 -0700336// ABSL_ATTRIBUTE_SECTION_START
337//
338// Returns `void*` pointers to start/end of a section of code with
mistergc2e75482017-09-19 16:54:40 -0400339// functions having ABSL_ATTRIBUTE_SECTION(name).
340// Returns 0 if no such functions exist.
341// One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
342// link.
343//
344#define ABSL_ATTRIBUTE_SECTION_START(name) \
345 (reinterpret_cast<void *>(__start_##name))
346#define ABSL_ATTRIBUTE_SECTION_STOP(name) \
347 (reinterpret_cast<void *>(__stop_##name))
Abseil Team6280bdd2017-12-14 12:36:12 -0800348
mistergc2e75482017-09-19 16:54:40 -0400349#else // !ABSL_HAVE_ATTRIBUTE_SECTION
350
351#define ABSL_HAVE_ATTRIBUTE_SECTION 0
352
353// provide dummy definitions
354#define ABSL_ATTRIBUTE_SECTION(name)
355#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
356#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
357#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
358#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
359#define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
360#define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
Abseil Team6280bdd2017-12-14 12:36:12 -0800361
mistergc2e75482017-09-19 16:54:40 -0400362#endif // ABSL_ATTRIBUTE_SECTION
363
364// ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
Abseil Team53c239d2017-09-19 17:15:26 -0700365//
mistergc2e75482017-09-19 16:54:40 -0400366// Support for aligning the stack on 32-bit x86.
367#if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
368 (defined(__GNUC__) && !defined(__clang__))
369#if defined(__i386__)
370#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
371 __attribute__((force_align_arg_pointer))
372#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
373#elif defined(__x86_64__)
374#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
375#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
376#else // !__i386__ && !__x86_64
377#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
378#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
379#endif // __i386__
380#else
381#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
382#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
383#endif
384
385// ABSL_MUST_USE_RESULT
Abseil Team53c239d2017-09-19 17:15:26 -0700386//
387// Tells the compiler to warn about unused return values for functions declared
mistergc2e75482017-09-19 16:54:40 -0400388// with this macro. The macro must appear as the very first part of a function
389// declaration or definition:
390//
Abseil Team53c239d2017-09-19 17:15:26 -0700391// Example:
392//
mistergc2e75482017-09-19 16:54:40 -0400393// ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
394//
395// This placement has the broadest compatibility with GCC, Clang, and MSVC, with
396// both defs and decls, and with GCC-style attributes, MSVC declspec, C++11
397// and C++17 attributes.
398//
399// ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
400// warning. For that, warn_unused_result is used only for clang but not for gcc.
401// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
402//
403// Note: past advice was to place the macro after the argument list.
404#if ABSL_HAVE_ATTRIBUTE(nodiscard)
405#define ABSL_MUST_USE_RESULT [[nodiscard]]
406#elif defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
407#define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
408#else
409#define ABSL_MUST_USE_RESULT
410#endif
411
412// ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
Abseil Team53c239d2017-09-19 17:15:26 -0700413//
414// Tells GCC that a function is hot or cold. GCC can use this information to
mistergc2e75482017-09-19 16:54:40 -0400415// improve static analysis, i.e. a conditional branch to a cold function
416// is likely to be not-taken.
Abseil Team53c239d2017-09-19 17:15:26 -0700417// This annotation is used for function declarations.
418//
419// Example:
420//
mistergc2e75482017-09-19 16:54:40 -0400421// int foo() ABSL_ATTRIBUTE_HOT;
422#if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
423#define ABSL_ATTRIBUTE_HOT __attribute__((hot))
424#else
425#define ABSL_ATTRIBUTE_HOT
426#endif
427
428#if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
429#define ABSL_ATTRIBUTE_COLD __attribute__((cold))
430#else
431#define ABSL_ATTRIBUTE_COLD
432#endif
433
434// ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
435//
436// We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
437// macro used as an attribute to mark functions that must always or never be
438// instrumented by XRay. Currently, this is only supported in Clang/LLVM.
439//
440// For reference on the LLVM XRay instrumentation, see
441// http://llvm.org/docs/XRay.html.
442//
443// A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
444// will always get the XRay instrumentation sleds. These sleds may introduce
445// some binary size and runtime overhead and must be used sparingly.
446//
447// These attributes only take effect when the following conditions are met:
448//
Abseil Team53c239d2017-09-19 17:15:26 -0700449// * The file/target is built in at least C++11 mode, with a Clang compiler
450// that supports XRay attributes.
451// * The file/target is built with the -fxray-instrument flag set for the
452// Clang/LLVM compiler.
453// * The function is defined in the translation unit (the compiler honors the
454// attribute in either the definition or the declaration, and must match).
mistergc2e75482017-09-19 16:54:40 -0400455//
456// There are cases when, even when building with XRay instrumentation, users
457// might want to control specifically which functions are instrumented for a
458// particular build using special-case lists provided to the compiler. These
459// special case lists are provided to Clang via the
460// -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
461// attributes in source take precedence over these special-case lists.
462//
463// To disable the XRay attributes at build-time, users may define
464// ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
465// packages/targets, as this may lead to conflicting definitions of functions at
466// link-time.
467//
468#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
469 !defined(ABSL_NO_XRAY_ATTRIBUTES)
470#define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
471#define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
472#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
473#define ABSL_XRAY_LOG_ARGS(N) \
474 [[clang::xray_always_instrument, clang::xray_log_args(N)]]
475#else
476#define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
477#endif
478#else
479#define ABSL_XRAY_ALWAYS_INSTRUMENT
480#define ABSL_XRAY_NEVER_INSTRUMENT
481#define ABSL_XRAY_LOG_ARGS(N)
482#endif
483
484// -----------------------------------------------------------------------------
485// Variable Attributes
486// -----------------------------------------------------------------------------
487
488// ABSL_ATTRIBUTE_UNUSED
Abseil Team53c239d2017-09-19 17:15:26 -0700489//
490// Prevents the compiler from complaining about or optimizing away variables
mistergc2e75482017-09-19 16:54:40 -0400491// that appear unused.
492#if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
493#undef ABSL_ATTRIBUTE_UNUSED
494#define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
495#else
496#define ABSL_ATTRIBUTE_UNUSED
497#endif
Abseil Team53c239d2017-09-19 17:15:26 -0700498
mistergc2e75482017-09-19 16:54:40 -0400499// ABSL_ATTRIBUTE_INITIAL_EXEC
Abseil Team53c239d2017-09-19 17:15:26 -0700500//
501// Tells the compiler to use "initial-exec" mode for a thread-local variable.
mistergc2e75482017-09-19 16:54:40 -0400502// See http://people.redhat.com/drepper/tls.pdf for the gory details.
503#if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
504#define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
505#else
506#define ABSL_ATTRIBUTE_INITIAL_EXEC
507#endif
508
509// ABSL_ATTRIBUTE_PACKED
Abseil Team53c239d2017-09-19 17:15:26 -0700510//
511// Prevents the compiler from padding a structure to natural alignment
mistergc2e75482017-09-19 16:54:40 -0400512#if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
513#define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
514#else
515#define ABSL_ATTRIBUTE_PACKED
516#endif
517
518// ABSL_CONST_INIT
Abseil Team53c239d2017-09-19 17:15:26 -0700519//
520// A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
mistergc2e75482017-09-19 16:54:40 -0400521// not compile (on supported platforms) unless the variable has a constant
522// initializer. This is useful for variables with static and thread storage
523// duration, because it guarantees that they will not suffer from the so-called
524// "static init order fiasco".
525//
Abseil Team53c239d2017-09-19 17:15:26 -0700526// Example:
mistergc2e75482017-09-19 16:54:40 -0400527//
Abseil Team53c239d2017-09-19 17:15:26 -0700528// ABSL_CONST_INIT static MyType my_var = MakeMyType(...);
mistergc2e75482017-09-19 16:54:40 -0400529//
530// Note that this attribute is redundant if the variable is declared constexpr.
531#if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
Abseil Teamcf6ab6b2017-09-24 08:20:48 -0700532// NOLINTNEXTLINE(whitespace/braces)
mistergc2e75482017-09-19 16:54:40 -0400533#define ABSL_CONST_INIT [[clang::require_constant_initialization]]
534#else
535#define ABSL_CONST_INIT
536#endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
537
538#endif // ABSL_BASE_ATTRIBUTES_H_