diff options
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 20 | ||||
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.h | 1 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 17 |
3 files changed, 37 insertions, 1 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index c2cb5b8027b..99e58321cd7 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -121,6 +121,10 @@ void UnwrappedLineParser::parseStatement() { case tok::kw_if: parseIfThenElse(); return; + case tok::kw_for: + case tok::kw_while: + parseForOrWhileLoop(); + return; case tok::kw_do: parseDoWhile(); return; @@ -219,6 +223,22 @@ void UnwrappedLineParser::parseIfThenElse() { } } +void UnwrappedLineParser::parseForOrWhileLoop() { + assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && + "'for' or 'while' expected"); + nextToken(); + parseParens(); + if (FormatTok.Tok.is(tok::l_brace)) { + parseBlock(); + addUnwrappedLine(); + } else { + addUnwrappedLine(); + ++Line.Level; + parseStatement(); + --Line.Level; + } +} + void UnwrappedLineParser::parseDoWhile() { assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); nextToken(); diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index 6e9d8723867..3fbc73fd75b 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -92,6 +92,7 @@ private: void parseStatement(); void parseParens(); void parseIfThenElse(); + void parseForOrWhileLoop(); void parseDoWhile(); void parseLabel(); void parseCaseLabel(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c9860d595ce..05a6d334f55 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -81,11 +81,26 @@ TEST_F(FormatTest, FormatsNestedBlockStatements) { TEST_F(FormatTest, FormatsForLoop) { verifyFormat( "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" - " ++VeryVeryLongLoopVariable);"); + " ++VeryVeryLongLoopVariable)\n" + " ;"); + verifyFormat("for (;;)\n" + " f();"); + verifyFormat("for (;;) {\n" + "}"); + verifyFormat("for (;;) {\n" + " f();\n" + "}"); } TEST_F(FormatTest, FormatsWhileLoop) { verifyFormat("while (true) {\n}"); + verifyFormat("while (true)\n" + " f();"); + verifyFormat("while () {\n" + "}"); + verifyFormat("while () {\n" + " f();\n" + "}"); } TEST_F(FormatTest, FormatsNestedCall) { |