sstwcw updated this revision to Diff 419009. sstwcw retitled this revision from "[clang-format] Take out common code for parsing blocks" to "[clang-format] Take out common code for parsing blocks NFC".
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D121757/new/ https://reviews.llvm.org/D121757 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/lib/Format/UnwrappedLineParser.h Index: clang/lib/Format/UnwrappedLineParser.h =================================================================== --- clang/lib/Format/UnwrappedLineParser.h +++ clang/lib/Format/UnwrappedLineParser.h @@ -123,6 +123,7 @@ void parseUnbracedBody(bool CheckEOF = false); FormatToken *parseIfThenElse(IfStmtKind *IfKind, bool KeepBraces = false); void parseTryCatch(); + void parseLoopBody(bool TryRemoveBraces, bool WrapRightBrace); void parseForOrWhileLoop(); void parseDoWhile(); void parseLabel(bool LeftAlignLabel = false); Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -2699,6 +2699,29 @@ } while (!eof()); } +void UnwrappedLineParser::parseLoopBody(bool TryRemoveBraces, + bool WrapRightBrace) { + keepAncestorBraces(); + + if (FormatTok->is(tok::l_brace)) { + CompoundStatementIndenter Indenter(this, Style, Line->Level); + FormatToken *LeftBrace = FormatTok; + parseBlock(); + if (TryRemoveBraces) { + assert(!NestedTooDeep.empty()); + if (!NestedTooDeep.back()) + markOptionalBraces(LeftBrace); + } + if (WrapRightBrace) + addUnwrappedLine(); + } else { + parseUnbracedBody(); + } + + if (TryRemoveBraces) + NestedTooDeep.pop_back(); +} + void UnwrappedLineParser::parseForOrWhileLoop() { assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && "'for', 'while' or foreach macro expected"); @@ -2717,43 +2740,15 @@ parseParens(); } - keepAncestorBraces(); - - if (FormatTok->is(tok::l_brace)) { - FormatToken *LeftBrace = FormatTok; - CompoundStatementIndenter Indenter(this, Style, Line->Level); - parseBlock(); - if (Style.RemoveBracesLLVM) { - assert(!NestedTooDeep.empty()); - if (!NestedTooDeep.back()) - markOptionalBraces(LeftBrace); - } - addUnwrappedLine(); - } else { - parseUnbracedBody(); - } - - if (Style.RemoveBracesLLVM) - NestedTooDeep.pop_back(); + parseLoopBody(/*TryRemoveBraces=*/Style.RemoveBracesLLVM, + /*WrapRightBrace=*/true); } void UnwrappedLineParser::parseDoWhile() { assert(FormatTok->is(tok::kw_do) && "'do' expected"); nextToken(); - keepAncestorBraces(); - - if (FormatTok->is(tok::l_brace)) { - CompoundStatementIndenter Indenter(this, Style, Line->Level); - parseBlock(); - if (Style.BraceWrapping.BeforeWhile) - addUnwrappedLine(); - } else { - parseUnbracedBody(); - } - - if (Style.RemoveBracesLLVM) - NestedTooDeep.pop_back(); + parseLoopBody(/*BracesAreoptional=*/false, Style.BraceWrapping.BeforeWhile); // FIXME: Add error handling. if (!FormatTok->is(tok::kw_while)) {
Index: clang/lib/Format/UnwrappedLineParser.h =================================================================== --- clang/lib/Format/UnwrappedLineParser.h +++ clang/lib/Format/UnwrappedLineParser.h @@ -123,6 +123,7 @@ void parseUnbracedBody(bool CheckEOF = false); FormatToken *parseIfThenElse(IfStmtKind *IfKind, bool KeepBraces = false); void parseTryCatch(); + void parseLoopBody(bool TryRemoveBraces, bool WrapRightBrace); void parseForOrWhileLoop(); void parseDoWhile(); void parseLabel(bool LeftAlignLabel = false); Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -2699,6 +2699,29 @@ } while (!eof()); } +void UnwrappedLineParser::parseLoopBody(bool TryRemoveBraces, + bool WrapRightBrace) { + keepAncestorBraces(); + + if (FormatTok->is(tok::l_brace)) { + CompoundStatementIndenter Indenter(this, Style, Line->Level); + FormatToken *LeftBrace = FormatTok; + parseBlock(); + if (TryRemoveBraces) { + assert(!NestedTooDeep.empty()); + if (!NestedTooDeep.back()) + markOptionalBraces(LeftBrace); + } + if (WrapRightBrace) + addUnwrappedLine(); + } else { + parseUnbracedBody(); + } + + if (TryRemoveBraces) + NestedTooDeep.pop_back(); +} + void UnwrappedLineParser::parseForOrWhileLoop() { assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && "'for', 'while' or foreach macro expected"); @@ -2717,43 +2740,15 @@ parseParens(); } - keepAncestorBraces(); - - if (FormatTok->is(tok::l_brace)) { - FormatToken *LeftBrace = FormatTok; - CompoundStatementIndenter Indenter(this, Style, Line->Level); - parseBlock(); - if (Style.RemoveBracesLLVM) { - assert(!NestedTooDeep.empty()); - if (!NestedTooDeep.back()) - markOptionalBraces(LeftBrace); - } - addUnwrappedLine(); - } else { - parseUnbracedBody(); - } - - if (Style.RemoveBracesLLVM) - NestedTooDeep.pop_back(); + parseLoopBody(/*TryRemoveBraces=*/Style.RemoveBracesLLVM, + /*WrapRightBrace=*/true); } void UnwrappedLineParser::parseDoWhile() { assert(FormatTok->is(tok::kw_do) && "'do' expected"); nextToken(); - keepAncestorBraces(); - - if (FormatTok->is(tok::l_brace)) { - CompoundStatementIndenter Indenter(this, Style, Line->Level); - parseBlock(); - if (Style.BraceWrapping.BeforeWhile) - addUnwrappedLine(); - } else { - parseUnbracedBody(); - } - - if (Style.RemoveBracesLLVM) - NestedTooDeep.pop_back(); + parseLoopBody(/*BracesAreoptional=*/false, Style.BraceWrapping.BeforeWhile); // FIXME: Add error handling. if (!FormatTok->is(tok::kw_while)) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits