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: |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 37 | explicit USRLocFindingASTVisitor(const std::vector<std::string> &USRs, |
| 38 | StringRef PrevName, |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 39 | const ASTContext &Context) |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 40 | : USRSet(USRs.begin(), USRs.end()), PrevName(PrevName), Context(Context) { |
| 41 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 42 | |
| 43 | // Declaration visitors: |
| 44 | |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 45 | bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) { |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 46 | for (auto &Initializer : ConstructorDecl->inits()) { |
Miklos Vajna | 6477682 | 2016-05-11 08:08:07 +0000 | [diff] [blame] | 47 | if (Initializer->getSourceOrder() == -1) { |
| 48 | // Ignore implicit initializers. |
| 49 | continue; |
| 50 | } |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 51 | if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) { |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 52 | if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end()) { |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 53 | // The initializer refers to a field that is to be renamed. |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 54 | SourceLocation Location = Initializer->getSourceLocation(); |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 55 | StringRef TokenName = Lexer::getSourceText( |
| 56 | CharSourceRange::getTokenRange(Location), |
| 57 | Context.getSourceManager(), Context.getLangOpts()); |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 58 | if (TokenName == PrevName) { |
Benjamin Kramer | 1afefc0 | 2016-07-14 09:46:03 +0000 | [diff] [blame] | 59 | // The token of the source location we find actually has the old |
| 60 | // name. |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 61 | LocationsFound.push_back(Initializer->getSourceLocation()); |
| 62 | } |
Miklos Vajna | 65f088f | 2016-05-07 14:32:59 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 69 | bool VisitNamedDecl(const NamedDecl *Decl) { |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 70 | if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) { |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 71 | checkAndAddLocation(Decl->getLocation()); |
Miklos Vajna | 7712bf3 | 2016-06-15 18:35:41 +0000 | [diff] [blame] | 72 | } |
Miklos Vajna | 7712bf3 | 2016-06-15 18:35:41 +0000 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 76 | // Expression visitors: |
| 77 | |
| 78 | bool VisitDeclRefExpr(const DeclRefExpr *Expr) { |
| 79 | const auto *Decl = Expr->getFoundDecl(); |
| 80 | |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 81 | if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) { |
Miklos Vajna | 10e2574 | 2016-05-24 19:08:53 +0000 | [diff] [blame] | 82 | const SourceManager &Manager = Decl->getASTContext().getSourceManager(); |
| 83 | SourceLocation Location = Manager.getSpellingLoc(Expr->getLocation()); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 84 | checkAndAddLocation(Location); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool VisitMemberExpr(const MemberExpr *Expr) { |
| 91 | const auto *Decl = Expr->getFoundDecl().getDecl(); |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 92 | if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) { |
Miklos Vajna | ed28d41 | 2016-05-20 11:43:59 +0000 | [diff] [blame] | 93 | const SourceManager &Manager = Decl->getASTContext().getSourceManager(); |
| 94 | SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc()); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 95 | checkAndAddLocation(Location); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 100 | // Other visitors: |
| 101 | |
| 102 | bool VisitTypeLoc(const TypeLoc Loc) { |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 103 | if (USRSet.find(getUSRForDecl(Loc.getType()->getAsCXXRecordDecl())) != |
| 104 | USRSet.end()) { |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 105 | checkAndAddLocation(Loc.getBeginLoc()); |
| 106 | } |
Kirill Bobyrev | 9e0dab9 | 2016-08-02 09:38:38 +0000 | [diff] [blame^] | 107 | if (const auto *TemplateTypeParm = |
| 108 | dyn_cast<TemplateTypeParmType>(Loc.getType())) { |
| 109 | if (USRSet.find(getUSRForDecl(TemplateTypeParm->getDecl())) != |
| 110 | USRSet.end()) { |
| 111 | checkAndAddLocation(Loc.getBeginLoc()); |
| 112 | } |
| 113 | } |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 117 | // Non-visitors: |
| 118 | |
| 119 | // \brief Returns a list of unique locations. Duplicate or overlapping |
| 120 | // locations are erroneous and should be reported! |
| 121 | const std::vector<clang::SourceLocation> &getLocationsFound() const { |
| 122 | return LocationsFound; |
| 123 | } |
| 124 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 125 | // Namespace traversal: |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 126 | void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 127 | while (NameLoc) { |
| 128 | const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace(); |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 129 | if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) { |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 130 | checkAndAddLocation(NameLoc.getLocalBeginLoc()); |
| 131 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 132 | NameLoc = NameLoc.getPrefix(); |
| 133 | } |
| 134 | } |
| 135 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 136 | private: |
| 137 | void checkAndAddLocation(SourceLocation Loc) { |
| 138 | const auto BeginLoc = Loc; |
| 139 | const auto EndLoc = Lexer::getLocForEndOfToken( |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 140 | BeginLoc, 0, Context.getSourceManager(), Context.getLangOpts()); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 141 | StringRef TokenName = |
| 142 | Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc), |
| 143 | Context.getSourceManager(), Context.getLangOpts()); |
| 144 | size_t Offset = TokenName.find(PrevName); |
| 145 | if (Offset != StringRef::npos) { |
| 146 | // The token of the source location we find actually has the old |
| 147 | // name. |
| 148 | LocationsFound.push_back(BeginLoc.getLocWithOffset(Offset)); |
| 149 | } |
| 150 | } |
| 151 | |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 152 | const std::set<std::string> USRSet; |
Miklos Vajna | a7445f1 | 2016-05-17 18:17:16 +0000 | [diff] [blame] | 153 | const std::string PrevName; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 154 | std::vector<clang::SourceLocation> LocationsFound; |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 155 | const ASTContext &Context; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 156 | }; |
| 157 | } // namespace |
| 158 | |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 159 | std::vector<SourceLocation> |
| 160 | getLocationsOfUSRs(const std::vector<std::string> &USRs, StringRef PrevName, |
| 161 | Decl *Decl) { |
| 162 | USRLocFindingASTVisitor Visitor(USRs, PrevName, Decl->getASTContext()); |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 163 | Visitor.TraverseDecl(Decl); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 164 | NestedNameSpecifierLocFinder Finder(Decl->getASTContext()); |
| 165 | for (const auto &Location : Finder.getNestedNameSpecifierLocations()) { |
| 166 | Visitor.handleNestedNameSpecifierLoc(Location); |
| 167 | } |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 168 | return Visitor.getLocationsFound(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } // namespace rename |
| 172 | } // namespace clang |