omtcyfz created this revision.
omtcyfz added reviewers: alexfh, klimek.
omtcyfz added a subscriber: cfe-commits.
Few simple tweaks allow template parameters to be renamed. See
TemplateTypenameFindBy{TemplateParam|TypeInside}.cpp for example how it works.
https://reviews.llvm.org/D22853
Files:
clang-rename/USRFinder.cpp
clang-rename/USRLocFinder.cpp
test/clang-rename/TemplateTypename.cpp
test/clang-rename/TemplateTypenameFindByTemplateParam.cpp
test/clang-rename/TemplateTypenameFindByTypeInside.cpp
Index: test/clang-rename/TemplateTypenameFindByTypeInside.cpp
===================================================================
--- test/clang-rename/TemplateTypenameFindByTypeInside.cpp
+++ test/clang-rename/TemplateTypenameFindByTypeInside.cpp
@@ -1,11 +1,7 @@
// RUN: cat %s > %t.cpp
-// RUN: clang-rename -offset=350 -new-name=U %t.cpp -i --
+// RUN: clang-rename -offset=205 -new-name=U %t.cpp -i --
// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s
-// Currently unsupported test.
-// FIXME: clang-rename should be able to rename template parameters correctly.
-// XFAIL: *
-
template <typename T> // CHECK: template <typename U>
class Foo {
T foo(T arg, T& ref, T* ptr) { // CHECK: U foo(U arg, U& ref, U* ptr) {
Index: test/clang-rename/TemplateTypenameFindByTemplateParam.cpp
===================================================================
--- test/clang-rename/TemplateTypenameFindByTemplateParam.cpp
+++ test/clang-rename/TemplateTypenameFindByTemplateParam.cpp
@@ -1,11 +1,7 @@
// RUN: cat %s > %t.cpp
-// RUN: clang-rename -offset=270 -new-name=U %t.cpp -i --
+// RUN: clang-rename -offset=147 -new-name=U %t.cpp -i --
// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s
-// Currently unsupported test.
-// FIXME: clang-rename should be able to rename template parameters correctly.
-// XFAIL: *
-
template <typename T> // CHECK: template <typename U>
class Foo {
T foo(T arg, T& ref, T* ptr) { // CHECK: U foo(U arg, U& ref, U* ptr) {
Index: test/clang-rename/TemplateTypename.cpp
===================================================================
--- test/clang-rename/TemplateTypename.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-// Currently unsupported test.
-// RUN: cat %s > %t.cpp
-// FIXME: clang-rename should be able to rename template parameters correctly.
-
-template <typename T>
-T foo(T arg, T& ref, T* ptr) {
- T value;
- int number = 42;
- value = (T)number;
- value = static_cast<T>(number);
- return value;
-}
Index: clang-rename/USRLocFinder.cpp
===================================================================
--- clang-rename/USRLocFinder.cpp
+++ clang-rename/USRLocFinder.cpp
@@ -117,6 +117,11 @@
if (getUSRForDecl(Loc.getType()->getAsCXXRecordDecl()) == USR) {
checkAndAddLocation(Loc.getBeginLoc());
}
+ if (const auto *TTPT = dyn_cast<TemplateTypeParmType>(Loc.getType())) {
+ if (getUSRForDecl(TTPT->getDecl()) == USR) {
+ checkAndAddLocation(Loc.getBeginLoc());
+ }
+ }
return true;
}
@@ -161,8 +166,7 @@
void checkAndAddLocation(SourceLocation Loc) {
const auto BeginLoc = Loc;
const auto EndLoc = Lexer::getLocForEndOfToken(
- BeginLoc, 0, Context.getSourceManager(),
- Context.getLangOpts());
+ BeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
StringRef TokenName =
Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
Context.getSourceManager(),
Context.getLangOpts());
Index: clang-rename/USRFinder.cpp
===================================================================
--- clang-rename/USRFinder.cpp
+++ clang-rename/USRFinder.cpp
@@ -75,7 +75,10 @@
bool VisitTypeLoc(const TypeLoc Loc) {
const auto TypeBeginLoc = Loc.getBeginLoc();
const auto TypeEndLoc = Lexer::getLocForEndOfToken(
- TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
+ TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
+ if (const auto *TTPT = dyn_cast<TemplateTypeParmType>(Loc.getType())) {
+ return setResult(TTPT->getDecl(), TypeBeginLoc, TypeEndLoc);
+ }
return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc,
TypeEndLoc);
}
Index: test/clang-rename/TemplateTypenameFindByTypeInside.cpp
===================================================================
--- test/clang-rename/TemplateTypenameFindByTypeInside.cpp
+++ test/clang-rename/TemplateTypenameFindByTypeInside.cpp
@@ -1,11 +1,7 @@
// RUN: cat %s > %t.cpp
-// RUN: clang-rename -offset=350 -new-name=U %t.cpp -i --
+// RUN: clang-rename -offset=205 -new-name=U %t.cpp -i --
// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s
-// Currently unsupported test.
-// FIXME: clang-rename should be able to rename template parameters correctly.
-// XFAIL: *
-
template <typename T> // CHECK: template <typename U>
class Foo {
T foo(T arg, T& ref, T* ptr) { // CHECK: U foo(U arg, U& ref, U* ptr) {
Index: test/clang-rename/TemplateTypenameFindByTemplateParam.cpp
===================================================================
--- test/clang-rename/TemplateTypenameFindByTemplateParam.cpp
+++ test/clang-rename/TemplateTypenameFindByTemplateParam.cpp
@@ -1,11 +1,7 @@
// RUN: cat %s > %t.cpp
-// RUN: clang-rename -offset=270 -new-name=U %t.cpp -i --
+// RUN: clang-rename -offset=147 -new-name=U %t.cpp -i --
// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s
-// Currently unsupported test.
-// FIXME: clang-rename should be able to rename template parameters correctly.
-// XFAIL: *
-
template <typename T> // CHECK: template <typename U>
class Foo {
T foo(T arg, T& ref, T* ptr) { // CHECK: U foo(U arg, U& ref, U* ptr) {
Index: test/clang-rename/TemplateTypename.cpp
===================================================================
--- test/clang-rename/TemplateTypename.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-// Currently unsupported test.
-// RUN: cat %s > %t.cpp
-// FIXME: clang-rename should be able to rename template parameters correctly.
-
-template <typename T>
-T foo(T arg, T& ref, T* ptr) {
- T value;
- int number = 42;
- value = (T)number;
- value = static_cast<T>(number);
- return value;
-}
Index: clang-rename/USRLocFinder.cpp
===================================================================
--- clang-rename/USRLocFinder.cpp
+++ clang-rename/USRLocFinder.cpp
@@ -117,6 +117,11 @@
if (getUSRForDecl(Loc.getType()->getAsCXXRecordDecl()) == USR) {
checkAndAddLocation(Loc.getBeginLoc());
}
+ if (const auto *TTPT = dyn_cast<TemplateTypeParmType>(Loc.getType())) {
+ if (getUSRForDecl(TTPT->getDecl()) == USR) {
+ checkAndAddLocation(Loc.getBeginLoc());
+ }
+ }
return true;
}
@@ -161,8 +166,7 @@
void checkAndAddLocation(SourceLocation Loc) {
const auto BeginLoc = Loc;
const auto EndLoc = Lexer::getLocForEndOfToken(
- BeginLoc, 0, Context.getSourceManager(),
- Context.getLangOpts());
+ BeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
StringRef TokenName =
Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
Context.getSourceManager(), Context.getLangOpts());
Index: clang-rename/USRFinder.cpp
===================================================================
--- clang-rename/USRFinder.cpp
+++ clang-rename/USRFinder.cpp
@@ -75,7 +75,10 @@
bool VisitTypeLoc(const TypeLoc Loc) {
const auto TypeBeginLoc = Loc.getBeginLoc();
const auto TypeEndLoc = Lexer::getLocForEndOfToken(
- TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
+ TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
+ if (const auto *TTPT = dyn_cast<TemplateTypeParmType>(Loc.getType())) {
+ return setResult(TTPT->getDecl(), TypeBeginLoc, TypeEndLoc);
+ }
return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc,
TypeEndLoc);
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits