@@ -934,9 +934,6 @@ FormatToken *UnwrappedLineParser::parseBlock(
934934 return IfLBrace;
935935 }
936936
937- Tok->MatchingParen = FormatTok;
938- FormatTok->MatchingParen = Tok;
939-
940937 const bool IsFunctionRBrace =
941938 FormatTok->is (tok::r_brace) && Tok->is (TT_FunctionLBrace);
942939
@@ -970,7 +967,10 @@ FormatToken *UnwrappedLineParser::parseBlock(
970967 }
971968 return mightFitOnOneLine ((*CurrentLines)[Index], Tok);
972969 };
973- Tok->Optional = RemoveBraces ();
970+ if (RemoveBraces ()) {
971+ Tok->MatchingParen = FormatTok;
972+ FormatTok->MatchingParen = Tok;
973+ }
974974
975975 size_t PPEndHash = computePPHash ();
976976
@@ -2707,20 +2707,10 @@ static void markOptionalBraces(FormatToken *LeftBrace) {
27072707
27082708 assert (RightBrace->is (tok::r_brace));
27092709 assert (RightBrace->MatchingParen == LeftBrace);
2710+ assert (LeftBrace->Optional == RightBrace->Optional );
27102711
2711- RightBrace->Optional = LeftBrace->Optional ;
2712- }
2713-
2714- static void resetOptional (FormatToken *LeftBrace) {
2715- if (!LeftBrace)
2716- return ;
2717-
2718- const auto *RightBrace = LeftBrace->MatchingParen ;
2719- const bool IsOptionalRightBrace = RightBrace && RightBrace->Optional ;
2720- assert (LeftBrace->Optional || !IsOptionalRightBrace);
2721-
2722- if (!IsOptionalRightBrace)
2723- LeftBrace->Optional = false ;
2712+ LeftBrace->Optional = true ;
2713+ RightBrace->Optional = true ;
27242714}
27252715
27262716void UnwrappedLineParser::handleAttributes () {
@@ -2788,7 +2778,8 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
27882778
27892779 if (Style.RemoveBracesLLVM ) {
27902780 assert (!NestedTooDeep.empty ());
2791- KeepIfBraces = KeepIfBraces || (IfLeftBrace && !IfLeftBrace->Optional ) ||
2781+ KeepIfBraces = KeepIfBraces ||
2782+ (IfLeftBrace && !IfLeftBrace->MatchingParen ) ||
27922783 NestedTooDeep.back () || IfBlockKind == IfStmtKind::IfOnly ||
27932784 IfBlockKind == IfStmtKind::IfElseIf;
27942785 }
@@ -2819,9 +2810,8 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
28192810 ElseBlockKind == IfStmtKind::IfElseIf;
28202811 } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional ) {
28212812 KeepElseBraces = true ;
2822- assert (ElseLeftBrace->Optional );
28232813 assert (ElseLeftBrace->MatchingParen );
2824- ElseLeftBrace-> MatchingParen -> Optional = true ;
2814+ markOptionalBraces ( ElseLeftBrace) ;
28252815 }
28262816 addUnwrappedLine ();
28272817 } else if (FormatTok->is (tok::kw_if)) {
@@ -2856,19 +2846,25 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
28562846
28572847 assert (!NestedTooDeep.empty ());
28582848 KeepElseBraces = KeepElseBraces ||
2859- (ElseLeftBrace && !ElseLeftBrace->Optional ) ||
2849+ (ElseLeftBrace && !ElseLeftBrace->MatchingParen ) ||
28602850 NestedTooDeep.back ();
28612851
28622852 NestedTooDeep.pop_back ();
28632853
28642854 if (!KeepIfBraces && !KeepElseBraces) {
28652855 markOptionalBraces (IfLeftBrace);
28662856 markOptionalBraces (ElseLeftBrace);
2857+ } else if (IfLeftBrace) {
2858+ FormatToken *IfRightBrace = IfLeftBrace->MatchingParen ;
2859+ if (IfRightBrace) {
2860+ assert (IfRightBrace->MatchingParen == IfLeftBrace);
2861+ assert (!IfLeftBrace->Optional );
2862+ assert (!IfRightBrace->Optional );
2863+ IfLeftBrace->MatchingParen = nullptr ;
2864+ IfRightBrace->MatchingParen = nullptr ;
2865+ }
28672866 }
28682867
2869- resetOptional (IfLeftBrace);
2870- resetOptional (ElseLeftBrace);
2871-
28722868 if (IfKind)
28732869 *IfKind = Kind;
28742870
@@ -3083,7 +3079,6 @@ void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
30833079 if (!NestedTooDeep.back ())
30843080 markOptionalBraces (LeftBrace);
30853081 }
3086- resetOptional (LeftBrace);
30873082 if (WrapRightBrace)
30883083 addUnwrappedLine ();
30893084 } else {
0 commit comments