Skip to content

Commit 1241c76

Browse files
owencatru
authored andcommitted
[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. (cherry picked from commit 714033a)
1 parent 1503d18 commit 1241c76

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

clang/lib/Format/TokenAnnotator.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -4467,10 +4467,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
44674467
}
44684468
if (Left.is(tok::colon))
44694469
return Left.isNot(TT_ObjCMethodExpr);
4470-
if (Left.is(tok::coloncolon)) {
4471-
return Right.is(tok::star) && Right.is(TT_PointerOrReference) &&
4472-
Style.PointerAlignment != FormatStyle::PAS_Left;
4473-
}
4470+
if (Left.is(tok::coloncolon))
4471+
return false;
44744472
if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
44754473
if (Style.Language == FormatStyle::LK_TextProto ||
44764474
(Style.Language == FormatStyle::LK_Proto &&
@@ -4580,8 +4578,14 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
45804578
if (!BeforeLeft)
45814579
return false;
45824580
if (BeforeLeft->is(tok::coloncolon)) {
4583-
return Left.is(tok::star) &&
4584-
Style.PointerAlignment != FormatStyle::PAS_Right;
4581+
if (Left.isNot(tok::star))
4582+
return false;
4583+
assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4584+
if (!Right.startsSequence(tok::identifier, tok::r_paren))
4585+
return true;
4586+
assert(Right.Next);
4587+
const auto *LParen = Right.Next->MatchingParen;
4588+
return !LParen || LParen->isNot(TT_FunctionTypeLParen);
45854589
}
45864590
return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
45874591
}

clang/unittests/Format/FormatTest.cpp

+18-18
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

@@ -11141,10 +11141,10 @@ TEST_F(FormatTest, UnderstandsBinaryOperators) {
1114111141
}
1114211142

1114311143
TEST_F(FormatTest, UnderstandsPointersToMembers) {
11144-
verifyFormat("int A:: *x;");
11145-
verifyFormat("int (S:: *func)(void *);");
11146-
verifyFormat("void f() { int (S:: *func)(void *); }");
11147-
verifyFormat("typedef bool *(Class:: *Member)() const;");
11144+
verifyFormat("int A::*x;");
11145+
verifyFormat("int (S::*func)(void *);");
11146+
verifyFormat("void f() { int (S::*func)(void *); }");
11147+
verifyFormat("typedef bool *(Class::*Member)() const;");
1114811148
verifyFormat("void f() {\n"
1114911149
" (a->*f)();\n"
1115011150
" a->*x;\n"
@@ -11162,16 +11162,16 @@ TEST_F(FormatTest, UnderstandsPointersToMembers) {
1116211162

1116311163
FormatStyle Style = getLLVMStyle();
1116411164
EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11165-
verifyFormat("typedef bool *(Class:: *Member)() const;", Style);
11166-
verifyFormat("void f(int A:: *p) { int A:: *v = &A::B; }", Style);
11165+
verifyFormat("typedef bool *(Class::*Member)() const;", Style);
11166+
verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style);
1116711167

1116811168
Style.PointerAlignment = FormatStyle::PAS_Left;
11169-
verifyFormat("typedef bool* (Class::* Member)() const;", Style);
11169+
verifyFormat("typedef bool* (Class::*Member)() const;", Style);
1117011170
verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
1117111171

1117211172
Style.PointerAlignment = FormatStyle::PAS_Middle;
11173-
verifyFormat("typedef bool * (Class:: * Member)() const;", Style);
11174-
verifyFormat("void f(int A:: * p) { int A:: * v = &A::B; }", Style);
11173+
verifyFormat("typedef bool * (Class::*Member)() const;", Style);
11174+
verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
1117511175
}
1117611176

1117711177
TEST_F(FormatTest, UnderstandsUnaryOperators) {
@@ -12514,7 +12514,7 @@ TEST_F(FormatTest, FormatsFunctionTypes) {
1251412514
verifyFormat("int (*func)(void *);");
1251512515
verifyFormat("void f() { int (*func)(void *); }");
1251612516
verifyFormat("template <class CallbackClass>\n"
12517-
"using Callback = void (CallbackClass:: *)(SomeObject *Data);");
12517+
"using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
1251812518

1251912519
verifyGoogleFormat("A<void*(int*, SomeType*)>;");
1252012520
verifyGoogleFormat("void* (*a)(int);");
@@ -19437,13 +19437,13 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1943719437
"int bbbbbbb = 0;",
1943819438
Alignment);
1943919439
// http://llvm.org/PR68079
19440-
verifyFormat("using Fn = int (A:: *)();\n"
19441-
"using RFn = int (A:: *)() &;\n"
19442-
"using RRFn = int (A:: *)() &&;",
19440+
verifyFormat("using Fn = int (A::*)();\n"
19441+
"using RFn = int (A::*)() &;\n"
19442+
"using RRFn = int (A::*)() &&;",
1944319443
Alignment);
19444-
verifyFormat("using Fn = int (A:: *)();\n"
19445-
"using RFn = int *(A:: *)() &;\n"
19446-
"using RRFn = double (A:: *)() &&;",
19444+
verifyFormat("using Fn = int (A::*)();\n"
19445+
"using RFn = int *(A::*)() &;\n"
19446+
"using RRFn = double (A::*)() &&;",
1944719447
Alignment);
1944819448

1944919449
// PAS_Right

clang/unittests/Format/QualifierFixerTest.cpp

+17-19
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)