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 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 90 | // Expression visitors: |
| 91 | |
| 92 | bool VisitDeclRefExpr(const DeclRefExpr *Expr) { |
| 93 | const auto *Decl = Expr->getFoundDecl(); |
| 94 | |
| 95 | checkNestedNameSpecifierLoc(Expr->getQualifierLoc()); |
| 96 | if (getUSRForDecl(Decl) == USR) { |
Miklos Vajna | 10e2574 | 2016-05-24 19:08:53 +0000 | [diff] [blame] | 97 | const SourceManager &Manager = Decl->getASTContext().getSourceManager(); |
| 98 | SourceLocation Location = Manager.getSpellingLoc(Expr->getLocation()); |
| 99 | LocationsFound.push_back(Location); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool VisitMemberExpr(const MemberExpr *Expr) { |
| 106 | const auto *Decl = Expr->getFoundDecl().getDecl(); |
| 107 | if (getUSRForDecl(Decl) == USR) { |
Miklos Vajna | ed28d41 | 2016-05-20 11:43:59 +0000 | [diff] [blame] | 108 | const SourceManager &Manager = Decl->getASTContext().getSourceManager(); |
| 109 | SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc()); |
| 110 | LocationsFound.push_back(Location); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
Miklos Vajna | 617409f | 2016-06-02 20:00:22 +0000 | [diff] [blame] | 115 | bool VisitCXXConstructExpr(const CXXConstructExpr *Expr) { |
| 116 | CXXConstructorDecl *Decl = Expr->getConstructor(); |
| 117 | |
| 118 | if (getUSRForDecl(Decl) == USR) { |
| 119 | // This takes care of 'new <name>' expressions. |
| 120 | LocationsFound.push_back(Expr->getLocation()); |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 126 | bool VisitCXXStaticCastExpr(clang::CXXStaticCastExpr *Expr) { |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame^] | 127 | return handleCXXNamedCastExpr(Expr); |
| 128 | } |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 129 | |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame^] | 130 | bool VisitCXXDynamicCastExpr(clang::CXXDynamicCastExpr *Expr) { |
| 131 | return handleCXXNamedCastExpr(Expr); |
Miklos Vajna | b54a26d | 2016-06-06 19:40:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 134 | // Non-visitors: |
| 135 | |
| 136 | // \brief Returns a list of unique locations. Duplicate or overlapping |
| 137 | // locations are erroneous and should be reported! |
| 138 | const std::vector<clang::SourceLocation> &getLocationsFound() const { |
| 139 | return LocationsFound; |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | // Namespace traversal: |
| 144 | void checkNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) { |
| 145 | while (NameLoc) { |
| 146 | const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace(); |
| 147 | if (Decl && getUSRForDecl(Decl) == USR) |
| 148 | LocationsFound.push_back(NameLoc.getLocalBeginLoc()); |
| 149 | NameLoc = NameLoc.getPrefix(); |
| 150 | } |
| 151 | } |
| 152 | |
Miklos Vajna | 85b7b1c | 2016-06-08 18:38:23 +0000 | [diff] [blame^] | 153 | bool handleCXXNamedCastExpr(clang::CXXNamedCastExpr *Expr) { |
| 154 | clang::QualType Type = Expr->getType(); |
| 155 | // See if this a cast of a pointer. |
| 156 | const RecordDecl* Decl = Type->getPointeeCXXRecordDecl(); |
| 157 | if (!Decl) { |
| 158 | // See if this is a cast of a reference. |
| 159 | Decl = Type->getAsCXXRecordDecl(); |
| 160 | } |
| 161 | |
| 162 | if (Decl && getUSRForDecl(Decl) == USR) { |
| 163 | SourceLocation Location = Expr->getTypeInfoAsWritten()->getTypeLoc().getBeginLoc(); |
| 164 | LocationsFound.push_back(Location); |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 170 | // All the locations of the USR were found. |
Miklos Vajna | a7445f1 | 2016-05-17 18:17:16 +0000 | [diff] [blame] | 171 | const std::string USR; |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 172 | // Old name that is renamed. |
Miklos Vajna | a7445f1 | 2016-05-17 18:17:16 +0000 | [diff] [blame] | 173 | const std::string PrevName; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 174 | std::vector<clang::SourceLocation> LocationsFound; |
| 175 | }; |
| 176 | } // namespace |
| 177 | |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 178 | std::vector<SourceLocation> getLocationsOfUSR(StringRef USR, |
| 179 | StringRef PrevName, |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 180 | Decl *Decl) { |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 181 | USRLocFindingASTVisitor visitor(USR, PrevName); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 182 | |
| 183 | visitor.TraverseDecl(Decl); |
| 184 | return visitor.getLocationsFound(); |
| 185 | } |
| 186 | |
| 187 | } // namespace rename |
| 188 | } // namespace clang |