diff options
| author | Daniel Jasper <djasper@google.com> | 2017-06-19 07:40:49 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2017-06-19 07:40:49 +0000 |
| commit | 6a7d5a7a22a6522e0bfb7e60b3a15acb2eed2f60 (patch) | |
| tree | e31da3c516b5de6dabbba11f72a2b56b23beb387 /clang/unittests/Format/FormatTest.cpp | |
| parent | 271dd0603e4ddb63e1c7cc140459cb42fe13a2ac (diff) | |
| download | bcm5719-llvm-6a7d5a7a22a6522e0bfb7e60b3a15acb2eed2f60.tar.gz bcm5719-llvm-6a7d5a7a22a6522e0bfb7e60b3a15acb2eed2f60.zip | |
clang-format: Handle "if constexpr".
c++1z adds the following constructions to the language:
if constexpr (cond)
statement1;
else if constexpr (cond)
statement2;
else if constexpr (cond)
statement3;
else
statement4;
A first version of this was proposed in reviews.llvm.org/D26953 by
Francis Visoiu Mistrih, but never commited. This patch additionally
fixes the behavior when allowing short if statements on a single line
and was authored by Jacob Bandes-Storch. Thank you to both authors.
llvm-svn: 305666
Diffstat (limited to 'clang/unittests/Format/FormatTest.cpp')
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index f4937171038..e6cd617abd8 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -341,6 +341,18 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { verifyFormat("if (true)\n f();\ng();"); verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); verifyFormat("if (a)\n if (b) {\n f();\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 @@ TEST_F(FormatTest, FormatShortBracedStatements) { 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 @@ TEST_F(FormatTest, ParseIfElse) { "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 @@ TEST_F(FormatTest, ElseIf) { " 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 @@ TEST_F(FormatTest, ElseIf) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" "}", getLLVMStyleWithColumns(62)); + verifyFormat("if (a) {\n" + "} else if constexpr (\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" + "}", + getLLVMStyleWithColumns(62)); } TEST_F(FormatTest, FormatsForLoop) { @@ -2506,6 +2544,9 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) { verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" " cccccc) {\n}"); + verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" + " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" + " cccccc) {\n}"); verifyFormat("b = a &&\n" " // Comment\n" " b.c && d;"); @@ -6627,6 +6668,10 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { " 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 (aaaaaaaaaa) return 1; \\\n" @@ -6638,6 +6683,11 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { " return 1; \\\n" " return 2;", ShortMergedIf); + verifyFormat("#define A \\\n" + " if constexpr (aaaaaaa) \\\n" + " return 1; \\\n" + " return 2;", + ShortMergedIf); } TEST_F(FormatTest, FormatStarDependingOnContext) { @@ -8847,11 +8897,24 @@ TEST_F(FormatTest, AllmanBraceBreaking) { 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); verifyFormat("void f(bool b)\n" "{\n" + " if constexpr (b) return;\n" + "}\n", + BreakBeforeBraceShortIfs); + verifyFormat("void f(bool b)\n" + "{\n" " while (b)\n" " {\n" " return;\n" @@ -10098,6 +10161,11 @@ TEST_F(FormatTest, FormatsLambdas) { " doo_dah();\n" " })) {\n" "}"); + verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" + " doo_dah();\n" + " doo_dah();\n" + " })) {\n" + "}"); verifyFormat("auto lambda = []() {\n" " int a = 2\n" "#if A\n" |

