clang-tools  4.0.0
USRFinder.h
Go to the documentation of this file.
1 //===--- tools/extra/clang-rename/USRFinder.h - 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
11 /// \brief Methods for determining the USR of a symbol at a location in source
12 /// code.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_RENAME_USR_FINDER_H
17 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_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 rename {
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 } // namespace rename
82 } // namespace clang
83 
84 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_RENAME_USR_FINDER_H
const std::string Name
Definition: USRFinder.cpp:164
const SourceLocation Point
Definition: USRFinder.cpp:163
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:262
const NamedDecl * getNamedDeclAt(const ASTContext &Context, const SourceLocation Point)
Definition: USRFinder.cpp:169
std::string getUSRForDecl(const Decl *Decl)
Definition: USRFinder.cpp:201
NestedNameSpecifierLocFinder(ASTContext &Context)
Definition: USRFinder.h:54
const NamedDecl * getNamedDeclFor(const ASTContext &Context, const std::string &Name)
Definition: USRFinder.cpp:193
std::vector< NestedNameSpecifierLoc > getNestedNameSpecifierLocations()
Definition: USRFinder.h:57
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
const NamedDecl * Result
Definition: USRFinder.cpp:162