return IfLBrace;
}
+ Tok->MatchingParen = FormatTok;
+ FormatTok->MatchingParen = Tok;
+
const bool IsFunctionRBrace =
FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
}
return mightFitOnOneLine((*CurrentLines)[Index], Tok);
};
- if (RemoveBraces()) {
- Tok->MatchingParen = FormatTok;
- FormatTok->MatchingParen = Tok;
- }
+ Tok->Optional = RemoveBraces();
size_t PPEndHash = computePPHash();
assert(RightBrace->is(tok::r_brace));
assert(RightBrace->MatchingParen == LeftBrace);
- assert(LeftBrace->Optional == RightBrace->Optional);
- LeftBrace->Optional = true;
- RightBrace->Optional = true;
+ RightBrace->Optional = LeftBrace->Optional;
+}
+
+static void resetOptional(FormatToken *LeftBrace) {
+ if (!LeftBrace)
+ return;
+
+ const auto *RightBrace = LeftBrace->MatchingParen;
+ const bool IsOptionalRightBrace = RightBrace && RightBrace->Optional;
+ assert(LeftBrace->Optional || !IsOptionalRightBrace);
+
+ if (!IsOptionalRightBrace)
+ LeftBrace->Optional = false;
}
void UnwrappedLineParser::handleAttributes() {
if (Style.RemoveBracesLLVM) {
assert(!NestedTooDeep.empty());
- KeepIfBraces = KeepIfBraces ||
- (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
+ KeepIfBraces = KeepIfBraces || (IfLeftBrace && !IfLeftBrace->Optional) ||
NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
IfBlockKind == IfStmtKind::IfElseIf;
}
ElseBlockKind == IfStmtKind::IfElseIf;
} else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
KeepElseBraces = true;
+ assert(ElseLeftBrace->Optional);
assert(ElseLeftBrace->MatchingParen);
- markOptionalBraces(ElseLeftBrace);
+ ElseLeftBrace->MatchingParen->Optional = true;
}
addUnwrappedLine();
} else if (FormatTok->is(tok::kw_if)) {
assert(!NestedTooDeep.empty());
KeepElseBraces = KeepElseBraces ||
- (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
+ (ElseLeftBrace && !ElseLeftBrace->Optional) ||
NestedTooDeep.back();
NestedTooDeep.pop_back();
if (!KeepIfBraces && !KeepElseBraces) {
markOptionalBraces(IfLeftBrace);
markOptionalBraces(ElseLeftBrace);
- } else if (IfLeftBrace) {
- FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
- if (IfRightBrace) {
- assert(IfRightBrace->MatchingParen == IfLeftBrace);
- assert(!IfLeftBrace->Optional);
- assert(!IfRightBrace->Optional);
- IfLeftBrace->MatchingParen = nullptr;
- IfRightBrace->MatchingParen = nullptr;
- }
}
+ resetOptional(IfLeftBrace);
+ resetOptional(ElseLeftBrace);
+
if (IfKind)
*IfKind = Kind;
if (!NestedTooDeep.back())
markOptionalBraces(LeftBrace);
}
+ resetOptional(LeftBrace);
if (WrapRightBrace)
addUnwrappedLine();
} else {