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. |
| 37 | explicit NamedDeclFindingASTVisitor(const SourceManager &SourceMgr, |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 38 | const SourceLocation Point, |
| 39 | const ASTContext *Context) |
| 40 | : Result(nullptr), SourceMgr(SourceMgr), Point(Point), Context(Context) {} |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 41 | |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 42 | // \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 Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 46 | : Result(nullptr), SourceMgr(SourceMgr), Name(Name) {} |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 47 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 48 | // 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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 62 | 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 Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 73 | // 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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 83 | // Other: |
| 84 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 85 | 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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private: |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 100 | // \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 Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 104 | 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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 115 | } |
| 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 Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 122 | bool setResult(const NamedDecl *Decl, SourceLocation Loc, unsigned Offset) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 123 | // 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 Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 139 | const std::string Name; |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 140 | const ASTContext *Context; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 141 | }; |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 142 | } // namespace |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 143 | |
| 144 | const NamedDecl *getNamedDeclAt(const ASTContext &Context, |
| 145 | const SourceLocation Point) { |
| 146 | const auto &SourceMgr = Context.getSourceManager(); |
| 147 | const auto SearchFile = SourceMgr.getFilename(Point); |
| 148 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 149 | NamedDeclFindingASTVisitor Visitor(SourceMgr, Point, &Context); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 150 | |
| 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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 162 | NestedNameSpecifierLocFinder Finder(const_cast<ASTContext &>(Context)); |
| 163 | for (const auto &Location : Finder.getNestedNameSpecifierLocations()) { |
| 164 | Visitor.handleNestedNameSpecifierLoc(Location); |
| 165 | } |
| 166 | |
| 167 | return Visitor.getNamedDecl(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 170 | const NamedDecl *getNamedDeclFor(const ASTContext &Context, |
| 171 | const std::string &Name) { |
| 172 | const auto &SourceMgr = Context.getSourceManager(); |
| 173 | NamedDeclFindingASTVisitor Visitor(SourceMgr, Name); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 174 | Visitor.TraverseDecl(Context.getTranslationUnitDecl()); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 175 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame^] | 176 | return Visitor.getNamedDecl(); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 179 | std::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 Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 189 | } // namespace rename |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 190 | } // namespace clang |