Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Sanitizer.h File Reference
#include "Stroika/Foundation/StroikaPreComp.h"
#include "Sanitizer.inl"

Go to the source code of this file.

Detailed Description

Note
Code-Status: Beta

Is this built WITH a sanitizer? Debug::kBuiltWithAddressSanitizer, kBuiltWithThreadSanitizer and kBuiltWithUndefinedBehaviorSanitizer are the answers. (Which sanitizers it COULD be built with is a question about the toolchain and configuration -

See also
configure –sanitize.)

Stroika deliberately defines no macro for this. Where you need the answer at compile time - in an #if - use the test documented on each variable, which is exactly the test that variable is defined with. None of them is ISO C++: o SANITIZE_ADDRESS and SANITIZE_THREAD are a g++ convention, which MSVC follows for the address sanitizer, and clang does from clang 22. o has_feature is a clang extension, which g++ adopted in g++ 14; MSVC has none (as of MSVC 19.51, VS 2026). o Nothing defines __SANITIZE_UNDEFINED (as of g++ 16, clang 22 and MSVC 19.51).

Note
TRAP: g++ before 14 ships a <sanitizer/common_interface_defs.h> - included by the <sanitizer/...> headers above - which defines a stand-in has_feature(x) as 0. So after including any sanitizer header, defined(__has_feature) is true there too, and says nothing about whether the compiler has it: to single out those g++, test the version (__GNUC < 14). (The tests documented below still work there - they just cannot say yes, for the undefined behavior sanitizer.)

So a test must allow for __has_feature not existing - and "#if defined(__has_feature) && __has_feature (x)" does NOT: where it does not exist, the second half is still a syntax error. Nest the #if, as each variable's test does.

What each compiler provides, at compile time (as of g++ 16, clang 22, Apple clang 17 and MSVC 19.51):

                                    address                 thread                  undefined behavior
g++ before 14                       __SANITIZE_ADDRESS__    __SANITIZE_THREAD__     (nothing)
g++ 14 and later                    both                    both                    __has_feature
clang before 22 (and Apple clang)   __has_feature           __has_feature           __has_feature
clang 22 and later                  both                    both                    __has_feature
MSVC                                __SANITIZE_ADDRESS__    (no such sanitizer)     (no such sanitizer)

The Stroika_Foundation_Debug_ATTRIBUTE_NO_SANITIZE_* macros are the one exception, since an attribute cannot be a variable. Each is defined for every compiler that has the attribute, whether or not built with that sanitizer - because a compiler ignores the attribute when not. (Were that not so, each definition would go inside its sanitizer's compile-time test.) All use the one generic attribute, no_sanitize ("address" / "thread" / "undefined" ...), wherever __has_attribute (no_sanitize) says the compiler has it (g++ from 8, clang) - the way boost and zstd do - plus MSVC's own __declspec (no_sanitize_address) for its one sanitizer.

Definition in file Sanitizer.h.