Skip to content

Commit 568c23b

Browse files
authored
Frontend: Define __SANITIZE_*__ macros for certain sanitizers.
Per discussion with @ojhunt and @AaronBallman we are moving towards predefined macros and away from __has_feature and __has_extension for detecting sanitizers and other similar features. The rationale is that __has_feature is only really meant for standardized features (see the comment at the top of clang/include/clang/Basic/Features.def), and __has_extension has the issues discovered as part of llvm#153104. Let's start by defining macros for ASan, HWASan and TSan, consistently with gcc. Reviewers: vitalybuka, ojhunt, AaronBallman, fmayer Reviewed By: fmayer, vitalybuka Pull Request: llvm#153888
1 parent be01355 commit 568c23b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,13 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
15191519
if (TI.getTriple().isOSBinFormatELF())
15201520
Builder.defineMacro("__ELF__");
15211521

1522+
if (LangOpts.Sanitize.has(SanitizerKind::Address))
1523+
Builder.defineMacro("__SANITIZE_ADDRESS__");
1524+
if (LangOpts.Sanitize.has(SanitizerKind::HWAddress))
1525+
Builder.defineMacro("__SANITIZE_HWADDRESS__");
1526+
if (LangOpts.Sanitize.has(SanitizerKind::Thread))
1527+
Builder.defineMacro("__SANITIZE_THREAD__");
1528+
15221529
// Target OS macro definitions.
15231530
if (PPOpts.DefineTargetOSMacros) {
15241531
const llvm::Triple &Triple = TI.getTriple();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -E -dM -triple aarch64-unknown-linux -fsanitize=address %s | FileCheck %s --check-prefix=ASAN
2+
// ASAN: #define __SANITIZE_ADDRESS__ 1
3+
4+
// RUN: %clang_cc1 -E -dM -triple aarch64-unknown-linux -fsanitize=hwaddress %s | FileCheck %s --check-prefix=HWASAN
5+
// HWASAN: #define __SANITIZE_HWADDRESS__ 1
6+
7+
// RUN: %clang_cc1 -E -dM -triple aarch64-unknown-linux -fsanitize=thread %s | FileCheck %s --check-prefix=TSAN
8+
// TSAN: #define __SANITIZE_THREAD__ 1

0 commit comments

Comments
 (0)