Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 1 | //===--- USRFinder.cpp - Clang refactoring library ------------------------===// |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 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 | |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 15 | #include "clang/Tooling/Refactoring/Rename/USRFinder.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 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" |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 21 | #include "clang/Tooling/Refactoring/RecursiveSymbolVisitor.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace clang { |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 27 | namespace tooling { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 28 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 29 | namespace { |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 30 | |
| 31 | /// Recursively visits each AST node to find the symbol underneath the cursor. |
| 32 | class NamedDeclOccurrenceFindingVisitor |
| 33 | : public RecursiveSymbolVisitor<NamedDeclOccurrenceFindingVisitor> { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 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. |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 37 | explicit NamedDeclOccurrenceFindingVisitor(const SourceLocation Point, |
| 38 | const ASTContext &Context) |
| 39 | : RecursiveSymbolVisitor(Context.getSourceManager(), |
| 40 | Context.getLangOpts()), |
| 41 | Point(Point), Context(Context) {} |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 42 | |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 43 | bool visitSymbolOccurrence(const NamedDecl *ND, |
| 44 | ArrayRef<SourceRange> NameRanges) { |
| 45 | if (!ND) |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 46 | return true; |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 47 | for (const auto &Range : NameRanges) { |
| 48 | SourceLocation Start = Range.getBegin(); |
| 49 | SourceLocation End = Range.getEnd(); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 50 | if (!Start.isValid() || !Start.isFileID() || !End.isValid() || |
Kirill Bobyrev | 8769778 | 2016-09-04 22:50:41 +0000 | [diff] [blame] | 51 | !End.isFileID() || !isPointWithin(Start, End)) |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 52 | return true; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 53 | } |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 54 | Result = ND; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 58 | const NamedDecl *getNamedDecl() const { return Result; } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 59 | |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 60 | private: |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 61 | // \brief Determines if the Point is within Start and End. |
| 62 | bool isPointWithin(const SourceLocation Start, const SourceLocation End) { |
| 63 | // FIXME: Add tests for Point == End. |
| 64 | return Point == Start || Point == End || |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 65 | (Context.getSourceManager().isBeforeInTranslationUnit(Start, |
| 66 | Point) && |
| 67 | Context.getSourceManager().isBeforeInTranslationUnit(Point, End)); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 70 | const NamedDecl *Result = nullptr; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 71 | const SourceLocation Point; // The location to find the NamedDecl. |
Kirill Bobyrev | 1f0ea35 | 2016-07-28 09:05:06 +0000 | [diff] [blame] | 72 | const ASTContext &Context; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 73 | }; |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 74 | |
| 75 | } // end anonymous namespace |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 76 | |
| 77 | const NamedDecl *getNamedDeclAt(const ASTContext &Context, |
| 78 | const SourceLocation Point) { |
Benjamin Kramer | b568d99 | 2016-11-22 17:29:45 +0000 | [diff] [blame] | 79 | const SourceManager &SM = Context.getSourceManager(); |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 80 | NamedDeclOccurrenceFindingVisitor Visitor(Point, Context); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 81 | |
Benjamin Kramer | b568d99 | 2016-11-22 17:29:45 +0000 | [diff] [blame] | 82 | // Try to be clever about pruning down the number of top-level declarations we |
| 83 | // see. If both start and end is either before or after the point we're |
| 84 | // looking for the point cannot be inside of this decl. Don't even look at it. |
Kirill Bobyrev | bc84fc9 | 2016-08-16 06:19:06 +0000 | [diff] [blame] | 85 | for (auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) { |
Benjamin Kramer | b568d99 | 2016-11-22 17:29:45 +0000 | [diff] [blame] | 86 | SourceLocation StartLoc = CurrDecl->getLocStart(); |
| 87 | SourceLocation EndLoc = CurrDecl->getLocEnd(); |
| 88 | if (StartLoc.isValid() && EndLoc.isValid() && |
| 89 | SM.isBeforeInTranslationUnit(StartLoc, Point) != |
| 90 | SM.isBeforeInTranslationUnit(EndLoc, Point)) |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 91 | Visitor.TraverseDecl(CurrDecl); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 94 | return Visitor.getNamedDecl(); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 97 | namespace { |
| 98 | |
| 99 | /// Recursively visits each NamedDecl node to find the declaration with a |
| 100 | /// specific name. |
| 101 | class NamedDeclFindingVisitor |
| 102 | : public RecursiveASTVisitor<NamedDeclFindingVisitor> { |
| 103 | public: |
| 104 | explicit NamedDeclFindingVisitor(StringRef Name) : Name(Name) {} |
| 105 | |
| 106 | // We don't have to traverse the uses to find some declaration with a |
| 107 | // specific name, so just visit the named declarations. |
| 108 | bool VisitNamedDecl(const NamedDecl *ND) { |
| 109 | if (!ND) |
| 110 | return true; |
| 111 | // Fully qualified name is used to find the declaration. |
| 112 | if (Name != ND->getQualifiedNameAsString() && |
| 113 | Name != "::" + ND->getQualifiedNameAsString()) |
| 114 | return true; |
| 115 | Result = ND; |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | const NamedDecl *getNamedDecl() const { return Result; } |
| 120 | |
| 121 | private: |
| 122 | const NamedDecl *Result = nullptr; |
| 123 | StringRef Name; |
| 124 | }; |
| 125 | |
| 126 | } // end anonymous namespace |
| 127 | |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 128 | const NamedDecl *getNamedDeclFor(const ASTContext &Context, |
| 129 | const std::string &Name) { |
Alex Lorenz | 98394f8 | 2017-07-13 10:36:33 +0000 | [diff] [blame] | 130 | NamedDeclFindingVisitor Visitor(Name); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 131 | Visitor.TraverseDecl(Context.getTranslationUnitDecl()); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 132 | return Visitor.getNamedDecl(); |
Miklos Vajna | 47bd463 | 2016-06-21 19:48:57 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 135 | std::string getUSRForDecl(const Decl *Decl) { |
| 136 | llvm::SmallVector<char, 128> Buff; |
| 137 | |
| 138 | // FIXME: Add test for the nullptr case. |
| 139 | if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff)) |
| 140 | return ""; |
| 141 | |
| 142 | return std::string(Buff.data(), Buff.size()); |
| 143 | } |
| 144 | |
Alex Lorenz | 4abbd92 | 2017-06-30 16:36:09 +0000 | [diff] [blame] | 145 | } // end namespace tooling |
| 146 | } // end namespace clang |