Skip to content

Commit 714033a

Browse files
authored
[clang-format] Don't insert a space between :: and * (#105043)
Also, don't insert a space after ::* for method pointers. See #86253 (comment). Fixes #100841.
1 parent 987ffc3 commit 714033a

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4478,10 +4478,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
44784478
}
44794479
if (Left.is(tok::colon))
44804480
return Left.isNot(TT_ObjCMethodExpr);
4481-
if (Left.is(tok::coloncolon)) {
4482-
return Right.is(tok::star) && Right.is(TT_PointerOrReference) &&
4483-
Style.PointerAlignment != FormatStyle::PAS_Left;
4484-
}
4481+
if (Left.is(tok::coloncolon))
4482+
return false;
44854483
if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
44864484
if (Style.Language == FormatStyle::LK_TextProto ||
44874485
(Style.Language == FormatStyle::LK_Proto &&
@@ -4591,8 +4589,14 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
45914589
if (!BeforeLeft)
45924590
return false;
45934591
if (BeforeLeft->is(tok::coloncolon)) {
4594-
return Left.is(tok::star) &&
4595-
Style.PointerAlignment != FormatStyle::PAS_Right;
4592+
if (Left.isNot(tok::star))
4593+
return false;
4594+
assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4595+
if (!Right.startsSequence(tok::identifier, tok::r_paren))
4596+
return true;
4597+
assert(Right.Next);
4598+
const auto *LParen = Right.Next->MatchingParen;
4599+
return !LParen || LParen->isNot(TT_FunctionTypeLParen);
45964600
}
45974601
return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
45984602
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,8 +3646,8 @@ TEST_F(FormatTest, FormatsClasses) {
36463646
" : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
36473647
" aaaaaaaaaaaaaaaaaaaaaa> {};");
36483648
verifyFormat("template <class R, class C>\n"
3649-
"struct Aaaaaaaaaaaaaaaaa<R (C:: *)(int) const>\n"
3650-
" : Aaaaaaaaaaaaaaaaa<R (C:: *)(int)> {};");
3649+
"struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3650+
" : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
36513651
verifyFormat("class ::A::B {};");
36523652
}
36533653

@@ -11166,10 +11166,10 @@ TEST_F(FormatTest, UnderstandsBinaryOperators) {
1116611166
}
1116711167

1116811168
TEST_F(FormatTest, UnderstandsPointersToMembers) {
11169-
verifyFormat("int A:: *x;");
11170-
verifyFormat("int (S:: *func)(void *);");
11171-
verifyFormat("void f() { int (S:: *func)(void *); }");
11172-
verifyFormat("typedef bool *(Class:: *Member)() const;");
11169+
verifyFormat("int A::*x;");
11170+
verifyFormat("int (S::*func)(void *);");
11171+
verifyFormat("void f() { int (S::*func)(void *); }");
11172+
verifyFormat("typedef bool *(Class::*Member)() const;");
1117311173
verifyFormat("void f() {\n"
1117411174
" (a->*f)();\n"
1117511175
" a->*x;\n"
@@ -11187,16 +11187,16 @@ TEST_F(FormatTest, UnderstandsPointersToMembers) {
1118711187

1118811188
FormatStyle Style = getLLVMStyle();
1118911189
EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11190-
verifyFormat("typedef bool *(Class:: *Member)() const;", Style);
11191-
verifyFormat("void f(int A:: *p) { int A:: *v = &A::B; }", Style);
11190+
verifyFormat("typedef bool *(Class::*Member)() const;", Style);
11191+
verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style);
1119211192

1119311193
Style.PointerAlignment = FormatStyle::PAS_Left;
11194-
verifyFormat("typedef bool* (Class::* Member)() const;", Style);
11194+
verifyFormat("typedef bool* (Class::*Member)() const;", Style);
1119511195
verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
1119611196

1119711197
Style.PointerAlignment = FormatStyle::PAS_Middle;
11198-
verifyFormat("typedef bool * (Class:: * Member)() const;", Style);
11199-
verifyFormat("void f(int A:: * p) { int A:: * v = &A::B; }", Style);
11198+
verifyFormat("typedef bool * (Class::*Member)() const;", Style);
11199+
verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
1120011200
}
1120111201

1120211202
TEST_F(FormatTest, UnderstandsUnaryOperators) {
@@ -12539,7 +12539,7 @@ TEST_F(FormatTest, FormatsFunctionTypes) {
1253912539
verifyFormat("int (*func)(void *);");
1254012540
verifyFormat("void f() { int (*func)(void *); }");
1254112541
verifyFormat("template <class CallbackClass>\n"
12542-
"using Callback = void (CallbackClass:: *)(SomeObject *Data);");
12542+
"using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
1254312543

1254412544
verifyGoogleFormat("A<void*(int*, SomeType*)>;");
1254512545
verifyGoogleFormat("void* (*a)(int);");
@@ -19462,13 +19462,13 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1946219462
"int bbbbbbb = 0;",
1946319463
Alignment);
1946419464
// http://llvm.org/PR68079
19465-
verifyFormat("using Fn = int (A:: *)();\n"
19466-
"using RFn = int (A:: *)() &;\n"
19467-
"using RRFn = int (A:: *)() &&;",
19465+
verifyFormat("using Fn = int (A::*)();\n"
19466+
"using RFn = int (A::*)() &;\n"
19467+
"using RRFn = int (A::*)() &&;",
1946819468
Alignment);
19469-
verifyFormat("using Fn = int (A:: *)();\n"
19470-
"using RFn = int *(A:: *)() &;\n"
19471-
"using RRFn = double (A:: *)() &&;",
19469+
verifyFormat("using Fn = int (A::*)();\n"
19470+
"using RFn = int *(A::*)() &;\n"
19471+
"using RRFn = double (A::*)() &&;",
1947219472
Alignment);
1947319473

1947419474
// PAS_Right

clang/unittests/Format/QualifierFixerTest.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ TEST_F(QualifierFixerTest, RightQualifier) {
305305
verifyFormat("Foo inline static const;", "Foo inline const static;", Style);
306306
verifyFormat("Foo inline static const;", Style);
307307

308-
verifyFormat("Foo<T volatile>::Bar<Type const, 5> const volatile A:: *;",
308+
verifyFormat("Foo<T volatile>::Bar<Type const, 5> const volatile A::*;",
309309
"volatile const Foo<volatile T>::Bar<const Type, 5> A::*;",
310310
Style);
311311

@@ -523,15 +523,14 @@ TEST_F(QualifierFixerTest, RightQualifier) {
523523
verifyFormat("const INTPTR a;", Style);
524524

525525
// Pointers to members
526-
verifyFormat("int S:: *a;", Style);
527-
verifyFormat("int const S:: *a;", "const int S:: *a;", Style);
528-
verifyFormat("int const S:: *const a;", "const int S::* const a;", Style);
529-
verifyFormat("int A:: *const A:: *p1;", Style);
530-
verifyFormat("float (C:: *p)(int);", Style);
531-
verifyFormat("float (C:: *const p)(int);", Style);
532-
verifyFormat("float (C:: *p)(int) const;", Style);
533-
verifyFormat("float const (C:: *p)(int);", "const float (C::*p)(int);",
534-
Style);
526+
verifyFormat("int S::*a;", Style);
527+
verifyFormat("int const S::*a;", "const int S::*a;", Style);
528+
verifyFormat("int const S::*const a;", "const int S::* const a;", Style);
529+
verifyFormat("int A::*const A::*p1;", Style);
530+
verifyFormat("float (C::*p)(int);", Style);
531+
verifyFormat("float (C::*const p)(int);", Style);
532+
verifyFormat("float (C::*p)(int) const;", Style);
533+
verifyFormat("float const (C::*p)(int);", "const float (C::*p)(int);", Style);
535534
}
536535

537536
TEST_F(QualifierFixerTest, LeftQualifier) {
@@ -831,15 +830,14 @@ TEST_F(QualifierFixerTest, LeftQualifier) {
831830
verifyFormat("INTPTR const a;", Style);
832831

833832
// Pointers to members
834-
verifyFormat("int S:: *a;", Style);
835-
verifyFormat("const int S:: *a;", "int const S:: *a;", Style);
836-
verifyFormat("const int S:: *const a;", "int const S::* const a;", Style);
837-
verifyFormat("int A:: *const A:: *p1;", Style);
838-
verifyFormat("float (C:: *p)(int);", Style);
839-
verifyFormat("float (C:: *const p)(int);", Style);
840-
verifyFormat("float (C:: *p)(int) const;", Style);
841-
verifyFormat("const float (C:: *p)(int);", "float const (C::*p)(int);",
842-
Style);
833+
verifyFormat("int S::*a;", Style);
834+
verifyFormat("const int S::*a;", "int const S::*a;", Style);
835+
verifyFormat("const int S::*const a;", "int const S::*const a;", Style);
836+
verifyFormat("int A::*const A::*p1;", Style);
837+
verifyFormat("float (C::*p)(int);", Style);
838+
verifyFormat("float (C::*const p)(int);", Style);
839+
verifyFormat("float (C::*p)(int) const;", Style);
840+
verifyFormat("const float (C::*p)(int);", "float const (C::*p)(int);", Style);
843841
}
844842

845843
TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) {

0 commit comments

Comments
 (0)