/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/Refactoring/Rename/USRFinder.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- USRFinder.cpp - Clang refactoring library ------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | /// |
9 | | /// \file Implements a recursive AST visitor that finds the USR of a symbol at a |
10 | | /// point. |
11 | | /// |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/Tooling/Refactoring/Rename/USRFinder.h" |
15 | | #include "clang/AST/AST.h" |
16 | | #include "clang/AST/ASTContext.h" |
17 | | #include "clang/AST/RecursiveASTVisitor.h" |
18 | | #include "clang/Basic/SourceManager.h" |
19 | | #include "clang/Index/USRGeneration.h" |
20 | | #include "clang/Lex/Lexer.h" |
21 | | #include "clang/Tooling/Refactoring/RecursiveSymbolVisitor.h" |
22 | | #include "llvm/ADT/SmallVector.h" |
23 | | |
24 | | using namespace llvm; |
25 | | |
26 | | namespace clang { |
27 | | namespace tooling { |
28 | | |
29 | | namespace { |
30 | | |
31 | | /// Recursively visits each AST node to find the symbol underneath the cursor. |
32 | | class NamedDeclOccurrenceFindingVisitor |
33 | | : public RecursiveSymbolVisitor<NamedDeclOccurrenceFindingVisitor> { |
34 | | public: |
35 | | // Finds the NamedDecl at a point in the source. |
36 | | // \param Point the location in the source to search for the NamedDecl. |
37 | | explicit NamedDeclOccurrenceFindingVisitor(const SourceLocation Point, |
38 | | const ASTContext &Context) |
39 | | : RecursiveSymbolVisitor(Context.getSourceManager(), |
40 | | Context.getLangOpts()), |
41 | 84 | Point(Point), Context(Context) {} |
42 | | |
43 | | bool visitSymbolOccurrence(const NamedDecl *ND, |
44 | 414 | ArrayRef<SourceRange> NameRanges) { |
45 | 414 | if (!ND) |
46 | 3 | return true; |
47 | 411 | for (const auto &Range : NameRanges) { |
48 | 411 | SourceLocation Start = Range.getBegin(); |
49 | 411 | SourceLocation End = Range.getEnd(); |
50 | 411 | if (!Start.isValid() || !Start.isFileID() || !End.isValid()406 || |
51 | 411 | !End.isFileID()406 || !isPointWithin(Start, End)406 ) |
52 | 334 | return true; |
53 | 411 | } |
54 | 77 | Result = ND; |
55 | 77 | return false; |
56 | 411 | } |
57 | | |
58 | 84 | const NamedDecl *getNamedDecl() const { return Result; } |
59 | | |
60 | | private: |
61 | | // Determines if the Point is within Start and End. |
62 | 406 | bool isPointWithin(const SourceLocation Start, const SourceLocation End) { |
63 | | // FIXME: Add tests for Point == End. |
64 | 406 | return Point == Start || Point == End339 || |
65 | 406 | (339 Context.getSourceManager().isBeforeInTranslationUnit(Start, |
66 | 339 | Point) && |
67 | 339 | Context.getSourceManager().isBeforeInTranslationUnit(Point, End)326 ); |
68 | 406 | } |
69 | | |
70 | | const NamedDecl *Result = nullptr; |
71 | | const SourceLocation Point; // The location to find the NamedDecl. |
72 | | const ASTContext &Context; |
73 | | }; |
74 | | |
75 | | } // end anonymous namespace |
76 | | |
77 | | const NamedDecl *getNamedDeclAt(const ASTContext &Context, |
78 | 84 | const SourceLocation Point) { |
79 | 84 | const SourceManager &SM = Context.getSourceManager(); |
80 | 84 | NamedDeclOccurrenceFindingVisitor Visitor(Point, Context); |
81 | | |
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. |
85 | 725 | for (auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) { |
86 | 725 | SourceLocation StartLoc = CurrDecl->getBeginLoc(); |
87 | 725 | SourceLocation EndLoc = CurrDecl->getEndLoc(); |
88 | 725 | if (StartLoc.isValid() && EndLoc.isValid()277 && |
89 | 725 | SM.isBeforeInTranslationUnit(StartLoc, Point) != |
90 | 277 | SM.isBeforeInTranslationUnit(EndLoc, Point)) |
91 | 78 | Visitor.TraverseDecl(CurrDecl); |
92 | 725 | } |
93 | | |
94 | 84 | return Visitor.getNamedDecl(); |
95 | 84 | } |
96 | | |
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 | 271 | 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 | 2.46k | bool VisitNamedDecl(const NamedDecl *ND) { |
109 | 2.46k | if (!ND) |
110 | 0 | return true; |
111 | | // Fully qualified name is used to find the declaration. |
112 | 2.46k | if (Name != ND->getQualifiedNameAsString() && |
113 | 2.46k | Name != "::" + ND->getQualifiedNameAsString()2.21k ) |
114 | 2.19k | return true; |
115 | 269 | Result = ND; |
116 | 269 | return false; |
117 | 2.46k | } |
118 | | |
119 | 271 | const NamedDecl *getNamedDecl() const { return Result; } |
120 | | |
121 | | private: |
122 | | const NamedDecl *Result = nullptr; |
123 | | StringRef Name; |
124 | | }; |
125 | | |
126 | | } // end anonymous namespace |
127 | | |
128 | | const NamedDecl *getNamedDeclFor(const ASTContext &Context, |
129 | 271 | const std::string &Name) { |
130 | 271 | NamedDeclFindingVisitor Visitor(Name); |
131 | 271 | Visitor.TraverseDecl(Context.getTranslationUnitDecl()); |
132 | 271 | return Visitor.getNamedDecl(); |
133 | 271 | } |
134 | | |
135 | 10.5k | std::string getUSRForDecl(const Decl *Decl) { |
136 | 10.5k | llvm::SmallString<128> Buff; |
137 | | |
138 | | // FIXME: Add test for the nullptr case. |
139 | 10.5k | if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff)10.3k ) |
140 | 216 | return ""; |
141 | | |
142 | 10.3k | return std::string(Buff); |
143 | 10.5k | } |
144 | | |
145 | | } // end namespace tooling |
146 | | } // end namespace clang |