Skip to content

Commit 1fadb2b

Browse files
committed
Revert "[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)"
This reverts commits e62ce1f, 5cd2804, and de641e2 due to buildbot failures.
1 parent f841ca0 commit 1fadb2b

13 files changed

+134
-119
lines changed

clang/lib/Format/Format.cpp

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

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

clang/lib/Format/FormatToken.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,59 @@ 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+
3774
// Sorted common C++ non-keyword types.
3875
static SmallVector<StringRef> CppNonKeywordTypes = {
3976
"clock_t", "int16_t", "int32_t", "int64_t", "int8_t",
4077
"intptr_t", "ptrdiff_t", "size_t", "time_t", "uint16_t",
4178
"uint32_t", "uint64_t", "uint8_t", "uintptr_t",
4279
};
4380

44-
bool FormatToken::isTypeName(const LangOptions &LangOpts) const {
45-
const bool IsCpp = LangOpts.CXXOperatorNames;
46-
return is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts) ||
81+
bool FormatToken::isTypeName(bool IsCpp) const {
82+
return is(TT_TypeName) || isSimpleTypeSpecifier() ||
4783
(IsCpp && is(tok::identifier) &&
4884
std::binary_search(CppNonKeywordTypes.begin(),
4985
CppNonKeywordTypes.end(), TokenText));
5086
}
5187

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

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

clang/lib/Format/FormatToken.h

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

687-
[[nodiscard]] bool isTypeName(const LangOptions &LangOpts) const;
688-
[[nodiscard]] bool isTypeOrIdentifier(const LangOptions &LangOpts) const;
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;
689693

690694
bool isObjCAccessSpecifier() const {
691695
return is(tok::at) && Next &&

clang/lib/Format/FormatTokenLexer.cpp

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

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

clang/lib/Format/QualifierAlignmentFixer.cpp

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

271+
const bool IsCpp = Style.isCpp();
272+
271273
// The case `const long long int volatile` -> `long long int const volatile`
272274
// The case `long const long int volatile` -> `long long int const volatile`
273275
// The case `long long volatile int const` -> `long long int const volatile`
274276
// The case `const long long volatile int` -> `long long int const volatile`
275-
if (TypeToken->isTypeName(LangOpts)) {
277+
if (TypeToken->isTypeName(IsCpp)) {
276278
// The case `const decltype(foo)` -> `const decltype(foo)`
277279
// The case `const typeof(foo)` -> `const typeof(foo)`
278280
// The case `const _Atomic(foo)` -> `const _Atomic(foo)`
@@ -281,7 +283,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
281283

282284
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
283285
while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(),
284-
LangOpts)) {
286+
IsCpp)) {
285287
LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment();
286288
}
287289

@@ -293,7 +295,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
293295
// The case `unsigned short const` -> `unsigned short const`
294296
// The case:
295297
// `unsigned short volatile const` -> `unsigned short const volatile`
296-
if (PreviousCheck && PreviousCheck->isTypeName(LangOpts)) {
298+
if (PreviousCheck && PreviousCheck->isTypeName(IsCpp)) {
297299
if (LastQual != Tok)
298300
rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
299301
return Tok;
@@ -410,11 +412,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
410412
// The case `volatile long long const int` -> `const volatile long long int`
411413
// The case `const long long volatile int` -> `const volatile long long int`
412414
// The case `long volatile long int const` -> `const volatile long long int`
413-
if (TypeToken->isTypeName(LangOpts)) {
415+
if (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) {
414416
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
415417
while (isConfiguredQualifierOrType(
416418
LastSimpleTypeSpecifier->getPreviousNonComment(),
417-
ConfiguredQualifierTokens, LangOpts)) {
419+
ConfiguredQualifierTokens, IsCpp)) {
418420
LastSimpleTypeSpecifier =
419421
LastSimpleTypeSpecifier->getPreviousNonComment();
420422
}
@@ -612,16 +614,16 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
612614
}
613615
}
614616

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));
617+
bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken *Tok,
618+
bool IsCpp) {
619+
return Tok &&
620+
(Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok));
619621
}
620622

621623
bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
622624
const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers,
623-
const LangOptions &LangOpts) {
624-
return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) ||
625+
bool IsCpp) {
626+
return Tok && (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) ||
625627
isConfiguredQualifier(Tok, Qualifiers));
626628
}
627629

clang/lib/Format/QualifierAlignmentFixer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,11 @@ 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,
75-
const LangOptions &LangOpts);
74+
static bool isQualifierOrType(const FormatToken *Tok, bool IsCpp = true);
7675
static bool
7776
isConfiguredQualifierOrType(const FormatToken *Tok,
7877
const std::vector<tok::TokenKind> &Qualifiers,
79-
const LangOptions &LangOpts);
78+
bool IsCpp = true);
8079

8180
// Is the Token likely a Macro
8281
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), LangOpts(getFormattingLangOpts(Style)), Env(Env),
87+
: Style(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(LangOpts);
104+
IdentifierTable IdentTable(getFormattingLangOpts(Style));
105105
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
106106
Env.getFirstStartColumn(), Style, Encoding, Allocator,
107107
IdentTable);

clang/lib/Format/TokenAnalyzer.h

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

9494
FormatStyle Style;
95-
LangOptions LangOpts;
9695
// Stores Style, FileID and SourceManager etc.
9796
const Environment &Env;
9897
// AffectedRangeMgr stores ranges to be fixed.

clang/lib/Format/TokenAnnotator.cpp

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

26282626
// MyClass a;
2629-
if (PreviousNotConst->isTypeName(LangOpts))
2627+
if (PreviousNotConst->isTypeName(IsCpp))
26302628
return true;
26312629

26322630
// type[] a in Java
@@ -2730,7 +2728,7 @@ class AnnotatingParser {
27302728
}
27312729

27322730
if (Tok.Next->is(tok::question) ||
2733-
(Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(LangOpts))) {
2731+
(Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(IsCpp))) {
27342732
return false;
27352733
}
27362734

@@ -2759,10 +2757,9 @@ class AnnotatingParser {
27592757
}
27602758

27612759
// Heuristically try to determine whether the parentheses contain a type.
2762-
auto IsQualifiedPointerOrReference = [](FormatToken *T,
2763-
const LangOptions &LangOpts) {
2760+
auto IsQualifiedPointerOrReference = [](FormatToken *T, bool IsCpp) {
27642761
// This is used to handle cases such as x = (foo *const)&y;
2765-
assert(!T->isTypeName(LangOpts) && "Should have already been checked");
2762+
assert(!T->isTypeName(IsCpp) && "Should have already been checked");
27662763
// Strip trailing qualifiers such as const or volatile when checking
27672764
// whether the parens could be a cast to a pointer/reference type.
27682765
while (T) {
@@ -2794,8 +2791,8 @@ class AnnotatingParser {
27942791
bool ParensAreType =
27952792
!Tok.Previous ||
27962793
Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2797-
Tok.Previous->isTypeName(LangOpts) ||
2798-
IsQualifiedPointerOrReference(Tok.Previous, LangOpts);
2794+
Tok.Previous->isTypeName(IsCpp) ||
2795+
IsQualifiedPointerOrReference(Tok.Previous, IsCpp);
27992796
bool ParensCouldEndDecl =
28002797
Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
28012798
if (ParensAreType && !ParensCouldEndDecl)
@@ -3068,7 +3065,6 @@ class AnnotatingParser {
30683065
FormatToken *CurrentToken;
30693066
bool AutoFound;
30703067
bool IsCpp;
3071-
LangOptions LangOpts;
30723068
const AdditionalKeywords &Keywords;
30733069

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

36443640
// This function heuristically determines whether 'Current' starts the name of a
36453641
// function declaration.
3646-
static bool isFunctionDeclarationName(const LangOptions &LangOpts,
3647-
const FormatToken &Current,
3642+
static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
36483643
const AnnotatedLine &Line,
36493644
FormatToken *&ClosingParen) {
36503645
assert(Current.Previous);
@@ -3663,7 +3658,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
36633658
}
36643659

36653660
auto skipOperatorName =
3666-
[&LangOpts](const FormatToken *Next) -> const FormatToken * {
3661+
[IsCpp](const FormatToken *Next) -> const FormatToken * {
36673662
for (; Next; Next = Next->Next) {
36683663
if (Next->is(TT_OverloadedOperatorLParen))
36693664
return Next;
@@ -3682,7 +3677,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
36823677
Next = Next->Next;
36833678
continue;
36843679
}
3685-
if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3680+
if ((Next->isTypeName(IsCpp) || Next->is(tok::identifier)) &&
36863681
Next->Next && Next->Next->isPointerOrReference()) {
36873682
// For operator void*(), operator char*(), operator Foo*().
36883683
Next = Next->Next;
@@ -3698,10 +3693,8 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
36983693
return nullptr;
36993694
};
37003695

3701-
const auto *Next = Current.Next;
3702-
const bool IsCpp = LangOpts.CXXOperatorNames;
3703-
37043696
// Find parentheses of parameter list.
3697+
const FormatToken *Next = Current.Next;
37053698
if (Current.is(tok::kw_operator)) {
37063699
if (Previous.Tok.getIdentifierInfo() &&
37073700
!Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
@@ -3781,7 +3774,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
37813774
Tok = Tok->MatchingParen;
37823775
continue;
37833776
}
3784-
if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3777+
if (Tok->is(tok::kw_const) || Tok->isTypeName(IsCpp) ||
37853778
Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
37863779
return true;
37873780
}
@@ -3844,7 +3837,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
38443837
AfterLastAttribute = Tok;
38453838
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
38463839
IsCtorOrDtor ||
3847-
isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {
3840+
isFunctionDeclarationName(IsCpp, *Tok, Line, ClosingParen)) {
38483841
if (!IsCtorOrDtor)
38493842
Tok->setFinalizedType(TT_FunctionDeclarationName);
38503843
LineIsFunctionDeclaration = true;
@@ -4454,7 +4447,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
44544447
if (Left.Tok.isLiteral())
44554448
return true;
44564449
// for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4457-
if (Left.isTypeOrIdentifier(LangOpts) && Right.Next && Right.Next->Next &&
4450+
if (Left.isTypeOrIdentifier(IsCpp) && Right.Next && Right.Next->Next &&
44584451
Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
44594452
return getTokenPointerOrReferenceAlignment(Right) !=
44604453
FormatStyle::PAS_Left;
@@ -4497,7 +4490,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
44974490
if (Right.is(tok::l_brace) && Right.is(BK_Block))
44984491
return true;
44994492
// for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4500-
if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) && Right.Next &&
4493+
if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(IsCpp) && Right.Next &&
45014494
Right.Next->is(TT_RangeBasedForLoopColon)) {
45024495
return getTokenPointerOrReferenceAlignment(Left) !=
45034496
FormatStyle::PAS_Right;
@@ -4541,7 +4534,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
45414534
if (Right.isPointerOrReference()) {
45424535
const FormatToken *Previous = &Left;
45434536
while (Previous && Previous->isNot(tok::kw_operator)) {
4544-
if (Previous->is(tok::identifier) || Previous->isTypeName(LangOpts)) {
4537+
if (Previous->is(tok::identifier) || Previous->isTypeName(IsCpp)) {
45454538
Previous = Previous->getPreviousNonComment();
45464539
continue;
45474540
}
@@ -4730,7 +4723,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
47304723
if (!Style.isVerilog() &&
47314724
(Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
47324725
tok::r_paren) ||
4733-
Left.isTypeName(LangOpts)) &&
4726+
Left.isTypeName(IsCpp)) &&
47344727
Right.is(tok::l_brace) && Right.getNextNonComment() &&
47354728
Right.isNot(BK_Block)) {
47364729
return false;

clang/lib/Format/TokenAnnotator.h

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

219216
/// Adapts the indent levels of comment lines to the indent of the
220217
/// subsequent line.
@@ -263,7 +260,6 @@ class TokenAnnotator {
263260
const FormatStyle &Style;
264261

265262
bool IsCpp;
266-
LangOptions LangOpts;
267263

268264
const AdditionalKeywords &Keywords;
269265

0 commit comments

Comments
 (0)