Skip to content

Commit 5cdefb4

Browse files
rymieltru
authored andcommitted
[clang-format] Don't move qualifiers past pointers-to-member
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 (cherry picked from commit 393e197)
1 parent 076ceb7 commit 5cdefb4

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

clang/lib/Format/QualifierAlignmentFixer.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
280280
// The case `const Foo &&` -> `Foo const &&`
281281
// The case `const std::Foo &&` -> `std::Foo const &&`
282282
// The case `const std::Foo<T> &&` -> `std::Foo<T> const &&`
283-
while (Next && Next->isOneOf(tok::identifier, tok::coloncolon))
283+
// However, `const Bar::*` remains the same.
284+
while (Next && Next->isOneOf(tok::identifier, tok::coloncolon) &&
285+
!Next->startsSequence(tok::coloncolon, tok::star)) {
284286
Next = Next->Next;
287+
}
285288
if (Next && Next->is(TT_TemplateOpener)) {
286289
Next = Next->MatchingParen;
287290
// Move to the end of any template class members e.g.

clang/unittests/Format/QualifierFixerTest.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,16 @@ TEST_F(QualifierFixerTest, RightQualifier) {
420420

421421
// don't adjust macros
422422
verifyFormat("const INTPTR a;", "const INTPTR a;", Style);
423+
424+
// Pointers to members
425+
verifyFormat("int S::*a;", Style);
426+
verifyFormat("int const S::*a;", "const int S:: *a;", Style);
427+
verifyFormat("int const S::*const a;", "const int S::* const a;", Style);
428+
verifyFormat("int A::*const A::*p1;", Style);
429+
verifyFormat("float (C::*p)(int);", Style);
430+
verifyFormat("float (C::*const p)(int);", Style);
431+
verifyFormat("float (C::*p)(int) const;", Style);
432+
verifyFormat("float const (C::*p)(int);", "const float (C::*p)(int);", Style);
423433
}
424434

425435
TEST_F(QualifierFixerTest, LeftQualifier) {
@@ -565,6 +575,16 @@ TEST_F(QualifierFixerTest, LeftQualifier) {
565575

566576
// don't adjust macros
567577
verifyFormat("INTPTR const a;", "INTPTR const a;", Style);
578+
579+
// Pointers to members
580+
verifyFormat("int S::*a;", Style);
581+
verifyFormat("const int S::*a;", "int const S:: *a;", Style);
582+
verifyFormat("const int S::*const a;", "int const S::* const a;", Style);
583+
verifyFormat("int A::*const A::*p1;", Style);
584+
verifyFormat("float (C::*p)(int);", Style);
585+
verifyFormat("float (C::*const p)(int);", Style);
586+
verifyFormat("float (C::*p)(int) const;", Style);
587+
verifyFormat("const float (C::*p)(int);", "float const (C::*p)(int);", Style);
568588
}
569589

570590
TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) {

0 commit comments

Comments
 (0)