[clang-format] Don't move qualifiers past pointers-to-member
authorEmilia Dreamer <emilia@rymiel.space>
Sat, 25 Feb 2023 10:19:13 +0000 (12:19 +0200)
committerEmilia Dreamer <emilia@rymiel.space>
Sat, 25 Feb 2023 10:19:20 +0000 (12:19 +0200)
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

Reviewed By: owenpan, MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D144537

clang/lib/Format/QualifierAlignmentFixer.cpp
clang/unittests/Format/QualifierFixerTest.cpp

index cef8b36ff75827bc8362e37e07d900e942b2e35f..609b412380f86042ed85fe64743c2572c7c80d9c 100644 (file)
@@ -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.
index 875ad8353e017c54ce1fa28c9c4260ca564fb178..d320eacc52c8d5e4f3eea147ebdbb5a597265dfb 100755 (executable)
@@ -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) {
@@ -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) {