1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
|
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This header file defines macros for declaring attributes for functions,
// types, and variables.
//
// These macros are used within Abseil and allow the compiler to optimize, where
// applicable, certain function calls.
//
// Most macros here are exposing GCC or Clang features, and are stubbed out for
// other compilers.
//
// GCC attributes documentation:
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Variable-Attributes.html
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Type-Attributes.html
//
// Most attributes in this file are already supported by GCC 4.7. However, some
// of them are not supported in older version of Clang. Thus, we check
// `__has_attribute()` first. If the check fails, we check if we are on GCC and
// assume the attribute exists on GCC (which is verified on GCC 4.7).
#ifndef Y_ABSL_BASE_ATTRIBUTES_H_
#define Y_ABSL_BASE_ATTRIBUTES_H_
#include "y_absl/base/config.h"
// Y_ABSL_HAVE_ATTRIBUTE
//
// A function-like feature checking macro that is a wrapper around
// `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
// nonzero constant integer if the attribute is supported or 0 if not.
//
// It evaluates to zero if `__has_attribute` is not defined by the compiler.
//
// GCC: https://gcc.gnu.org/gcc-5/changes.html
// Clang: https://clang.llvm.org/docs/LanguageExtensions.html
#ifdef __has_attribute
#define Y_ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x)
#else
#define Y_ABSL_HAVE_ATTRIBUTE(x) 0
#endif
// Y_ABSL_HAVE_CPP_ATTRIBUTE
//
// A function-like feature checking macro that accepts C++11 style attributes.
// It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
// (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
// find `__has_cpp_attribute`, will evaluate to 0.
#if defined(__cplusplus) && defined(__has_cpp_attribute)
// NOTE: requiring __cplusplus above should not be necessary, but
// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
#define Y_ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#else
#define Y_ABSL_HAVE_CPP_ATTRIBUTE(x) 0
#endif
// -----------------------------------------------------------------------------
// Function Attributes
// -----------------------------------------------------------------------------
//
// GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
// Clang: https://clang.llvm.org/docs/AttributeReference.html
// Y_ABSL_PRINTF_ATTRIBUTE
// Y_ABSL_SCANF_ATTRIBUTE
//
// Tells the compiler to perform `printf` format string checking if the
// compiler supports it; see the 'format' attribute in
// <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>.
//
// Note: As the GCC manual states, "[s]ince non-static C++ methods
// have an implicit 'this' argument, the arguments of such methods
// should be counted from two, not one."
#if Y_ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
__attribute__((__format__(__printf__, string_index, first_to_check)))
#define Y_ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
__attribute__((__format__(__scanf__, string_index, first_to_check)))
#else
#define Y_ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
#define Y_ABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
#endif
// Y_ABSL_ATTRIBUTE_ALWAYS_INLINE
// Y_ABSL_ATTRIBUTE_NOINLINE
//
// Forces functions to either inline or not inline. Introduced in gcc 3.1.
#if Y_ABSL_HAVE_ATTRIBUTE(always_inline) || \
(defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
#define Y_ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1
#else
#define Y_ABSL_ATTRIBUTE_ALWAYS_INLINE
#endif
#if Y_ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline))
#define Y_ABSL_HAVE_ATTRIBUTE_NOINLINE 1
#else
#define Y_ABSL_ATTRIBUTE_NOINLINE
#endif
// Y_ABSL_ATTRIBUTE_NO_TAIL_CALL
//
// Prevents the compiler from optimizing away stack frames for functions which
// end in a call to another function.
#if Y_ABSL_HAVE_ATTRIBUTE(disable_tail_calls)
#define Y_ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
#define Y_ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls))
#elif defined(__GNUC__) && !defined(__clang__) && !defined(__e2k__)
#define Y_ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
#define Y_ABSL_ATTRIBUTE_NO_TAIL_CALL \
__attribute__((optimize("no-optimize-sibling-calls")))
#else
#define Y_ABSL_ATTRIBUTE_NO_TAIL_CALL
#define Y_ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0
#endif
// Y_ABSL_ATTRIBUTE_WEAK
//
// Tags a function as weak for the purposes of compilation and linking.
// Weak attributes did not work properly in LLVM's Windows backend before
// 9.0.0, so disable them there. See https://bugs.llvm.org/show_bug.cgi?id=37598
// for further information.
// The MinGW compiler doesn't complain about the weak attribute until the link
// step, presumably because Windows doesn't use ELF binaries.
#if (Y_ABSL_HAVE_ATTRIBUTE(weak) || \
(defined(__GNUC__) && !defined(__clang__))) && \
(!defined(_WIN32) || (defined(__clang__) && __clang_major__ >= 9)) && \
!defined(__MINGW32__)
#undef Y_ABSL_ATTRIBUTE_WEAK
#define Y_ABSL_ATTRIBUTE_WEAK __attribute__((weak))
#define Y_ABSL_HAVE_ATTRIBUTE_WEAK 1
#else
#define Y_ABSL_ATTRIBUTE_WEAK
#define Y_ABSL_HAVE_ATTRIBUTE_WEAK 0
#endif
// Y_ABSL_ATTRIBUTE_NONNULL
//
// Tells the compiler either (a) that a particular function parameter
// should be a non-null pointer, or (b) that all pointer arguments should
// be non-null.
//
// Note: As the GCC manual states, "[s]ince non-static C++ methods
// have an implicit 'this' argument, the arguments of such methods
// should be counted from two, not one."
//
// Args are indexed starting at 1.
//
// For non-static class member functions, the implicit `this` argument
// is arg 1, and the first explicit argument is arg 2. For static class member
// functions, there is no implicit `this`, and the first explicit argument is
// arg 1.
//
// Example:
//
// /* arg_a cannot be null, but arg_b can */
// void Function(void* arg_a, void* arg_b) Y_ABSL_ATTRIBUTE_NONNULL(1);
//
// class C {
// /* arg_a cannot be null, but arg_b can */
// void Method(void* arg_a, void* arg_b) Y_ABSL_ATTRIBUTE_NONNULL(2);
//
// /* arg_a cannot be null, but arg_b can */
// static void StaticMethod(void* arg_a, void* arg_b)
// Y_ABSL_ATTRIBUTE_NONNULL(1);
// };
//
// If no arguments are provided, then all pointer arguments should be non-null.
//
// /* No pointer arguments may be null. */
// void Function(void* arg_a, void* arg_b, int arg_c) Y_ABSL_ATTRIBUTE_NONNULL();
//
// NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
// Y_ABSL_ATTRIBUTE_NONNULL does not.
#if Y_ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
#else
#define Y_ABSL_ATTRIBUTE_NONNULL(...)
#endif
// Y_ABSL_ATTRIBUTE_NORETURN
//
// Tells the compiler that a given function never returns.
#if Y_ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
#define Y_ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
#else
#define Y_ABSL_ATTRIBUTE_NORETURN
#endif
// Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
//
// Tells the AddressSanitizer (or other memory testing tools) to ignore a given
// function. Useful for cases when a function reads random locations on stack,
// calls _exit from a cloned subprocess, deliberately accesses buffer
// out of bounds or does other scary things with memory.
// NOTE: GCC supports AddressSanitizer(asan) since 4.8.
// https://gcc.gnu.org/gcc-4.8/changes.html
#if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize_address)
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
#elif defined(_MSC_VER) && _MSC_VER >= 1928
// https://docs.microsoft.com/en-us/cpp/cpp/no-sanitize-address
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __declspec(no_sanitize_address)
#else
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
#endif
// Y_ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
//
// Tells the MemorySanitizer to relax the handling of a given function. All "Use
// of uninitialized value" warnings from such functions will be suppressed, and
// all values loaded from memory will be considered fully initialized. This
// attribute is similar to the Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS attribute
// above, but deals with initialized-ness rather than addressability issues.
// NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
#if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize_memory)
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
#else
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
#endif
// Y_ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
//
// Tells the ThreadSanitizer to not instrument a given function.
// NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
// https://gcc.gnu.org/gcc-4.8/changes.html
#if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize_thread)
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
#else
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
#endif
// Y_ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
//
// Tells the UndefinedSanitizer to ignore a given function. Useful for cases
// where certain behavior (eg. division by zero) is being used intentionally.
// NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
// https://gcc.gnu.org/gcc-4.9/changes.html
#if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize_undefined)
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
__attribute__((no_sanitize_undefined))
#elif Y_ABSL_HAVE_ATTRIBUTE(no_sanitize)
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
__attribute__((no_sanitize("undefined")))
#else
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
#endif
// Y_ABSL_ATTRIBUTE_NO_SANITIZE_CFI
//
// Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
// See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
#if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize)
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
#else
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_CFI
#endif
// Y_ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
//
// Tells the SafeStack to not instrument a given function.
// See https://clang.llvm.org/docs/SafeStack.html for details.
#if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize)
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \
__attribute__((no_sanitize("safe-stack")))
#else
#define Y_ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
#endif
// Y_ABSL_ATTRIBUTE_RETURNS_NONNULL
//
// Tells the compiler that a particular function never returns a null pointer.
#if Y_ABSL_HAVE_ATTRIBUTE(returns_nonnull)
#define Y_ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
#else
#define Y_ABSL_ATTRIBUTE_RETURNS_NONNULL
#endif
// Y_ABSL_HAVE_ATTRIBUTE_SECTION
//
// Indicates whether labeled sections are supported. Weak symbol support is
// a prerequisite. Labeled sections are not supported on Darwin/iOS.
#ifdef Y_ABSL_HAVE_ATTRIBUTE_SECTION
#error Y_ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
#elif (Y_ABSL_HAVE_ATTRIBUTE(section) || \
(defined(__GNUC__) && !defined(__clang__))) && \
!defined(__APPLE__) && Y_ABSL_HAVE_ATTRIBUTE_WEAK
#define Y_ABSL_HAVE_ATTRIBUTE_SECTION 1
// Y_ABSL_ATTRIBUTE_SECTION
//
// Tells the compiler/linker to put a given function into a section and define
// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
// This functionality is supported by GNU linker. Any function annotated with
// `Y_ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
// whatever section its caller is placed into.
//
#ifndef Y_ABSL_ATTRIBUTE_SECTION
#define Y_ABSL_ATTRIBUTE_SECTION(name) \
__attribute__((section(#name))) __attribute__((noinline))
#endif
// Y_ABSL_ATTRIBUTE_SECTION_VARIABLE
//
// Tells the compiler/linker to put a given variable into a section and define
// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
// This functionality is supported by GNU linker.
#ifndef Y_ABSL_ATTRIBUTE_SECTION_VARIABLE
#ifdef _AIX
// __attribute__((section(#name))) on AIX is achived by using the `.csect` psudo
// op which includes an additional integer as part of its syntax indcating
// alignment. If data fall under different alignments then you might get a
// compilation error indicating a `Section type conflict`.
#define Y_ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
#else
#define Y_ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
#endif
#endif
// Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
//
// A weak section declaration to be used as a global declaration
// for Y_ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
// even without functions with Y_ABSL_ATTRIBUTE_SECTION(name).
// Y_ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
// a no-op on ELF but not on Mach-O.
//
#ifndef Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
#define Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
extern char __start_##name[] Y_ABSL_ATTRIBUTE_WEAK; \
extern char __stop_##name[] Y_ABSL_ATTRIBUTE_WEAK
#endif
#ifndef Y_ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
#define Y_ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
#define Y_ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
#endif
// Y_ABSL_ATTRIBUTE_SECTION_START
//
// Returns `void*` pointers to start/end of a section of code with
// functions having Y_ABSL_ATTRIBUTE_SECTION(name).
// Returns 0 if no such functions exist.
// One must Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
// link.
//
#define Y_ABSL_ATTRIBUTE_SECTION_START(name) \
(reinterpret_cast<void *>(__start_##name))
#define Y_ABSL_ATTRIBUTE_SECTION_STOP(name) \
(reinterpret_cast<void *>(__stop_##name))
#else // !Y_ABSL_HAVE_ATTRIBUTE_SECTION
#define Y_ABSL_HAVE_ATTRIBUTE_SECTION 0
// provide dummy definitions
#define Y_ABSL_ATTRIBUTE_SECTION(name)
#define Y_ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
#define Y_ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
#define Y_ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
#define Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
#define Y_ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
#define Y_ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
#endif // Y_ABSL_ATTRIBUTE_SECTION
// Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
//
// Support for aligning the stack on 32-bit x86.
#if Y_ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
(defined(__GNUC__) && !defined(__clang__))
#if defined(__i386__)
#define Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
__attribute__((force_align_arg_pointer))
#define Y_ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
#elif defined(__x86_64__)
#define Y_ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
#define Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
#else // !__i386__ && !__x86_64
#define Y_ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
#define Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
#endif // __i386__
#else
#define Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
#define Y_ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
#endif
// Y_ABSL_MUST_USE_RESULT
//
// Tells the compiler to warn about unused results.
//
// For code or headers that are assured to only build with C++17 and up, prefer
// just using the standard `[[nodiscard]]` directly over this macro.
//
// When annotating a function, it must appear as the first part of the
// declaration or definition. The compiler will warn if the return value from
// such a function is unused:
//
// Y_ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
// AllocateSprocket(); // Triggers a warning.
//
// When annotating a class, it is equivalent to annotating every function which
// returns an instance.
//
// class Y_ABSL_MUST_USE_RESULT Sprocket {};
// Sprocket(); // Triggers a warning.
//
// Sprocket MakeSprocket();
// MakeSprocket(); // Triggers a warning.
//
// Note that references and pointers are not instances:
//
// Sprocket* SprocketPointer();
// SprocketPointer(); // Does *not* trigger a warning.
//
// Y_ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
// warning. For that, warn_unused_result is used only for clang but not for gcc.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
//
// Note: past advice was to place the macro after the argument list.
//
// TODO(b/176172494): Use Y_ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) when all code is
// compliant with the stricter [[nodiscard]].
#if defined(__clang__) && Y_ABSL_HAVE_ATTRIBUTE(warn_unused_result)
#define Y_ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
#else
#define Y_ABSL_MUST_USE_RESULT
#endif
// Y_ABSL_ATTRIBUTE_HOT, Y_ABSL_ATTRIBUTE_COLD
//
// Tells GCC that a function is hot or cold. GCC can use this information to
// improve static analysis, i.e. a conditional branch to a cold function
// is likely to be not-taken.
// This annotation is used for function declarations.
//
// Example:
//
// int foo() Y_ABSL_ATTRIBUTE_HOT;
#if Y_ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_HOT __attribute__((hot))
#else
#define Y_ABSL_ATTRIBUTE_HOT
#endif
#if Y_ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_COLD __attribute__((cold))
#else
#define Y_ABSL_ATTRIBUTE_COLD
#endif
// Y_ABSL_XRAY_ALWAYS_INSTRUMENT, Y_ABSL_XRAY_NEVER_INSTRUMENT, Y_ABSL_XRAY_LOG_ARGS
//
// We define the Y_ABSL_XRAY_ALWAYS_INSTRUMENT and Y_ABSL_XRAY_NEVER_INSTRUMENT
// macro used as an attribute to mark functions that must always or never be
// instrumented by XRay. Currently, this is only supported in Clang/LLVM.
//
// For reference on the LLVM XRay instrumentation, see
// http://llvm.org/docs/XRay.html.
//
// A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
// will always get the XRay instrumentation sleds. These sleds may introduce
// some binary size and runtime overhead and must be used sparingly.
//
// These attributes only take effect when the following conditions are met:
//
// * The file/target is built in at least C++11 mode, with a Clang compiler
// that supports XRay attributes.
// * The file/target is built with the -fxray-instrument flag set for the
// Clang/LLVM compiler.
// * The function is defined in the translation unit (the compiler honors the
// attribute in either the definition or the declaration, and must match).
//
// There are cases when, even when building with XRay instrumentation, users
// might want to control specifically which functions are instrumented for a
// particular build using special-case lists provided to the compiler. These
// special case lists are provided to Clang via the
// -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
// attributes in source take precedence over these special-case lists.
//
// To disable the XRay attributes at build-time, users may define
// Y_ABSL_NO_XRAY_ATTRIBUTES. Do NOT define Y_ABSL_NO_XRAY_ATTRIBUTES on specific
// packages/targets, as this may lead to conflicting definitions of functions at
// link-time.
//
// XRay isn't currently supported on Android:
// https://github.com/android/ndk/issues/368
#if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
!defined(Y_ABSL_NO_XRAY_ATTRIBUTES) && !defined(__ANDROID__)
#define Y_ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
#define Y_ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
#if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
#define Y_ABSL_XRAY_LOG_ARGS(N) \
[[clang::xray_always_instrument, clang::xray_log_args(N)]]
#else
#define Y_ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
#endif
#else
#define Y_ABSL_XRAY_ALWAYS_INSTRUMENT
#define Y_ABSL_XRAY_NEVER_INSTRUMENT
#define Y_ABSL_XRAY_LOG_ARGS(N)
#endif
// Y_ABSL_ATTRIBUTE_REINITIALIZES
//
// Indicates that a member function reinitializes the entire object to a known
// state, independent of the previous state of the object.
//
// The clang-tidy check bugprone-use-after-move allows member functions marked
// with this attribute to be called on objects that have been moved from;
// without the attribute, this would result in a use-after-move warning.
#if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
#define Y_ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
#else
#define Y_ABSL_ATTRIBUTE_REINITIALIZES
#endif
// -----------------------------------------------------------------------------
// Variable Attributes
// -----------------------------------------------------------------------------
// Y_ABSL_ATTRIBUTE_UNUSED
//
// Prevents the compiler from complaining about variables that appear unused.
//
// For code or headers that are assured to only build with C++17 and up, prefer
// just using the standard '[[maybe_unused]]' directly over this macro.
//
// Due to differences in positioning requirements between the old, compiler
// specific __attribute__ syntax and the now standard [[maybe_unused]], this
// macro does not attempt to take advantage of '[[maybe_unused]]'.
#if Y_ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
#undef Y_ABSL_ATTRIBUTE_UNUSED
#define Y_ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
#else
#define Y_ABSL_ATTRIBUTE_UNUSED
#endif
// Y_ABSL_ATTRIBUTE_INITIAL_EXEC
//
// Tells the compiler to use "initial-exec" mode for a thread-local variable.
// See http://people.redhat.com/drepper/tls.pdf for the gory details.
#if Y_ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
#else
#define Y_ABSL_ATTRIBUTE_INITIAL_EXEC
#endif
// Y_ABSL_ATTRIBUTE_PACKED
//
// Instructs the compiler not to use natural alignment for a tagged data
// structure, but instead to reduce its alignment to 1.
//
// Therefore, DO NOT APPLY THIS ATTRIBUTE TO STRUCTS CONTAINING ATOMICS. Doing
// so can cause atomic variables to be mis-aligned and silently violate
// atomicity on x86.
//
// This attribute can either be applied to members of a structure or to a
// structure in its entirety. Applying this attribute (judiciously) to a
// structure in its entirety to optimize the memory footprint of very
// commonly-used structs is fine. Do not apply this attribute to a structure in
// its entirety if the purpose is to control the offsets of the members in the
// structure. Instead, apply this attribute only to structure members that need
// it.
//
// When applying Y_ABSL_ATTRIBUTE_PACKED only to specific structure members the
// natural alignment of structure members not annotated is preserved. Aligned
// member accesses are faster than non-aligned member accesses even if the
// targeted microprocessor supports non-aligned accesses.
#if Y_ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
#else
#define Y_ABSL_ATTRIBUTE_PACKED
#endif
// Y_ABSL_ATTRIBUTE_FUNC_ALIGN
//
// Tells the compiler to align the function start at least to certain
// alignment boundary
#if Y_ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
#define Y_ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
#else
#define Y_ABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
#endif
// Y_ABSL_FALLTHROUGH_INTENDED
//
// Annotates implicit fall-through between switch labels, allowing a case to
// indicate intentional fallthrough and turn off warnings about any lack of a
// `break` statement. The Y_ABSL_FALLTHROUGH_INTENDED macro should be followed by
// a semicolon and can be used in most places where `break` can, provided that
// no statements exist between it and the next switch label.
//
// Example:
//
// switch (x) {
// case 40:
// case 41:
// if (truth_is_out_there) {
// ++x;
// Y_ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
// // in comments
// } else {
// return x;
// }
// case 42:
// ...
//
// Notes: When supported, GCC and Clang can issue a warning on switch labels
// with unannotated fallthrough using the warning `-Wimplicit-fallthrough`. See
// clang documentation on language extensions for details:
// https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
//
// When used with unsupported compilers, the Y_ABSL_FALLTHROUGH_INTENDED macro has
// no effect on diagnostics. In any case this macro has no effect on runtime
// behavior and performance of code.
#ifdef Y_ABSL_FALLTHROUGH_INTENDED
#error "Y_ABSL_FALLTHROUGH_INTENDED should not be defined."
#elif Y_ABSL_HAVE_CPP_ATTRIBUTE(fallthrough)
#define Y_ABSL_FALLTHROUGH_INTENDED [[fallthrough]]
#elif Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::fallthrough)
#define Y_ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
#elif Y_ABSL_HAVE_CPP_ATTRIBUTE(gnu::fallthrough)
#define Y_ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
#else
#define Y_ABSL_FALLTHROUGH_INTENDED \
do { \
} while (0)
#endif
// Y_ABSL_DEPRECATED()
//
// Marks a deprecated class, struct, enum, function, method and variable
// declarations. The macro argument is used as a custom diagnostic message (e.g.
// suggestion of a better alternative).
//
// For code or headers that are assured to only build with C++14 and up, prefer
// just using the standard `[[deprecated("message")]]` directly over this macro.
//
// Examples:
//
// class Y_ABSL_DEPRECATED("Use Bar instead") Foo {...};
//
// Y_ABSL_DEPRECATED("Use Baz() instead") void Bar() {...}
//
// template <typename T>
// Y_ABSL_DEPRECATED("Use DoThat() instead")
// void DoThis();
//
// enum FooEnum {
// kBar Y_ABSL_DEPRECATED("Use kBaz instead"),
// };
//
// Every usage of a deprecated entity will trigger a warning when compiled with
// GCC/Clang's `-Wdeprecated-declarations` option. Google's production toolchain
// turns this warning off by default, instead relying on clang-tidy to report
// new uses of deprecated code.
#if Y_ABSL_HAVE_ATTRIBUTE(deprecated)
#define Y_ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
#else
#define Y_ABSL_DEPRECATED(message)
#endif
// Y_ABSL_CONST_INIT
//
// A variable declaration annotated with the `Y_ABSL_CONST_INIT` attribute will
// not compile (on supported platforms) unless the variable has a constant
// initializer. This is useful for variables with static and thread storage
// duration, because it guarantees that they will not suffer from the so-called
// "static init order fiasco".
//
// This attribute must be placed on the initializing declaration of the
// variable. Some compilers will give a -Wmissing-constinit warning when this
// attribute is placed on some other declaration but missing from the
// initializing declaration.
//
// In some cases (notably with thread_local variables), `Y_ABSL_CONST_INIT` can
// also be used in a non-initializing declaration to tell the compiler that a
// variable is already initialized, reducing overhead that would otherwise be
// incurred by a hidden guard variable. Thus annotating all declarations with
// this attribute is recommended to potentially enhance optimization.
//
// Example:
//
// class MyClass {
// public:
// Y_ABSL_CONST_INIT static MyType my_var;
// };
//
// Y_ABSL_CONST_INIT MyType MyClass::my_var = MakeMyType(...);
//
// For code or headers that are assured to only build with C++20 and up, prefer
// just using the standard `constinit` keyword directly over this macro.
//
// Note that this attribute is redundant if the variable is declared constexpr.
#if defined(__cpp_constinit) && __cpp_constinit >= 201907L
#define Y_ABSL_CONST_INIT constinit
#elif Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
#define Y_ABSL_CONST_INIT [[clang::require_constant_initialization]]
#else
#define Y_ABSL_CONST_INIT
#endif
// These annotations are not available yet due to fear of breaking code.
#define Y_ABSL_ATTRIBUTE_PURE_FUNCTION
#define Y_ABSL_ATTRIBUTE_CONST_FUNCTION
// Y_ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
// parameter or implicit object parameter is retained by the return value of the
// annotated function (or, for a parameter of a constructor, in the value of the
// constructed object). This attribute causes warnings to be produced if a
// temporary object does not live long enough.
//
// When applied to a reference parameter, the referenced object is assumed to be
// retained by the return value of the function. When applied to a non-reference
// parameter (for example, a pointer or a class type), all temporaries
// referenced by the parameter are assumed to be retained by the return value of
// the function.
//
// See also the upstream documentation:
// https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
#if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetimebound)
#define Y_ABSL_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]]
#elif Y_ABSL_HAVE_ATTRIBUTE(lifetimebound)
#define Y_ABSL_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound))
#else
#define Y_ABSL_ATTRIBUTE_LIFETIME_BOUND
#endif
// Y_ABSL_ATTRIBUTE_TRIVIAL_ABI
// Indicates that a type is "trivially relocatable" -- meaning it can be
// relocated without invoking the constructor/destructor, using a form of move
// elision.
//
// From a memory safety point of view, putting aside destructor ordering, it's
// safe to apply Y_ABSL_ATTRIBUTE_TRIVIAL_ABI if an object's location
// can change over the course of its lifetime: if a constructor can be run one
// place, and then the object magically teleports to another place where some
// methods are run, and then the object teleports to yet another place where it
// is destroyed. This is notably not true for self-referential types, where the
// move-constructor must keep the self-reference up to date. If the type changed
// location without invoking the move constructor, it would have a dangling
// self-reference.
//
// The use of this teleporting machinery means that the number of paired
// move/destroy operations can change, and so it is a bad idea to apply this to
// a type meant to count the number of moves.
//
// Warning: applying this can, rarely, break callers. Objects passed by value
// will be destroyed at the end of the call, instead of the end of the
// full-expression containing the call. In addition, it changes the ABI
// of functions accepting this type by value (e.g. to pass in registers).
//
// See also the upstream documentation:
// https://clang.llvm.org/docs/AttributeReference.html#trivial-abi
//
#if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::trivial_abi)
#define Y_ABSL_ATTRIBUTE_TRIVIAL_ABI [[clang::trivial_abi]]
#define Y_ABSL_HAVE_ATTRIBUTE_TRIVIAL_ABI 1
#elif Y_ABSL_HAVE_ATTRIBUTE(trivial_abi)
#define Y_ABSL_ATTRIBUTE_TRIVIAL_ABI __attribute__((trivial_abi))
#define Y_ABSL_HAVE_ATTRIBUTE_TRIVIAL_ABI 1
#else
#define Y_ABSL_ATTRIBUTE_TRIVIAL_ABI
#endif
#endif // Y_ABSL_BASE_ATTRIBUTES_H_
|