blob: 809cfcf460f88d15193d05f43d20aac2be80b6d4 [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.
37 explicit NamedDeclFindingASTVisitor(const SourceManager &SourceMgr,
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000038 const SourceLocation Point,
39 const ASTContext *Context)
40 : Result(nullptr), SourceMgr(SourceMgr), Point(Point), Context(Context) {}
Manuel Klimekde237262014-08-20 01:39:05 +000041
Miklos Vajna47bd4632016-06-21 19:48:57 +000042 // \brief Finds the NamedDecl for a name in the source.
43 // \param Name the fully qualified name.
44 explicit NamedDeclFindingASTVisitor(const SourceManager &SourceMgr,
45 const std::string &Name)
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000046 : Result(nullptr), SourceMgr(SourceMgr), Name(Name) {}
Miklos Vajna47bd4632016-06-21 19:48:57 +000047
Manuel Klimekde237262014-08-20 01:39:05 +000048 // Declaration visitors:
49
50 // \brief Checks if the point falls within the NameDecl. This covers every
51 // declaration of a named entity that we may come across. Usually, just
52 // checking if the point lies within the length of the name of the declaration
53 // and the start location is sufficient.
54 bool VisitNamedDecl(const NamedDecl *Decl) {
55 return setResult(Decl, Decl->getLocation(),
56 Decl->getNameAsString().length());
57 }
58
59 // Expression visitors:
60
61 bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
Manuel Klimekde237262014-08-20 01:39:05 +000062 const auto *Decl = Expr->getFoundDecl();
63 return setResult(Decl, Expr->getLocation(),
64 Decl->getNameAsString().length());
65 }
66
67 bool VisitMemberExpr(const MemberExpr *Expr) {
68 const auto *Decl = Expr->getFoundDecl().getDecl();
69 return setResult(Decl, Expr->getMemberLoc(),
70 Decl->getNameAsString().length());
71 }
72
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000073 // Other visitors:
74
75 bool VisitTypeLoc(const TypeLoc Loc) {
76 const auto TypeBeginLoc = Loc.getBeginLoc();
77 const auto TypeEndLoc = Lexer::getLocForEndOfToken(
78 TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
79 return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc,
80 TypeEndLoc);
81 }
82
Manuel Klimekde237262014-08-20 01:39:05 +000083 // Other:
84
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000085 const NamedDecl *getNamedDecl() { return Result; }
86
87 // \brief Determines if a namespace qualifier contains the point.
88 // \returns false on success and sets Result.
89 void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
90 while (NameLoc) {
91 const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
92 if (Decl) {
93 setResult(Decl, NameLoc.getLocalBeginLoc(), NameLoc.getLocalEndLoc());
94 }
95 NameLoc = NameLoc.getPrefix();
96 }
Manuel Klimekde237262014-08-20 01:39:05 +000097 }
98
99private:
Manuel Klimekde237262014-08-20 01:39:05 +0000100 // \brief Sets Result to Decl if the Point is within Start and End.
101 // \returns false on success.
102 bool setResult(const NamedDecl *Decl, SourceLocation Start,
103 SourceLocation End) {
Miklos Vajna47bd4632016-06-21 19:48:57 +0000104 if (Name.empty()) {
105 // Offset is used to find the declaration.
106 if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||
107 !End.isFileID() || !isPointWithin(Start, End)) {
108 return true;
109 }
110 } else {
111 // Fully qualified name is used to find the declaration.
112 if (Name != Decl->getQualifiedNameAsString()) {
113 return true;
114 }
Manuel Klimekde237262014-08-20 01:39:05 +0000115 }
116 Result = Decl;
117 return false;
118 }
119
120 // \brief Sets Result to Decl if Point is within Loc and Loc + Offset.
121 // \returns false on success.
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000122 bool setResult(const NamedDecl *Decl, SourceLocation Loc, unsigned Offset) {
Manuel Klimekde237262014-08-20 01:39:05 +0000123 // FIXME: Add test for Offset == 0. Add test for Offset - 1 (vs -2 etc).
124 return Offset == 0 ||
125 setResult(Decl, Loc, Loc.getLocWithOffset(Offset - 1));
126 }
127
128 // \brief Determines if the Point is within Start and End.
129 bool isPointWithin(const SourceLocation Start, const SourceLocation End) {
130 // FIXME: Add tests for Point == End.
131 return Point == Start || Point == End ||
132 (SourceMgr.isBeforeInTranslationUnit(Start, Point) &&
133 SourceMgr.isBeforeInTranslationUnit(Point, End));
134 }
135
136 const NamedDecl *Result;
137 const SourceManager &SourceMgr;
138 const SourceLocation Point; // The location to find the NamedDecl.
Miklos Vajna47bd4632016-06-21 19:48:57 +0000139 const std::string Name;
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000140 const ASTContext *Context;
Manuel Klimekde237262014-08-20 01:39:05 +0000141};
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000142} // namespace
Manuel Klimekde237262014-08-20 01:39:05 +0000143
144const NamedDecl *getNamedDeclAt(const ASTContext &Context,
145 const SourceLocation Point) {
146 const auto &SourceMgr = Context.getSourceManager();
147 const auto SearchFile = SourceMgr.getFilename(Point);
148
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000149 NamedDeclFindingASTVisitor Visitor(SourceMgr, Point, &Context);
Manuel Klimekde237262014-08-20 01:39:05 +0000150
151 // We only want to search the decls that exist in the same file as the point.
152 auto Decls = Context.getTranslationUnitDecl()->decls();
153 for (auto &CurrDecl : Decls) {
154 const auto FileLoc = CurrDecl->getLocStart();
155 const auto FileName = SourceMgr.getFilename(FileLoc);
156 // FIXME: Add test.
157 if (FileName == SearchFile) {
158 Visitor.TraverseDecl(CurrDecl);
Manuel Klimekde237262014-08-20 01:39:05 +0000159 }
160 }
161
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000162 NestedNameSpecifierLocFinder Finder(const_cast<ASTContext &>(Context));
163 for (const auto &Location : Finder.getNestedNameSpecifierLocations()) {
164 Visitor.handleNestedNameSpecifierLoc(Location);
165 }
166
167 return Visitor.getNamedDecl();
Manuel Klimekde237262014-08-20 01:39:05 +0000168}
169
Miklos Vajna47bd4632016-06-21 19:48:57 +0000170const NamedDecl *getNamedDeclFor(const ASTContext &Context,
171 const std::string &Name) {
172 const auto &SourceMgr = Context.getSourceManager();
173 NamedDeclFindingASTVisitor Visitor(SourceMgr, Name);
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000174 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
Miklos Vajna47bd4632016-06-21 19:48:57 +0000175
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000176 return Visitor.getNamedDecl();
Miklos Vajna47bd4632016-06-21 19:48:57 +0000177}
178
Manuel Klimekde237262014-08-20 01:39:05 +0000179std::string getUSRForDecl(const Decl *Decl) {
180 llvm::SmallVector<char, 128> Buff;
181
182 // FIXME: Add test for the nullptr case.
183 if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff))
184 return "";
185
186 return std::string(Buff.data(), Buff.size());
187}
188
Manuel Klimekde237262014-08-20 01:39:05 +0000189} // namespace rename
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000190} // namespace clang