Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 1 | //===--- tools/extra/clang-rename/USRLocFinder.cpp - Clang rename tool ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Mehtods for finding all instances of a USR. Our strategy is very |
| 12 | /// simple; we just compare the USR at every relevant AST node with the one |
| 13 | /// provided. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "USRLocFinder.h" |
| 18 | #include "USRFinder.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/RecursiveASTVisitor.h" |
| 21 | #include "clang/Basic/SourceLocation.h" |
| 22 | #include "clang/Index/USRGeneration.h" |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Lexer.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallVector.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | namespace clang { |
| 29 | namespace rename { |
| 30 | |
| 31 | namespace { |
| 32 | // \brief This visitor recursively searches for all instances of a USR in a |
| 33 | // translation unit and stores them for later usage. |
| 34 | class USRLocFindingASTVisitor |
| 35 | : public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> { |
| 36 | public: |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 37 | explicit USRLocFindingASTVisitor(StringRef USR, StringRef PrevName) : USR(USR), PrevName(PrevName) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Declaration visitors: |
| 41 | |
| 42 | bool VisitNamedDecl(const NamedDecl *Decl) { |
| 43 | if (getUSRForDecl(Decl) == USR) { |
| 44 | LocationsFound.push_back(Decl->getLocation()); |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
Manuel Klimek | 13e2c1a | 2016-05-04 09:45:44 +0000 | [diff] [blame] | 49 | bool VisitVarDecl(clang::VarDecl *Decl) { |
| 50 | clang::QualType Type = Decl->getType(); |
| 51 | const clang::RecordDecl *RecordDecl = Type->getPointeeCXXRecordDecl(); |
| 52 | if (RecordDecl) { |
| 53 | if (getUSRForDecl(RecordDecl) == USR) { |
| 54 | // The declaration refers to a type that is to be renamed. |
| 55 | LocationsFound.push_back(Decl->getTypeSpecStartLoc()); |
| 56 | } |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 61 | bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) { |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 62 | const ASTContext &Context = ConstructorDecl->getASTContext(); |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 63 | for (clang::CXXConstructorDecl::init_const_iterator it = ConstructorDecl->init_begin(); it != ConstructorDecl->init_end(); ++it) { |
| 64 | const clang::CXXCtorInitializer* Initializer = *it; |
Miklos Vajna | 6477682 | 2016-05-11 08:08:07 +0000 | [diff] [blame] | 65 | if (Initializer->getSourceOrder() == -1) { |
| 66 | // Ignore implicit initializers. |
| 67 | continue; |
| 68 | } |
| 69 | |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 70 | if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) { |
| 71 | if (getUSRForDecl(FieldDecl) == USR) { |
| 72 | // The initializer refers to a field that is to be renamed. |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 73 | SourceLocation Location = Initializer->getSourceLocation(); |
| 74 | StringRef TokenName = Lexer::getSourceText(CharSourceRange::getTokenRange(Location), Context.getSourceManager(), Context.getLangOpts()); |
| 75 | if (TokenName == PrevName) { |
| 76 | // The token of the source location we find actually has the old name. |
| 77 | LocationsFound.push_back(Initializer->getSourceLocation()); |
| 78 | } |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
Miklos Vajna | 5a6d298 | 2016-05-18 16:12:48 +0000 | [diff] [blame] | 82 | |
| 83 | if (getUSRForDecl(ConstructorDecl) == USR) { |
| 84 | // This takes care of the class name part of a non-inline ctor definition. |
| 85 | LocationsFound.push_back(ConstructorDecl->getLocStart()); |
| 86 | } |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
Miklos Vajna | 7712bf3 | 2016-06-15 18:35:41 +0000 | [diff] [blame] | 90 | bool VisitCXXDestructorDecl(clang::CXXDestructorDecl *DestructorDecl) { |
| 91 | if (getUSRForDecl(DestructorDecl->getParent()) == USR) { |
| 92 | // Handles "~Foo" from "Foo::~Foo". |
| 93 | SourceLocation Location = DestructorDecl->getLocation(); |
| 94 | const ASTContext &Context = DestructorDecl->getASTContext(); |
Artem Belevich | 0ccbe69 | 2016-06-15 23:04:42 +0000 | [diff] [blame] | 95 | StringRef LLVM_ATTRIBUTE_UNUSED TokenName = Lexer::getSourceText( |
Miklos Vajna | 7712bf3 | 2016-06-15 18:35:41 +0000 | [diff] [blame] | 96 | CharSourceRange::getTokenRange(Location), Context.getSourceManager(), |
| 97 | Context.getLangOpts()); |
| 98 | // 1 is the length of the "~" string that is not to be touched by the |
| 99 | // rename. |
| 100 | assert(TokenName.startswith("~")); |
| 101 | LocationsFound.push_back(Location.getLocWithOffset(1)); |
| 102 | |
| 103 | if (DestructorDecl->isThisDeclarationADefinition()) { |
| 104 | // Handles "Foo" from "Foo::~Foo". |
| 105 | LocationsFound.push_back(DestructorDecl->getLocStart()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 112 | // Expression visitors: |
| 113 | |
| 114 | bool VisitDeclRefExpr(const DeclRefExpr *Expr) { |
| 115 | const auto *Decl = Expr->getFoundDecl(); |
| 116 | |
| 117 | checkNestedNameSpecifierLoc(Expr->getQualifierLoc()); |
| 118 | if (getUSRForDecl(Decl) == USR) { |
Miklos Vajna | 10e2574 | 2016-05-24 19:08:53 +0000 | [diff] [blame] | 119 | const SourceManager &Manager = Decl->getASTContext().getSourceManager(); |
| 120 | SourceLocation Location = Manager.getSpellingLoc(Expr->getLocation()); |
| 121 | LocationsFound.push_back(Location); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | bool VisitMemberExpr(const MemberExpr *Expr) { |
| 128 | const auto *Decl = Expr->getFoundDecl().getDecl(); |
| 129 | if (getUSRForDecl(Decl) == USR) { |
Miklos Vajna | ed28d41 | 2016-05-20 11:43:59 +0000 | [diff] [blame] | 130 | const SourceManager &Manager = Decl->getASTContext().getSourceManager(); |
| 131 | SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc()); |
| 132 | LocationsFound.push_back(Location); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Miklos Vajna | 617409f | 2016-06-02 20:00:22 +0000 | [diff] [blame] | 137 | bool VisitCXXConstructExpr(const CXXConstructExpr *Expr) { |
| 138 | CXXConstructorDecl *Decl = Expr->getConstructor(); |
| 139 | |
| 140 | if (getUSRForDecl(Decl) == USR) { |
| 141 | // This takes care of 'new <name>' expressions. |
| 142 | LocationsFound.push_back(Expr->getLocation()); |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 148 | bool VisitCXXStaticCastExpr(clang::CXXStaticCastExpr *Expr) { |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame] | 149 | return handleCXXNamedCastExpr(Expr); |
| 150 | } |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 151 | |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame] | 152 | bool VisitCXXDynamicCastExpr(clang::CXXDynamicCastExpr *Expr) { |
| 153 | return handleCXXNamedCastExpr(Expr); |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Miklos Vajna | 6ff8f25 | 2016-06-13 18:50:45 +0000 | [diff] [blame] | 156 | bool VisitCXXReinterpretCastExpr(clang::CXXReinterpretCastExpr *Expr) { |
| 157 | return handleCXXNamedCastExpr(Expr); |
| 158 | } |
| 159 | |
| 160 | bool VisitCXXConstCastExpr(clang::CXXConstCastExpr *Expr) { |
| 161 | return handleCXXNamedCastExpr(Expr); |
| 162 | } |
| 163 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 164 | // Non-visitors: |
| 165 | |
| 166 | // \brief Returns a list of unique locations. Duplicate or overlapping |
| 167 | // locations are erroneous and should be reported! |
| 168 | const std::vector<clang::SourceLocation> &getLocationsFound() const { |
| 169 | return LocationsFound; |
| 170 | } |
| 171 | |
| 172 | private: |
| 173 | // Namespace traversal: |
| 174 | void checkNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) { |
| 175 | while (NameLoc) { |
| 176 | const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace(); |
| 177 | if (Decl && getUSRForDecl(Decl) == USR) |
| 178 | LocationsFound.push_back(NameLoc.getLocalBeginLoc()); |
| 179 | NameLoc = NameLoc.getPrefix(); |
| 180 | } |
| 181 | } |
| 182 | |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame] | 183 | bool handleCXXNamedCastExpr(clang::CXXNamedCastExpr *Expr) { |
| 184 | clang::QualType Type = Expr->getType(); |
| 185 | // See if this a cast of a pointer. |
| 186 | const RecordDecl* Decl = Type->getPointeeCXXRecordDecl(); |
| 187 | if (!Decl) { |
| 188 | // See if this is a cast of a reference. |
| 189 | Decl = Type->getAsCXXRecordDecl(); |
| 190 | } |
| 191 | |
| 192 | if (Decl && getUSRForDecl(Decl) == USR) { |
| 193 | SourceLocation Location = Expr->getTypeInfoAsWritten()->getTypeLoc().getBeginLoc(); |
| 194 | LocationsFound.push_back(Location); |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 200 | // All the locations of the USR were found. |
Miklos Vajna | a7445f1 | 2016-05-17 18:17:16 +0000 | [diff] [blame] | 201 | const std::string USR; |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 202 | // Old name that is renamed. |
Miklos Vajna | a7445f1 | 2016-05-17 18:17:16 +0000 | [diff] [blame] | 203 | const std::string PrevName; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 204 | std::vector<clang::SourceLocation> LocationsFound; |
| 205 | }; |
| 206 | } // namespace |
| 207 | |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 208 | std::vector<SourceLocation> getLocationsOfUSR(StringRef USR, |
| 209 | StringRef PrevName, |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 210 | Decl *Decl) { |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 211 | USRLocFindingASTVisitor visitor(USR, PrevName); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 212 | |
| 213 | visitor.TraverseDecl(Decl); |
| 214 | return visitor.getLocationsFound(); |
| 215 | } |
| 216 | |
| 217 | } // namespace rename |
| 218 | } // namespace clang |