]> granicus.if.org Git - clang/commitdiff
[rename] Don't overwrite the template argument when renaming a template function.
authorHaojian Wu <hokein@google.com>
Mon, 23 Oct 2017 08:58:50 +0000 (08:58 +0000)
committerHaojian Wu <hokein@google.com>
Mon, 23 Oct 2017 08:58:50 +0000 (08:58 +0000)
Reviewers: ioeric

Reviewed By: ioeric

Subscribers: cierpuchaw, cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316314 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
unittests/Rename/RenameFunctionTest.cpp

index 265e3c2072c6e42f298c0218cdf6de14f18c310a..38b2a624eaefaf8cbd2f52203046b8a40cc34092 100644 (file)
@@ -221,7 +221,12 @@ public:
     }
 
     auto StartLoc = Expr->getLocStart();
-    auto EndLoc = Expr->getLocEnd();
+    // For template function call expressions like `foo<int>()`, we want to
+    // restrict the end of location to just before the `<` character.
+    SourceLocation EndLoc = Expr->hasExplicitTemplateArgs()
+                                ? Expr->getLAngleLoc().getLocWithOffset(-1)
+                                : Expr->getLocEnd();
+
     // In case of renaming an enum declaration, we have to explicitly handle
     // unscoped enum constants referenced in expressions (e.g.
     // "auto r = ns1::ns2::Green" where Green is an enum constant of an unscoped
index ef84b4bdb72bd9b7eb22c8b1e955285bec33be9f..b27bbe273af8fa19819863b9979d9316abb43cd7 100644 (file)
@@ -220,6 +220,25 @@ TEST_F(RenameFunctionTest, RenameFunctionDecls) {
   CompareSnippets(Expected, After);
 }
 
+TEST_F(RenameFunctionTest, RenameTemplateFunctions) {
+  std::string Before = R"(
+      namespace na {
+      template<typename T> T X();
+      }
+      namespace na { void f() { X<int>(); } }
+      namespace nb { void g() { na::X          <int>(); } }
+      )";
+  std::string Expected = R"(
+      namespace na {
+      template<typename T> T Y();
+      }
+      namespace na { void f() { nb::Y<int>(); } }
+      namespace nb { void g() { Y<int>(); } }
+      )";
+  std::string After = runClangRenameOnCode(Before, "na::X", "nb::Y");
+  CompareSnippets(Expected, After);
+}
+
 TEST_F(RenameFunctionTest, RenameOutOfLineFunctionDecls) {
   std::string Before = R"(
       namespace na {