blob: d3c64baf742ea092ef028bd292b0597c62e49c9f [file] [log] [blame]
Manuel Klimekde237262014-08-20 01:39:05 +00001//===--- 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 Vajna1d48e502016-05-13 09:17:32 +000023#include "clang/Lex/Lexer.h"
Manuel Klimekde237262014-08-20 01:39:05 +000024#include "llvm/ADT/SmallVector.h"
25
26using namespace llvm;
27
28namespace clang {
29namespace rename {
30
31namespace {
32// \brief This visitor recursively searches for all instances of a USR in a
33// translation unit and stores them for later usage.
34class USRLocFindingASTVisitor
35 : public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> {
36public:
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000037 explicit USRLocFindingASTVisitor(const std::vector<std::string> &USRs,
38 StringRef PrevName,
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000039 const ASTContext &Context)
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000040 : USRSet(USRs.begin(), USRs.end()), PrevName(PrevName), Context(Context) {
41 }
Manuel Klimekde237262014-08-20 01:39:05 +000042
43 // Declaration visitors:
44
Miklos Vajna65f088f2016-05-07 14:32:59 +000045 bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
Benjamin Kramer1afefc02016-07-14 09:46:03 +000046 for (auto &Initializer : ConstructorDecl->inits()) {
Miklos Vajna64776822016-05-11 08:08:07 +000047 if (Initializer->getSourceOrder() == -1) {
48 // Ignore implicit initializers.
49 continue;
50 }
Miklos Vajna65f088f2016-05-07 14:32:59 +000051 if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) {
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000052 if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end()) {
Miklos Vajna65f088f2016-05-07 14:32:59 +000053 // The initializer refers to a field that is to be renamed.
Miklos Vajna1d48e502016-05-13 09:17:32 +000054 SourceLocation Location = Initializer->getSourceLocation();
Benjamin Kramer1afefc02016-07-14 09:46:03 +000055 StringRef TokenName = Lexer::getSourceText(
56 CharSourceRange::getTokenRange(Location),
57 Context.getSourceManager(), Context.getLangOpts());
Miklos Vajna1d48e502016-05-13 09:17:32 +000058 if (TokenName == PrevName) {
Benjamin Kramer1afefc02016-07-14 09:46:03 +000059 // The token of the source location we find actually has the old
60 // name.
Miklos Vajna1d48e502016-05-13 09:17:32 +000061 LocationsFound.push_back(Initializer->getSourceLocation());
62 }
Miklos Vajna65f088f2016-05-07 14:32:59 +000063 }
64 }
65 }
66 return true;
67 }
68
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000069 bool VisitNamedDecl(const NamedDecl *Decl) {
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000070 if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000071 checkAndAddLocation(Decl->getLocation());
Miklos Vajna7712bf32016-06-15 18:35:41 +000072 }
Miklos Vajna7712bf32016-06-15 18:35:41 +000073 return true;
74 }
75
Manuel Klimekde237262014-08-20 01:39:05 +000076 // Expression visitors:
77
78 bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
79 const auto *Decl = Expr->getFoundDecl();
80
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000081 if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
Miklos Vajna10e25742016-05-24 19:08:53 +000082 const SourceManager &Manager = Decl->getASTContext().getSourceManager();
83 SourceLocation Location = Manager.getSpellingLoc(Expr->getLocation());
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000084 checkAndAddLocation(Location);
Manuel Klimekde237262014-08-20 01:39:05 +000085 }
86
87 return true;
88 }
89
90 bool VisitMemberExpr(const MemberExpr *Expr) {
91 const auto *Decl = Expr->getFoundDecl().getDecl();
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000092 if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
Miklos Vajnaed28d412016-05-20 11:43:59 +000093 const SourceManager &Manager = Decl->getASTContext().getSourceManager();
94 SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc());
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000095 checkAndAddLocation(Location);
Manuel Klimekde237262014-08-20 01:39:05 +000096 }
97 return true;
98 }
99
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000100 // Other visitors:
101
102 bool VisitTypeLoc(const TypeLoc Loc) {
Kirill Bobyrev83d5d562016-07-29 10:16:45 +0000103 if (USRSet.find(getUSRForDecl(Loc.getType()->getAsCXXRecordDecl())) !=
104 USRSet.end()) {
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000105 checkAndAddLocation(Loc.getBeginLoc());
106 }
Kirill Bobyrev9e0dab92016-08-02 09:38:38 +0000107 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 Bobyreva3432fa2016-07-22 13:41:09 +0000114 return true;
115 }
116
Manuel Klimekde237262014-08-20 01:39:05 +0000117 // 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 Klimekde237262014-08-20 01:39:05 +0000125 // Namespace traversal:
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000126 void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
Manuel Klimekde237262014-08-20 01:39:05 +0000127 while (NameLoc) {
128 const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
Kirill Bobyrev83d5d562016-07-29 10:16:45 +0000129 if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000130 checkAndAddLocation(NameLoc.getLocalBeginLoc());
131 }
Manuel Klimekde237262014-08-20 01:39:05 +0000132 NameLoc = NameLoc.getPrefix();
133 }
134 }
135
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000136private:
137 void checkAndAddLocation(SourceLocation Loc) {
138 const auto BeginLoc = Loc;
139 const auto EndLoc = Lexer::getLocForEndOfToken(
Kirill Bobyrev83d5d562016-07-29 10:16:45 +0000140 BeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000141 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 Bobyrev83d5d562016-07-29 10:16:45 +0000152 const std::set<std::string> USRSet;
Miklos Vajnaa7445f12016-05-17 18:17:16 +0000153 const std::string PrevName;
Manuel Klimekde237262014-08-20 01:39:05 +0000154 std::vector<clang::SourceLocation> LocationsFound;
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000155 const ASTContext &Context;
Manuel Klimekde237262014-08-20 01:39:05 +0000156};
157} // namespace
158
Kirill Bobyrev83d5d562016-07-29 10:16:45 +0000159std::vector<SourceLocation>
160getLocationsOfUSRs(const std::vector<std::string> &USRs, StringRef PrevName,
161 Decl *Decl) {
162 USRLocFindingASTVisitor Visitor(USRs, PrevName, Decl->getASTContext());
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000163 Visitor.TraverseDecl(Decl);
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000164 NestedNameSpecifierLocFinder Finder(Decl->getASTContext());
165 for (const auto &Location : Finder.getNestedNameSpecifierLocations()) {
166 Visitor.handleNestedNameSpecifierLoc(Location);
167 }
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000168 return Visitor.getLocationsFound();
Manuel Klimekde237262014-08-20 01:39:05 +0000169}
170
171} // namespace rename
172} // namespace clang