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
This commit is contained in:
Alexander Kornienko 2012-12-05 15:06:06 +00:00
parent aa1c920db8
commit 37d6c94e28
3 changed files with 37 additions and 1 deletions

View File

@ -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();

View File

@ -92,6 +92,7 @@ private:
void parseStatement();
void parseParens();
void parseIfThenElse();
void parseForOrWhileLoop();
void parseDoWhile();
void parseLabel();
void parseCaseLabel();

View File

@ -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) {