blob: a554250ef0d23c1c43eace28710b4c4662b1fa50 [file] [log] [blame]
Manuel Klimekde237262014-08-20 01:39:05 +00001//===--- tools/extra/clang-rename/USRFinder.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 Implements a recursive AST visitor that finds the USR of a symbol at a
11/// point.
12///
13//===----------------------------------------------------------------------===//
14
15#include "USRFinder.h"
16#include "clang/AST/AST.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/RecursiveASTVisitor.h"
Manuel Klimekde237262014-08-20 01:39:05 +000019#include "clang/Index/USRGeneration.h"
Chandler Carruth3cbd71c2015-01-14 11:24:38 +000020#include "clang/Lex/Lexer.h"
Manuel Klimekde237262014-08-20 01:39:05 +000021#include "llvm/ADT/SmallVector.h"
22
23using namespace llvm;
24
25namespace clang {
26namespace rename {
27
28// NamedDeclFindingASTVisitor recursively visits each AST node to find the
29// symbol underneath the cursor.
30// FIXME: move to seperate .h/.cc file if this gets too large.
31namespace {
32class NamedDeclFindingASTVisitor
33 : public clang::RecursiveASTVisitor<NamedDeclFindingASTVisitor> {
34public:
35 // \brief Finds the NamedDecl at a point in the source.
36 // \param Point the location in the source to search for the NamedDecl.
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +000037 explicit NamedDeclFindingASTVisitor(const SourceLocation Point,
38 const ASTContext &Context)
39 : Result(nullptr), Point(Point), Context(Context) {}
Manuel Klimekde237262014-08-20 01:39:05 +000040
Miklos Vajna47bd4632016-06-21 19:48:57 +000041 // \brief Finds the NamedDecl for a name in the source.
42 // \param Name the fully qualified name.
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +000043 explicit NamedDeclFindingASTVisitor(const std::string &Name,
44 const ASTContext &Context)
45 : Result(nullptr), Name(Name), Context(Context) {}
Miklos Vajna47bd4632016-06-21 19:48:57 +000046
Manuel Klimekde237262014-08-20 01:39:05 +000047 // Declaration visitors:
48
49 // \brief Checks if the point falls within the NameDecl. This covers every
50 // declaration of a named entity that we may come across. Usually, just
51 // checking if the point lies within the length of the name of the declaration
52 // and the start location is sufficient.
53 bool VisitNamedDecl(const NamedDecl *Decl) {
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +000054 return dyn_cast<CXXConversionDecl>(Decl)
55 ? true
56 : setResult(Decl, Decl->getLocation(),
57 Decl->getNameAsString().length());
Manuel Klimekde237262014-08-20 01:39:05 +000058 }
59
60 // Expression visitors:
61
62 bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
Kirill Bobyrev6b7d8c22016-08-15 23:20:05 +000063 const NamedDecl *Decl = Expr->getFoundDecl();
Manuel Klimekde237262014-08-20 01:39:05 +000064 return setResult(Decl, Expr->getLocation(),
65 Decl->getNameAsString().length());
66 }
67
68 bool VisitMemberExpr(const MemberExpr *Expr) {
Kirill Bobyrev6b7d8c22016-08-15 23:20:05 +000069 const NamedDecl *Decl = Expr->getFoundDecl().getDecl();
Manuel Klimekde237262014-08-20 01:39:05 +000070 return setResult(Decl, Expr->getMemberLoc(),
71 Decl->getNameAsString().length());
72 }
73
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000074 // Other visitors:
75
76 bool VisitTypeLoc(const TypeLoc Loc) {
Kirill Bobyrev6b7d8c22016-08-15 23:20:05 +000077 const SourceLocation TypeBeginLoc = Loc.getBeginLoc();
78 const SourceLocation TypeEndLoc = Lexer::getLocForEndOfToken(
79 TypeBeginLoc, 0, Context.getSourceManager(),
80 Context.getLangOpts());
Kirill Bobyrev9e0dab92016-08-02 09:38:38 +000081 if (const auto *TemplateTypeParm =
82 dyn_cast<TemplateTypeParmType>(Loc.getType())) {
83 return setResult(TemplateTypeParm->getDecl(), TypeBeginLoc, TypeEndLoc);
84 }
Kirill Bobyrevd6ab7d42016-08-03 23:00:32 +000085 if (const auto *TemplateSpecType =
86 dyn_cast<TemplateSpecializationType>(Loc.getType())) {
87 return setResult(TemplateSpecType->getTemplateName().getAsTemplateDecl(),
88 TypeBeginLoc, TypeEndLoc);
89 }
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +000090 return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc,
91 TypeEndLoc);
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000092 }
93
Kirill Bobyrev31fd7fb2016-08-09 07:14:48 +000094 bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
Kirill Bobyrev7fa33952016-08-09 10:03:33 +000095 for (const auto *Initializer : ConstructorDecl->inits()) {
96 if (!Initializer->isWritten()) {
Kirill Bobyrev31fd7fb2016-08-09 07:14:48 +000097 // Ignore implicit initializers.
98 continue;
99 }
100 if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) {
101 const SourceLocation InitBeginLoc = Initializer->getSourceLocation(),
102 InitEndLoc = Lexer::getLocForEndOfToken(
103 InitBeginLoc, 0, Context.getSourceManager(),
104 Context.getLangOpts());
105 if (!setResult(FieldDecl, InitBeginLoc, InitEndLoc)) {
106 return false;
107 }
108 }
109 }
110 return true;
111 }
112
Manuel Klimekde237262014-08-20 01:39:05 +0000113 // Other:
114
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000115 const NamedDecl *getNamedDecl() { return Result; }
116
117 // \brief Determines if a namespace qualifier contains the point.
118 // \returns false on success and sets Result.
119 void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
120 while (NameLoc) {
Kirill Bobyrev6b7d8c22016-08-15 23:20:05 +0000121 const NamespaceDecl *Decl =
122 NameLoc.getNestedNameSpecifier()->getAsNamespace();
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000123 setResult(Decl, NameLoc.getLocalBeginLoc(), NameLoc.getLocalEndLoc());
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000124 NameLoc = NameLoc.getPrefix();
125 }
Manuel Klimekde237262014-08-20 01:39:05 +0000126 }
127
128private:
Manuel Klimekde237262014-08-20 01:39:05 +0000129 // \brief Sets Result to Decl if the Point is within Start and End.
130 // \returns false on success.
131 bool setResult(const NamedDecl *Decl, SourceLocation Start,
132 SourceLocation End) {
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000133 if (!Decl) {
134 return true;
135 }
Miklos Vajna47bd4632016-06-21 19:48:57 +0000136 if (Name.empty()) {
137 // Offset is used to find the declaration.
138 if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||
139 !End.isFileID() || !isPointWithin(Start, End)) {
140 return true;
141 }
142 } else {
143 // Fully qualified name is used to find the declaration.
144 if (Name != Decl->getQualifiedNameAsString()) {
145 return true;
146 }
Manuel Klimekde237262014-08-20 01:39:05 +0000147 }
148 Result = Decl;
149 return false;
150 }
151
152 // \brief Sets Result to Decl if Point is within Loc and Loc + Offset.
153 // \returns false on success.
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000154 bool setResult(const NamedDecl *Decl, SourceLocation Loc, unsigned Offset) {
Manuel Klimekde237262014-08-20 01:39:05 +0000155 // FIXME: Add test for Offset == 0. Add test for Offset - 1 (vs -2 etc).
156 return Offset == 0 ||
157 setResult(Decl, Loc, Loc.getLocWithOffset(Offset - 1));
158 }
159
160 // \brief Determines if the Point is within Start and End.
161 bool isPointWithin(const SourceLocation Start, const SourceLocation End) {
162 // FIXME: Add tests for Point == End.
163 return Point == Start || Point == End ||
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000164 (Context.getSourceManager().isBeforeInTranslationUnit(Start,
165 Point) &&
166 Context.getSourceManager().isBeforeInTranslationUnit(Point, End));
Manuel Klimekde237262014-08-20 01:39:05 +0000167 }
168
169 const NamedDecl *Result;
Manuel Klimekde237262014-08-20 01:39:05 +0000170 const SourceLocation Point; // The location to find the NamedDecl.
Miklos Vajna47bd4632016-06-21 19:48:57 +0000171 const std::string Name;
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000172 const ASTContext &Context;
Manuel Klimekde237262014-08-20 01:39:05 +0000173};
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000174} // namespace
Manuel Klimekde237262014-08-20 01:39:05 +0000175
176const NamedDecl *getNamedDeclAt(const ASTContext &Context,
177 const SourceLocation Point) {
Kirill Bobyrev6b7d8c22016-08-15 23:20:05 +0000178 StringRef SearchFile = Context.getSourceManager().getFilename(Point);
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000179 NamedDeclFindingASTVisitor Visitor(Point, Context);
Manuel Klimekde237262014-08-20 01:39:05 +0000180
181 // We only want to search the decls that exist in the same file as the point.
Kirill Bobyrev6b7d8c22016-08-15 23:20:05 +0000182 for (const auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) {
183 const SourceLocation FileLoc = CurrDecl->getLocStart();
184 StringRef FileName = Context.getSourceManager().getFilename(FileLoc);
Manuel Klimekde237262014-08-20 01:39:05 +0000185 // FIXME: Add test.
186 if (FileName == SearchFile) {
187 Visitor.TraverseDecl(CurrDecl);
Manuel Klimekde237262014-08-20 01:39:05 +0000188 }
189 }
190
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000191 NestedNameSpecifierLocFinder Finder(const_cast<ASTContext &>(Context));
192 for (const auto &Location : Finder.getNestedNameSpecifierLocations()) {
193 Visitor.handleNestedNameSpecifierLoc(Location);
194 }
195
196 return Visitor.getNamedDecl();
Manuel Klimekde237262014-08-20 01:39:05 +0000197}
198
Miklos Vajna47bd4632016-06-21 19:48:57 +0000199const NamedDecl *getNamedDeclFor(const ASTContext &Context,
200 const std::string &Name) {
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000201 NamedDeclFindingASTVisitor Visitor(Name, Context);
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000202 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
Miklos Vajna47bd4632016-06-21 19:48:57 +0000203
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000204 return Visitor.getNamedDecl();
Miklos Vajna47bd4632016-06-21 19:48:57 +0000205}
206
Manuel Klimekde237262014-08-20 01:39:05 +0000207std::string getUSRForDecl(const Decl *Decl) {
208 llvm::SmallVector<char, 128> Buff;
209
210 // FIXME: Add test for the nullptr case.
211 if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff))
212 return "";
213
214 return std::string(Buff.data(), Buff.size());
215}
216
Manuel Klimekde237262014-08-20 01:39:05 +0000217} // namespace rename
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000218} // namespace clang