[cfe-commits] [PATCH] Clang-format: parse for and while loops
Alexander Kornienko
alexfh at google.com
Wed Dec 5 06:24:50 PST 2012
Hi djasper, klimek,
Adds support for formatting for and while loops.
http://llvm-reviews.chandlerc.com/D174
Files:
lib/Format/UnwrappedLineParser.cpp
lib/Format/UnwrappedLineParser.h
unittests/Format/FormatTest.cpp
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -115,6 +115,10 @@
case tok::kw_if:
parseIfThenElse();
return;
+ case tok::kw_for:
+ case tok::kw_while:
+ parseForOrWhileLoop();
+ return;
case tok::kw_do:
parseDoWhile();
return;
@@ -213,6 +217,22 @@
}
}
+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();
Index: lib/Format/UnwrappedLineParser.h
===================================================================
--- lib/Format/UnwrappedLineParser.h
+++ lib/Format/UnwrappedLineParser.h
@@ -92,6 +92,7 @@
void parseStatement();
void parseParens();
void parseIfThenElse();
+ void parseForOrWhileLoop();
void parseDoWhile();
void parseLabel();
void parseCaseLabel();
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -81,11 +81,26 @@
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) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D174.1.patch
Type: text/x-patch
Size: 2283 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121205/3f206a97/attachment.bin>
More information about the cfe-commits
mailing list