[clang-tools-extra] [clangd] Preserve Qualified Names in `Override pure virtual methods` tweak (PR #163726)

Marco Maia via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 16 02:34:12 PDT 2025


https://github.com/marcogmaia created https://github.com/llvm/llvm-project/pull/163726

Prevents the tweak from splitting **qualified names** (e.g., `foo::Type`) by incorrectly inserting a space around the scope resolution (`::`).

**Before:**

```cpp
// input:
virtual foo::Type::func() = 0 
// output:
foo :: Type :: func()
```

**After:**

```cpp
// input:
virtual foo::Type::func() = 0 
// output:
foo::Type::func()
```


>From 61c200dbcf46e032c8cd4252c3012499a5557a78 Mon Sep 17 00:00:00 2001
From: Marco Maia <marcogmaia at gmail.com>
Date: Thu, 16 Oct 2025 06:02:08 -0300
Subject: [PATCH 1/2] Don't add space between coloncolon in qualified names

---
 .../refactor/tweaks/OverridePureVirtuals.cpp  |  5 +--
 .../tweaks/OverridePureVirtualsTests.cpp      | 32 +++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clangd/refactor/tweaks/OverridePureVirtuals.cpp b/clang-tools-extra/clangd/refactor/tweaks/OverridePureVirtuals.cpp
index 16febeca70809..b557066d979f5 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/OverridePureVirtuals.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/OverridePureVirtuals.cpp
@@ -79,7 +79,7 @@
 
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclCXX.h"
-#include "clang/AST/Type.h"
+#include "clang/AST/TypeBase.h"
 #include "clang/AST/TypeLoc.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
@@ -116,7 +116,8 @@ std::string removePureVirtualSyntax(const std::string &MethodDecl,
 
     DeclString += Tk.text();
     if (Tk.Kind != tok::l_paren && Next.Kind != tok::comma &&
-        Next.Kind != tok::r_paren && Next.Kind != tok::l_paren)
+        Next.Kind != tok::r_paren && Next.Kind != tok::l_paren &&
+        Tk.Kind != tok::coloncolon && Next.Kind != tok::coloncolon)
       DeclString += ' ';
   }
   // Trim the last whitespace.
diff --git a/clang-tools-extra/clangd/unittests/tweaks/OverridePureVirtualsTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/OverridePureVirtualsTests.cpp
index b7dcbee1650ec..30d5826fa09d0 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/OverridePureVirtualsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/OverridePureVirtualsTests.cpp
@@ -715,6 +715,38 @@ class D : public B {
   EXPECT_EQ(Expected, Applied) << "Applied result:\n" << Applied;
 }
 
+TEST_F(OverridePureVirtualsTests, QualifiedNames) {
+  constexpr auto Before = R"cpp(
+namespace foo { struct S{}; }
+
+class B {
+public:
+  virtual foo::S foo(int var = 0) = 0;
+};
+
+class ^D : public B {};
+)cpp";
+
+  constexpr auto Expected = R"cpp(
+namespace foo { struct S{}; }
+
+class B {
+public:
+  virtual foo::S foo(int var = 0) = 0;
+};
+
+class D : public B {
+public:
+  foo::S foo(int var = 0) override {
+    // TODO: Implement this pure virtual method.
+    static_assert(false, "Method `foo` is not implemented.");
+  }
+};
+)cpp";
+  auto Applied = apply(Before);
+  EXPECT_EQ(Expected, Applied) << "Applied result:\n" << Applied;
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang

>From 2803e76c6566f2ecca5e999ec45bccf6737f77cd Mon Sep 17 00:00:00 2001
From: Marco Maia <marcogmaia at gmail.com>
Date: Thu, 16 Oct 2025 06:30:55 -0300
Subject: [PATCH 2/2] Add nested namespaces

---
 .../unittests/tweaks/OverridePureVirtualsTests.cpp    | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clangd/unittests/tweaks/OverridePureVirtualsTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/OverridePureVirtualsTests.cpp
index 30d5826fa09d0..72095ab2f5982 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/OverridePureVirtualsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/OverridePureVirtualsTests.cpp
@@ -717,22 +717,24 @@ class D : public B {
 
 TEST_F(OverridePureVirtualsTests, QualifiedNames) {
   constexpr auto Before = R"cpp(
-namespace foo { struct S{}; }
+namespace foo { struct S{}; namespace bar { struct S2{}; } }
 
 class B {
 public:
   virtual foo::S foo(int var = 0) = 0;
+  virtual foo::bar::S2 bar(int var = 0) = 0;
 };
 
 class ^D : public B {};
 )cpp";
 
   constexpr auto Expected = R"cpp(
-namespace foo { struct S{}; }
+namespace foo { struct S{}; namespace bar { struct S2{}; } }
 
 class B {
 public:
   virtual foo::S foo(int var = 0) = 0;
+  virtual foo::bar::S2 bar(int var = 0) = 0;
 };
 
 class D : public B {
@@ -741,6 +743,11 @@ class D : public B {
     // TODO: Implement this pure virtual method.
     static_assert(false, "Method `foo` is not implemented.");
   }
+
+  foo::bar::S2 bar(int var = 0) override {
+    // TODO: Implement this pure virtual method.
+    static_assert(false, "Method `bar` is not implemented.");
+  }
 };
 )cpp";
   auto Applied = apply(Before);



More information about the cfe-commits mailing list