summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Format
diff options
context:
space:
mode:
Diffstat (limited to 'clang/unittests/Format')
-rw-r--r--clang/unittests/Format/FormatTest.cpp154
-rw-r--r--clang/unittests/Format/FormatTestObjC.cpp4
2 files changed, 151 insertions, 7 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d7759bc6df3..e982c8b2ab0 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -644,7 +644,8 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
FormatStyle::SIS_WithoutElse;
AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
- AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
+ AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
+ FormatStyle::BWACS_Always;
verifyFormat("if (true) {}", AllowSimpleBracedStatements);
verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
@@ -1168,7 +1169,7 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterCaseLabel = true;
- Style.BraceWrapping.AfterControlStatement = true;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
EXPECT_EQ("switch (n)\n"
"{\n"
" case 0:\n"
@@ -1370,7 +1371,7 @@ TEST_F(FormatTest, ShortCaseLabels) {
Style.AllowShortCaseLabelsOnASingleLine = true;
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterCaseLabel = true;
- Style.BraceWrapping.AfterControlStatement = true;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
EXPECT_EQ("switch (n)\n"
"{\n"
" case 0:\n"
@@ -1441,6 +1442,131 @@ TEST_F(FormatTest, FormatsLabels) {
"}");
}
+TEST_F(FormatTest, MultiLineControlStatements) {
+ FormatStyle Style = getLLVMStyle();
+ Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
+ Style.ColumnLimit = 20;
+ // Short lines should keep opening brace on same line.
+ EXPECT_EQ("if (foo) {\n"
+ " bar();\n"
+ "}",
+ format("if(foo){bar();}", Style));
+ EXPECT_EQ("if (foo) {\n"
+ " bar();\n"
+ "} else {\n"
+ " baz();\n"
+ "}",
+ format("if(foo){bar();}else{baz();}", Style));
+ EXPECT_EQ("if (foo && bar) {\n"
+ " baz();\n"
+ "}",
+ format("if(foo&&bar){baz();}", Style));
+ EXPECT_EQ("if (foo) {\n"
+ " bar();\n"
+ "} else if (baz) {\n"
+ " quux();\n"
+ "}",
+ format("if(foo){bar();}else if(baz){quux();}", Style));
+ EXPECT_EQ(
+ "if (foo) {\n"
+ " bar();\n"
+ "} else if (baz) {\n"
+ " quux();\n"
+ "} else {\n"
+ " foobar();\n"
+ "}",
+ format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
+ EXPECT_EQ("for (;;) {\n"
+ " foo();\n"
+ "}",
+ format("for(;;){foo();}"));
+ EXPECT_EQ("while (1) {\n"
+ " foo();\n"
+ "}",
+ format("while(1){foo();}", Style));
+ EXPECT_EQ("switch (foo) {\n"
+ "case bar:\n"
+ " return;\n"
+ "}",
+ format("switch(foo){case bar:return;}", Style));
+ EXPECT_EQ("try {\n"
+ " foo();\n"
+ "} catch (...) {\n"
+ " bar();\n"
+ "}",
+ format("try{foo();}catch(...){bar();}", Style));
+ EXPECT_EQ("do {\n"
+ " foo();\n"
+ "} while (bar &&\n"
+ " baz);",
+ format("do{foo();}while(bar&&baz);", Style));
+ // Long lines should put opening brace on new line.
+ EXPECT_EQ("if (foo && bar &&\n"
+ " baz)\n"
+ "{\n"
+ " quux();\n"
+ "}",
+ format("if(foo&&bar&&baz){quux();}", Style));
+ EXPECT_EQ("if (foo && bar &&\n"
+ " baz)\n"
+ "{\n"
+ " quux();\n"
+ "}",
+ format("if (foo && bar &&\n"
+ " baz) {\n"
+ " quux();\n"
+ "}",
+ Style));
+ EXPECT_EQ("if (foo) {\n"
+ " bar();\n"
+ "} else if (baz ||\n"
+ " quux)\n"
+ "{\n"
+ " foobar();\n"
+ "}",
+ format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
+ EXPECT_EQ(
+ "if (foo) {\n"
+ " bar();\n"
+ "} else if (baz ||\n"
+ " quux)\n"
+ "{\n"
+ " foobar();\n"
+ "} else {\n"
+ " barbaz();\n"
+ "}",
+ format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
+ Style));
+ EXPECT_EQ("for (int i = 0;\n"
+ " i < 10; ++i)\n"
+ "{\n"
+ " foo();\n"
+ "}",
+ format("for(int i=0;i<10;++i){foo();}", Style));
+ EXPECT_EQ("while (foo || bar ||\n"
+ " baz)\n"
+ "{\n"
+ " quux();\n"
+ "}",
+ format("while(foo||bar||baz){quux();}", Style));
+ EXPECT_EQ("switch (\n"
+ " foo = barbaz)\n"
+ "{\n"
+ "case quux:\n"
+ " return;\n"
+ "}",
+ format("switch(foo=barbaz){case quux:return;}", Style));
+ EXPECT_EQ("try {\n"
+ " foo();\n"
+ "} catch (\n"
+ " Exception &bar)\n"
+ "{\n"
+ " baz();\n"
+ "}",
+ format("try{foo();}catch(Exception&bar){baz();}", Style));
+}
+
//===----------------------------------------------------------------------===//
// Tests for classes, namespaces, etc.
//===----------------------------------------------------------------------===//
@@ -2940,7 +3066,7 @@ TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
"};"));
FormatStyle Style = getLLVMStyle();
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
- Style.BraceWrapping.AfterControlStatement = true;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
Style.BraceWrapping.AfterFunction = true;
EXPECT_EQ("void f()\n"
"try\n"
@@ -12261,7 +12387,6 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
- CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
@@ -12469,6 +12594,25 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
FormatStyle::BS_Custom);
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
+ CHECK_PARSE("BraceWrapping:\n"
+ " AfterControlStatement: MultiLine",
+ BraceWrapping.AfterControlStatement,
+ FormatStyle::BWACS_MultiLine);
+ CHECK_PARSE("BraceWrapping:\n"
+ " AfterControlStatement: Always",
+ BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
+ CHECK_PARSE("BraceWrapping:\n"
+ " AfterControlStatement: Never",
+ BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
+ // For backward compatibility:
+ CHECK_PARSE("BraceWrapping:\n"
+ " AfterControlStatement: true",
+ BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
+ CHECK_PARSE("BraceWrapping:\n"
+ " AfterControlStatement: false",
+ BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
+
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
FormatStyle::RTBS_None);
diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp
index 8fb93195881..063eb5f58f1 100644
--- a/clang/unittests/Format/FormatTestObjC.cpp
+++ b/clang/unittests/Format/FormatTestObjC.cpp
@@ -207,7 +207,7 @@ TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
" f();\n"
"}\n");
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
- Style.BraceWrapping.AfterControlStatement = true;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
verifyFormat("@autoreleasepool\n"
"{\n"
" f();\n"
@@ -237,7 +237,7 @@ TEST_F(FormatTestObjC, FormatObjCSynchronized) {
" f();\n"
"}\n");
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
- Style.BraceWrapping.AfterControlStatement = true;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
verifyFormat("@synchronized(self)\n"
"{\n"
" f();\n"
OpenPOWER on IntegriCloud