Skip to content

Commit 051e910

Browse files
authored
[clang-format][NFC] Replace Style.isCpp() with IsCpp (#83533)
1 parent 8ec28af commit 051e910

File tree

5 files changed

+45
-46
lines changed

5 files changed

+45
-46
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,8 @@ struct FormatToken {
821821

822822
/// Returns whether the token is the left square bracket of a C++
823823
/// structured binding declaration.
824-
bool isCppStructuredBinding(const FormatStyle &Style) const {
825-
if (!Style.isCpp() || isNot(tok::l_square))
824+
bool isCppStructuredBinding(bool IsCpp) const {
825+
if (!IsCpp || isNot(tok::l_square))
826826
return false;
827827
const FormatToken *T = this;
828828
do {

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class AnnotatingParser {
126126
const AdditionalKeywords &Keywords,
127127
SmallVector<ScopeType> &Scopes)
128128
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
129-
Keywords(Keywords), Scopes(Scopes) {
129+
IsCpp(Style.isCpp()), Keywords(Keywords), Scopes(Scopes) {
130130
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
131131
resetTokenMetadata();
132132
}
@@ -676,26 +676,25 @@ class AnnotatingParser {
676676
// In C++, this can happen either in array of templates (foo<int>[10])
677677
// or when array is a nested template type (unique_ptr<type1<type2>[]>).
678678
bool CppArrayTemplates =
679-
Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
679+
IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
680680
(Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
681681
Contexts.back().ContextType == Context::TemplateArgument);
682682

683683
const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
684684
const bool IsCpp11AttributeSpecifier =
685-
isCppAttribute(Style.isCpp(), *Left) || IsInnerSquare;
685+
isCppAttribute(IsCpp, *Left) || IsInnerSquare;
686686

687687
// Treat C# Attributes [STAThread] much like C++ attributes [[...]].
688688
bool IsCSharpAttributeSpecifier =
689689
isCSharpAttributeSpecifier(*Left) ||
690690
Contexts.back().InCSharpAttributeSpecifier;
691691

692692
bool InsideInlineASM = Line.startsWith(tok::kw_asm);
693-
bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
693+
bool IsCppStructuredBinding = Left->isCppStructuredBinding(IsCpp);
694694
bool StartsObjCMethodExpr =
695695
!IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
696-
Style.isCpp() && !IsCpp11AttributeSpecifier &&
697-
!IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression &&
698-
Left->isNot(TT_LambdaLSquare) &&
696+
IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
697+
Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
699698
!CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
700699
(!Parent ||
701700
Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
@@ -723,7 +722,7 @@ class AnnotatingParser {
723722
Contexts.back().ContextKind == tok::l_brace &&
724723
Parent->isOneOf(tok::l_brace, tok::comma)) {
725724
Left->setType(TT_JsComputedPropertyName);
726-
} else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace &&
725+
} else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
727726
Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
728727
Left->setType(TT_DesignatedInitializerLSquare);
729728
} else if (IsCSharpAttributeSpecifier) {
@@ -1161,7 +1160,7 @@ class AnnotatingParser {
11611160
if (Previous->is(TT_JsTypeOptionalQuestion))
11621161
Previous = Previous->getPreviousNonComment();
11631162
if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1164-
(!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
1163+
(!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
11651164
Style.isProto()) {
11661165
OpeningBrace.setType(TT_DictLiteral);
11671166
if (Previous->Tok.getIdentifierInfo() ||
@@ -1230,7 +1229,7 @@ class AnnotatingParser {
12301229
}
12311230

12321231
bool consumeToken() {
1233-
if (Style.isCpp()) {
1232+
if (IsCpp) {
12341233
const auto *Prev = CurrentToken->getPreviousNonComment();
12351234
if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
12361235
CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
@@ -1424,7 +1423,7 @@ class AnnotatingParser {
14241423
if (CurrentToken && CurrentToken->is(Keywords.kw_await))
14251424
next();
14261425
}
1427-
if (Style.isCpp() && CurrentToken && CurrentToken->is(tok::kw_co_await))
1426+
if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
14281427
next();
14291428
Contexts.back().ColonIsForRangeExpr = true;
14301429
if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
@@ -2590,7 +2589,7 @@ class AnnotatingParser {
25902589
/// Determine whether '(' is starting a C++ cast.
25912590
bool lParenStartsCppCast(const FormatToken &Tok) {
25922591
// C-style casts are only used in C++.
2593-
if (!Style.isCpp())
2592+
if (!IsCpp)
25942593
return false;
25952594

25962595
FormatToken *LeftOfParens = Tok.getPreviousNonComment();
@@ -2611,10 +2610,8 @@ class AnnotatingParser {
26112610
/// Determine whether ')' is ending a cast.
26122611
bool rParenEndsCast(const FormatToken &Tok) {
26132612
// C-style casts are only used in C++, C# and Java.
2614-
if (!Style.isCSharp() && !Style.isCpp() &&
2615-
Style.Language != FormatStyle::LK_Java) {
2613+
if (!Style.isCSharp() && !IsCpp && Style.Language != FormatStyle::LK_Java)
26162614
return false;
2617-
}
26182615

26192616
// Empty parens aren't casts and there are no casts at the end of the line.
26202617
if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
@@ -2691,7 +2688,7 @@ class AnnotatingParser {
26912688
if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
26922689
tok::kw_requires, tok::kw_throw, tok::arrow,
26932690
Keywords.kw_override, Keywords.kw_final) ||
2694-
isCppAttribute(Style.isCpp(), *Tok.Next)) {
2691+
isCppAttribute(IsCpp, *Tok.Next)) {
26952692
return false;
26962693
}
26972694

@@ -3012,6 +3009,7 @@ class AnnotatingParser {
30123009
AnnotatedLine &Line;
30133010
FormatToken *CurrentToken;
30143011
bool AutoFound;
3012+
bool IsCpp;
30153013
const AdditionalKeywords &Keywords;
30163014

30173015
SmallVector<ScopeType> &Scopes;
@@ -3559,7 +3557,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
35593557
ExpressionParser ExprParser(Style, Keywords, Line);
35603558
ExprParser.parse();
35613559

3562-
if (Style.isCpp()) {
3560+
if (IsCpp) {
35633561
auto *Tok = getFunctionName(Line);
35643562
if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
35653563
Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
@@ -3766,7 +3764,6 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
37663764
if (AlignArrayOfStructures)
37673765
calculateArrayInitializerColumnList(Line);
37683766

3769-
const bool IsCpp = Style.isCpp();
37703767
bool SeenName = false;
37713768
bool LineIsFunctionDeclaration = false;
37723769
FormatToken *ClosingParen = nullptr;
@@ -3779,7 +3776,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
37793776
AfterLastAttribute = Tok;
37803777
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
37813778
IsCtorOrDtor ||
3782-
isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3779+
isFunctionDeclarationName(IsCpp, *Tok, Line, ClosingParen)) {
37833780
if (!IsCtorOrDtor)
37843781
Tok->setFinalizedType(TT_FunctionDeclarationName);
37853782
LineIsFunctionDeclaration = true;
@@ -4717,7 +4714,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
47174714
if (Left.is(tok::star) && Right.is(tok::comment))
47184715
return true;
47194716

4720-
if (Style.isCpp()) {
4717+
if (IsCpp) {
47214718
if (Left.is(TT_OverloadedOperator) &&
47224719
Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
47234720
return true;
@@ -5425,7 +5422,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54255422
if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
54265423
return true;
54275424
} else if (Style.BreakAdjacentStringLiterals &&
5428-
(Style.isCpp() || Style.isProto() ||
5425+
(IsCpp || Style.isProto() ||
54295426
Style.Language == FormatStyle::LK_TableGen)) {
54305427
if (Left.isStringLiteral() && Right.isStringLiteral())
54315428
return true;

clang/lib/Format/TokenAnnotator.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class AnnotatedLine {
212212
class TokenAnnotator {
213213
public:
214214
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
215-
: Style(Style), Keywords(Keywords) {}
215+
: Style(Style), IsCpp(Style.isCpp()), Keywords(Keywords) {}
216216

217217
/// Adapts the indent levels of comment lines to the indent of the
218218
/// subsequent line.
@@ -260,6 +260,8 @@ class TokenAnnotator {
260260

261261
const FormatStyle &Style;
262262

263+
bool IsCpp;
264+
263265
const AdditionalKeywords &Keywords;
264266

265267
SmallVector<ScopeType> Scopes;

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ UnwrappedLineParser::UnwrappedLineParser(
159159
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
160160
IdentifierTable &IdentTable)
161161
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
162-
CurrentLines(&Lines), Style(Style), Keywords(Keywords),
163-
CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
164-
Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
162+
CurrentLines(&Lines), Style(Style), IsCpp(Style.isCpp()),
163+
Keywords(Keywords), CommentPragmasRegex(Style.CommentPragmas),
164+
Tokens(nullptr), Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
165165
IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
166166
? IG_Rejected
167167
: IG_Inited),
@@ -572,8 +572,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
572572
(Style.isJavaScript() &&
573573
NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
574574
Keywords.kw_as));
575-
ProbablyBracedList = ProbablyBracedList ||
576-
(Style.isCpp() && NextTok->is(tok::l_paren));
575+
ProbablyBracedList =
576+
ProbablyBracedList || (IsCpp && NextTok->is(tok::l_paren));
577577

578578
// If there is a comma, semicolon or right paren after the closing
579579
// brace, we assume this is a braced initializer list.
@@ -1428,7 +1428,7 @@ void UnwrappedLineParser::parseStructuralElement(
14281428
return;
14291429
}
14301430

1431-
if (Style.isCpp()) {
1431+
if (IsCpp) {
14321432
while (FormatTok->is(tok::l_square) && handleCppAttributes()) {
14331433
}
14341434
} else if (Style.isVerilog()) {
@@ -1602,7 +1602,7 @@ void UnwrappedLineParser::parseStructuralElement(
16021602
parseJavaScriptEs6ImportExport();
16031603
return;
16041604
}
1605-
if (Style.isCpp()) {
1605+
if (IsCpp) {
16061606
nextToken();
16071607
if (FormatTok->is(tok::kw_namespace)) {
16081608
parseNamespace();
@@ -1646,24 +1646,23 @@ void UnwrappedLineParser::parseStructuralElement(
16461646
addUnwrappedLine();
16471647
return;
16481648
}
1649-
if (Style.isCpp() && parseModuleImport())
1649+
if (IsCpp && parseModuleImport())
16501650
return;
16511651
}
1652-
if (Style.isCpp() &&
1653-
FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1654-
Keywords.kw_slots, Keywords.kw_qslots)) {
1652+
if (IsCpp && FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1653+
Keywords.kw_slots, Keywords.kw_qslots)) {
16551654
nextToken();
16561655
if (FormatTok->is(tok::colon)) {
16571656
nextToken();
16581657
addUnwrappedLine();
16591658
return;
16601659
}
16611660
}
1662-
if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1661+
if (IsCpp && FormatTok->is(TT_StatementMacro)) {
16631662
parseStatementMacro();
16641663
return;
16651664
}
1666-
if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)) {
1665+
if (IsCpp && FormatTok->is(TT_NamespaceMacro)) {
16671666
parseNamespace();
16681667
return;
16691668
}
@@ -1759,7 +1758,7 @@ void UnwrappedLineParser::parseStructuralElement(
17591758
}
17601759
break;
17611760
case tok::kw_requires: {
1762-
if (Style.isCpp()) {
1761+
if (IsCpp) {
17631762
bool ParsedClause = parseRequires();
17641763
if (ParsedClause)
17651764
return;
@@ -1780,7 +1779,7 @@ void UnwrappedLineParser::parseStructuralElement(
17801779
if (!parseEnum())
17811780
break;
17821781
// This only applies to C++ and Verilog.
1783-
if (!Style.isCpp() && !Style.isVerilog()) {
1782+
if (!IsCpp && !Style.isVerilog()) {
17841783
addUnwrappedLine();
17851784
return;
17861785
}
@@ -1848,7 +1847,7 @@ void UnwrappedLineParser::parseStructuralElement(
18481847
parseParens();
18491848
// Break the unwrapped line if a K&R C function definition has a parameter
18501849
// declaration.
1851-
if (OpeningBrace || !Style.isCpp() || !Previous || eof())
1850+
if (OpeningBrace || !IsCpp || !Previous || eof())
18521851
break;
18531852
if (isC78ParameterDecl(FormatTok,
18541853
Tokens->peekNextToken(/*SkipComment=*/true),
@@ -1977,13 +1976,13 @@ void UnwrappedLineParser::parseStructuralElement(
19771976
}
19781977
}
19791978

1980-
if (!Style.isCpp() && FormatTok->is(Keywords.kw_interface)) {
1979+
if (!IsCpp && FormatTok->is(Keywords.kw_interface)) {
19811980
if (parseStructLike())
19821981
return;
19831982
break;
19841983
}
19851984

1986-
if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1985+
if (IsCpp && FormatTok->is(TT_StatementMacro)) {
19871986
parseStatementMacro();
19881987
return;
19891988
}
@@ -2211,7 +2210,7 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() {
22112210

22122211
bool UnwrappedLineParser::tryToParseLambda() {
22132212
assert(FormatTok->is(tok::l_square));
2214-
if (!Style.isCpp()) {
2213+
if (!IsCpp) {
22152214
nextToken();
22162215
return false;
22172216
}
@@ -2340,7 +2339,7 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
23402339
!Previous->isOneOf(tok::kw_return, tok::kw_co_await,
23412340
tok::kw_co_yield, tok::kw_co_return)) ||
23422341
Previous->closesScope())) ||
2343-
LeftSquare->isCppStructuredBinding(Style)) {
2342+
LeftSquare->isCppStructuredBinding(IsCpp)) {
23442343
return false;
23452344
}
23462345
if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind()))
@@ -3153,7 +3152,7 @@ void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) {
31533152
// JS' for await ( ...
31543153
if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await))
31553154
nextToken();
3156-
if (Style.isCpp() && FormatTok->is(tok::kw_co_await))
3155+
if (IsCpp && FormatTok->is(tok::kw_co_await))
31573156
nextToken();
31583157
if (HasParens && FormatTok->is(tok::l_paren)) {
31593158
// The type is only set for Verilog basically because we were afraid to
@@ -3737,7 +3736,7 @@ bool UnwrappedLineParser::parseEnum() {
37373736
nextToken();
37383737
// If there are two identifiers in a row, this is likely an elaborate
37393738
// return type. In Java, this can be "implements", etc.
3740-
if (Style.isCpp() && FormatTok->is(tok::identifier))
3739+
if (IsCpp && FormatTok->is(tok::identifier))
37413740
return false;
37423741
}
37433742
}

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ class UnwrappedLineParser {
324324
llvm::BitVector DeclarationScopeStack;
325325

326326
const FormatStyle &Style;
327+
bool IsCpp;
327328
const AdditionalKeywords &Keywords;
328329

329330
llvm::Regex CommentPragmasRegex;

0 commit comments

Comments
 (0)