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) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 63 | 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 Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 74 | // Other visitors: |
| 75 | |
| 76 | bool VisitTypeLoc(const TypeLoc Loc) { |
| 77 | const auto TypeBeginLoc = Loc.getBeginLoc(); |
| 78 | const auto TypeEndLoc = Lexer::getLocForEndOfToken( |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 79 | TypeBeginLoc, 0, Context.getSourceManager(), Context.getLangOpts()); |
Kirill Bobyrev | 9e0dab9 | 2016-08-02 09:38:38 +0000 | [diff] [blame^] | 80 | if (const auto *TemplateTypeParm = |
| 81 | dyn_cast<TemplateTypeParmType>(Loc.getType())) { |
| 82 | return setResult(TemplateTypeParm->getDecl(), TypeBeginLoc, TypeEndLoc); |
| 83 | } |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 84 | return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc, |
| 85 | TypeEndLoc); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 88 | // Other: |
| 89 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 90 | const NamedDecl *getNamedDecl() { return Result; } |
| 91 | |
| 92 | // \brief Determines if a namespace qualifier contains the point. |
| 93 | // \returns false on success and sets Result. |
| 94 | void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) { |
| 95 | while (NameLoc) { |
| 96 | const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace(); |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 97 | setResult(Decl, NameLoc.getLocalBeginLoc(), NameLoc.getLocalEndLoc()); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 98 | NameLoc = NameLoc.getPrefix(); |
| 99 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 103 | // \brief Sets Result to Decl if the Point is within Start and End. |
| 104 | // \returns false on success. |
| 105 | bool setResult(const NamedDecl *Decl, SourceLocation Start, |
| 106 | SourceLocation End) { |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 107 | if (!Decl) { |
| 108 | return true; |
| 109 | } |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 110 | if (Name.empty()) { |
| 111 | // Offset is used to find the declaration. |
| 112 | if (!Start.isValid() || !Start.isFileID() || !End.isValid() || |
| 113 | !End.isFileID() || !isPointWithin(Start, End)) { |
| 114 | return true; |
| 115 | } |
| 116 | } else { |
| 117 | // Fully qualified name is used to find the declaration. |
| 118 | if (Name != Decl->getQualifiedNameAsString()) { |
| 119 | return true; |
| 120 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 121 | } |
| 122 | Result = Decl; |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // \brief Sets Result to Decl if Point is within Loc and Loc + Offset. |
| 127 | // \returns false on success. |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 128 | bool setResult(const NamedDecl *Decl, SourceLocation Loc, unsigned Offset) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 129 | // FIXME: Add test for Offset == 0. Add test for Offset - 1 (vs -2 etc). |
| 130 | return Offset == 0 || |
| 131 | setResult(Decl, Loc, Loc.getLocWithOffset(Offset - 1)); |
| 132 | } |
| 133 | |
| 134 | // \brief Determines if the Point is within Start and End. |
| 135 | bool isPointWithin(const SourceLocation Start, const SourceLocation End) { |
| 136 | // FIXME: Add tests for Point == End. |
| 137 | return Point == Start || Point == End || |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 138 | (Context.getSourceManager().isBeforeInTranslationUnit(Start, |
| 139 | Point) && |
| 140 | Context.getSourceManager().isBeforeInTranslationUnit(Point, End)); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | const NamedDecl *Result; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 144 | const SourceLocation Point; // The location to find the NamedDecl. |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 145 | const std::string Name; |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 146 | const ASTContext &Context; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 147 | }; |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 148 | } // namespace |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 149 | |
| 150 | const NamedDecl *getNamedDeclAt(const ASTContext &Context, |
| 151 | const SourceLocation Point) { |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 152 | const auto SearchFile = Context.getSourceManager().getFilename(Point); |
| 153 | NamedDeclFindingASTVisitor Visitor(Point, Context); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 154 | |
| 155 | // We only want to search the decls that exist in the same file as the point. |
| 156 | auto Decls = Context.getTranslationUnitDecl()->decls(); |
| 157 | for (auto &CurrDecl : Decls) { |
| 158 | const auto FileLoc = CurrDecl->getLocStart(); |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 159 | const auto FileName = Context.getSourceManager().getFilename(FileLoc); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 160 | // FIXME: Add test. |
| 161 | if (FileName == SearchFile) { |
| 162 | Visitor.TraverseDecl(CurrDecl); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 166 | NestedNameSpecifierLocFinder Finder(const_cast<ASTContext &>(Context)); |
| 167 | for (const auto &Location : Finder.getNestedNameSpecifierLocations()) { |
| 168 | Visitor.handleNestedNameSpecifierLoc(Location); |
| 169 | } |
| 170 | |
| 171 | return Visitor.getNamedDecl(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 174 | const NamedDecl *getNamedDeclFor(const ASTContext &Context, |
| 175 | const std::string &Name) { |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 176 | NamedDeclFindingASTVisitor Visitor(Name, Context); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 177 | Visitor.TraverseDecl(Context.getTranslationUnitDecl()); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 178 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 179 | return Visitor.getNamedDecl(); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 182 | std::string getUSRForDecl(const Decl *Decl) { |
| 183 | llvm::SmallVector<char, 128> Buff; |
| 184 | |
| 185 | // FIXME: Add test for the nullptr case. |
| 186 | if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff)) |
| 187 | return ""; |
| 188 | |
| 189 | return std::string(Buff.data(), Buff.size()); |
| 190 | } |
| 191 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 192 | } // namespace rename |
Kirill Bobyrev | 32db769 | 2016-07-15 11:29:16 +0000 | [diff] [blame] | 193 | } // namespace clang |