abseil base/attributes 源码分析
标准库检测
C++ 20 开始终于有了 feature testing macro __has_cpp_attribute()
, 见 https://en.cppreference.com/w/cpp/feature_test
1 |
Explicitly mark as weak symbol
__attribute__((weak))
Used to declare a weak symbol, which can be overriden by a strong symbol at link time.
1 | void my_function() __attribute__((weak)); |
Notnull tags
__attribute__((nonnull(arg_index)))
__attribute__((returns_nonnull))
They only can check at compile-time and will be UB of violoated at runtime.
and can use syntax __attribute__((nonnull(arg_index1, arg_index2)))
for multiple args.
Sanitizers related
__attribute__((no_sanitize_address))
or in MSVC __declspec(no_sanitize_address)
to ignore a given function on memory address issues
__attribute__((no_sanitize_memory))
supported by Clang only; and handles with uninitialized memory issues
__attribute__((no_sanitize_thread))
for tsan, and
__attribute__((no_sanitize("undefined")))
for ubsan
__attribute__((no_sanitize("cfi")))
for CFI(Control Flow Integrity)
https://clang.llvm.org/docs/ControlFlowIntegrity.html
__attribute__((no_sanitize("safe-stack")))
for safe stack
For safe-stack sanitizer https://clang.llvm.org/docs/SafeStack.html
constinit
1 |
Lifetime bound
1 |
https://clang.llvm.org/docs/AttributeReference.html#lifetimebound