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