Skip to content

Commit baeb500

Browse files
mydeveloperdaytstellar
authored andcommitted
[clang-format] [PR45357] Fix issue found with operator spacing
Summary: This is a tentative fix for https://bugs.llvm.org/show_bug.cgi?id=45357 Spaces seem to be introduced between * and * due to changes brought in for {D69573} Reviewers: sylvestre.ledru, mitchell-stellar, sammccall, Abpostelnicu, krasimir, jbcoe Reviewed By: Abpostelnicu Subscribers: tstellar, hans, Abpostelnicu, cfe-commits Tags: #clang, #clang-format Differential Revision: https://reviews.llvm.org/D78879 (cherry picked from commit b01dca5)
1 parent 7ae6db9 commit baeb500

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,10 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
21762176
Next = Next->Next;
21772177
continue;
21782178
}
2179+
if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
2180+
Next = Next->MatchingParen;
2181+
continue;
2182+
}
21792183

21802184
break;
21812185
}
@@ -2705,6 +2709,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
27052709
tok::l_square));
27062710
if (Right.is(tok::star) && Left.is(tok::l_paren))
27072711
return false;
2712+
if (Right.is(tok::star) && Left.is(tok::star))
2713+
return false;
27082714
if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
27092715
const FormatToken *Previous = &Left;
27102716
while (Previous && !Previous->is(tok::kw_operator)) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6185,7 +6185,17 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
61856185
"void\n"
61866186
"A::operator[]() {}\n"
61876187
"void\n"
6188-
"A::operator!() {}\n",
6188+
"A::operator!() {}\n"
6189+
"void\n"
6190+
"A::operator**() {}\n"
6191+
"void\n"
6192+
"A::operator<Foo> *() {}\n"
6193+
"void\n"
6194+
"A::operator<Foo> **() {}\n"
6195+
"void\n"
6196+
"A::operator<Foo> &() {}\n"
6197+
"void\n"
6198+
"A::operator void **() {}\n",
61896199
Style);
61906200
verifyFormat("constexpr auto\n"
61916201
"operator()() const -> reference {}\n"
@@ -6202,6 +6212,10 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
62026212
"constexpr auto\n"
62036213
"operator void *() const -> reference {}\n"
62046214
"constexpr auto\n"
6215+
"operator void **() const -> reference {}\n"
6216+
"constexpr auto\n"
6217+
"operator void *() const -> reference {}\n"
6218+
"constexpr auto\n"
62056219
"operator void &() const -> reference {}\n"
62066220
"constexpr auto\n"
62076221
"operator void &&() const -> reference {}\n"
@@ -14986,9 +15000,20 @@ TEST_F(FormatTest, OperatorSpacing) {
1498615000
Style.PointerAlignment = FormatStyle::PAS_Right;
1498715001
verifyFormat("Foo::operator*();", Style);
1498815002
verifyFormat("Foo::operator void *();", Style);
15003+
verifyFormat("Foo::operator void **();", Style);
1498915004
verifyFormat("Foo::operator()(void *);", Style);
1499015005
verifyFormat("Foo::operator*(void *);", Style);
1499115006
verifyFormat("Foo::operator*();", Style);
15007+
verifyFormat("Foo::operator**();", Style);
15008+
verifyFormat("Foo::operator&();", Style);
15009+
verifyFormat("Foo::operator<int> *();", Style);
15010+
verifyFormat("Foo::operator<Foo> *();", Style);
15011+
verifyFormat("Foo::operator<int> **();", Style);
15012+
verifyFormat("Foo::operator<Foo> **();", Style);
15013+
verifyFormat("Foo::operator<int> &();", Style);
15014+
verifyFormat("Foo::operator<Foo> &();", Style);
15015+
verifyFormat("Foo::operator<int> &&();", Style);
15016+
verifyFormat("Foo::operator<Foo> &&();", Style);
1499215017
verifyFormat("operator*(int (*)(), class Foo);", Style);
1499315018

1499415019
verifyFormat("Foo::operator&();", Style);
@@ -14999,21 +15024,39 @@ TEST_F(FormatTest, OperatorSpacing) {
1499915024
verifyFormat("operator&(int (&)(), class Foo);", Style);
1500015025

1500115026
verifyFormat("Foo::operator&&();", Style);
15027+
verifyFormat("Foo::operator**();", Style);
1500215028
verifyFormat("Foo::operator void &&();", Style);
1500315029
verifyFormat("Foo::operator()(void &&);", Style);
1500415030
verifyFormat("Foo::operator&&(void &&);", Style);
1500515031
verifyFormat("Foo::operator&&();", Style);
1500615032
verifyFormat("operator&&(int(&&)(), class Foo);", Style);
15033+
verifyFormat("operator const nsTArrayRight<E> &()", Style);
15034+
verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
15035+
Style);
15036+
verifyFormat("operator void **()", Style);
15037+
verifyFormat("operator const FooRight<Object> &()", Style);
15038+
verifyFormat("operator const FooRight<Object> *()", Style);
15039+
verifyFormat("operator const FooRight<Object> **()", Style);
1500715040

1500815041
Style.PointerAlignment = FormatStyle::PAS_Left;
1500915042
verifyFormat("Foo::operator*();", Style);
15043+
verifyFormat("Foo::operator**();", Style);
1501015044
verifyFormat("Foo::operator void*();", Style);
15045+
verifyFormat("Foo::operator void**();", Style);
1501115046
verifyFormat("Foo::operator/*comment*/ void*();", Style);
1501215047
verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
1501315048
verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
1501415049
verifyFormat("Foo::operator()(void*);", Style);
1501515050
verifyFormat("Foo::operator*(void*);", Style);
1501615051
verifyFormat("Foo::operator*();", Style);
15052+
verifyFormat("Foo::operator<int>*();", Style);
15053+
verifyFormat("Foo::operator<Foo>*();", Style);
15054+
verifyFormat("Foo::operator<int>**();", Style);
15055+
verifyFormat("Foo::operator<Foo>**();", Style);
15056+
verifyFormat("Foo::operator<int>&();", Style);
15057+
verifyFormat("Foo::operator<Foo>&();", Style);
15058+
verifyFormat("Foo::operator<int>&&();", Style);
15059+
verifyFormat("Foo::operator<Foo>&&();", Style);
1501715060
verifyFormat("operator*(int (*)(), class Foo);", Style);
1501815061

1501915062
verifyFormat("Foo::operator&();", Style);
@@ -15035,9 +15078,17 @@ TEST_F(FormatTest, OperatorSpacing) {
1503515078
verifyFormat("Foo::operator&&(void&&);", Style);
1503615079
verifyFormat("Foo::operator&&();", Style);
1503715080
verifyFormat("operator&&(int(&&)(), class Foo);", Style);
15081+
verifyFormat("operator const nsTArrayLeft<E>&()", Style);
15082+
verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
15083+
Style);
15084+
verifyFormat("operator void**()", Style);
15085+
verifyFormat("operator const FooLeft<Object>&()", Style);
15086+
verifyFormat("operator const FooLeft<Object>*()", Style);
15087+
verifyFormat("operator const FooLeft<Object>**()", Style);
1503815088

1503915089
// PR45107
1504015090
verifyFormat("operator Vector<String>&();", Style);
15091+
verifyFormat("operator const Vector<String>&();", Style);
1504115092
verifyFormat("operator foo::Bar*();", Style);
1504215093
verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
1504315094
verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",

0 commit comments

Comments
 (0)