1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
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/Index/USRGeneration.h" |
19 | #include "clang/Lex/Lexer.h" |
20 | #include "clang/Tooling/Refactoring/RecursiveSymbolVisitor.h" |
21 | #include "llvm/ADT/SmallVector.h" |
22 | |
23 | using namespace llvm; |
24 | |
25 | namespace clang { |
26 | namespace tooling { |
27 | |
28 | namespace { |
29 | |
30 | |
31 | class NamedDeclOccurrenceFindingVisitor |
32 | : public RecursiveSymbolVisitor<NamedDeclOccurrenceFindingVisitor> { |
33 | public: |
34 | |
35 | |
36 | explicit NamedDeclOccurrenceFindingVisitor(const SourceLocation Point, |
37 | const ASTContext &Context) |
38 | : RecursiveSymbolVisitor(Context.getSourceManager(), |
39 | Context.getLangOpts()), |
40 | Point(Point), Context(Context) {} |
41 | |
42 | bool visitSymbolOccurrence(const NamedDecl *ND, |
43 | ArrayRef<SourceRange> NameRanges) { |
44 | if (!ND) |
45 | return true; |
46 | for (const auto &Range : NameRanges) { |
47 | SourceLocation Start = Range.getBegin(); |
48 | SourceLocation End = Range.getEnd(); |
49 | if (!Start.isValid() || !Start.isFileID() || !End.isValid() || |
50 | !End.isFileID() || !isPointWithin(Start, End)) |
51 | return true; |
52 | } |
53 | Result = ND; |
54 | return false; |
55 | } |
56 | |
57 | const NamedDecl *getNamedDecl() const { return Result; } |
58 | |
59 | private: |
60 | |
61 | bool isPointWithin(const SourceLocation Start, const SourceLocation End) { |
62 | |
63 | return Point == Start || Point == End || |
64 | (Context.getSourceManager().isBeforeInTranslationUnit(Start, |
65 | Point) && |
66 | Context.getSourceManager().isBeforeInTranslationUnit(Point, End)); |
67 | } |
68 | |
69 | const NamedDecl *Result = nullptr; |
70 | const SourceLocation Point; |
71 | const ASTContext &Context; |
72 | }; |
73 | |
74 | } |
75 | |
76 | const NamedDecl *getNamedDeclAt(const ASTContext &Context, |
77 | const SourceLocation Point) { |
78 | const SourceManager &SM = Context.getSourceManager(); |
79 | NamedDeclOccurrenceFindingVisitor Visitor(Point, Context); |
80 | |
81 | |
82 | |
83 | |
84 | for (auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) { |
85 | SourceLocation StartLoc = CurrDecl->getBeginLoc(); |
86 | SourceLocation EndLoc = CurrDecl->getEndLoc(); |
87 | if (StartLoc.isValid() && EndLoc.isValid() && |
88 | SM.isBeforeInTranslationUnit(StartLoc, Point) != |
89 | SM.isBeforeInTranslationUnit(EndLoc, Point)) |
90 | Visitor.TraverseDecl(CurrDecl); |
91 | } |
92 | |
93 | return Visitor.getNamedDecl(); |
94 | } |
95 | |
96 | namespace { |
97 | |
98 | |
99 | |
100 | class NamedDeclFindingVisitor |
101 | : public RecursiveASTVisitor<NamedDeclFindingVisitor> { |
102 | public: |
103 | explicit NamedDeclFindingVisitor(StringRef Name) : Name(Name) {} |
104 | |
105 | |
106 | |
107 | bool VisitNamedDecl(const NamedDecl *ND) { |
108 | if (!ND) |
109 | return true; |
110 | |
111 | if (Name != ND->getQualifiedNameAsString() && |
112 | Name != "::" + ND->getQualifiedNameAsString()) |
113 | return true; |
114 | Result = ND; |
115 | return false; |
116 | } |
117 | |
118 | const NamedDecl *getNamedDecl() const { return Result; } |
119 | |
120 | private: |
121 | const NamedDecl *Result = nullptr; |
122 | StringRef Name; |
123 | }; |
124 | |
125 | } |
126 | |
127 | const NamedDecl *getNamedDeclFor(const ASTContext &Context, |
128 | const std::string &Name) { |
129 | NamedDeclFindingVisitor Visitor(Name); |
130 | Visitor.TraverseDecl(Context.getTranslationUnitDecl()); |
131 | return Visitor.getNamedDecl(); |
132 | } |
133 | |
134 | std::string getUSRForDecl(const Decl *Decl) { |
135 | llvm::SmallVector<char, 128> Buff; |
136 | |
137 | |
138 | if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff)) |
139 | return ""; |
140 | |
141 | return std::string(Buff.data(), Buff.size()); |
142 | } |
143 | |
144 | } |
145 | } |
146 | |