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 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 85 | // Expression visitors: |
| 86 | |
| 87 | bool VisitDeclRefExpr(const DeclRefExpr *Expr) { |
| 88 | const auto *Decl = Expr->getFoundDecl(); |
| 89 | |
| 90 | checkNestedNameSpecifierLoc(Expr->getQualifierLoc()); |
| 91 | if (getUSRForDecl(Decl) == USR) { |
| 92 | LocationsFound.push_back(Expr->getLocation()); |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool VisitMemberExpr(const MemberExpr *Expr) { |
| 99 | const auto *Decl = Expr->getFoundDecl().getDecl(); |
| 100 | if (getUSRForDecl(Decl) == USR) { |
| 101 | LocationsFound.push_back(Expr->getMemberLoc()); |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // Non-visitors: |
| 107 | |
| 108 | // \brief Returns a list of unique locations. Duplicate or overlapping |
| 109 | // locations are erroneous and should be reported! |
| 110 | const std::vector<clang::SourceLocation> &getLocationsFound() const { |
| 111 | return LocationsFound; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | // Namespace traversal: |
| 116 | void checkNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) { |
| 117 | while (NameLoc) { |
| 118 | const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace(); |
| 119 | if (Decl && getUSRForDecl(Decl) == USR) |
| 120 | LocationsFound.push_back(NameLoc.getLocalBeginLoc()); |
| 121 | NameLoc = NameLoc.getPrefix(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // All the locations of the USR were found. |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 126 | StringRef USR; |
| 127 | // Old name that is renamed. |
| 128 | StringRef PrevName; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 129 | std::vector<clang::SourceLocation> LocationsFound; |
| 130 | }; |
| 131 | } // namespace |
| 132 | |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 133 | std::vector<SourceLocation> getLocationsOfUSR(StringRef USR, |
| 134 | StringRef PrevName, |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 135 | Decl *Decl) { |
Miklos Vajna | 1d48e50 | 2016-05-13 09:17:32 +0000 | [diff] [blame] | 136 | USRLocFindingASTVisitor visitor(USR, PrevName); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 137 | |
| 138 | visitor.TraverseDecl(Decl); |
| 139 | return visitor.getLocationsFound(); |
| 140 | } |
| 141 | |
| 142 | } // namespace rename |
| 143 | } // namespace clang |