summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Format
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2015-10-01 10:06:54 +0000
committerDaniel Jasper <djasper@google.com>2015-10-01 10:06:54 +0000
commite12597cc230b96064236df9527be7e1e95a8d94e (patch)
tree900b33b2572c73ae4b14c3727eda1e65748e7cd6 /clang/unittests/Format
parent539cd118b2771feaecc3499ed7af7eab69703dc2 (diff)
downloadbcm5719-llvm-e12597cc230b96064236df9527be7e1e95a8d94e.tar.gz
bcm5719-llvm-e12597cc230b96064236df9527be7e1e95a8d94e.zip
[clang-format] Add support of consecutive declarations alignment
This allows clang-format to align identifiers in consecutive declarations. This is useful for increasing the readability of the code in the same way the alignment of assignations is. The code is a slightly modified version of the consecutive assignment alignment code. Currently only the identifiers are aligned, and there is no support of alignment of the pointer star or reference symbol. The patch also solve the issue of alignments not being possible due to the ColumnLimit for both the existing AlignConsecutiveAligments and the new AlignConsecutiveDeclarations. Patch by Beren Minor, thank you. Review: http://reviews.llvm.org/D12362 llvm-svn: 248999
Diffstat (limited to 'clang/unittests/Format')
-rw-r--r--clang/unittests/Format/FormatTest.cpp184
1 files changed, 184 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index b0c9d46b1ac..0921b38093f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8645,6 +8645,189 @@ TEST_F(FormatTest, AlignConsecutiveAssignments) {
Alignment);
}
+TEST_F(FormatTest, AlignConsecutiveDeclarations) {
+ FormatStyle Alignment = getLLVMStyle();
+ Alignment.AlignConsecutiveDeclarations = false;
+ verifyFormat("float const a = 5;\n"
+ "int oneTwoThree = 123;",
+ Alignment);
+ verifyFormat("int a = 5;\n"
+ "float const oneTwoThree = 123;",
+ Alignment);
+
+ Alignment.AlignConsecutiveDeclarations = true;
+ verifyFormat("float const a = 5;\n"
+ "int oneTwoThree = 123;",
+ Alignment);
+ verifyFormat("int a = method();\n"
+ "float const oneTwoThree = 133;",
+ Alignment);
+ verifyFormat("int i = 1, j = 10;\n"
+ "something = 2000;",
+ Alignment);
+ verifyFormat("something = 2000;\n"
+ "int i = 1, j = 10;\n",
+ Alignment);
+ verifyFormat("float something = 2000;\n"
+ "double another = 911;\n"
+ "int i = 1, j = 10;\n"
+ "const int *oneMore = 1;\n"
+ "unsigned i = 2;",
+ Alignment);
+ verifyFormat("float a = 5;\n"
+ "int one = 1;\n"
+ "method();\n"
+ "const double oneTwoThree = 123;\n"
+ "const unsigned int oneTwo = 12;",
+ Alignment);
+ verifyFormat("int oneTwoThree{0}; // comment\n"
+ "unsigned oneTwo; // comment",
+ Alignment);
+ EXPECT_EQ("float const a = 5;\n"
+ "\n"
+ "int oneTwoThree = 123;",
+ format("float const a = 5;\n"
+ "\n"
+ "int oneTwoThree= 123;",
+ Alignment));
+ EXPECT_EQ("float a = 5;\n"
+ "int one = 1;\n"
+ "\n"
+ "unsigned oneTwoThree = 123;",
+ format("float a = 5;\n"
+ "int one = 1;\n"
+ "\n"
+ "unsigned oneTwoThree = 123;",
+ Alignment));
+ EXPECT_EQ("float a = 5;\n"
+ "int one = 1;\n"
+ "\n"
+ "unsigned oneTwoThree = 123;\n"
+ "int oneTwo = 12;",
+ format("float a = 5;\n"
+ "int one = 1;\n"
+ "\n"
+ "unsigned oneTwoThree = 123;\n"
+ "int oneTwo = 12;",
+ Alignment));
+ Alignment.AlignConsecutiveAssignments = true;
+ verifyFormat("float something = 2000;\n"
+ "double another = 911;\n"
+ "int i = 1, j = 10;\n"
+ "const int *oneMore = 1;\n"
+ "unsigned i = 2;",
+ Alignment);
+ verifyFormat("int oneTwoThree = {0}; // comment\n"
+ "unsigned oneTwo = 0; // comment",
+ Alignment);
+ EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
+ " int const i = 1;\n"
+ " int * j = 2;\n"
+ " int big = 10000;\n"
+ "\n"
+ " unsigned oneTwoThree = 123;\n"
+ " int oneTwo = 12;\n"
+ " method();\n"
+ " float k = 2;\n"
+ " int ll = 10000;\n"
+ "}",
+ format("void SomeFunction(int parameter= 0) {\n"
+ " int const i= 1;\n"
+ " int *j=2;\n"
+ " int big = 10000;\n"
+ "\n"
+ "unsigned oneTwoThree =123;\n"
+ "int oneTwo = 12;\n"
+ " method();\n"
+ "float k= 2;\n"
+ "int ll=10000;\n"
+ "}",
+ Alignment));
+ Alignment.AlignConsecutiveAssignments = false;
+ Alignment.AlignEscapedNewlinesLeft = true;
+ verifyFormat("#define A \\\n"
+ " int aaaa = 12; \\\n"
+ " float b = 23; \\\n"
+ " const int ccc = 234; \\\n"
+ " unsigned dddddddddd = 2345;",
+ Alignment);
+ Alignment.AlignEscapedNewlinesLeft = false;
+ Alignment.ColumnLimit = 30;
+ verifyFormat("#define A \\\n"
+ " int aaaa = 12; \\\n"
+ " float b = 23; \\\n"
+ " const int ccc = 234; \\\n"
+ " int dddddddddd = 2345;",
+ Alignment);
+ Alignment.ColumnLimit = 80;
+ verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
+ "k = 4, int l = 5,\n"
+ " int m = 6) {\n"
+ " const int j = 10;\n"
+ " otherThing = 1;\n"
+ "}",
+ Alignment);
+ verifyFormat("void SomeFunction(int parameter = 0) {\n"
+ " int const i = 1;\n"
+ " int * j = 2;\n"
+ " int big = 10000;\n"
+ "}",
+ Alignment);
+ verifyFormat("class C {\n"
+ "public:\n"
+ " int i = 1;\n"
+ " virtual void f() = 0;\n"
+ "};",
+ Alignment);
+ verifyFormat("float i = 1;\n"
+ "if (SomeType t = getSomething()) {\n"
+ "}\n"
+ "const unsigned j = 2;\n"
+ "int big = 10000;",
+ Alignment);
+ verifyFormat("float j = 7;\n"
+ "for (int k = 0; k < N; ++k) {\n"
+ "}\n"
+ "unsigned j = 2;\n"
+ "int big = 10000;\n"
+ "}",
+ Alignment);
+ Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
+ verifyFormat("float i = 1;\n"
+ "LooooooooooongType loooooooooooooooooooooongVariable\n"
+ " = someLooooooooooooooooongFunction();\n"
+ "int j = 2;",
+ Alignment);
+ Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
+ verifyFormat("int i = 1;\n"
+ "LooooooooooongType loooooooooooooooooooooongVariable =\n"
+ " someLooooooooooooooooongFunction();\n"
+ "int j = 2;",
+ Alignment);
+ // FIXME: Should align all three declarations
+ verifyFormat(
+ "int i = 1;\n"
+ "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
+ " loooooooooooooooooooooongParameterB);\n"
+ "int j = 2;",
+ Alignment);
+
+ // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
+ // We expect declarations and assignments to align, as long as it doesn't
+ // exceed the column limit, starting a new alignemnt sequence whenever it
+ // happens.
+ Alignment.AlignConsecutiveAssignments = true;
+ Alignment.ColumnLimit = 30;
+ verifyFormat("float ii = 1;\n"
+ "unsigned j = 2;\n"
+ "int someVerylongVariable = 1;\n"
+ "AnotherLongType ll = 123456;\n"
+ "VeryVeryLongType k = 2;\n"
+ "int myvar = 1;",
+ Alignment);
+ Alignment.ColumnLimit = 80;
+}
+
TEST_F(FormatTest, LinuxBraceBreaking) {
FormatStyle LinuxBraceStyle = getLLVMStyle();
LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
@@ -9290,6 +9473,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(AlignOperands);
CHECK_PARSE_BOOL(AlignTrailingComments);
CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
+ CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
OpenPOWER on IntegriCloud