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

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 24 00:46:41 PDT 2023


kadircet created this revision.
kadircet added a reviewer: VitaNuo.
Herald added a subscriber: arphaman.
Herald added a project: All.
kadircet requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151303

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


Index: clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
+++ clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
@@ -498,6 +498,30 @@
   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 @@
 }
 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");
Index: clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -288,11 +288,18 @@
   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>()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151303.525042.patch
Type: text/x-patch
Size: 2190 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230524/f61cf7dd/attachment-0001.bin>


More information about the cfe-commits mailing list