Skip to content

Commit e62ce1f

Browse files
authored
[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)
Remove FormatToken::isSimpleTypeSpecifier() and call Token::isSimpleTypeSpecifier(LangOpts) instead.
1 parent df88d61 commit e62ce1f

13 files changed

+121
-134
lines changed

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3858,8 +3858,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) {
38583858
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
38593859

38603860
LangOpts.LineComment = 1;
3861-
bool AlternativeOperators = Style.isCpp();
3862-
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
3861+
LangOpts.CXXOperatorNames = Style.isCpp();
38633862
LangOpts.Bool = 1;
38643863
LangOpts.ObjC = 1;
38653864
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.

clang/lib/Format/FormatToken.cpp

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,59 +34,23 @@ const char *getTokenTypeName(TokenType Type) {
3434
return nullptr;
3535
}
3636

37-
// FIXME: This is copy&pasted from Sema. Put it in a common place and remove
38-
// duplication.
39-
bool FormatToken::isSimpleTypeSpecifier() const {
40-
switch (Tok.getKind()) {
41-
case tok::kw_short:
42-
case tok::kw_long:
43-
case tok::kw___int64:
44-
case tok::kw___int128:
45-
case tok::kw_signed:
46-
case tok::kw_unsigned:
47-
case tok::kw_void:
48-
case tok::kw_char:
49-
case tok::kw_int:
50-
case tok::kw_half:
51-
case tok::kw_float:
52-
case tok::kw_double:
53-
case tok::kw___bf16:
54-
case tok::kw__Float16:
55-
case tok::kw___float128:
56-
case tok::kw___ibm128:
57-
case tok::kw_wchar_t:
58-
case tok::kw_bool:
59-
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
60-
#include "clang/Basic/TransformTypeTraits.def"
61-
case tok::annot_typename:
62-
case tok::kw_char8_t:
63-
case tok::kw_char16_t:
64-
case tok::kw_char32_t:
65-
case tok::kw_typeof:
66-
case tok::kw_decltype:
67-
case tok::kw__Atomic:
68-
return true;
69-
default:
70-
return false;
71-
}
72-
}
73-
7437
// Sorted common C++ non-keyword types.
7538
static SmallVector<StringRef> CppNonKeywordTypes = {
7639
"clock_t", "int16_t", "int32_t", "int64_t", "int8_t",
7740
"intptr_t", "ptrdiff_t", "size_t", "time_t", "uint16_t",
7841
"uint32_t", "uint64_t", "uint8_t", "uintptr_t",
7942
};
8043

81-
bool FormatToken::isTypeName(bool IsCpp) const {
82-
return is(TT_TypeName) || isSimpleTypeSpecifier() ||
44+
bool FormatToken::isTypeName(const LangOptions &LangOpts) const {
45+
const bool IsCpp = LangOpts.CXXOperatorNames;
46+
return is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts) ||
8347
(IsCpp && is(tok::identifier) &&
8448
std::binary_search(CppNonKeywordTypes.begin(),
8549
CppNonKeywordTypes.end(), TokenText));
8650
}
8751

88-
bool FormatToken::isTypeOrIdentifier(bool IsCpp) const {
89-
return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier);
52+
bool FormatToken::isTypeOrIdentifier(const LangOptions &LangOpts) const {
53+
return isTypeName(LangOpts) || isOneOf(tok::kw_auto, tok::identifier);
9054
}
9155

9256
bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const {

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -684,12 +684,8 @@ struct FormatToken {
684684
isAttribute();
685685
}
686686

687-
/// Determine whether the token is a simple-type-specifier.
688-
[[nodiscard]] bool isSimpleTypeSpecifier() const;
689-
690-
[[nodiscard]] bool isTypeName(bool IsCpp) const;
691-
692-
[[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const;
687+
[[nodiscard]] bool isTypeName(const LangOptions &LangOpts) const;
688+
[[nodiscard]] bool isTypeOrIdentifier(const LangOptions &LangOpts) const;
693689

694690
bool isObjCAccessSpecifier() const {
695691
return is(tok::at) && Next &&

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,6 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) {
14421442

14431443
void FormatTokenLexer::resetLexer(unsigned Offset) {
14441444
StringRef Buffer = SourceMgr.getBufferData(ID);
1445-
LangOpts = getFormattingLangOpts(Style);
14461445
Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts,
14471446
Buffer.begin(), Buffer.begin() + Offset, Buffer.end()));
14481447
Lex->SetKeepWhitespaceMode(true);

clang/lib/Format/QualifierAlignmentFixer.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
268268
if (isPossibleMacro(TypeToken))
269269
return Tok;
270270

271-
const bool IsCpp = Style.isCpp();
272-
273271
// The case `const long long int volatile` -> `long long int const volatile`
274272
// The case `long const long int volatile` -> `long long int const volatile`
275273
// The case `long long volatile int const` -> `long long int const volatile`
276274
// The case `const long long volatile int` -> `long long int const volatile`
277-
if (TypeToken->isTypeName(IsCpp)) {
275+
if (TypeToken->isTypeName(LangOpts)) {
278276
// The case `const decltype(foo)` -> `const decltype(foo)`
279277
// The case `const typeof(foo)` -> `const typeof(foo)`
280278
// The case `const _Atomic(foo)` -> `const _Atomic(foo)`
@@ -283,7 +281,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
283281

284282
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
285283
while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(),
286-
IsCpp)) {
284+
LangOpts)) {
287285
LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment();
288286
}
289287

@@ -295,7 +293,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
295293
// The case `unsigned short const` -> `unsigned short const`
296294
// The case:
297295
// `unsigned short volatile const` -> `unsigned short const volatile`
298-
if (PreviousCheck && PreviousCheck->isTypeName(IsCpp)) {
296+
if (PreviousCheck && PreviousCheck->isTypeName(LangOpts)) {
299297
if (LastQual != Tok)
300298
rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
301299
return Tok;
@@ -412,11 +410,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
412410
// The case `volatile long long const int` -> `const volatile long long int`
413411
// The case `const long long volatile int` -> `const volatile long long int`
414412
// The case `long volatile long int const` -> `const volatile long long int`
415-
if (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) {
413+
if (TypeToken->isTypeName(LangOpts)) {
416414
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
417415
while (isConfiguredQualifierOrType(
418416
LastSimpleTypeSpecifier->getPreviousNonComment(),
419-
ConfiguredQualifierTokens, IsCpp)) {
417+
ConfiguredQualifierTokens, LangOpts)) {
420418
LastSimpleTypeSpecifier =
421419
LastSimpleTypeSpecifier->getPreviousNonComment();
422420
}
@@ -614,16 +612,16 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
614612
}
615613
}
616614

617-
bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken *Tok,
618-
bool IsCpp) {
619-
return Tok &&
620-
(Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok));
615+
bool LeftRightQualifierAlignmentFixer::isQualifierOrType(
616+
const FormatToken *Tok, const LangOptions &LangOpts) {
617+
return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) ||
618+
isQualifier(Tok));
621619
}
622620

623621
bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
624622
const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers,
625-
bool IsCpp) {
626-
return Tok && (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) ||
623+
const LangOptions &LangOpts) {
624+
return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) ||
627625
isConfiguredQualifier(Tok, Qualifiers));
628626
}
629627

clang/lib/Format/QualifierAlignmentFixer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ class LeftRightQualifierAlignmentFixer : public TokenAnalyzer {
7171
tok::TokenKind QualifierType);
7272

7373
// Is the Token a simple or qualifier type
74-
static bool isQualifierOrType(const FormatToken *Tok, bool IsCpp = true);
74+
static bool isQualifierOrType(const FormatToken *Tok,
75+
const LangOptions &LangOpts);
7576
static bool
7677
isConfiguredQualifierOrType(const FormatToken *Tok,
7778
const std::vector<tok::TokenKind> &Qualifiers,
78-
bool IsCpp = true);
79+
const LangOptions &LangOpts);
7980

8081
// Is the Token likely a Macro
8182
static bool isPossibleMacro(const FormatToken *Tok);

clang/lib/Format/TokenAnalyzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Environment::Environment(StringRef Code, StringRef FileName,
8484
NextStartColumn(NextStartColumn), LastStartColumn(LastStartColumn) {}
8585

8686
TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
87-
: Style(Style), Env(Env),
87+
: Style(Style), LangOpts(getFormattingLangOpts(Style)), Env(Env),
8888
AffectedRangeMgr(Env.getSourceManager(), Env.getCharRanges()),
8989
UnwrappedLines(1),
9090
Encoding(encoding::detectEncoding(
@@ -101,7 +101,7 @@ std::pair<tooling::Replacements, unsigned>
101101
TokenAnalyzer::process(bool SkipAnnotation) {
102102
tooling::Replacements Result;
103103
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
104-
IdentifierTable IdentTable(getFormattingLangOpts(Style));
104+
IdentifierTable IdentTable(LangOpts);
105105
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
106106
Env.getFirstStartColumn(), Style, Encoding, Allocator,
107107
IdentTable);

clang/lib/Format/TokenAnalyzer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class TokenAnalyzer : public UnwrappedLineConsumer {
9292
void finishRun() override;
9393

9494
FormatStyle Style;
95+
LangOptions LangOpts;
9596
// Stores Style, FileID and SourceManager etc.
9697
const Environment &Env;
9798
// AffectedRangeMgr stores ranges to be fixed.

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ class AnnotatingParser {
126126
const AdditionalKeywords &Keywords,
127127
SmallVector<ScopeType> &Scopes)
128128
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
129-
IsCpp(Style.isCpp()), Keywords(Keywords), Scopes(Scopes) {
129+
IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)),
130+
Keywords(Keywords), Scopes(Scopes) {
131+
assert(IsCpp == LangOpts.CXXOperatorNames);
130132
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
131133
resetTokenMetadata();
132134
}
@@ -562,7 +564,7 @@ class AnnotatingParser {
562564
(CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
563565
CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
564566
if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
565-
CurrentToken->Previous->isTypeName(IsCpp)) &&
567+
CurrentToken->Previous->isTypeName(LangOpts)) &&
566568
!(CurrentToken->is(tok::l_brace) ||
567569
(CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
568570
Contexts.back().IsExpression = false;
@@ -2624,7 +2626,7 @@ class AnnotatingParser {
26242626
return true;
26252627

26262628
// MyClass a;
2627-
if (PreviousNotConst->isTypeName(IsCpp))
2629+
if (PreviousNotConst->isTypeName(LangOpts))
26282630
return true;
26292631

26302632
// type[] a in Java
@@ -2728,7 +2730,7 @@ class AnnotatingParser {
27282730
}
27292731

27302732
if (Tok.Next->is(tok::question) ||
2731-
(Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(IsCpp))) {
2733+
(Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(LangOpts))) {
27322734
return false;
27332735
}
27342736

@@ -2757,9 +2759,10 @@ class AnnotatingParser {
27572759
}
27582760

27592761
// Heuristically try to determine whether the parentheses contain a type.
2760-
auto IsQualifiedPointerOrReference = [](FormatToken *T, bool IsCpp) {
2762+
auto IsQualifiedPointerOrReference = [](FormatToken *T,
2763+
const LangOptions &LangOpts) {
27612764
// This is used to handle cases such as x = (foo *const)&y;
2762-
assert(!T->isTypeName(IsCpp) && "Should have already been checked");
2765+
assert(!T->isTypeName(LangOpts) && "Should have already been checked");
27632766
// Strip trailing qualifiers such as const or volatile when checking
27642767
// whether the parens could be a cast to a pointer/reference type.
27652768
while (T) {
@@ -2791,8 +2794,8 @@ class AnnotatingParser {
27912794
bool ParensAreType =
27922795
!Tok.Previous ||
27932796
Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2794-
Tok.Previous->isTypeName(IsCpp) ||
2795-
IsQualifiedPointerOrReference(Tok.Previous, IsCpp);
2797+
Tok.Previous->isTypeName(LangOpts) ||
2798+
IsQualifiedPointerOrReference(Tok.Previous, LangOpts);
27962799
bool ParensCouldEndDecl =
27972800
Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
27982801
if (ParensAreType && !ParensCouldEndDecl)
@@ -3065,6 +3068,7 @@ class AnnotatingParser {
30653068
FormatToken *CurrentToken;
30663069
bool AutoFound;
30673070
bool IsCpp;
3071+
LangOptions LangOpts;
30683072
const AdditionalKeywords &Keywords;
30693073

30703074
SmallVector<ScopeType> &Scopes;
@@ -3639,7 +3643,8 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
36393643

36403644
// This function heuristically determines whether 'Current' starts the name of a
36413645
// function declaration.
3642-
static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3646+
static bool isFunctionDeclarationName(const LangOptions &LangOpts,
3647+
const FormatToken &Current,
36433648
const AnnotatedLine &Line,
36443649
FormatToken *&ClosingParen) {
36453650
assert(Current.Previous);
@@ -3658,7 +3663,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
36583663
}
36593664

36603665
auto skipOperatorName =
3661-
[IsCpp](const FormatToken *Next) -> const FormatToken * {
3666+
[&LangOpts](const FormatToken *Next) -> const FormatToken * {
36623667
for (; Next; Next = Next->Next) {
36633668
if (Next->is(TT_OverloadedOperatorLParen))
36643669
return Next;
@@ -3677,7 +3682,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
36773682
Next = Next->Next;
36783683
continue;
36793684
}
3680-
if ((Next->isTypeName(IsCpp) || Next->is(tok::identifier)) &&
3685+
if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
36813686
Next->Next && Next->Next->isPointerOrReference()) {
36823687
// For operator void*(), operator char*(), operator Foo*().
36833688
Next = Next->Next;
@@ -3693,8 +3698,10 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
36933698
return nullptr;
36943699
};
36953700

3701+
const auto *Next = Current.Next;
3702+
const bool IsCpp = LangOpts.CXXOperatorNames;
3703+
36963704
// Find parentheses of parameter list.
3697-
const FormatToken *Next = Current.Next;
36983705
if (Current.is(tok::kw_operator)) {
36993706
if (Previous.Tok.getIdentifierInfo() &&
37003707
!Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
@@ -3774,7 +3781,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
37743781
Tok = Tok->MatchingParen;
37753782
continue;
37763783
}
3777-
if (Tok->is(tok::kw_const) || Tok->isTypeName(IsCpp) ||
3784+
if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
37783785
Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
37793786
return true;
37803787
}
@@ -3837,7 +3844,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
38373844
AfterLastAttribute = Tok;
38383845
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
38393846
IsCtorOrDtor ||
3840-
isFunctionDeclarationName(IsCpp, *Tok, Line, ClosingParen)) {
3847+
isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {
38413848
if (!IsCtorOrDtor)
38423849
Tok->setFinalizedType(TT_FunctionDeclarationName);
38433850
LineIsFunctionDeclaration = true;
@@ -4447,7 +4454,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
44474454
if (Left.Tok.isLiteral())
44484455
return true;
44494456
// for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4450-
if (Left.isTypeOrIdentifier(IsCpp) && Right.Next && Right.Next->Next &&
4457+
if (Left.isTypeOrIdentifier(LangOpts) && Right.Next && Right.Next->Next &&
44514458
Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
44524459
return getTokenPointerOrReferenceAlignment(Right) !=
44534460
FormatStyle::PAS_Left;
@@ -4490,7 +4497,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
44904497
if (Right.is(tok::l_brace) && Right.is(BK_Block))
44914498
return true;
44924499
// for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4493-
if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(IsCpp) && Right.Next &&
4500+
if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) && Right.Next &&
44944501
Right.Next->is(TT_RangeBasedForLoopColon)) {
44954502
return getTokenPointerOrReferenceAlignment(Left) !=
44964503
FormatStyle::PAS_Right;
@@ -4534,7 +4541,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
45344541
if (Right.isPointerOrReference()) {
45354542
const FormatToken *Previous = &Left;
45364543
while (Previous && Previous->isNot(tok::kw_operator)) {
4537-
if (Previous->is(tok::identifier) || Previous->isTypeName(IsCpp)) {
4544+
if (Previous->is(tok::identifier) || Previous->isTypeName(LangOpts)) {
45384545
Previous = Previous->getPreviousNonComment();
45394546
continue;
45404547
}
@@ -4723,7 +4730,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
47234730
if (!Style.isVerilog() &&
47244731
(Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
47254732
tok::r_paren) ||
4726-
Left.isTypeName(IsCpp)) &&
4733+
Left.isTypeName(LangOpts)) &&
47274734
Right.is(tok::l_brace) && Right.getNextNonComment() &&
47284735
Right.isNot(BK_Block)) {
47294736
return false;

clang/lib/Format/TokenAnnotator.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ class AnnotatedLine {
211211
class TokenAnnotator {
212212
public:
213213
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
214-
: Style(Style), IsCpp(Style.isCpp()), Keywords(Keywords) {}
214+
: Style(Style), IsCpp(Style.isCpp()),
215+
LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) {
216+
assert(IsCpp == LangOpts.CXXOperatorNames);
217+
}
215218

216219
/// Adapts the indent levels of comment lines to the indent of the
217220
/// subsequent line.
@@ -260,6 +263,7 @@ class TokenAnnotator {
260263
const FormatStyle &Style;
261264

262265
bool IsCpp;
266+
LangOptions LangOpts;
263267

264268
const AdditionalKeywords &Keywords;
265269

0 commit comments

Comments
 (0)