rymiel created this revision. rymiel added a project: clang-format. rymiel added reviewers: owenpan, MyDeveloperDay, HazardyKnusperkeks. Herald added a project: All. rymiel requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Previously, given a pointer-to-member type such as `Foo const Bar::*`, clang-format would see the `const Bar::` part as a regular type name with scope resolution operators, and with `QualifierAlignment: Right` it would attempt to "fix" it, resulting in `Foo Bar::const *`, a syntax error. This patch no longer allows qualifiers to be moved across `::*`. Fixes https://github.com/llvm/llvm-project/issues/60898 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D144537 Files: clang/lib/Format/QualifierAlignmentFixer.cpp clang/unittests/Format/QualifierFixerTest.cpp Index: clang/unittests/Format/QualifierFixerTest.cpp =================================================================== --- clang/unittests/Format/QualifierFixerTest.cpp +++ clang/unittests/Format/QualifierFixerTest.cpp @@ -420,6 +420,16 @@ // don't adjust macros verifyFormat("const INTPTR a;", "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); } TEST_F(QualifierFixerTest, LeftQualifier) { @@ -565,6 +575,16 @@ // don't adjust macros verifyFormat("INTPTR const a;", "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); } TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) { Index: clang/lib/Format/QualifierAlignmentFixer.cpp =================================================================== --- clang/lib/Format/QualifierAlignmentFixer.cpp +++ clang/lib/Format/QualifierAlignmentFixer.cpp @@ -280,8 +280,11 @@ // The case `const Foo &&` -> `Foo const &&` // The case `const std::Foo &&` -> `std::Foo const &&` // The case `const std::Foo<T> &&` -> `std::Foo<T> const &&` - while (Next && Next->isOneOf(tok::identifier, tok::coloncolon)) + // However, `const Bar::*` remains the same. + while (Next && Next->isOneOf(tok::identifier, tok::coloncolon) && + !Next->startsSequence(tok::coloncolon, tok::star)) { Next = Next->Next; + } if (Next && Next->is(TT_TemplateOpener)) { Next = Next->MatchingParen; // Move to the end of any template class members e.g.
Index: clang/unittests/Format/QualifierFixerTest.cpp =================================================================== --- clang/unittests/Format/QualifierFixerTest.cpp +++ clang/unittests/Format/QualifierFixerTest.cpp @@ -420,6 +420,16 @@ // don't adjust macros verifyFormat("const INTPTR a;", "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); } TEST_F(QualifierFixerTest, LeftQualifier) { @@ -565,6 +575,16 @@ // don't adjust macros verifyFormat("INTPTR const a;", "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); } TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) { Index: clang/lib/Format/QualifierAlignmentFixer.cpp =================================================================== --- clang/lib/Format/QualifierAlignmentFixer.cpp +++ clang/lib/Format/QualifierAlignmentFixer.cpp @@ -280,8 +280,11 @@ // The case `const Foo &&` -> `Foo const &&` // The case `const std::Foo &&` -> `std::Foo const &&` // The case `const std::Foo<T> &&` -> `std::Foo<T> const &&` - while (Next && Next->isOneOf(tok::identifier, tok::coloncolon)) + // However, `const Bar::*` remains the same. + while (Next && Next->isOneOf(tok::identifier, tok::coloncolon) && + !Next->startsSequence(tok::coloncolon, tok::star)) { Next = Next->Next; + } if (Next && Next->is(TT_TemplateOpener)) { Next = Next->MatchingParen; // Move to the end of any template class members e.g.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits