diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -125,6 +125,7 @@ bool handleCppAttributes(); FormatToken *parseIfThenElse(IfStmtKind *IfKind, bool KeepBraces = false); void parseTryCatch(); + void parseLoopBody(bool TryRemoveBraces, bool WrapRightBrace); void parseForOrWhileLoop(); void parseDoWhile(); void parseLabel(bool LeftAlignLabel = false); diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2713,55 +2713,49 @@ } while (!eof()); } -void UnwrappedLineParser::parseForOrWhileLoop() { - assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && - "'for', 'while' or foreach macro expected"); - nextToken(); - // JS' for await ( ... - if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await)) - nextToken(); - if (Style.isCpp() && FormatTok->is(tok::kw_co_await)) - nextToken(); - if (FormatTok->is(tok::l_paren)) - parseParens(); - +void UnwrappedLineParser::parseLoopBody(bool TryRemoveBraces, + bool WrapRightBrace) { keepAncestorBraces(); if (FormatTok->is(tok::l_brace)) { FormatToken *LeftBrace = FormatTok; CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(); - if (Style.RemoveBracesLLVM) { + if (TryRemoveBraces) { assert(!NestedTooDeep.empty()); if (!NestedTooDeep.back()) markOptionalBraces(LeftBrace); } - addUnwrappedLine(); + if (WrapRightBrace) + addUnwrappedLine(); } else { parseUnbracedBody(); } - if (Style.RemoveBracesLLVM) + if (TryRemoveBraces) NestedTooDeep.pop_back(); } -void UnwrappedLineParser::parseDoWhile() { - assert(FormatTok->is(tok::kw_do) && "'do' expected"); +void UnwrappedLineParser::parseForOrWhileLoop() { + assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && + "'for', 'while' or foreach macro expected"); nextToken(); + // JS' for await ( ... + if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await)) + nextToken(); + if (Style.isCpp() && FormatTok->is(tok::kw_co_await)) + nextToken(); + if (FormatTok->is(tok::l_paren)) + parseParens(); - keepAncestorBraces(); + parseLoopBody(Style.RemoveBracesLLVM, true); +} - if (FormatTok->is(tok::l_brace)) { - CompoundStatementIndenter Indenter(this, Style, Line->Level); - parseBlock(); - if (Style.BraceWrapping.BeforeWhile) - addUnwrappedLine(); - } else { - parseUnbracedBody(); - } +void UnwrappedLineParser::parseDoWhile() { + assert(FormatTok->is(tok::kw_do) && "'do' expected"); + nextToken(); - if (Style.RemoveBracesLLVM) - NestedTooDeep.pop_back(); + parseLoopBody(false, Style.BraceWrapping.BeforeWhile); // FIXME: Add error handling. if (!FormatTok->is(tok::kw_while)) {