jtbandes created this revision.
Herald added a subscriber: klimek.
Changes to handle `if constexpr` the same way as `if`.
https://reviews.llvm.org/D34330
Files:
lib/Format/ContinuationIndenter.cpp
lib/Format/TokenAnnotator.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp
Index: unittests/Format/FormatTest.cpp
===
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -341,6 +341,18 @@
verifyFormat("if (true)\n f();\ng();");
verifyFormat("if (a)\n if (b)\nif (c)\n g();\nh();");
verifyFormat("if (a)\n if (b) {\nf();\n }\ng();");
+ verifyFormat("if constexpr (true)\n"
+ " f();\ng();");
+ verifyFormat("if constexpr (a)\n"
+ " if constexpr (b)\n"
+ "if constexpr (c)\n"
+ " g();\n"
+ "h();");
+ verifyFormat("if constexpr (a)\n"
+ " if constexpr (b) {\n"
+ "f();\n"
+ " }\n"
+ "g();");
FormatStyle AllowsMergedIf = getLLVMStyle();
AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
@@ -423,9 +435,11 @@
AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
verifyFormat("if (true) {}", AllowSimpleBracedStatements);
+ verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
verifyFormat("while (true) {}", AllowSimpleBracedStatements);
verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
+ verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
verifyFormat("if (true) { //\n"
@@ -496,6 +510,19 @@
"else {\n"
" i();\n"
"}");
+ verifyFormat("if (true)\n"
+ " if constexpr (true)\n"
+ "if (true) {\n"
+ " if constexpr (true)\n"
+ "f();\n"
+ "} else {\n"
+ " g();\n"
+ "}\n"
+ " else\n"
+ "h();\n"
+ "else {\n"
+ " i();\n"
+ "}");
verifyFormat("void f() {\n"
" if (a) {\n"
" } else {\n"
@@ -511,6 +538,12 @@
" g();\n"
"else\n"
" h();");
+ verifyFormat("if constexpr (a)\n"
+ " f();\n"
+ "else if constexpr (b)\n"
+ " g();\n"
+ "else\n"
+ " h();");
verifyFormat("if (a) {\n"
" f();\n"
"}\n"
@@ -528,6 +561,11 @@
"a) {\n"
"}",
getLLVMStyleWithColumns(62));
+ verifyFormat("if (a) {\n"
+ "} else if constexpr (\n"
+ "a) {\n"
+ "}",
+ getLLVMStyleWithColumns(62));
}
TEST_F(FormatTest, FormatsForLoop) {
@@ -2371,6 +2409,9 @@
verifyFormat("if ((aa ||\n"
" bb) && // \n"
"cc) {\n}");
+ verifyFormat("if constexpr ((aa ||\n"
+ " bb) && // aaa\n"
+ " cc) {\n}");
verifyFormat("b = a &&\n"
"// Comment\n"
"b.c && d;");
@@ -6492,6 +6533,10 @@
" if (true) continue;\n"
"}",
ShortMergedIf);
+ ShortMergedIf.ColumnLimit = 33;
+ verifyFormat("#define A \\\n"
+ " if constexpr (true) return 42;",
+ ShortMergedIf);
ShortMergedIf.ColumnLimit = 29;
verifyFormat("#define A \\\n"
" if (aa) return 1; \\\n"
@@ -6503,6 +6548,11 @@
"return 1; \\\n"
" return 2;",
ShortMergedIf);
+ verifyFormat("#define A\\\n"
+ " if constexpr (aaa) \\\n"
+ "return 1;\\\n"
+ " return 2;",
+ ShortMergedIf);
}
TEST_F(FormatTest, FormatStarDependingOnContext) {
@@ -8712,11 +8762,24 @@
BreakBeforeBraceShortIfs);
verifyFormat("void f(bool b)\n"
"{\n"
+ " if constexpr (b)\n"
+ " {\n"
+ "return;\n"
+ " }\n"
+ "}\n",
+ BreakBeforeBraceShortIfs);
+ verifyFormat("void f(bool b)\n"
+ "{\n"
" if (b) return;\n"
"}\n",
BreakBeforeBraceShortIfs);