22 #include "llvm/ADT/SmallVector.h"
32 class NamedDeclOccurrenceFindingVisitor
33 :
public RecursiveSymbolVisitor<NamedDeclOccurrenceFindingVisitor> {
37 explicit NamedDeclOccurrenceFindingVisitor(
const SourceLocation
Point,
39 : RecursiveSymbolVisitor(Context.getSourceManager(),
40 Context.getLangOpts()),
41 Point(Point), Context(Context) {}
43 bool visitSymbolOccurrence(
const NamedDecl *ND,
47 for (
const auto &Range : NameRanges) {
48 SourceLocation Start = Range.getBegin();
49 SourceLocation
End = Range.getEnd();
50 if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||
51 !End.isFileID() || !isPointWithin(Start, End))
58 const NamedDecl *getNamedDecl()
const {
return Result; }
62 bool isPointWithin(
const SourceLocation Start,
const SourceLocation End) {
65 (
Context.getSourceManager().isBeforeInTranslationUnit(Start,
67 Context.getSourceManager().isBeforeInTranslationUnit(
Point, End));
80 NamedDeclOccurrenceFindingVisitor Visitor(Point, Context);
91 Visitor.TraverseDecl(CurrDecl);
94 return Visitor.getNamedDecl();
101 class NamedDeclFindingVisitor
104 explicit NamedDeclFindingVisitor(StringRef
Name) : Name(Name) {}
108 bool VisitNamedDecl(
const NamedDecl *ND) {
112 if (
Name != ND->getQualifiedNameAsString() &&
113 Name !=
"::" + ND->getQualifiedNameAsString())
119 const NamedDecl *getNamedDecl()
const {
return Result; }
122 const NamedDecl *
Result =
nullptr;
129 const std::string &
Name) {
130 NamedDeclFindingVisitor Visitor(Name);
132 return Visitor.getNamedDecl();
142 return std::string(Buff.data(), Buff.size());
Defines the clang::ASTContext interface.
const SourceLocation Point
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Decl - This represents one declaration (or definition), e.g.
A wrapper class around RecursiveASTVisitor that visits each occurrences of a named symbol...
const ASTContext & Context
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Methods for determining the USR of a symbol at a location in source code.
A class that does preordor or postorder depth-first traversal on the entire Clang AST and visits each...
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
TranslationUnitDecl * getTranslationUnitDecl() const
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceManager & getSourceManager()
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
NamedDecl - This represents a decl with a name.
This class handles loading and caching of source files into memory.