blob: 7f26945c32926f8828743f37c0c476fb502c73b0 [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) {
Manuel Klimekde237262014-08-20 01:39:05 +000063 const auto *Decl = Expr->getFoundDecl();
64 return setResult(Decl, Expr->getLocation(),
65 Decl->getNameAsString().length());
66 }
67
68 bool VisitMemberExpr(const MemberExpr *Expr) {
69 const auto *Decl = Expr->getFoundDecl().getDecl();
70 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) {
77 const auto TypeBeginLoc = Loc.getBeginLoc();
78 const auto TypeEndLoc = Lexer::getLocForEndOfToken(
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +000079 TypeBeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
80 return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc,
81 TypeEndLoc);
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000082 }
83
Manuel Klimekde237262014-08-20 01:39:05 +000084 // Other:
85
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000086 const NamedDecl *getNamedDecl() { return Result; }
87
88 // \brief Determines if a namespace qualifier contains the point.
89 // \returns false on success and sets Result.
90 void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
91 while (NameLoc) {
92 const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +000093 setResult(Decl, NameLoc.getLocalBeginLoc(), NameLoc.getLocalEndLoc());
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000094 NameLoc = NameLoc.getPrefix();
95 }
Manuel Klimekde237262014-08-20 01:39:05 +000096 }
97
98private:
Manuel Klimekde237262014-08-20 01:39:05 +000099 // \brief Sets Result to Decl if the Point is within Start and End.
100 // \returns false on success.
101 bool setResult(const NamedDecl *Decl, SourceLocation Start,
102 SourceLocation End) {
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000103 if (!Decl) {
104 return true;
105 }
Miklos Vajna47bd4632016-06-21 19:48:57 +0000106 if (Name.empty()) {
107 // Offset is used to find the declaration.
108 if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||
109 !End.isFileID() || !isPointWithin(Start, End)) {
110 return true;
111 }
112 } else {
113 // Fully qualified name is used to find the declaration.
114 if (Name != Decl->getQualifiedNameAsString()) {
115 return true;
116 }
Manuel Klimekde237262014-08-20 01:39:05 +0000117 }
118 Result = Decl;
119 return false;
120 }
121
122 // \brief Sets Result to Decl if Point is within Loc and Loc + Offset.
123 // \returns false on success.
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000124 bool setResult(const NamedDecl *Decl, SourceLocation Loc, unsigned Offset) {
Manuel Klimekde237262014-08-20 01:39:05 +0000125 // FIXME: Add test for Offset == 0. Add test for Offset - 1 (vs -2 etc).
126 return Offset == 0 ||
127 setResult(Decl, Loc, Loc.getLocWithOffset(Offset - 1));
128 }
129
130 // \brief Determines if the Point is within Start and End.
131 bool isPointWithin(const SourceLocation Start, const SourceLocation End) {
132 // FIXME: Add tests for Point == End.
133 return Point == Start || Point == End ||
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000134 (Context.getSourceManager().isBeforeInTranslationUnit(Start,
135 Point) &&
136 Context.getSourceManager().isBeforeInTranslationUnit(Point, End));
Manuel Klimekde237262014-08-20 01:39:05 +0000137 }
138
139 const NamedDecl *Result;
Manuel Klimekde237262014-08-20 01:39:05 +0000140 const SourceLocation Point; // The location to find the NamedDecl.
Miklos Vajna47bd4632016-06-21 19:48:57 +0000141 const std::string Name;
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000142 const ASTContext &Context;
Manuel Klimekde237262014-08-20 01:39:05 +0000143};
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000144} // namespace
Manuel Klimekde237262014-08-20 01:39:05 +0000145
146const NamedDecl *getNamedDeclAt(const ASTContext &Context,
147 const SourceLocation Point) {
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000148 const auto SearchFile = Context.getSourceManager().getFilename(Point);
149 NamedDeclFindingASTVisitor Visitor(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();
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000155 const auto FileName = Context.getSourceManager().getFilename(FileLoc);
Manuel Klimekde237262014-08-20 01:39:05 +0000156 // 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) {
Kirill Bobyrev1f0ea352016-07-28 09:05:06 +0000172 NamedDeclFindingASTVisitor Visitor(Name, Context);
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000173 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
Miklos Vajna47bd4632016-06-21 19:48:57 +0000174
Kirill Bobyreva3432fa2016-07-22 13:41:09 +0000175 return Visitor.getNamedDecl();
Miklos Vajna47bd4632016-06-21 19:48:57 +0000176}
177
Manuel Klimekde237262014-08-20 01:39:05 +0000178std::string getUSRForDecl(const Decl *Decl) {
179 llvm::SmallVector<char, 128> Buff;
180
181 // FIXME: Add test for the nullptr case.
182 if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff))
183 return "";
184
185 return std::string(Buff.data(), Buff.size());
186}
187
Manuel Klimekde237262014-08-20 01:39:05 +0000188} // namespace rename
Kirill Bobyrev32db7692016-07-15 11:29:16 +0000189} // namespace clang