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: |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 37 | explicit USRLocFindingASTVisitor(StringRef USR, StringRef PrevName) |
| 38 | : USR(USR), PrevName(PrevName) {} |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 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(); |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 63 | for (auto &Initializer : ConstructorDecl->inits()) { |
Miklos Vajna | 6477682 | 2016-05-11 08:08:07 +0000 | [diff] [blame] | 64 | if (Initializer->getSourceOrder() == -1) { |
| 65 | // Ignore implicit initializers. |
| 66 | continue; |
| 67 | } |
| 68 | |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 69 | if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) { |
| 70 | if (getUSRForDecl(FieldDecl) == USR) { |
| 71 | // The initializer refers to a field that is to be renamed. |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 72 | SourceLocation Location = Initializer->getSourceLocation(); |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 73 | StringRef TokenName = Lexer::getSourceText( |
| 74 | CharSourceRange::getTokenRange(Location), |
| 75 | Context.getSourceManager(), Context.getLangOpts()); |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 76 | if (TokenName == PrevName) { |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 77 | // The token of the source location we find actually has the old |
| 78 | // name. |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 79 | LocationsFound.push_back(Initializer->getSourceLocation()); |
| 80 | } |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
Miklos Vajna | 5a6d298 | 2016-05-18 16:12:48 +0000 | [diff] [blame] | 84 | |
| 85 | if (getUSRForDecl(ConstructorDecl) == USR) { |
| 86 | // This takes care of the class name part of a non-inline ctor definition. |
| 87 | LocationsFound.push_back(ConstructorDecl->getLocStart()); |
| 88 | } |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
Miklos Vajna | 7712bf3 | 2016-06-15 18:35:41 +0000 | [diff] [blame] | 92 | bool VisitCXXDestructorDecl(clang::CXXDestructorDecl *DestructorDecl) { |
| 93 | if (getUSRForDecl(DestructorDecl->getParent()) == USR) { |
| 94 | // Handles "~Foo" from "Foo::~Foo". |
| 95 | SourceLocation Location = DestructorDecl->getLocation(); |
| 96 | const ASTContext &Context = DestructorDecl->getASTContext(); |
Artem Belevich | 0ccbe69 | 2016-06-15 23:04:42 +0000 | [diff] [blame] | 97 | StringRef LLVM_ATTRIBUTE_UNUSED TokenName = Lexer::getSourceText( |
Miklos Vajna | 7712bf3 | 2016-06-15 18:35:41 +0000 | [diff] [blame] | 98 | CharSourceRange::getTokenRange(Location), Context.getSourceManager(), |
| 99 | Context.getLangOpts()); |
| 100 | // 1 is the length of the "~" string that is not to be touched by the |
| 101 | // rename. |
| 102 | assert(TokenName.startswith("~")); |
| 103 | LocationsFound.push_back(Location.getLocWithOffset(1)); |
| 104 | |
| 105 | if (DestructorDecl->isThisDeclarationADefinition()) { |
| 106 | // Handles "Foo" from "Foo::~Foo". |
| 107 | LocationsFound.push_back(DestructorDecl->getLocStart()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 114 | // Expression visitors: |
| 115 | |
| 116 | bool VisitDeclRefExpr(const DeclRefExpr *Expr) { |
| 117 | const auto *Decl = Expr->getFoundDecl(); |
| 118 | |
| 119 | checkNestedNameSpecifierLoc(Expr->getQualifierLoc()); |
| 120 | if (getUSRForDecl(Decl) == USR) { |
Miklos Vajna | 10e2574 | 2016-05-24 19:08:53 +0000 | [diff] [blame] | 121 | const SourceManager &Manager = Decl->getASTContext().getSourceManager(); |
| 122 | SourceLocation Location = Manager.getSpellingLoc(Expr->getLocation()); |
| 123 | LocationsFound.push_back(Location); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | bool VisitMemberExpr(const MemberExpr *Expr) { |
| 130 | const auto *Decl = Expr->getFoundDecl().getDecl(); |
| 131 | if (getUSRForDecl(Decl) == USR) { |
Miklos Vajna | ed28d41 | 2016-05-20 11:43:59 +0000 | [diff] [blame] | 132 | const SourceManager &Manager = Decl->getASTContext().getSourceManager(); |
| 133 | SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc()); |
| 134 | LocationsFound.push_back(Location); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
Miklos Vajna | 617409f | 2016-06-02 20:00:22 +0000 | [diff] [blame] | 139 | bool VisitCXXConstructExpr(const CXXConstructExpr *Expr) { |
| 140 | CXXConstructorDecl *Decl = Expr->getConstructor(); |
| 141 | |
| 142 | if (getUSRForDecl(Decl) == USR) { |
| 143 | // This takes care of 'new <name>' expressions. |
| 144 | LocationsFound.push_back(Expr->getLocation()); |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 150 | bool VisitCXXStaticCastExpr(clang::CXXStaticCastExpr *Expr) { |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame] | 151 | return handleCXXNamedCastExpr(Expr); |
| 152 | } |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 153 | |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame] | 154 | bool VisitCXXDynamicCastExpr(clang::CXXDynamicCastExpr *Expr) { |
| 155 | return handleCXXNamedCastExpr(Expr); |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Miklos Vajna | 6ff8f25 | 2016-06-13 18:50:45 +0000 | [diff] [blame] | 158 | bool VisitCXXReinterpretCastExpr(clang::CXXReinterpretCastExpr *Expr) { |
| 159 | return handleCXXNamedCastExpr(Expr); |
| 160 | } |
| 161 | |
| 162 | bool VisitCXXConstCastExpr(clang::CXXConstCastExpr *Expr) { |
| 163 | return handleCXXNamedCastExpr(Expr); |
| 164 | } |
| 165 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 166 | // Non-visitors: |
| 167 | |
| 168 | // \brief Returns a list of unique locations. Duplicate or overlapping |
| 169 | // locations are erroneous and should be reported! |
| 170 | const std::vector<clang::SourceLocation> &getLocationsFound() const { |
| 171 | return LocationsFound; |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | // Namespace traversal: |
| 176 | void checkNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) { |
| 177 | while (NameLoc) { |
| 178 | const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace(); |
| 179 | if (Decl && getUSRForDecl(Decl) == USR) |
| 180 | LocationsFound.push_back(NameLoc.getLocalBeginLoc()); |
| 181 | NameLoc = NameLoc.getPrefix(); |
| 182 | } |
| 183 | } |
| 184 | |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame] | 185 | bool handleCXXNamedCastExpr(clang::CXXNamedCastExpr *Expr) { |
| 186 | clang::QualType Type = Expr->getType(); |
| 187 | // See if this a cast of a pointer. |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 188 | const RecordDecl *Decl = Type->getPointeeCXXRecordDecl(); |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame] | 189 | if (!Decl) { |
| 190 | // See if this is a cast of a reference. |
| 191 | Decl = Type->getAsCXXRecordDecl(); |
| 192 | } |
| 193 | |
| 194 | if (Decl && getUSRForDecl(Decl) == USR) { |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 195 | SourceLocation Location = |
| 196 | Expr->getTypeInfoAsWritten()->getTypeLoc().getBeginLoc(); |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame] | 197 | LocationsFound.push_back(Location); |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 203 | // All the locations of the USR were found. |
Miklos Vajna | a7445f1 | 2016-05-17 18:17:16 +0000 | [diff] [blame] | 204 | const std::string USR; |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 205 | // Old name that is renamed. |
Miklos Vajna | a7445f1 | 2016-05-17 18:17:16 +0000 | [diff] [blame] | 206 | const std::string PrevName; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 207 | std::vector<clang::SourceLocation> LocationsFound; |
| 208 | }; |
| 209 | } // namespace |
| 210 | |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 211 | std::vector<SourceLocation> getLocationsOfUSR(StringRef USR, StringRef PrevName, |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 212 | Decl *Decl) { |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame^] | 213 | USRLocFindingASTVisitor Visitor(USR, PrevName); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 214 | |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame^] | 215 | Visitor.TraverseDecl(Decl); |
| 216 | return Visitor.getLocationsFound(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | } // namespace rename |
| 220 | } // namespace clang |