[clang-tools-extra] [clangd] Fix erroneous qualification of template type parameters (PR #116821)

Christian Kandeler via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 19 07:06:17 PST 2024


https://github.com/ckandeler created https://github.com/llvm/llvm-project/pull/116821

...in DefineOutline tweak.
E.g. moving the following definition:
  template<typename T> struct S { T f^oo() const { return T(); } };
would result in:
  // ...
  template<typename T> S<T>::T S::foo() const { return T(); }
instead of:
  template<typename T> T S::foo() const { return T(); }

>From 00f85ed13e689331adbbc3513dde09023dfc68d4 Mon Sep 17 00:00:00 2001
From: Christian Kandeler <christian.kandeler at qt.io>
Date: Tue, 19 Nov 2024 16:00:29 +0100
Subject: [PATCH] [clangd] Fix erroneous qualification of template type
 parameters

...in DefineOutline tweak.
E.g. moving the following definition:
  template<typename T> struct S { T f^oo() const { return T(); } };
would result in:
  // ...
  template<typename T> S<T>::T S::foo() const { return T(); }
instead of:
  template<typename T> T S::foo() const { return T(); }
---
 .../clangd/refactor/tweaks/DefineOutline.cpp         |  2 ++
 .../clangd/unittests/tweaks/DefineOutlineTests.cpp   | 12 ++++++------
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
index 2a96b305bd7a25..789c10bdd4822a 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
@@ -239,6 +239,8 @@ getFunctionSourceCode(const FunctionDecl *FD, const DeclContext *TargetContext,
           return;
 
         for (const NamedDecl *ND : Ref.Targets) {
+          if (ND->getKind() == Decl::TemplateTypeParm)
+            return;
           if (ND->getDeclContext() != Ref.Targets.front()->getDeclContext()) {
             elog("Targets from multiple contexts: {0}, {1}",
                  printQualifiedName(*Ref.Targets.front()),
diff --git a/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
index 1af1bc31bf6486..d2d2ae9e7bb610 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
@@ -411,14 +411,14 @@ inline typename O1<T, U...>::template O2<V, A>::E O1<T, U...>::template O2<V, A>
           R"cpp(
             struct Foo {
               template <typename T, bool B = true>
-              void ^bar() {}
+              T ^bar() { return {}; }
             };)cpp",
           R"cpp(
             struct Foo {
               template <typename T, bool B = true>
-              void bar() ;
+              T bar() ;
             };template <typename T, bool B>
-inline void Foo::bar() {}
+inline T Foo::bar() { return {}; }
 )cpp",
           ""},
 
@@ -426,14 +426,14 @@ inline void Foo::bar() {}
       {
           R"cpp(
             template <typename T> struct Foo {
-              template <typename U> void ^bar(const T& t, const U& u) {}
+              template <typename U> T ^bar(const T& t, const U& u) { return {}; }
             };)cpp",
           R"cpp(
             template <typename T> struct Foo {
-              template <typename U> void bar(const T& t, const U& u) ;
+              template <typename U> T bar(const T& t, const U& u) ;
             };template <typename T>
 template <typename U>
-inline void Foo<T>::bar(const T& t, const U& u) {}
+inline T Foo<T>::bar(const T& t, const U& u) { return {}; }
 )cpp",
           ""},
   };



More information about the cfe-commits mailing list