Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Don't insert a space between :: and * #105043

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4478,10 +4478,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
}
if (Left.is(tok::colon))
return Left.isNot(TT_ObjCMethodExpr);
if (Left.is(tok::coloncolon)) {
return Right.is(tok::star) && Right.is(TT_PointerOrReference) &&
Style.PointerAlignment != FormatStyle::PAS_Left;
}
if (Left.is(tok::coloncolon))
return false;
if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
if (Style.Language == FormatStyle::LK_TextProto ||
(Style.Language == FormatStyle::LK_Proto &&
Expand Down Expand Up @@ -4591,8 +4589,14 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (!BeforeLeft)
return false;
if (BeforeLeft->is(tok::coloncolon)) {
return Left.is(tok::star) &&
Style.PointerAlignment != FormatStyle::PAS_Right;
if (Left.isNot(tok::star))
return false;
assert(Style.PointerAlignment != FormatStyle::PAS_Right);
if (!Right.startsSequence(tok::identifier, tok::r_paren))
return true;
assert(Right.Next);
const auto *LParen = Right.Next->MatchingParen;
return !LParen || LParen->isNot(TT_FunctionTypeLParen);
}
return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
}
Expand Down
36 changes: 18 additions & 18 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3646,8 +3646,8 @@ TEST_F(FormatTest, FormatsClasses) {
" : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaa> {};");
verifyFormat("template <class R, class C>\n"
"struct Aaaaaaaaaaaaaaaaa<R (C:: *)(int) const>\n"
" : Aaaaaaaaaaaaaaaaa<R (C:: *)(int)> {};");
"struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
" : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
verifyFormat("class ::A::B {};");
}

Expand Down Expand Up @@ -11166,10 +11166,10 @@ TEST_F(FormatTest, UnderstandsBinaryOperators) {
}

TEST_F(FormatTest, UnderstandsPointersToMembers) {
verifyFormat("int A:: *x;");
verifyFormat("int (S:: *func)(void *);");
verifyFormat("void f() { int (S:: *func)(void *); }");
verifyFormat("typedef bool *(Class:: *Member)() const;");
verifyFormat("int A::*x;");
verifyFormat("int (S::*func)(void *);");
verifyFormat("void f() { int (S::*func)(void *); }");
verifyFormat("typedef bool *(Class::*Member)() const;");
verifyFormat("void f() {\n"
" (a->*f)();\n"
" a->*x;\n"
Expand All @@ -11187,16 +11187,16 @@ TEST_F(FormatTest, UnderstandsPointersToMembers) {

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

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

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

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

verifyGoogleFormat("A<void*(int*, SomeType*)>;");
verifyGoogleFormat("void* (*a)(int);");
Expand Down Expand Up @@ -19462,13 +19462,13 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
"int bbbbbbb = 0;",
Alignment);
// http://llvm.org/PR68079
verifyFormat("using Fn = int (A:: *)();\n"
"using RFn = int (A:: *)() &;\n"
"using RRFn = int (A:: *)() &&;",
verifyFormat("using Fn = int (A::*)();\n"
"using RFn = int (A::*)() &;\n"
"using RRFn = int (A::*)() &&;",
Alignment);
verifyFormat("using Fn = int (A:: *)();\n"
"using RFn = int *(A:: *)() &;\n"
"using RRFn = double (A:: *)() &&;",
verifyFormat("using Fn = int (A::*)();\n"
"using RFn = int *(A::*)() &;\n"
"using RRFn = double (A::*)() &&;",
Alignment);

// PAS_Right
Expand Down
36 changes: 17 additions & 19 deletions clang/unittests/Format/QualifierFixerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ TEST_F(QualifierFixerTest, RightQualifier) {
verifyFormat("Foo inline static const;", "Foo inline const static;", Style);
verifyFormat("Foo inline static const;", Style);

verifyFormat("Foo<T volatile>::Bar<Type const, 5> const volatile A:: *;",
verifyFormat("Foo<T volatile>::Bar<Type const, 5> const volatile A::*;",
"volatile const Foo<volatile T>::Bar<const Type, 5> A::*;",
Style);

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

// Pointers to members
verifyFormat("int S:: *a;", Style);
verifyFormat("int const S:: *a;", "const int S:: *a;", Style);
verifyFormat("int const S:: *const a;", "const int S::* const a;", Style);
verifyFormat("int A:: *const A:: *p1;", Style);
verifyFormat("float (C:: *p)(int);", Style);
verifyFormat("float (C:: *const p)(int);", Style);
verifyFormat("float (C:: *p)(int) const;", Style);
verifyFormat("float const (C:: *p)(int);", "const float (C::*p)(int);",
Style);
verifyFormat("int S::*a;", Style);
verifyFormat("int const S::*a;", "const int S::*a;", Style);
verifyFormat("int const S::*const a;", "const int S::* const a;", Style);
verifyFormat("int A::*const A::*p1;", Style);
verifyFormat("float (C::*p)(int);", Style);
verifyFormat("float (C::*const p)(int);", Style);
verifyFormat("float (C::*p)(int) const;", Style);
verifyFormat("float const (C::*p)(int);", "const float (C::*p)(int);", Style);
}

TEST_F(QualifierFixerTest, LeftQualifier) {
Expand Down Expand Up @@ -831,15 +830,14 @@ TEST_F(QualifierFixerTest, LeftQualifier) {
verifyFormat("INTPTR const a;", Style);

// Pointers to members
verifyFormat("int S:: *a;", Style);
verifyFormat("const int S:: *a;", "int const S:: *a;", Style);
verifyFormat("const int S:: *const a;", "int const S::* const a;", Style);
verifyFormat("int A:: *const A:: *p1;", Style);
verifyFormat("float (C:: *p)(int);", Style);
verifyFormat("float (C:: *const p)(int);", Style);
verifyFormat("float (C:: *p)(int) const;", Style);
verifyFormat("const float (C:: *p)(int);", "float const (C::*p)(int);",
Style);
verifyFormat("int S::*a;", Style);
verifyFormat("const int S::*a;", "int const S::*a;", Style);
verifyFormat("const int S::*const a;", "int const S::*const a;", Style);
verifyFormat("int A::*const A::*p1;", Style);
verifyFormat("float (C::*p)(int);", Style);
verifyFormat("float (C::*const p)(int);", Style);
verifyFormat("float (C::*p)(int) const;", Style);
verifyFormat("const float (C::*p)(int);", "float const (C::*p)(int);", Style);
}

TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) {
Expand Down
Loading