Skip to content

Commit ec90bc0

Browse files
committed
[clang-format][NFC] Clean up the unwrapped line parser
Change the signatures of parseBlock(), parseLevel(), and parseStructuralElement() to support combining else and if when removing braces. See #55663. Differential Revision: https://reviews.llvm.org/D127005
1 parent 63e3035 commit ec90bc0

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ void UnwrappedLineParser::parseFile() {
395395
if (Style.Language == FormatStyle::LK_TextProto)
396396
parseBracedList();
397397
else
398-
parseLevel(/*OpeningBrace=*/nullptr, /*CanContainBracedList=*/true);
398+
parseLevel();
399399
// Make sure to format the remaining tokens.
400400
//
401401
// LK_TextProto is special since its top-level is parsed as the body of a
@@ -469,12 +469,13 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() const {
469469
/// \param CanContainBracedList If the content can contain (at any level) a
470470
/// braced list.
471471
/// \param NextLBracesType The type for left brace found in this level.
472+
/// \param IfKind The if statement kind in the level.
472473
/// \returns true if a simple block of if/else/for/while, or false otherwise.
473474
/// (A simple block has a single statement.)
474475
bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
475476
bool CanContainBracedList,
476-
IfStmtKind *IfKind,
477-
TokenType NextLBracesType) {
477+
TokenType NextLBracesType,
478+
IfStmtKind *IfKind) {
478479
auto NextLevelLBracesType = NextLBracesType == TT_CompoundRequirementLBrace
479480
? TT_BracedListLBrace
480481
: TT_Unknown;
@@ -484,6 +485,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
484485
bool HasLabel = false;
485486
unsigned StatementCount = 0;
486487
bool SwitchLabelEncountered = false;
488+
487489
do {
488490
if (FormatTok->getType() == TT_AttributeMacro) {
489491
nextToken();
@@ -495,9 +497,9 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
495497
else if (FormatTok->getType() == TT_MacroBlockEnd)
496498
kind = tok::r_brace;
497499

498-
auto ParseDefault = [this, OpeningBrace, IfKind, NextLevelLBracesType,
500+
auto ParseDefault = [this, OpeningBrace, NextLevelLBracesType, IfKind,
499501
&HasDoWhile, &HasLabel, &StatementCount] {
500-
parseStructuralElement(IfKind, !OpeningBrace, NextLevelLBracesType,
502+
parseStructuralElement(!OpeningBrace, NextLevelLBracesType, IfKind,
501503
HasDoWhile ? nullptr : &HasDoWhile,
502504
HasLabel ? nullptr : &HasLabel);
503505
++StatementCount;
@@ -524,7 +526,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
524526
continue;
525527
}
526528
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
527-
/*MunchSemi=*/true, /*KeepBraces=*/true,
529+
/*MunchSemi=*/true, /*KeepBraces=*/true, /*IfKind=*/nullptr,
528530
/*UnindentWhitesmithsBraces=*/false, CanContainBracedList,
529531
NextLBracesType);
530532
++StatementCount;
@@ -593,6 +595,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
593595
break;
594596
}
595597
} while (!eof());
598+
596599
return false;
597600
}
598601

@@ -815,10 +818,12 @@ bool UnwrappedLineParser::mightFitOnOneLine(
815818
return Line.Level * Style.IndentWidth + Length <= ColumnLimit;
816819
}
817820

818-
UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
819-
bool MustBeDeclaration, unsigned AddLevels, bool MunchSemi, bool KeepBraces,
820-
bool UnindentWhitesmithsBraces, bool CanContainBracedList,
821-
TokenType NextLBracesType) {
821+
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels,
822+
bool MunchSemi, bool KeepBraces,
823+
IfStmtKind *IfKind,
824+
bool UnindentWhitesmithsBraces,
825+
bool CanContainBracedList,
826+
TokenType NextLBracesType) {
822827
assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
823828
"'{' or macro block token expected");
824829
FormatToken *Tok = FormatTok;
@@ -859,18 +864,17 @@ UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
859864
if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths)
860865
Line->Level += AddLevels;
861866

862-
IfStmtKind IfKind = IfStmtKind::NotIf;
863867
const bool SimpleBlock =
864-
parseLevel(Tok, CanContainBracedList, &IfKind, NextLBracesType);
868+
parseLevel(Tok, CanContainBracedList, NextLBracesType, IfKind);
865869

866870
if (eof())
867-
return IfKind;
871+
return;
868872

869873
if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
870874
: !FormatTok->is(tok::r_brace)) {
871875
Line->Level = InitialLevel;
872876
FormatTok->setBlockKind(BK_Block);
873-
return IfKind;
877+
return;
874878
}
875879

876880
auto RemoveBraces = [=]() mutable {
@@ -935,8 +939,6 @@ UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
935939
CurrentLines->size() - 1;
936940
}
937941
}
938-
939-
return IfKind;
940942
}
941943

942944
static bool isGoogScope(const UnwrappedLine &Line) {
@@ -1010,8 +1012,7 @@ void UnwrappedLineParser::parseChildBlock(
10101012
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
10111013
/*MustBeDeclaration=*/false);
10121014
Line->Level += SkipIndent ? 0 : 1;
1013-
parseLevel(OpeningBrace, CanContainBracedList, /*IfKind=*/nullptr,
1014-
NextLBracesType);
1015+
parseLevel(OpeningBrace, CanContainBracedList, NextLBracesType);
10151016
flushComments(isOnNewLine(*FormatTok));
10161017
Line->Level -= SkipIndent ? 0 : 1;
10171018
}
@@ -1414,9 +1415,9 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
14141415
}
14151416
}
14161417

1417-
void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
1418-
bool IsTopLevel,
1418+
void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel,
14191419
TokenType NextLBracesType,
1420+
IfStmtKind *IfKind,
14201421
bool *HasDoWhile,
14211422
bool *HasLabel) {
14221423
if (Style.Language == FormatStyle::LK_TableGen &&
@@ -2543,8 +2544,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
25432544
if (FormatTok->is(tok::kw_consteval)) {
25442545
nextToken();
25452546
} else {
2546-
if (Style.RemoveBracesLLVM)
2547-
KeepIfBraces = KeepBraces;
2547+
KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces;
25482548
if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier))
25492549
nextToken();
25502550
if (FormatTok->is(tok::l_paren))
@@ -2562,8 +2562,8 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
25622562
FormatTok->setFinalizedType(TT_ControlStatementLBrace);
25632563
IfLeftBrace = FormatTok;
25642564
CompoundStatementIndenter Indenter(this, Style, Line->Level);
2565-
IfBlockKind = parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2566-
/*MunchSemi=*/true, KeepIfBraces);
2565+
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2566+
/*MunchSemi=*/true, KeepIfBraces, &IfBlockKind);
25672567
if (Style.BraceWrapping.BeforeElse)
25682568
addUnwrappedLine();
25692569
else
@@ -2595,9 +2595,9 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
25952595
FormatTok->setFinalizedType(TT_ElseLBrace);
25962596
ElseLeftBrace = FormatTok;
25972597
CompoundStatementIndenter Indenter(this, Style, Line->Level);
2598-
const IfStmtKind ElseBlockKind =
2599-
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2600-
/*MunchSemi=*/true, KeepElseBraces);
2598+
IfStmtKind ElseBlockKind;
2599+
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2600+
/*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind);
26012601
if ((ElseBlockKind == IfStmtKind::IfOnly ||
26022602
ElseBlockKind == IfStmtKind::IfElseIf) &&
26032603
FormatTok->is(tok::kw_else)) {
@@ -2626,8 +2626,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
26262626
parseUnbracedBody(/*CheckEOF=*/true);
26272627
}
26282628
} else {
2629-
if (Style.RemoveBracesLLVM)
2630-
KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
2629+
KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
26312630
if (NeedsUnwrappedLine)
26322631
addUnwrappedLine();
26332632
}
@@ -2796,7 +2795,8 @@ void UnwrappedLineParser::parseNamespace() {
27962795
++Line->Level;
27972796

27982797
parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true,
2799-
/*KeepBraces=*/true, ManageWhitesmithsBraces);
2798+
/*KeepBraces=*/true, /*IfKind=*/nullptr,
2799+
ManageWhitesmithsBraces);
28002800

28012801
// Munch the semicolon after a namespace. This is more common than one would
28022802
// think. Putting the semicolon into its own line is very ugly.
@@ -3612,7 +3612,7 @@ void UnwrappedLineParser::parseJavaEnumBody() {
36123612
}
36133613

36143614
// Parse the class body after the enum's ";" if any.
3615-
parseLevel(OpeningBrace, /*CanContainBracedList=*/true);
3615+
parseLevel(OpeningBrace);
36163616
nextToken();
36173617
--Line->Level;
36183618
addUnwrappedLine();

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,18 @@ class UnwrappedLineParser {
9292
void reset();
9393
void parseFile();
9494
bool precededByCommentOrPPDirective() const;
95-
bool parseLevel(const FormatToken *OpeningBrace, bool CanContainBracedList,
96-
IfStmtKind *IfKind = nullptr,
97-
TokenType NextLBracesType = TT_Unknown);
95+
bool parseLevel(const FormatToken *OpeningBrace = nullptr,
96+
bool CanContainBracedList = true,
97+
TokenType NextLBracesType = TT_Unknown,
98+
IfStmtKind *IfKind = nullptr);
9899
bool mightFitOnOneLine(UnwrappedLine &Line,
99100
const FormatToken *OpeningBrace = nullptr) const;
100-
IfStmtKind parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u,
101-
bool MunchSemi = true, bool KeepBraces = true,
102-
bool UnindentWhitesmithsBraces = false,
103-
bool CanContainBracedList = true,
104-
TokenType NextLBracesType = TT_Unknown);
101+
void parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u,
102+
bool MunchSemi = true, bool KeepBraces = true,
103+
IfStmtKind *IfKind = nullptr,
104+
bool UnindentWhitesmithsBraces = false,
105+
bool CanContainBracedList = true,
106+
TokenType NextLBracesType = TT_Unknown);
105107
void parseChildBlock(bool CanContainBracedList = true,
106108
TokenType NextLBracesType = TT_Unknown);
107109
void parsePPDirective();
@@ -112,9 +114,9 @@ class UnwrappedLineParser {
112114
void parsePPEndIf();
113115
void parsePPUnknown();
114116
void readTokenWithJavaScriptASI();
115-
void parseStructuralElement(IfStmtKind *IfKind = nullptr,
116-
bool IsTopLevel = false,
117+
void parseStructuralElement(bool IsTopLevel = false,
117118
TokenType NextLBracesType = TT_Unknown,
119+
IfStmtKind *IfKind = nullptr,
118120
bool *HasDoWhile = nullptr,
119121
bool *HasLabel = nullptr);
120122
bool tryToParseBracedList();

0 commit comments

Comments
 (0)