[clang-tools-extra] 51df1a9 - [clangd] Fix add-using tweak on declrefs with template arguments

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed May 24 00:57:37 PDT 2023


Author: Kadir Cetinkaya
Date: 2023-05-24T09:56:53+02:00
New Revision: 51df1a9ac96f0cf588e97d3174d5c16ed9577b96

URL: https://github.com/llvm/llvm-project/commit/51df1a9ac96f0cf588e97d3174d5c16ed9577b96
DIFF: https://github.com/llvm/llvm-project/commit/51df1a9ac96f0cf588e97d3174d5c16ed9577b96.diff

LOG: [clangd] Fix add-using tweak on declrefs with template arguments

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
    clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
index 5ca45f94ea9f0..ca96da34e0920 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -288,11 +288,18 @@ bool AddUsing::prepare(const Selection &Inputs) {
   if (Node == nullptr)
     return false;
 
+  // Closed range for the fully qualified name as spelled in source code.
   SourceRange SpelledNameRange;
   if (auto *D = Node->ASTNode.get<DeclRefExpr>()) {
     if (D->getDecl()->getIdentifier()) {
       QualifierToRemove = D->getQualifierLoc();
+      // Use the name range rather than expr, as the latter can contain template
+      // arguments in the range.
       SpelledNameRange = D->getSourceRange();
+      // Remove the template arguments from the name, as they shouldn't be
+      // spelled in the using declaration.
+      if (auto AngleLoc = D->getLAngleLoc(); AngleLoc.isValid())
+        SpelledNameRange.setEnd(AngleLoc.getLocWithOffset(-1));
       MustInsertAfterLoc = D->getDecl()->getBeginLoc();
     }
   } else if (auto *T = Node->ASTNode.get<TypeLoc>()) {

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
index f3a479a9a240f..da76ecad14554 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
@@ -498,6 +498,30 @@ void foo() {
   switch(one::two::ee{}) { case ee_one:break; }
 }
 )cpp"},
+      {R"cpp(
+#include "test.hpp"
+void foo() {
+  one::f^unc_temp<int>();
+})cpp",
+       R"cpp(
+#include "test.hpp"
+using one::func_temp;
+
+void foo() {
+  func_temp<int>();
+})cpp"},
+      {R"cpp(
+#include "test.hpp"
+void foo() {
+  one::va^r_temp<int>;
+})cpp",
+       R"cpp(
+#include "test.hpp"
+using one::var_temp;
+
+void foo() {
+  var_temp<int>;
+})cpp"},
   };
   llvm::StringMap<std::string> EditedFiles;
   for (const auto &Case : Cases) {
@@ -515,6 +539,8 @@ class cc {
 }
 using uu = two::cc;
 template<typename T> struct vec {};
+template <typename T> void func_temp();
+template <typename T> T var_temp();
 })cpp";
     // Typo correction is disabled in msvc-compatibility mode.
     ExtraArgs.push_back("-fno-ms-compatibility");


        


More information about the cfe-commits mailing list