blob: 8d4c7e42360041a71051ea84a88983a6ff51ae9d [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) {
Kirill Bobyrev7fa33952016-08-09 10:03:33 +000046 for (const auto *Initializer : ConstructorDecl->inits()) {
47 if (!Initializer->isWritten()) {
Miklos Vajna64776822016-05-11 08:08:07 +000048 // Ignore implicit initializers.
49 continue;
50 }
Kirill Bobyrev31fd7fb2016-08-09 07:14:48 +000051 if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) {
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000052 if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end()) {
Kirill Bobyrev31fd7fb2016-08-09 07:14:48 +000053 LocationsFound.push_back(Initializer->getSourceLocation());
Miklos Vajna65f088f2016-05-07 14:32:59 +000054 }
55 }
56 }
57 return true;
58 }
59
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000060 bool VisitNamedDecl(const NamedDecl *Decl) {
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000061 if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000062 checkAndAddLocation(Decl->getLocation());
Miklos Vajna7712bf32016-06-15 18:35:41 +000063 }
Miklos Vajna7712bf32016-06-15 18:35:41 +000064 return true;
65 }
66
Manuel Klimekde237262014-08-20 01:39:05 +000067 // Expression visitors:
68
69 bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
70 const auto *Decl = Expr->getFoundDecl();
71
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000072 if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
Miklos Vajna10e25742016-05-24 19:08:53 +000073 const SourceManager &Manager = Decl->getASTContext().getSourceManager();
74 SourceLocation Location = Manager.getSpellingLoc(Expr->getLocation());
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000075 checkAndAddLocation(Location);
Manuel Klimekde237262014-08-20 01:39:05 +000076 }
77
78 return true;
79 }
80
81 bool VisitMemberExpr(const MemberExpr *Expr) {
82 const auto *Decl = Expr->getFoundDecl().getDecl();
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000083 if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
Miklos Vajnaed28d412016-05-20 11:43:59 +000084 const SourceManager &Manager = Decl->getASTContext().getSourceManager();
85 SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc());
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000086 checkAndAddLocation(Location);
Manuel Klimekde237262014-08-20 01:39:05 +000087 }
88 return true;
89 }
90
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000091 // Other visitors:
92
93 bool VisitTypeLoc(const TypeLoc Loc) {
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000094 if (USRSet.find(getUSRForDecl(Loc.getType()->getAsCXXRecordDecl())) !=
95 USRSet.end()) {
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000096 checkAndAddLocation(Loc.getBeginLoc());
97 }
Kirill Bobyrev9e0dab92016-08-02 09:38:38 +000098 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 Bobyreva3432fa2016-07-22 13:41:09 +0000105 return true;
106 }
107
Manuel Klimekde237262014-08-20 01:39:05 +0000108 // 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 Klimekde237262014-08-20 01:39:05 +0000116 // Namespace traversal:
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000117 void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
Manuel Klimekde237262014-08-20 01:39:05 +0000118 while (NameLoc) {
119 const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
Kirill Bobyrev83d5d562016-07-29 10:16:45 +0000120 if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000121 checkAndAddLocation(NameLoc.getLocalBeginLoc());
122 }
Manuel Klimekde237262014-08-20 01:39:05 +0000123 NameLoc = NameLoc.getPrefix();
124 }
125 }
126
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000127private:
128 void checkAndAddLocation(SourceLocation Loc) {
129 const auto BeginLoc = Loc;
130 const auto EndLoc = Lexer::getLocForEndOfToken(
Kirill Bobyrev83d5d562016-07-29 10:16:45 +0000131 BeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000132 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 Bobyrev83d5d562016-07-29 10:16:45 +0000143 const std::set<std::string> USRSet;
Miklos Vajnaa7445f12016-05-17 18:17:16 +0000144 const std::string PrevName;
Manuel Klimekde237262014-08-20 01:39:05 +0000145 std::vector<clang::SourceLocation> LocationsFound;
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000146 const ASTContext &Context;
Manuel Klimekde237262014-08-20 01:39:05 +0000147};
148} // namespace
149
Kirill Bobyrev83d5d562016-07-29 10:16:45 +0000150std::vector<SourceLocation>
151getLocationsOfUSRs(const std::vector<std::string> &USRs, StringRef PrevName,
152 Decl *Decl) {
153 USRLocFindingASTVisitor Visitor(USRs, PrevName, Decl->getASTContext());
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000154 Visitor.TraverseDecl(Decl);
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000155 NestedNameSpecifierLocFinder Finder(Decl->getASTContext());
156 for (const auto &Location : Finder.getNestedNameSpecifierLocations()) {
157 Visitor.handleNestedNameSpecifierLoc(Location);
158 }
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000159 return Visitor.getLocationsFound();
Manuel Klimekde237262014-08-20 01:39:05 +0000160}
161
162} // namespace rename
163} // namespace clang