From b54a26d19d8a4de6941d083c7c2692fbc3555513 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Mon, 6 Jun 2016 19:40:12 +0000 Subject: [PATCH] clang-rename: implement renaming of classes inside static_cast "Derived" in static_cast(...) wasn't renamed, nor in its pointer equivalent. Reviewers: klimek Differential Revision: http://reviews.llvm.org/D21012 llvm-svn: 271933 --- .../clang-rename/USRLocFinder.cpp | 17 +++++++++++++ .../test/clang-rename/StaticCastExpr.cpp | 24 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 clang-tools-extra/test/clang-rename/StaticCastExpr.cpp diff --git a/clang-tools-extra/clang-rename/USRLocFinder.cpp b/clang-tools-extra/clang-rename/USRLocFinder.cpp index caf4f14900974..be110f152763d 100644 --- a/clang-tools-extra/clang-rename/USRLocFinder.cpp +++ b/clang-tools-extra/clang-rename/USRLocFinder.cpp @@ -123,6 +123,23 @@ class USRLocFindingASTVisitor return true; } + bool VisitCXXStaticCastExpr(clang::CXXStaticCastExpr *Expr) { + clang::QualType Type = Expr->getType(); + // See if this a cast of a pointer. + const RecordDecl* Decl = Type->getPointeeCXXRecordDecl(); + if (!Decl) { + // See if this is a cast of a reference. + Decl = Type->getAsCXXRecordDecl(); + } + + if (Decl && getUSRForDecl(Decl) == USR) { + SourceLocation Location = Expr->getTypeInfoAsWritten()->getTypeLoc().getBeginLoc(); + LocationsFound.push_back(Location); + } + + return true; + } + // Non-visitors: // \brief Returns a list of unique locations. Duplicate or overlapping diff --git a/clang-tools-extra/test/clang-rename/StaticCastExpr.cpp b/clang-tools-extra/test/clang-rename/StaticCastExpr.cpp new file mode 100644 index 0000000000000..2dd80c9f64953 --- /dev/null +++ b/clang-tools-extra/test/clang-rename/StaticCastExpr.cpp @@ -0,0 +1,24 @@ +// RUN: cat %s > %t.cpp +// RUN: clang-rename -offset=150 -new-name=X %t.cpp -i -- +// RUN: sed 's,//.*,,' %t.cpp | FileCheck %s +class Base { +}; + +class Derived : public Base { +public: + int getValue() const { + return 0; + } +}; + +int main() { + Derived D; + const Base &Reference = D; + const Base *Pointer = &D; + + static_cast(Reference).getValue(); // CHECK: static_cast + static_cast(Pointer)->getValue(); // CHECK: static_cast +} + +// Use grep -FUbo 'Derived' to get the correct offset of foo when changing +// this file.