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