Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 1 | //===--- 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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 19 | #include "clang/Index/USRGeneration.h" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Lexer.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace 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. |
| 31 | namespace { |
| 32 | class NamedDeclFindingASTVisitor |
| 33 | : public clang::RecursiveASTVisitor<NamedDeclFindingASTVisitor> { |
| 34 | public: |
| 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 Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 37 | explicit NamedDeclFindingASTVisitor(const SourceLocation Point, |
| 38 | const ASTContext &Context) |
| 39 | : Result(nullptr), Point(Point), Context(Context) {} |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 40 | |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 41 | // \brief Finds the NamedDecl for a name in the source. |
| 42 | // \param Name the fully qualified name. |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 43 | explicit NamedDeclFindingASTVisitor(const std::string &Name, |
| 44 | const ASTContext &Context) |
| 45 | : Result(nullptr), Name(Name), Context(Context) {} |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 46 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 47 | // 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 Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 54 | return dyn_cast<CXXConversionDecl>(Decl) |
| 55 | ? true |
| 56 | : setResult(Decl, Decl->getLocation(), |
| 57 | Decl->getNameAsString().length()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Expression visitors: |
| 61 | |
| 62 | bool VisitDeclRefExpr(const DeclRefExpr *Expr) { |
Kirill Bobyrev | 6b7d8c2 | 2016-08-15 23:20:05 +0000 | [diff] [blame^] | 63 | const NamedDecl *Decl = Expr->getFoundDecl(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 64 | return setResult(Decl, Expr->getLocation(), |
| 65 | Decl->getNameAsString().length()); |
| 66 | } |
| 67 | |
| 68 | bool VisitMemberExpr(const MemberExpr *Expr) { |
Kirill Bobyrev | 6b7d8c2 | 2016-08-15 23:20:05 +0000 | [diff] [blame^] | 69 | const NamedDecl *Decl = Expr->getFoundDecl().getDecl(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 70 | return setResult(Decl, Expr->getMemberLoc(), |
| 71 | Decl->getNameAsString().length()); |
| 72 | } |
| 73 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 74 | // Other visitors: |
| 75 | |
| 76 | bool VisitTypeLoc(const TypeLoc Loc) { |
Kirill Bobyrev | 6b7d8c2 | 2016-08-15 23:20:05 +0000 | [diff] [blame^] | 77 | const SourceLocation TypeBeginLoc = Loc.getBeginLoc(); |
| 78 | const SourceLocation TypeEndLoc = Lexer::getLocForEndOfToken( |
| 79 | TypeBeginLoc, 0, Context.getSourceManager(), |
| 80 | Context.getLangOpts()); |
Kirill Bobyrev | 9e0dab9 | 2016-08-02 09:38:38 +0000 | [diff] [blame] | 81 | if (const auto *TemplateTypeParm = |
| 82 | dyn_cast<TemplateTypeParmType>(Loc.getType())) { |
| 83 | return setResult(TemplateTypeParm->getDecl(), TypeBeginLoc, TypeEndLoc); |
| 84 | } |
Kirill Bobyrev | d6ab7d4 | 2016-08-03 23:00:32 +0000 | [diff] [blame] | 85 | if (const auto *TemplateSpecType = |
| 86 | dyn_cast<TemplateSpecializationType>(Loc.getType())) { |
| 87 | return setResult(TemplateSpecType->getTemplateName().getAsTemplateDecl(), |
| 88 | TypeBeginLoc, TypeEndLoc); |
| 89 | } |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 90 | return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc, |
| 91 | TypeEndLoc); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Kirill Bobyrev | 31fd7fb | 2016-08-09 07:14:48 +0000 | [diff] [blame] | 94 | bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) { |
Kirill Bobyrev | 7fa3395 | 2016-08-09 10:03:33 +0000 | [diff] [blame] | 95 | for (const auto *Initializer : ConstructorDecl->inits()) { |
| 96 | if (!Initializer->isWritten()) { |
Kirill Bobyrev | 31fd7fb | 2016-08-09 07:14:48 +0000 | [diff] [blame] | 97 | // 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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 113 | // Other: |
| 114 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 115 | 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 Bobyrev | 6b7d8c2 | 2016-08-15 23:20:05 +0000 | [diff] [blame^] | 121 | const NamespaceDecl *Decl = |
| 122 | NameLoc.getNestedNameSpecifier()->getAsNamespace(); |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 123 | setResult(Decl, NameLoc.getLocalBeginLoc(), NameLoc.getLocalEndLoc()); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 124 | NameLoc = NameLoc.getPrefix(); |
| 125 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private: |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 129 | // \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 Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 133 | if (!Decl) { |
| 134 | return true; |
| 135 | } |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 136 | 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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 147 | } |
| 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 Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 154 | bool setResult(const NamedDecl *Decl, SourceLocation Loc, unsigned Offset) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 155 | // 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 Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 164 | (Context.getSourceManager().isBeforeInTranslationUnit(Start, |
| 165 | Point) && |
| 166 | Context.getSourceManager().isBeforeInTranslationUnit(Point, End)); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | const NamedDecl *Result; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 170 | const SourceLocation Point; // The location to find the NamedDecl. |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 171 | const std::string Name; |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 172 | const ASTContext &Context; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 173 | }; |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 174 | } // namespace |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 175 | |
| 176 | const NamedDecl *getNamedDeclAt(const ASTContext &Context, |
| 177 | const SourceLocation Point) { |
Kirill Bobyrev | 6b7d8c2 | 2016-08-15 23:20:05 +0000 | [diff] [blame^] | 178 | StringRef SearchFile = Context.getSourceManager().getFilename(Point); |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 179 | NamedDeclFindingASTVisitor Visitor(Point, Context); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 180 | |
| 181 | // We only want to search the decls that exist in the same file as the point. |
Kirill Bobyrev | 6b7d8c2 | 2016-08-15 23:20:05 +0000 | [diff] [blame^] | 182 | for (const auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) { |
| 183 | const SourceLocation FileLoc = CurrDecl->getLocStart(); |
| 184 | StringRef FileName = Context.getSourceManager().getFilename(FileLoc); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 185 | // FIXME: Add test. |
| 186 | if (FileName == SearchFile) { |
| 187 | Visitor.TraverseDecl(CurrDecl); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 191 | NestedNameSpecifierLocFinder Finder(const_cast<ASTContext &>(Context)); |
| 192 | for (const auto &Location : Finder.getNestedNameSpecifierLocations()) { |
| 193 | Visitor.handleNestedNameSpecifierLoc(Location); |
| 194 | } |
| 195 | |
| 196 | return Visitor.getNamedDecl(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 199 | const NamedDecl *getNamedDeclFor(const ASTContext &Context, |
| 200 | const std::string &Name) { |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 201 | NamedDeclFindingASTVisitor Visitor(Name, Context); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 202 | Visitor.TraverseDecl(Context.getTranslationUnitDecl()); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 203 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 204 | return Visitor.getNamedDecl(); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 207 | std::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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 217 | } // namespace rename |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 218 | } // namespace clang |