diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2706,12 +2706,26 @@ if (Right.is(tok::star) && Left.is(tok::l_paren)) return false; if (Right.isOneOf(tok::star, tok::amp, tok::ampamp) && - (Left.is(tok::identifier) || Left.isSimpleTypeSpecifier()) && - Left.Previous && Left.Previous->is(tok::kw_operator)) - // Space between the type and the * - // operator void*(), operator char*(), operator Foo*() dependant - // on PointerAlignment style. - return (Style.PointerAlignment != FormatStyle::PAS_Left); + (Left.is(tok::identifier) || Left.isSimpleTypeSpecifier())) { + // Space between the type and the * in: + // operator void*() + // operator void*() + // operator char*() + // operator /*comment*/ const char*() + // operator volatile /*comment*/ char*() + // operator Foo*() + // dependant on PointerAlignment style. + + // Here, Left points to the type specifier. Skip back over const and + // volatile, looking for `operator`. + FormatToken *Before = Left.getPreviousNonComment(); + while (Before && Before->isOneOf(tok::kw_const, tok::kw_volatile)) { + Before = Before->getPreviousNonComment(); + } + if (Before && Before->is(tok::kw_operator)) { + return (Style.PointerAlignment != FormatStyle::PAS_Left); + } + } const auto SpaceRequiredForArrayInitializerLSquare = [](const FormatToken &LSquareTok, const FormatStyle &Style) { return Style.SpacesInContainerLiterals || diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -15007,6 +15007,9 @@ Style.PointerAlignment = FormatStyle::PAS_Left; verifyFormat("Foo::operator*();", Style); verifyFormat("Foo::operator void*();", Style); + verifyFormat("Foo::operator/*comment*/ void*();", Style); + verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); + verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); verifyFormat("Foo::operator()(void*);", Style); verifyFormat("Foo::operator*(void*);", Style); verifyFormat("Foo::operator*();", Style);