Skip to content

Commit dedbdfb

Browse files
cor3ntintru
authored andcommitted
[Clang] Improve type traits recognition in __has_builtin (llvm#111516)
`__has_builtin` was relying on reversible identifiers and string matching to recognize builtin-type traits, leading to some newer type traits not being recognized. Fixes llvm#111477
1 parent 92f4a2b commit dedbdfb

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

clang/include/clang/Basic/TokenKinds.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
#ifndef EXPRESSION_TRAIT
6565
#define EXPRESSION_TRAIT(I,E,K) KEYWORD(I,K)
6666
#endif
67+
#ifndef TRANSFORM_TYPE_TRAIT_DEF
68+
#define TRANSFORM_TYPE_TRAIT_DEF(K, Trait) KEYWORD(__##Trait, KEYCXX)
69+
#endif
70+
6771
#ifndef ALIAS
6872
#define ALIAS(X,Y,Z)
6973
#endif
@@ -534,7 +538,6 @@ TYPE_TRAIT_1(__has_unique_object_representations,
534538
TYPE_TRAIT_2(__is_layout_compatible, IsLayoutCompatible, KEYCXX)
535539
TYPE_TRAIT_2(__is_pointer_interconvertible_base_of, IsPointerInterconvertibleBaseOf, KEYCXX)
536540

537-
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) KEYWORD(__##Trait, KEYCXX)
538541
#include "clang/Basic/TransformTypeTraits.def"
539542

540543
// Clang-only C++ Type Traits

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,34 @@ static bool isTargetVariantEnvironment(const TargetInfo &TI,
16021602
return false;
16031603
}
16041604

1605+
static bool IsBuiltinTrait(Token &Tok) {
1606+
1607+
#define TYPE_TRAIT_1(Spelling, Name, Key) \
1608+
case tok::kw_##Spelling: \
1609+
return true;
1610+
#define TYPE_TRAIT_2(Spelling, Name, Key) \
1611+
case tok::kw_##Spelling: \
1612+
return true;
1613+
#define TYPE_TRAIT_N(Spelling, Name, Key) \
1614+
case tok::kw_##Spelling: \
1615+
return true;
1616+
#define ARRAY_TYPE_TRAIT(Spelling, Name, Key) \
1617+
case tok::kw_##Spelling: \
1618+
return true;
1619+
#define EXPRESSION_TRAIT(Spelling, Name, Key) \
1620+
case tok::kw_##Spelling: \
1621+
return true;
1622+
#define TRANSFORM_TYPE_TRAIT_DEF(K, Spelling) \
1623+
case tok::kw___##Spelling: \
1624+
return true;
1625+
1626+
switch (Tok.getKind()) {
1627+
default:
1628+
return false;
1629+
#include "clang/Basic/TokenKinds.def"
1630+
}
1631+
}
1632+
16051633
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
16061634
/// as a builtin macro, handle it and return the next token as 'Tok'.
16071635
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1798,25 +1826,11 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
17981826
getTargetInfo().getTargetOpts().FeatureMap);
17991827
}
18001828
return true;
1801-
} else if (II->getTokenID() != tok::identifier ||
1802-
II->hasRevertedTokenIDToIdentifier()) {
1803-
// Treat all keywords that introduce a custom syntax of the form
1804-
//
1805-
// '__some_keyword' '(' [...] ')'
1806-
//
1807-
// as being "builtin functions", even if the syntax isn't a valid
1808-
// function call (for example, because the builtin takes a type
1809-
// argument).
1810-
if (II->getName().starts_with("__builtin_") ||
1811-
II->getName().starts_with("__is_") ||
1812-
II->getName().starts_with("__has_"))
1813-
return true;
1814-
return llvm::StringSwitch<bool>(II->getName())
1815-
.Case("__array_rank", true)
1816-
.Case("__array_extent", true)
1817-
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) .Case("__" #Trait, true)
1818-
#include "clang/Basic/TransformTypeTraits.def"
1819-
.Default(false);
1829+
} else if (IsBuiltinTrait(Tok)) {
1830+
return true;
1831+
} else if (II->getTokenID() != tok::identifier &&
1832+
II->getName().starts_with("__builtin_")) {
1833+
return true;
18201834
} else {
18211835
return llvm::StringSwitch<bool>(II->getName())
18221836
// Report builtin templates as being builtins.

clang/test/Preprocessor/feature_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
!__has_builtin(__underlying_type) || \
3232
!__has_builtin(__is_trivial) || \
3333
!__has_builtin(__is_same_as) || \
34-
!__has_builtin(__has_unique_object_representations)
34+
!__has_builtin(__has_unique_object_representations) || \
35+
!__has_builtin(__is_trivially_equality_comparable) || \
36+
!__has_builtin(__reference_constructs_from_temporary) || \
37+
!__has_builtin(__reference_binds_to_temporary) || \
38+
!__has_builtin(__reference_converts_from_temporary)
3539
#error Clang should have these
3640
#endif
3741

0 commit comments

Comments
 (0)