Skip to content

Commit

Permalink
[clang-format] Don't move qualifiers past pointers-to-member
Browse files Browse the repository at this point in the history
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 #60898

Reviewed By: owenpan, MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D144537
  • Loading branch information
rymiel committed Feb 25, 2023
1 parent 7ba9101 commit 393e197
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/Format/QualifierAlignmentFixer.cpp
Expand Up @@ -280,8 +280,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
// 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.
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Format/QualifierFixerTest.cpp
Expand Up @@ -420,6 +420,16 @@ TEST_F(QualifierFixerTest, RightQualifier) {

// 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) {
Expand Down Expand Up @@ -565,6 +575,16 @@ TEST_F(QualifierFixerTest, LeftQualifier) {

// 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) {
Expand Down

0 comments on commit 393e197

Please sign in to comment.