]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Tooling/Refactoring/Rename/USRFinder.h
Vendor import of clang trunk r306956:
[FreeBSD/FreeBSD.git] / include / clang / Tooling / Refactoring / Rename / USRFinder.h
1 //===--- USRFinder.h - Clang refactoring library --------------------------===//
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
11 /// \brief Methods for determining the USR of a symbol at a location in source
12 /// code.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CLANG_TOOLING_REFACTOR_RENAME_USR_FINDER_H
17 #define LLVM_CLANG_TOOLING_REFACTOR_RENAME_USR_FINDER_H
18
19 #include "clang/AST/AST.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/ASTMatchers/ASTMatchFinder.h"
22 #include <string>
23 #include <vector>
24
25 using namespace llvm;
26 using namespace clang::ast_matchers;
27
28 namespace clang {
29
30 class ASTContext;
31 class Decl;
32 class SourceLocation;
33 class NamedDecl;
34
35 namespace tooling {
36
37 // Given an AST context and a point, returns a NamedDecl identifying the symbol
38 // at the point. Returns null if nothing is found at the point.
39 const NamedDecl *getNamedDeclAt(const ASTContext &Context,
40                                 const SourceLocation Point);
41
42 // Given an AST context and a fully qualified name, returns a NamedDecl
43 // identifying the symbol with a matching name. Returns null if nothing is
44 // found for the name.
45 const NamedDecl *getNamedDeclFor(const ASTContext &Context,
46                                  const std::string &Name);
47
48 // Converts a Decl into a USR.
49 std::string getUSRForDecl(const Decl *Decl);
50
51 // FIXME: Implement RecursiveASTVisitor<T>::VisitNestedNameSpecifier instead.
52 class NestedNameSpecifierLocFinder : public MatchFinder::MatchCallback {
53 public:
54   explicit NestedNameSpecifierLocFinder(ASTContext &Context)
55       : Context(Context) {}
56
57   std::vector<NestedNameSpecifierLoc> getNestedNameSpecifierLocations() {
58     addMatchers();
59     Finder.matchAST(Context);
60     return Locations;
61   }
62
63 private:
64   void addMatchers() {
65     const auto NestedNameSpecifierLocMatcher =
66         nestedNameSpecifierLoc().bind("nestedNameSpecifierLoc");
67     Finder.addMatcher(NestedNameSpecifierLocMatcher, this);
68   }
69
70   void run(const MatchFinder::MatchResult &Result) override {
71     const auto *NNS = Result.Nodes.getNodeAs<NestedNameSpecifierLoc>(
72         "nestedNameSpecifierLoc");
73     Locations.push_back(*NNS);
74   }
75
76   ASTContext &Context;
77   std::vector<NestedNameSpecifierLoc> Locations;
78   MatchFinder Finder;
79 };
80
81 } // end namespace tooling
82 } // end namespace clang
83
84 #endif // LLVM_CLANG_TOOLING_REFACTOR_RENAME_USR_FINDER_H