Skip to content

Commit e0b1df9

Browse files
darwin-xHazardyKnusperkeks
authored andcommitted
[clang-format] Fix AlignConsecutiveDeclarations handling of pointers
This is a bug fix of https://bugs.llvm.org/show_bug.cgi?id=49175 The expected code format: unsigned int* a; int* b; unsigned int Const* c; The actual code after formatting (without this patch): unsigned int* a; int* b; unsigned int Const* c; Differential Revision: https://reviews.llvm.org/D97137
1 parent 6f9dd84 commit e0b1df9

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
709709
for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
710710
if (Next->is(tok::comment))
711711
continue;
712+
if (Next->is(TT_PointerOrReference))
713+
return false;
712714
if (!Next->Tok.getIdentifierInfo())
713715
break;
714716
if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,

clang/unittests/Format/FormatTest.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13945,6 +13945,20 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1394513945
verifyFormat("int oneTwoThree{0}; // comment\n"
1394613946
"unsigned oneTwo; // comment",
1394713947
Alignment);
13948+
verifyFormat("unsigned int * a;\n"
13949+
"int * b;\n"
13950+
"unsigned int Const *c;\n"
13951+
"unsigned int const *d;\n"
13952+
"unsigned int Const &e;\n"
13953+
"unsigned int const &f;",
13954+
Alignment);
13955+
verifyFormat("Const unsigned int *c;\n"
13956+
"const unsigned int *d;\n"
13957+
"Const unsigned int &e;\n"
13958+
"const unsigned int &f;\n"
13959+
"const unsigned g;\n"
13960+
"Const unsigned h;",
13961+
Alignment);
1394813962
EXPECT_EQ("float const a = 5;\n"
1394913963
"\n"
1395013964
"int oneTwoThree = 123;",
@@ -14249,6 +14263,38 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1424914263
EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
1425014264
"foo(int a);",
1425114265
format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
14266+
14267+
Alignment.PointerAlignment = FormatStyle::PAS_Left;
14268+
verifyFormat("unsigned int* a;\n"
14269+
"int* b;\n"
14270+
"unsigned int Const* c;\n"
14271+
"unsigned int const* d;\n"
14272+
"unsigned int Const& e;\n"
14273+
"unsigned int const& f;",
14274+
Alignment);
14275+
verifyFormat("Const unsigned int* c;\n"
14276+
"const unsigned int* d;\n"
14277+
"Const unsigned int& e;\n"
14278+
"const unsigned int& f;\n"
14279+
"const unsigned g;\n"
14280+
"Const unsigned h;",
14281+
Alignment);
14282+
14283+
Alignment.PointerAlignment = FormatStyle::PAS_Middle;
14284+
verifyFormat("unsigned int * a;\n"
14285+
"int * b;\n"
14286+
"unsigned int Const * c;\n"
14287+
"unsigned int const * d;\n"
14288+
"unsigned int Const & e;\n"
14289+
"unsigned int const & f;",
14290+
Alignment);
14291+
verifyFormat("Const unsigned int * c;\n"
14292+
"const unsigned int * d;\n"
14293+
"Const unsigned int & e;\n"
14294+
"const unsigned int & f;\n"
14295+
"const unsigned g;\n"
14296+
"Const unsigned h;",
14297+
Alignment);
1425214298
}
1425314299

1425414300
TEST_F(FormatTest, LinuxBraceBreaking) {

0 commit comments

Comments
 (0)