Skip to content

Commit a52e9ec

Browse files
authored
[Preprocessor] Fix __has_builtin for CPU ID functions (#80058)
My recent commit (67c1c1d) made the CPU ID builtins target-independent so they can be used on PPC as well. However, that had the unintended consequence of changing the behaviour of __has_builtin in that it reports these as supported at the pre-processor level. This makes it impossible to guard the use of these with this feature test macro which is clearly not ideal. This patch restores the behaviour of __has_builtin for __builtin_cpu_is, __builtin_cpu_init, __builtin_cpu_supports. Now the preprocessor queries the target to determine whether the target supports the builtin.
1 parent acf6811 commit a52e9ec

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
16721672
return false;
16731673
else if (II->getBuiltinID() != 0) {
16741674
switch (II->getBuiltinID()) {
1675+
case Builtin::BI__builtin_cpu_is:
1676+
return getTargetInfo().supportsCpuIs();
1677+
case Builtin::BI__builtin_cpu_init:
1678+
return getTargetInfo().supportsCpuInit();
1679+
case Builtin::BI__builtin_cpu_supports:
1680+
return getTargetInfo().supportsCpuSupports();
16751681
case Builtin::BI__builtin_operator_new:
16761682
case Builtin::BI__builtin_operator_delete:
16771683
// denotes date of behavior change to support calling arbitrary
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -fsyntax-only -triple arm64-- -DARM -verify %s
2+
// RUN: %clang_cc1 -fsyntax-only -triple x86_64-- -DX86 -verify %s
3+
// RUN: %clang_cc1 -fsyntax-only -triple powerpc64-unknown-linux-gnu -DPPC \
4+
// RUN: -verify %s
5+
// expected-no-diagnostics
6+
#if __has_builtin(__builtin_cpu_is)
7+
# ifdef ARM
8+
# error "ARM shouldn't have __builtin_cpu_is"
9+
# endif
10+
#endif
11+
#if __has_builtin(__builtin_cpu_init)
12+
# if defined(ARM) || defined(PPC)
13+
# error "ARM/PPC shouldn't have __builtin_cpu_init"
14+
# endif
15+
#endif
16+
#if __has_builtin(__builtin_cpu_supports)
17+
# ifdef ARM
18+
# error "ARM shouldn't have __builtin_cpu_supports"
19+
# endif
20+
#endif

0 commit comments

Comments
 (0)