@@ -640,6 +640,14 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
640640 FormatTok = Tokens->setPosition (StoredPosition);
641641}
642642
643+ // Sets the token type of the directly previous right brace.
644+ void UnwrappedLineParser::setPreviousRBraceType (TokenType Type) {
645+ if (auto Prev = FormatTok->getPreviousNonComment ();
646+ Prev && Prev->is (tok::r_brace)) {
647+ Prev->setFinalizedType (Type);
648+ }
649+ }
650+
643651template <class T >
644652static inline void hash_combine (std::size_t &seed, const T &v) {
645653 std::hash<T> hasher;
@@ -2756,6 +2764,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
27562764 CompoundStatementIndenter Indenter (this , Style, Line->Level );
27572765 parseBlock (/* MustBeDeclaration=*/ false , /* AddLevels=*/ 1u ,
27582766 /* MunchSemi=*/ true , KeepIfBraces, &IfBlockKind);
2767+ setPreviousRBraceType (TT_ControlStatementRBrace);
27592768 if (Style.BraceWrapping .BeforeElse )
27602769 addUnwrappedLine ();
27612770 else
@@ -2794,6 +2803,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
27942803 FormatToken *IfLBrace =
27952804 parseBlock (/* MustBeDeclaration=*/ false , /* AddLevels=*/ 1u ,
27962805 /* MunchSemi=*/ true , KeepElseBraces, &ElseBlockKind);
2806+ setPreviousRBraceType (TT_ElseRBrace);
27972807 if (FormatTok->is (tok::kw_else)) {
27982808 KeepElseBraces = KeepElseBraces ||
27992809 ElseBlockKind == IfStmtKind::IfOnly ||
@@ -3057,12 +3067,12 @@ void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
30573067 keepAncestorBraces ();
30583068
30593069 if (isBlockBegin (*FormatTok)) {
3060- if (!KeepBraces)
3061- FormatTok->setFinalizedType (TT_ControlStatementLBrace);
3070+ FormatTok->setFinalizedType (TT_ControlStatementLBrace);
30623071 FormatToken *LeftBrace = FormatTok;
30633072 CompoundStatementIndenter Indenter (this , Style, Line->Level );
30643073 parseBlock (/* MustBeDeclaration=*/ false , /* AddLevels=*/ 1u ,
30653074 /* MunchSemi=*/ true , KeepBraces);
3075+ setPreviousRBraceType (TT_ControlStatementRBrace);
30663076 if (!KeepBraces) {
30673077 assert (!NestedTooDeep.empty ());
30683078 if (!NestedTooDeep.back ())
@@ -3196,7 +3206,9 @@ void UnwrappedLineParser::parseSwitch() {
31963206
31973207 if (FormatTok->is (tok::l_brace)) {
31983208 CompoundStatementIndenter Indenter (this , Style, Line->Level );
3209+ FormatTok->setFinalizedType (TT_ControlStatementLBrace);
31993210 parseBlock ();
3211+ setPreviousRBraceType (TT_ControlStatementRBrace);
32003212 addUnwrappedLine ();
32013213 } else {
32023214 addUnwrappedLine ();
@@ -3713,10 +3725,7 @@ bool UnwrappedLineParser::parseEnum() {
37133725 nextToken ();
37143726 addUnwrappedLine ();
37153727 }
3716- if (auto Prev = FormatTok->getPreviousNonComment ();
3717- Prev && Prev->is (tok::r_brace)) {
3718- Prev->setFinalizedType (TT_EnumRBrace);
3719- }
3728+ setPreviousRBraceType (TT_EnumRBrace);
37203729 return true ;
37213730
37223731 // There is no addUnwrappedLine() here so that we fall through to parsing a
@@ -3950,10 +3959,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
39503959 unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u ;
39513960 parseBlock (/* MustBeDeclaration=*/ true , AddLevels, /* MunchSemi=*/ false );
39523961 }
3953- if (auto Prev = FormatTok->getPreviousNonComment ();
3954- Prev && Prev->is (tok::r_brace)) {
3955- Prev->setFinalizedType (ClosingBraceType);
3956- }
3962+ setPreviousRBraceType (ClosingBraceType);
39573963 }
39583964 // There is no addUnwrappedLine() here so that we fall through to parsing a
39593965 // structural element afterwards. Thus, in "class A {} n, m;",
0 commit comments