Skip to content

Commit 99e5b2f

Browse files
krasimirggzmodem
authored andcommitted
clang-format: fix spacing in operator const char*()
Summary: Revision a75f8d9 fixed spacing for operators, but caused the const and non-const versions to diverge: ``` // With Style.PointerAlignment = FormatStyle::PAS_Left: struct A { operator char*() { return ""; } operator const char *() const { return ""; } }; ``` The code was checking if the type specifier was directly preceded by `operator`. However there could be comments and `const/volatile` in between. Reviewers: mprobst Reviewed By: mprobst Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72911 (cherry picked from commit 33463cf)
1 parent 6b16ce9 commit 99e5b2f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,10 +2707,17 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
27072707
return false;
27082708
if (Right.isOneOf(tok::star, tok::amp, tok::ampamp) &&
27092709
(Left.is(tok::identifier) || Left.isSimpleTypeSpecifier()) &&
2710-
Left.Previous && Left.Previous->is(tok::kw_operator))
2711-
// Space between the type and the *
2712-
// operator void*(), operator char*(), operator Foo*() dependant
2713-
// on PointerAlignment style.
2710+
// Space between the type and the * in:
2711+
// operator void*()
2712+
// operator char*()
2713+
// operator /*comment*/ const char*()
2714+
// operator volatile /*comment*/ char*()
2715+
// operator Foo*()
2716+
// dependent on PointerAlignment style.
2717+
Left.Previous &&
2718+
(Left.Previous->endsSequence(tok::kw_operator) ||
2719+
Left.Previous->endsSequence(tok::kw_const, tok::kw_operator) ||
2720+
Left.Previous->endsSequence(tok::kw_volatile, tok::kw_operator)))
27142721
return (Style.PointerAlignment != FormatStyle::PAS_Left);
27152722
const auto SpaceRequiredForArrayInitializerLSquare =
27162723
[](const FormatToken &LSquareTok, const FormatStyle &Style) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15008,20 +15008,29 @@ TEST_F(FormatTest, OperatorSpacing) {
1500815008
Style.PointerAlignment = FormatStyle::PAS_Left;
1500915009
verifyFormat("Foo::operator*();", Style);
1501015010
verifyFormat("Foo::operator void*();", Style);
15011+
verifyFormat("Foo::operator/*comment*/ void*();", Style);
15012+
verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
15013+
verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
1501115014
verifyFormat("Foo::operator()(void*);", Style);
1501215015
verifyFormat("Foo::operator*(void*);", Style);
1501315016
verifyFormat("Foo::operator*();", Style);
1501415017
verifyFormat("operator*(int (*)(), class Foo);", Style);
1501515018

1501615019
verifyFormat("Foo::operator&();", Style);
1501715020
verifyFormat("Foo::operator void&();", Style);
15021+
verifyFormat("Foo::operator/*comment*/ void&();", Style);
15022+
verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
15023+
verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
1501815024
verifyFormat("Foo::operator()(void&);", Style);
1501915025
verifyFormat("Foo::operator&(void&);", Style);
1502015026
verifyFormat("Foo::operator&();", Style);
1502115027
verifyFormat("operator&(int (&)(), class Foo);", Style);
1502215028

1502315029
verifyFormat("Foo::operator&&();", Style);
1502415030
verifyFormat("Foo::operator void&&();", Style);
15031+
verifyFormat("Foo::operator/*comment*/ void&&();", Style);
15032+
verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
15033+
verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
1502515034
verifyFormat("Foo::operator()(void&&);", Style);
1502615035
verifyFormat("Foo::operator&&(void&&);", Style);
1502715036
verifyFormat("Foo::operator&&();", Style);

0 commit comments

Comments
 (0)