Skip to content

Commit f6d74af

Browse files
authored
[clang][X86] Only define __CET__ macro for X86 targets (#127616)
The `-fcf-protection` flag is now also used to enable CFI features for the RISC-V target, so it's not suitable to define `__CET__` solely based on the flag anymore. This patch moves the definition of the `__CET__` macro into X86 target hook, so only X86 targets with the `-fcf-protection` flag would enable the `__CET__` macro. See #109784 and #112477 for the adoption of `-fcf-protection` flag for RISC-V targets.
1 parent b100c50 commit f6d74af

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

clang/lib/Basic/Targets/X86.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,10 @@ void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
11091109

11101110
if (HasFloat128)
11111111
Builder.defineMacro("__SIZEOF_FLOAT128__", "16");
1112+
1113+
if (Opts.CFProtectionReturn || Opts.CFProtectionBranch)
1114+
Builder.defineMacro("__CET__", Twine{(Opts.CFProtectionReturn << 1) |
1115+
Opts.CFProtectionBranch});
11121116
}
11131117

11141118
bool X86TargetInfo::isValidFeatureName(StringRef Name) const {

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4776,17 +4776,6 @@ static bool ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
47764776
}
47774777
}
47784778

4779-
// Add the __CET__ macro if a CFProtection option is set.
4780-
if (const Arg *A = Args.getLastArg(OPT_fcf_protection_EQ)) {
4781-
StringRef Name = A->getValue();
4782-
if (Name == "branch")
4783-
Opts.addMacroDef("__CET__=1");
4784-
else if (Name == "return")
4785-
Opts.addMacroDef("__CET__=2");
4786-
else if (Name == "full")
4787-
Opts.addMacroDef("__CET__=3");
4788-
}
4789-
47904779
// Add macros from the command line.
47914780
for (const auto *A : Args.filtered(OPT_D, OPT_U)) {
47924781
if (A->getOption().matches(OPT_D))

clang/test/Preprocessor/riscv-cf-protection-return.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@
4040
// RUN: -menable-experimental-extensions -fcf-protection=full -E -dM %s -o - \
4141
// RUN: | FileCheck --check-prefixes=SHSTK-MACRO %s
4242

43+
// SHSTK-MACRO-NOT: __CET__
4344
// SHSTK-MACRO: __riscv_shadow_stack 1{{$}}
45+
// SHSTK-MACRO-NOT: __CET__
4446
// NO-MACRO-NOT: __riscv_shadow_stack

0 commit comments

Comments
 (0)