NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/nostd/internal/absl/base/attributes.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef OTABSL_BASE_ATTRIBUTES_H_
19 #define OTABSL_BASE_ATTRIBUTES_H_
20 
21 // OTABSL_HAVE_ATTRIBUTE
22 //
23 // A function-like feature checking macro that is a wrapper around
24 // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
25 // nonzero constant integer if the attribute is supported or 0 if not.
26 //
27 // It evaluates to zero if `__has_attribute` is not defined by the compiler.
28 //
29 // GCC: https://gcc.gnu.org/gcc-5/changes.html
30 // Clang: https://clang.llvm.org/docs/LanguageExtensions.html
31 #ifdef __has_attribute
32 #define OTABSL_HAVE_ATTRIBUTE(x) __has_attribute(x)
33 #else
34 #define OTABSL_HAVE_ATTRIBUTE(x) 0
35 #endif
36 
37 // OTABSL_HAVE_CPP_ATTRIBUTE
38 //
39 // A function-like feature checking macro that accepts C++11 style attributes.
40 // It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
41 // (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
42 // find `__has_cpp_attribute`, will evaluate to 0.
43 #if defined(__cplusplus) && defined(__has_cpp_attribute)
44 // NOTE: requiring __cplusplus above should not be necessary, but
45 // works around https://bugs.llvm.org/show_bug.cgi?id=23435.
46 #define OTABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
47 #else
48 #define OTABSL_HAVE_CPP_ATTRIBUTE(x) 0
49 #endif
50 
51 // -----------------------------------------------------------------------------
52 // Function Attributes
53 // -----------------------------------------------------------------------------
54 //
55 // GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
56 // Clang: https://clang.llvm.org/docs/AttributeReference.html
57 
58 // OTABSL_PRINTF_ATTRIBUTE
59 // OTABSL_SCANF_ATTRIBUTE
60 //
61 // Tells the compiler to perform `printf` format string checking if the
62 // compiler supports it; see the 'format' attribute in
63 // <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>.
64 //
65 // Note: As the GCC manual states, "[s]ince non-static C++ methods
66 // have an implicit 'this' argument, the arguments of such methods
67 // should be counted from two, not one."
68 #if OTABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__))
69 #define OTABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
70  __attribute__((__format__(__printf__, string_index, first_to_check)))
71 #define OTABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
72  __attribute__((__format__(__scanf__, string_index, first_to_check)))
73 #else
74 #define OTABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
75 #define OTABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
76 #endif
77 
78 // OTABSL_ATTRIBUTE_ALWAYS_INLINE
79 // OTABSL_ATTRIBUTE_NOINLINE
80 //
81 // Forces functions to either inline or not inline. Introduced in gcc 3.1.
82 #if OTABSL_HAVE_ATTRIBUTE(always_inline) || \
83  (defined(__GNUC__) && !defined(__clang__))
84 #define OTABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
85 #define OTABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1
86 #else
87 #define OTABSL_ATTRIBUTE_ALWAYS_INLINE
88 #endif
89 
90 #if OTABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
91 #define OTABSL_ATTRIBUTE_NOINLINE __attribute__((noinline))
92 #define OTABSL_HAVE_ATTRIBUTE_NOINLINE 1
93 #else
94 #define OTABSL_ATTRIBUTE_NOINLINE
95 #endif
96 
97 // OTABSL_ATTRIBUTE_NO_TAIL_CALL
98 //
99 // Prevents the compiler from optimizing away stack frames for functions which
100 // end in a call to another function.
101 #if OTABSL_HAVE_ATTRIBUTE(disable_tail_calls)
102 #define OTABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
103 #define OTABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls))
104 #elif defined(__GNUC__) && !defined(__clang__)
105 #define OTABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
106 #define OTABSL_ATTRIBUTE_NO_TAIL_CALL \
107  __attribute__((optimize("no-optimize-sibling-calls")))
108 #else
109 #define OTABSL_ATTRIBUTE_NO_TAIL_CALL
110 #define OTABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0
111 #endif
112 
113 // OTABSL_ATTRIBUTE_WEAK
114 //
115 // Tags a function as weak for the purposes of compilation and linking.
116 // Weak attributes currently do not work properly in LLVM's Windows backend,
117 // so disable them there. See https://bugs.llvm.org/show_bug.cgi?id=37598
118 // for further information.
119 // The MinGW compiler doesn't complain about the weak attribute until the link
120 // step, presumably because Windows doesn't use ELF binaries.
121 #if (OTABSL_HAVE_ATTRIBUTE(weak) || \
122  (defined(__GNUC__) && !defined(__clang__))) && \
123  !(defined(__llvm__) && defined(_WIN32)) && !defined(__MINGW32__)
124 #undef OTABSL_ATTRIBUTE_WEAK
125 #define OTABSL_ATTRIBUTE_WEAK __attribute__((weak))
126 #define OTABSL_HAVE_ATTRIBUTE_WEAK 1
127 #else
128 #define OTABSL_ATTRIBUTE_WEAK
129 #define OTABSL_HAVE_ATTRIBUTE_WEAK 0
130 #endif
131 
132 // OTABSL_ATTRIBUTE_NONNULL
133 //
134 // Tells the compiler either (a) that a particular function parameter
135 // should be a non-null pointer, or (b) that all pointer arguments should
136 // be non-null.
137 //
138 // Note: As the GCC manual states, "[s]ince non-static C++ methods
139 // have an implicit 'this' argument, the arguments of such methods
140 // should be counted from two, not one."
141 //
142 // Args are indexed starting at 1.
143 //
144 // For non-static class member functions, the implicit `this` argument
145 // is arg 1, and the first explicit argument is arg 2. For static class member
146 // functions, there is no implicit `this`, and the first explicit argument is
147 // arg 1.
148 //
149 // Example:
150 //
151 // /* arg_a cannot be null, but arg_b can */
152 // void Function(void* arg_a, void* arg_b) OTABSL_ATTRIBUTE_NONNULL(1);
153 //
154 // class C {
155 // /* arg_a cannot be null, but arg_b can */
156 // void Method(void* arg_a, void* arg_b) OTABSL_ATTRIBUTE_NONNULL(2);
157 //
158 // /* arg_a cannot be null, but arg_b can */
159 // static void StaticMethod(void* arg_a, void* arg_b)
160 // OTABSL_ATTRIBUTE_NONNULL(1);
161 // };
162 //
163 // If no arguments are provided, then all pointer arguments should be non-null.
164 //
165 // /* No pointer arguments may be null. */
166 // void Function(void* arg_a, void* arg_b, int arg_c) OTABSL_ATTRIBUTE_NONNULL();
167 //
168 // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
169 // OTABSL_ATTRIBUTE_NONNULL does not.
170 #if OTABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
171 #define OTABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
172 #else
173 #define OTABSL_ATTRIBUTE_NONNULL(...)
174 #endif
175 
176 // OTABSL_ATTRIBUTE_NORETURN
177 //
178 // Tells the compiler that a given function never returns.
179 #if OTABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
180 #define OTABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
181 #elif defined(_MSC_VER)
182 #define OTABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
183 #else
184 #define OTABSL_ATTRIBUTE_NORETURN
185 #endif
186 
187 // OTABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
188 //
189 // Tells the AddressSanitizer (or other memory testing tools) to ignore a given
190 // function. Useful for cases when a function reads random locations on stack,
191 // calls _exit from a cloned subprocess, deliberately accesses buffer
192 // out of bounds or does other scary things with memory.
193 // NOTE: GCC supports AddressSanitizer(asan) since 4.8.
194 // https://gcc.gnu.org/gcc-4.8/changes.html
195 #if defined(__GNUC__)
196 #define OTABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
197 #else
198 #define OTABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
199 #endif
200 
201 // OTABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
202 //
203 // Tells the MemorySanitizer to relax the handling of a given function. All
204 // "Use of uninitialized value" warnings from such functions will be suppressed,
205 // and all values loaded from memory will be considered fully initialized.
206 // This attribute is similar to the ADDRESS_SANITIZER attribute above, but deals
207 // with initialized-ness rather than addressability issues.
208 // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
209 #if defined(__clang__)
210 #define OTABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
211 #else
212 #define OTABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
213 #endif
214 
215 // OTABSL_ATTRIBUTE_NO_SANITIZE_THREAD
216 //
217 // Tells the ThreadSanitizer to not instrument a given function.
218 // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
219 // https://gcc.gnu.org/gcc-4.8/changes.html
220 #if defined(__GNUC__)
221 #define OTABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
222 #else
223 #define OTABSL_ATTRIBUTE_NO_SANITIZE_THREAD
224 #endif
225 
226 // OTABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
227 //
228 // Tells the UndefinedSanitizer to ignore a given function. Useful for cases
229 // where certain behavior (eg. division by zero) is being used intentionally.
230 // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
231 // https://gcc.gnu.org/gcc-4.9/changes.html
232 #if defined(__GNUC__) && \
233  (defined(UNDEFINED_BEHAVIOR_SANITIZER) || defined(ADDRESS_SANITIZER))
234 #define OTABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
235  __attribute__((no_sanitize("undefined")))
236 #else
237 #define OTABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
238 #endif
239 
240 // OTABSL_ATTRIBUTE_NO_SANITIZE_CFI
241 //
242 // Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
243 // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
244 #if defined(__GNUC__) && defined(CONTROL_FLOW_INTEGRITY)
245 #define OTABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
246 #else
247 #define OTABSL_ATTRIBUTE_NO_SANITIZE_CFI
248 #endif
249 
250 // OTABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
251 //
252 // Tells the SafeStack to not instrument a given function.
253 // See https://clang.llvm.org/docs/SafeStack.html for details.
254 #if defined(__GNUC__) && defined(SAFESTACK_SANITIZER)
255 #define OTABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \
256  __attribute__((no_sanitize("safe-stack")))
257 #else
258 #define OTABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
259 #endif
260 
261 // OTABSL_ATTRIBUTE_RETURNS_NONNULL
262 //
263 // Tells the compiler that a particular function never returns a null pointer.
264 #if OTABSL_HAVE_ATTRIBUTE(returns_nonnull) || \
265  (defined(__GNUC__) && \
266  (__GNUC__ > 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && \
267  !defined(__clang__))
268 #define OTABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
269 #else
270 #define OTABSL_ATTRIBUTE_RETURNS_NONNULL
271 #endif
272 
273 // OTABSL_HAVE_ATTRIBUTE_SECTION
274 //
275 // Indicates whether labeled sections are supported. Weak symbol support is
276 // a prerequisite. Labeled sections are not supported on Darwin/iOS.
277 #ifdef OTABSL_HAVE_ATTRIBUTE_SECTION
278 #error OTABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
279 #elif (OTABSL_HAVE_ATTRIBUTE(section) || \
280  (defined(__GNUC__) && !defined(__clang__))) && \
281  !defined(__APPLE__) && OTABSL_HAVE_ATTRIBUTE_WEAK
282 #define OTABSL_HAVE_ATTRIBUTE_SECTION 1
283 
284 // OTABSL_ATTRIBUTE_SECTION
285 //
286 // Tells the compiler/linker to put a given function into a section and define
287 // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
288 // This functionality is supported by GNU linker. Any function annotated with
289 // `OTABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
290 // whatever section its caller is placed into.
291 //
292 #ifndef OTABSL_ATTRIBUTE_SECTION
293 #define OTABSL_ATTRIBUTE_SECTION(name) \
294  __attribute__((section(#name))) __attribute__((noinline))
295 #endif
296 
297 
298 // OTABSL_ATTRIBUTE_SECTION_VARIABLE
299 //
300 // Tells the compiler/linker to put a given variable into a section and define
301 // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
302 // This functionality is supported by GNU linker.
303 #ifndef OTABSL_ATTRIBUTE_SECTION_VARIABLE
304 #define OTABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
305 #endif
306 
307 // OTABSL_DECLARE_ATTRIBUTE_SECTION_VARS
308 //
309 // A weak section declaration to be used as a global declaration
310 // for OTABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
311 // even without functions with OTABSL_ATTRIBUTE_SECTION(name).
312 // OTABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
313 // a no-op on ELF but not on Mach-O.
314 //
315 #ifndef OTABSL_DECLARE_ATTRIBUTE_SECTION_VARS
316 #define OTABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
317  extern char __start_##name[] OTABSL_ATTRIBUTE_WEAK; \
318  extern char __stop_##name[] OTABSL_ATTRIBUTE_WEAK
319 #endif
320 #ifndef OTABSL_DEFINE_ATTRIBUTE_SECTION_VARS
321 #define OTABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
322 #define OTABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
323 #endif
324 
325 // OTABSL_ATTRIBUTE_SECTION_START
326 //
327 // Returns `void*` pointers to start/end of a section of code with
328 // functions having OTABSL_ATTRIBUTE_SECTION(name).
329 // Returns 0 if no such functions exist.
330 // One must OTABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
331 // link.
332 //
333 #define OTABSL_ATTRIBUTE_SECTION_START(name) \
334  (reinterpret_cast<void *>(__start_##name))
335 #define OTABSL_ATTRIBUTE_SECTION_STOP(name) \
336  (reinterpret_cast<void *>(__stop_##name))
337 
338 #else // !OTABSL_HAVE_ATTRIBUTE_SECTION
339 
340 #define OTABSL_HAVE_ATTRIBUTE_SECTION 0
341 
342 // provide dummy definitions
343 #define OTABSL_ATTRIBUTE_SECTION(name)
344 #define OTABSL_ATTRIBUTE_SECTION_VARIABLE(name)
345 #define OTABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
346 #define OTABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
347 #define OTABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
348 #define OTABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
349 #define OTABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
350 
351 #endif // OTABSL_ATTRIBUTE_SECTION
352 
353 // OTABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
354 //
355 // Support for aligning the stack on 32-bit x86.
356 #if OTABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
357  (defined(__GNUC__) && !defined(__clang__))
358 #if defined(__i386__)
359 #define OTABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
360  __attribute__((force_align_arg_pointer))
361 #define OTABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
362 #elif defined(__x86_64__)
363 #define OTABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
364 #define OTABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
365 #else // !__i386__ && !__x86_64
366 #define OTABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
367 #define OTABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
368 #endif // __i386__
369 #else
370 #define OTABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
371 #define OTABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
372 #endif
373 
374 // OTABSL_MUST_USE_RESULT
375 //
376 // Tells the compiler to warn about unused results.
377 //
378 // When annotating a function, it must appear as the first part of the
379 // declaration or definition. The compiler will warn if the return value from
380 // such a function is unused:
381 //
382 // OTABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
383 // AllocateSprocket(); // Triggers a warning.
384 //
385 // When annotating a class, it is equivalent to annotating every function which
386 // returns an instance.
387 //
388 // class OTABSL_MUST_USE_RESULT Sprocket {};
389 // Sprocket(); // Triggers a warning.
390 //
391 // Sprocket MakeSprocket();
392 // MakeSprocket(); // Triggers a warning.
393 //
394 // Note that references and pointers are not instances:
395 //
396 // Sprocket* SprocketPointer();
397 // SprocketPointer(); // Does *not* trigger a warning.
398 //
399 // OTABSL_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 OTABSL_HAVE_ATTRIBUTE(nodiscard)
405 #define OTABSL_MUST_USE_RESULT [[nodiscard]]
406 #elif defined(__clang__) && OTABSL_HAVE_ATTRIBUTE(warn_unused_result)
407 #define OTABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
408 #else
409 #define OTABSL_MUST_USE_RESULT
410 #endif
411 
412 // OTABSL_ATTRIBUTE_HOT, OTABSL_ATTRIBUTE_COLD
413 //
414 // Tells GCC that a function is hot or cold. GCC can use this information to
415 // improve static analysis, i.e. a conditional branch to a cold function
416 // is likely to be not-taken.
417 // This annotation is used for function declarations.
418 //
419 // Example:
420 //
421 // int foo() OTABSL_ATTRIBUTE_HOT;
422 #if OTABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
423 #define OTABSL_ATTRIBUTE_HOT __attribute__((hot))
424 #else
425 #define OTABSL_ATTRIBUTE_HOT
426 #endif
427 
428 #if OTABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
429 #define OTABSL_ATTRIBUTE_COLD __attribute__((cold))
430 #else
431 #define OTABSL_ATTRIBUTE_COLD
432 #endif
433 
434 // OTABSL_XRAY_ALWAYS_INSTRUMENT, OTABSL_XRAY_NEVER_INSTRUMENT, OTABSL_XRAY_LOG_ARGS
435 //
436 // We define the OTABSL_XRAY_ALWAYS_INSTRUMENT and OTABSL_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 //
449 // * 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).
455 //
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 // OTABSL_NO_XRAY_ATTRIBUTES. Do NOT define OTABSL_NO_XRAY_ATTRIBUTES on specific
465 // packages/targets, as this may lead to conflicting definitions of functions at
466 // link-time.
467 //
468 #if OTABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
469  !defined(OTABSL_NO_XRAY_ATTRIBUTES)
470 #define OTABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
471 #define OTABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
472 #if OTABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
473 #define OTABSL_XRAY_LOG_ARGS(N) \
474  [[clang::xray_always_instrument, clang::xray_log_args(N)]]
475 #else
476 #define OTABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
477 #endif
478 #else
479 #define OTABSL_XRAY_ALWAYS_INSTRUMENT
480 #define OTABSL_XRAY_NEVER_INSTRUMENT
481 #define OTABSL_XRAY_LOG_ARGS(N)
482 #endif
483 
484 // OTABSL_ATTRIBUTE_REINITIALIZES
485 //
486 // Indicates that a member function reinitializes the entire object to a known
487 // state, independent of the previous state of the object.
488 //
489 // The clang-tidy check bugprone-use-after-move allows member functions marked
490 // with this attribute to be called on objects that have been moved from;
491 // without the attribute, this would result in a use-after-move warning.
492 #if OTABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
493 #define OTABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
494 #else
495 #define OTABSL_ATTRIBUTE_REINITIALIZES
496 #endif
497 
498 // -----------------------------------------------------------------------------
499 // Variable Attributes
500 // -----------------------------------------------------------------------------
501 
502 // OTABSL_ATTRIBUTE_UNUSED
503 //
504 // Prevents the compiler from complaining about variables that appear unused.
505 #if OTABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
506 #undef OTABSL_ATTRIBUTE_UNUSED
507 #define OTABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
508 #else
509 #define OTABSL_ATTRIBUTE_UNUSED
510 #endif
511 
512 // OTABSL_ATTRIBUTE_INITIAL_EXEC
513 //
514 // Tells the compiler to use "initial-exec" mode for a thread-local variable.
515 // See http://people.redhat.com/drepper/tls.pdf for the gory details.
516 #if OTABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
517 #define OTABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
518 #else
519 #define OTABSL_ATTRIBUTE_INITIAL_EXEC
520 #endif
521 
522 // OTABSL_ATTRIBUTE_PACKED
523 //
524 // Instructs the compiler not to use natural alignment for a tagged data
525 // structure, but instead to reduce its alignment to 1. This attribute can
526 // either be applied to members of a structure or to a structure in its
527 // entirety. Applying this attribute (judiciously) to a structure in its
528 // entirety to optimize the memory footprint of very commonly-used structs is
529 // fine. Do not apply this attribute to a structure in its entirety if the
530 // purpose is to control the offsets of the members in the structure. Instead,
531 // apply this attribute only to structure members that need it.
532 //
533 // When applying OTABSL_ATTRIBUTE_PACKED only to specific structure members the
534 // natural alignment of structure members not annotated is preserved. Aligned
535 // member accesses are faster than non-aligned member accesses even if the
536 // targeted microprocessor supports non-aligned accesses.
537 #if OTABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
538 #define OTABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
539 #else
540 #define OTABSL_ATTRIBUTE_PACKED
541 #endif
542 
543 // OTABSL_ATTRIBUTE_FUNC_ALIGN
544 //
545 // Tells the compiler to align the function start at least to certain
546 // alignment boundary
547 #if OTABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
548 #define OTABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
549 #else
550 #define OTABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
551 #endif
552 
553 // OTABSL_CONST_INIT
554 //
555 // A variable declaration annotated with the `OTABSL_CONST_INIT` attribute will
556 // not compile (on supported platforms) unless the variable has a constant
557 // initializer. This is useful for variables with static and thread storage
558 // duration, because it guarantees that they will not suffer from the so-called
559 // "static init order fiasco". Prefer to put this attribute on the most visible
560 // declaration of the variable, if there's more than one, because code that
561 // accesses the variable can then use the attribute for optimization.
562 //
563 // Example:
564 //
565 // class MyClass {
566 // public:
567 // OTABSL_CONST_INIT static MyType my_var;
568 // };
569 //
570 // MyType MyClass::my_var = MakeMyType(...);
571 //
572 // Note that this attribute is redundant if the variable is declared constexpr.
573 #if OTABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
574 #define OTABSL_CONST_INIT [[clang::require_constant_initialization]]
575 #else
576 #define OTABSL_CONST_INIT
577 #endif // OTABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
578 
579 #endif // OTABSL_BASE_ATTRIBUTES_H_