[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP added inline comments. Comment at: unittests/Format/FormatTest.cpp:7878 +" int const i = 1;\n" +" int **j = 2, ***k;\n" +" int &k = i;\n" djasper wrote: > This looks wrong to me. Wouldn't you want to align on the */& then? I.e.: > > int const i = 1; > int **j = 2, ***k; > int &k = i; Sorry for the very late reply. I believe no. In my mind (and how my organization wanted it) was to align on the variable names, and 'just' put the */& just before to it, without any space in between. An other way of saying it would be: "PAS_Right means the */& stick with the variable name". AlignConsecutiveDeclaration aligns the variable names, which give the results we have here. We believe it is fine. What you suggest breaks the alignment on the variable names, it looks wrong too... Probably, there's not a perfect answer here. I'd say it's better to have the variable names aligned. Wdyt ? https://reviews.llvm.org/D27651 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP updated this revision to Diff 95375. KP added a comment. Rebase on current trunk. Is there any chance to get this merge ? Please let me know if anything else is needed. Thanks, Ken-Patrick https://reviews.llvm.org/D27651 Files: lib/Format/WhitespaceManager.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -7848,9 +7848,11 @@ " doublebar();\n" "};\n", Alignment); + + // PAS_RIGHT EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" " int const i = 1;\n" -" int * j = 2;\n" +" int *j = 2;\n" " int big = 1;\n" "\n" " unsigned oneTwoThree = 123;\n" @@ -7871,6 +7873,141 @@ "int ll=1;\n" "}", Alignment)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int **j = 2, ***k;\n" +" int &k = i;\n" +" int &&l = i + j;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + Alignment)); + + // PAS_LEFT + FormatStyle AlignmentLeft = Alignment; + AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int* j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int** j = 2;\n" +" int& k = i;\n" +" int&& l = i + j;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + // PAS_MIDDLE + FormatStyle AlignmentMiddle = Alignment; + AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int * j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "i
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP added a comment. ping https://reviews.llvm.org/D27651 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP updated this revision to Diff 86681. KP added a comment. Rebase after r293616. Replace isPointerOrReference with a direct check on the token type. https://reviews.llvm.org/D27651 Files: lib/Format/WhitespaceManager.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -9409,9 +9409,11 @@ verifyFormat("int oneTwoThree = {0}; // comment\n" "unsigned oneTwo = 0; // comment", Alignment); + + // PAS_RIGHT EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" " int const i = 1;\n" -" int * j = 2;\n" +" int *j = 2;\n" " int big = 1;\n" "\n" " unsigned oneTwoThree = 123;\n" @@ -9432,6 +9434,141 @@ "int ll=1;\n" "}", Alignment)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int **j = 2, ***k;\n" +" int &k = i;\n" +" int &&l = i + j;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + Alignment)); + + // PAS_LEFT + FormatStyle AlignmentLeft = Alignment; + AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int* j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int** j = 2;\n" +" int& k = i;\n" +" int&& l = i + j;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + // PAS_MIDDLE + FormatStyle AlignmentMiddle = Alignment; + AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int * j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" +
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP added a comment. In https://reviews.llvm.org/D27651#661490, @djasper wrote: > I have given stuff in WhitespaceManager access to the actual FormatToken in > r293616. Hopefully that simplifies this patch. Thanks! I'll try to rebase and see what can be improved. https://reviews.llvm.org/D27651 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP updated this revision to Diff 83829. KP added a comment. Rename IsPointerOrReference to isPointerOrReference to comply with coding style https://reviews.llvm.org/D27651 Files: lib/Format/WhitespaceManager.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -8769,9 +8769,11 @@ verifyFormat("int oneTwoThree = {0}; // comment\n" "unsigned oneTwo = 0; // comment", Alignment); + + // PAS_RIGHT EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" " int const i = 1;\n" -" int * j = 2;\n" +" int *j = 2;\n" " int big = 1;\n" "\n" " unsigned oneTwoThree = 123;\n" @@ -8792,6 +8794,141 @@ "int ll=1;\n" "}", Alignment)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int **j = 2, ***k;\n" +" int &k = i;\n" +" int &&l = i + j;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + Alignment)); + + // PAS_LEFT + FormatStyle AlignmentLeft = Alignment; + AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int* j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int** j = 2;\n" +" int& k = i;\n" +" int&& l = i + j;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + // PAS_MIDDLE + FormatStyle AlignmentMiddle = Alignment; + AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int * j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP updated this revision to Diff 83676. KP added a comment. Thanks for the review! Split declarations in test cases with PAS_Middle/PAS_Left. Add tests with references and rvalue references. https://reviews.llvm.org/D27651 Files: lib/Format/WhitespaceManager.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -8769,9 +8769,11 @@ verifyFormat("int oneTwoThree = {0}; // comment\n" "unsigned oneTwo = 0; // comment", Alignment); + + // PAS_RIGHT EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" " int const i = 1;\n" -" int * j = 2;\n" +" int *j = 2;\n" " int big = 1;\n" "\n" " unsigned oneTwoThree = 123;\n" @@ -8792,6 +8794,141 @@ "int ll=1;\n" "}", Alignment)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int **j = 2, ***k;\n" +" int &k = i;\n" +" int &&l = i + j;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + Alignment)); + + // PAS_LEFT + FormatStyle AlignmentLeft = Alignment; + AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int* j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int** j = 2;\n" +" int& k = i;\n" +" int&& l = i + j;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + // PAS_MIDDLE + FormatStyle AlignmentMiddle = Alignment; + AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int * j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" +
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP updated this revision to Diff 83639. KP added a comment. Thanks Beren. No problem for me to rebase once https://reviews.llvm.org/D21279 is merged. I've added test cases for PAS_Middle and PAS_Left, I'm a bit surprised with some of the results (although it is not impacted by my changes). With PAS_Middle and PAS_Left, I get: void SomeFunction(int parameter = 0) { int const i = 1; int **j = 2, ***k = 3; int big = 1; I would have expected with PAS_Middle: int **j = 2, *** k = 3; and with PAS_Left: int**j = 2, *** k = 3; Am I right ? IIUC it is some bug/limitation in PAS_Left and Middle. https://reviews.llvm.org/D27651 Files: lib/Format/WhitespaceManager.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -8769,9 +8769,11 @@ verifyFormat("int oneTwoThree = {0}; // comment\n" "unsigned oneTwo = 0; // comment", Alignment); + + // PAS_RIGHT EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" " int const i = 1;\n" -" int * j = 2;\n" +" int *j = 2;\n" " int big = 1;\n" "\n" " unsigned oneTwoThree = 123;\n" @@ -8792,6 +8794,131 @@ "int ll=1;\n" "}", Alignment)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int **j = 2, ***k = 3;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k=3;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + Alignment)); + + // PAS_LEFT + FormatStyle AlignmentLeft = Alignment; + AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int* j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + // FIXME: one would expect " int** j = 2, *** k = 3;\n" + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int **j = 2, ***k = 3;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k=3;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + AlignmentLeft)); + // PAS_MIDDLE + FormatStyle AlignmentMiddle = Alignment; + AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int * j = 2;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" +
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP added a comment. ping https://reviews.llvm.org/D27651 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27651: [clang-format] Even with AlignConsecutiveDeclarations, PointerAlignment: Right should keep *s and &s to the right
KP created this revision. KP added a reviewer: djasper. KP added a subscriber: cfe-commits. Herald added a subscriber: klimek. With AlignConsecutiveDeclarations and PointerAlignment: Right *s and &s were left dangling. For instance const char* const* v1; float const* v2; SomeVeryLongType const& v3; was formatted as const char *const * v1; float const * v2; SomeVeryLongType const &v3; This patch keep the *s or &s aligned to the right, next to their variable. The above example is now formatted as const char *const *v1; float const*v2; SomeVeryLongType const &v3; https://reviews.llvm.org/D27651 Files: lib/Format/WhitespaceManager.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -9445,7 +9445,7 @@ Alignment); EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" " int const i = 1;\n" -" int * j = 2;\n" +" int *j = 2;\n" " int big = 1;\n" "\n" " unsigned oneTwoThree = 123;\n" @@ -9466,6 +9466,29 @@ "int ll=1;\n" "}", Alignment)); + EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" +" int const i = 1;\n" +" int **j = 2, ***k = 3;\n" +" int big = 1;\n" +"\n" +" unsigned oneTwoThree = 123;\n" +" int oneTwo = 12;\n" +" method();\n" +" float k = 2;\n" +" int ll = 1;\n" +"}", +format("void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k=3;\n" + " int big = 1;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=1;\n" + "}", + Alignment)); Alignment.AlignConsecutiveAssignments = false; Alignment.AlignEscapedNewlinesLeft = true; verifyFormat("#define A \\\n" @@ -9492,7 +9515,7 @@ Alignment); verifyFormat("void SomeFunction(int parameter = 0) {\n" " int const i = 1;\n" - " int * j = 2;\n" + " int *j = 2;\n" " int big = 1;\n" "}", Alignment); @@ -9596,7 +9619,7 @@ " float b,\n" " int c,\n" " uint32_t *d) {\n" - " int * e = 0;\n" + " int *e = 0;\n" " float f = 0;\n" " double g = 0;\n" "}\n" Index: lib/Format/WhitespaceManager.cpp === --- lib/Format/WhitespaceManager.cpp +++ lib/Format/WhitespaceManager.cpp @@ -160,10 +160,15 @@ } } +static bool IsPointerOrReference(tok::TokenKind Kind) { + return Kind == tok::star || Kind == tok::amp || Kind == tok::ampamp; +} + // Align a single sequence of tokens, see AlignTokens below. template static void -AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches, +AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End, + unsigned Column, F &&Matches, SmallVector &Changes) { bool FoundMatchOnLine = false; int Shift = 0; @@ -183,9 +188,23 @@ } assert(Shift >= 0); +if (Shift == 0) + continue; + Changes[i].StartOfTokenColumn += Shift; if (i + 1 != Changes.size()) Changes[i + 1].PreviousEndOfTokenColumn += Shift; + +// If PointerAlignment is PAS_Right, keep *s or &s next to the token +if (Style.PointerAlignment == FormatStyle::PAS_Right && +Changes[i].Spaces != 0) { + for (int previous = i - 1; + previous >= 0 && IsPointerOrReference(Changes[previous].Kind); + previous--) { +Changes[previous + 1].Spaces -= Shift; +Changes[previous].Spaces += Shift; + } +} } } @@ -232,8 +251,8 @@ // containing any matching token to be aligned and located after such token. auto AlignCurrentSequence = [&] { if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) - AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches, - Changes); + AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn, + Matches, Changes); MinColumn = 0; MaxColumn = UINT_MAX; StartOfSequence = 0; @@ -328,13 +347,6 @@ if (!Style.AlignConsecutiveDeclarations) re