diff options
| author | Alexander Kornienko <alexfh@google.com> | 2012-12-05 15:06:06 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2012-12-05 15:06:06 +0000 |
| commit | 37d6c94e28653f37611d268acca79644c6ecde9d (patch) | |
| tree | 4c4b6e6e8063f1117ec19d04a1f732ea4794e313 | |
| parent | aa1c920db85e7ac07d09b80eedc66bae7e358f26 (diff) | |
| download | bcm5719-llvm-37d6c94e28653f37611d268acca79644c6ecde9d.tar.gz bcm5719-llvm-37d6c94e28653f37611d268acca79644c6ecde9d.zip | |
Clang-format: parse for and while loops
Summary: Adds support for formatting for and while loops.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D174
llvm-svn: 169387
| -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) { |

