@@ -395,7 +395,7 @@ void UnwrappedLineParser::parseFile() {
395
395
if (Style.Language == FormatStyle::LK_TextProto)
396
396
parseBracedList ();
397
397
else
398
- parseLevel (/* OpeningBrace= */ nullptr , /* CanContainBracedList= */ true );
398
+ parseLevel ();
399
399
// Make sure to format the remaining tokens.
400
400
//
401
401
// LK_TextProto is special since its top-level is parsed as the body of a
@@ -469,12 +469,13 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() const {
469
469
// / \param CanContainBracedList If the content can contain (at any level) a
470
470
// / braced list.
471
471
// / \param NextLBracesType The type for left brace found in this level.
472
+ // / \param IfKind The if statement kind in the level.
472
473
// / \returns true if a simple block of if/else/for/while, or false otherwise.
473
474
// / (A simple block has a single statement.)
474
475
bool UnwrappedLineParser::parseLevel (const FormatToken *OpeningBrace,
475
476
bool CanContainBracedList,
476
- IfStmtKind *IfKind ,
477
- TokenType NextLBracesType ) {
477
+ TokenType NextLBracesType ,
478
+ IfStmtKind *IfKind ) {
478
479
auto NextLevelLBracesType = NextLBracesType == TT_CompoundRequirementLBrace
479
480
? TT_BracedListLBrace
480
481
: TT_Unknown;
@@ -484,6 +485,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
484
485
bool HasLabel = false ;
485
486
unsigned StatementCount = 0 ;
486
487
bool SwitchLabelEncountered = false ;
488
+
487
489
do {
488
490
if (FormatTok->getType () == TT_AttributeMacro) {
489
491
nextToken ();
@@ -495,9 +497,9 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
495
497
else if (FormatTok->getType () == TT_MacroBlockEnd)
496
498
kind = tok::r_brace;
497
499
498
- auto ParseDefault = [this , OpeningBrace, IfKind, NextLevelLBracesType ,
500
+ auto ParseDefault = [this , OpeningBrace, NextLevelLBracesType, IfKind ,
499
501
&HasDoWhile, &HasLabel, &StatementCount] {
500
- parseStructuralElement (IfKind, !OpeningBrace, NextLevelLBracesType,
502
+ parseStructuralElement (!OpeningBrace, NextLevelLBracesType, IfKind ,
501
503
HasDoWhile ? nullptr : &HasDoWhile,
502
504
HasLabel ? nullptr : &HasLabel);
503
505
++StatementCount;
@@ -524,7 +526,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
524
526
continue ;
525
527
}
526
528
parseBlock (/* MustBeDeclaration=*/ false , /* AddLevels=*/ 1u ,
527
- /* MunchSemi=*/ true , /* KeepBraces=*/ true ,
529
+ /* MunchSemi=*/ true , /* KeepBraces=*/ true , /* IfKind= */ nullptr ,
528
530
/* UnindentWhitesmithsBraces=*/ false , CanContainBracedList,
529
531
NextLBracesType);
530
532
++StatementCount;
@@ -593,6 +595,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
593
595
break ;
594
596
}
595
597
} while (!eof ());
598
+
596
599
return false ;
597
600
}
598
601
@@ -815,10 +818,12 @@ bool UnwrappedLineParser::mightFitOnOneLine(
815
818
return Line.Level * Style.IndentWidth + Length <= ColumnLimit;
816
819
}
817
820
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) {
822
827
assert (FormatTok->isOneOf (tok::l_brace, TT_MacroBlockBegin) &&
823
828
" '{' or macro block token expected" );
824
829
FormatToken *Tok = FormatTok;
@@ -859,18 +864,17 @@ UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
859
864
if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths)
860
865
Line->Level += AddLevels;
861
866
862
- IfStmtKind IfKind = IfStmtKind::NotIf;
863
867
const bool SimpleBlock =
864
- parseLevel (Tok, CanContainBracedList, &IfKind, NextLBracesType );
868
+ parseLevel (Tok, CanContainBracedList, NextLBracesType, IfKind );
865
869
866
870
if (eof ())
867
- return IfKind ;
871
+ return ;
868
872
869
873
if (MacroBlock ? !FormatTok->is (TT_MacroBlockEnd)
870
874
: !FormatTok->is (tok::r_brace)) {
871
875
Line->Level = InitialLevel;
872
876
FormatTok->setBlockKind (BK_Block);
873
- return IfKind ;
877
+ return ;
874
878
}
875
879
876
880
auto RemoveBraces = [=]() mutable {
@@ -935,8 +939,6 @@ UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
935
939
CurrentLines->size () - 1 ;
936
940
}
937
941
}
938
-
939
- return IfKind;
940
942
}
941
943
942
944
static bool isGoogScope (const UnwrappedLine &Line) {
@@ -1010,8 +1012,7 @@ void UnwrappedLineParser::parseChildBlock(
1010
1012
ScopedDeclarationState DeclarationState (*Line, DeclarationScopeStack,
1011
1013
/* MustBeDeclaration=*/ false );
1012
1014
Line->Level += SkipIndent ? 0 : 1 ;
1013
- parseLevel (OpeningBrace, CanContainBracedList, /* IfKind=*/ nullptr ,
1014
- NextLBracesType);
1015
+ parseLevel (OpeningBrace, CanContainBracedList, NextLBracesType);
1015
1016
flushComments (isOnNewLine (*FormatTok));
1016
1017
Line->Level -= SkipIndent ? 0 : 1 ;
1017
1018
}
@@ -1414,9 +1415,9 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
1414
1415
}
1415
1416
}
1416
1417
1417
- void UnwrappedLineParser::parseStructuralElement (IfStmtKind *IfKind,
1418
- bool IsTopLevel,
1418
+ void UnwrappedLineParser::parseStructuralElement (bool IsTopLevel,
1419
1419
TokenType NextLBracesType,
1420
+ IfStmtKind *IfKind,
1420
1421
bool *HasDoWhile,
1421
1422
bool *HasLabel) {
1422
1423
if (Style.Language == FormatStyle::LK_TableGen &&
@@ -2543,8 +2544,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2543
2544
if (FormatTok->is (tok::kw_consteval)) {
2544
2545
nextToken ();
2545
2546
} else {
2546
- if (Style.RemoveBracesLLVM )
2547
- KeepIfBraces = KeepBraces;
2547
+ KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces;
2548
2548
if (FormatTok->isOneOf (tok::kw_constexpr, tok::identifier))
2549
2549
nextToken ();
2550
2550
if (FormatTok->is (tok::l_paren))
@@ -2562,8 +2562,8 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2562
2562
FormatTok->setFinalizedType (TT_ControlStatementLBrace);
2563
2563
IfLeftBrace = FormatTok;
2564
2564
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 );
2567
2567
if (Style.BraceWrapping .BeforeElse )
2568
2568
addUnwrappedLine ();
2569
2569
else
@@ -2595,9 +2595,9 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2595
2595
FormatTok->setFinalizedType (TT_ElseLBrace);
2596
2596
ElseLeftBrace = FormatTok;
2597
2597
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 );
2601
2601
if ((ElseBlockKind == IfStmtKind::IfOnly ||
2602
2602
ElseBlockKind == IfStmtKind::IfElseIf) &&
2603
2603
FormatTok->is (tok::kw_else)) {
@@ -2626,8 +2626,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2626
2626
parseUnbracedBody (/* CheckEOF=*/ true );
2627
2627
}
2628
2628
} else {
2629
- if (Style.RemoveBracesLLVM )
2630
- KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
2629
+ KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
2631
2630
if (NeedsUnwrappedLine)
2632
2631
addUnwrappedLine ();
2633
2632
}
@@ -2796,7 +2795,8 @@ void UnwrappedLineParser::parseNamespace() {
2796
2795
++Line->Level ;
2797
2796
2798
2797
parseBlock (/* MustBeDeclaration=*/ true , AddLevels, /* MunchSemi=*/ true ,
2799
- /* KeepBraces=*/ true , ManageWhitesmithsBraces);
2798
+ /* KeepBraces=*/ true , /* IfKind=*/ nullptr ,
2799
+ ManageWhitesmithsBraces);
2800
2800
2801
2801
// Munch the semicolon after a namespace. This is more common than one would
2802
2802
// think. Putting the semicolon into its own line is very ugly.
@@ -3612,7 +3612,7 @@ void UnwrappedLineParser::parseJavaEnumBody() {
3612
3612
}
3613
3613
3614
3614
// Parse the class body after the enum's ";" if any.
3615
- parseLevel (OpeningBrace, /* CanContainBracedList= */ true );
3615
+ parseLevel (OpeningBrace);
3616
3616
nextToken ();
3617
3617
--Line->Level ;
3618
3618
addUnwrappedLine ();
0 commit comments