21 #include "llvm/ADT/SmallVector.h" 31 class NamedDeclOccurrenceFindingVisitor
32 :
public RecursiveSymbolVisitor<NamedDeclOccurrenceFindingVisitor> {
36 explicit NamedDeclOccurrenceFindingVisitor(
const SourceLocation Point,
37 const ASTContext &Context)
38 : RecursiveSymbolVisitor(Context.getSourceManager(),
39 Context.getLangOpts()),
40 Point(Point), Context(Context) {}
42 bool visitSymbolOccurrence(
const NamedDecl *ND,
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))
57 const NamedDecl *getNamedDecl()
const {
return Result; }
61 bool isPointWithin(
const SourceLocation Start,
const SourceLocation End) {
63 return Point == Start || Point == End ||
64 (Context.getSourceManager().isBeforeInTranslationUnit(Start,
66 Context.getSourceManager().isBeforeInTranslationUnit(Point, End));
69 const NamedDecl *Result =
nullptr;
70 const SourceLocation Point;
71 const ASTContext &Context;
79 NamedDeclOccurrenceFindingVisitor Visitor(Point, Context);
90 Visitor.TraverseDecl(CurrDecl);
93 return Visitor.getNamedDecl();
100 class NamedDeclFindingVisitor
103 explicit NamedDeclFindingVisitor(StringRef Name) : Name(Name) {}
107 bool VisitNamedDecl(
const NamedDecl *ND) {
118 const NamedDecl *getNamedDecl()
const {
return Result; }
128 const std::string &Name) {
129 NamedDeclFindingVisitor Visitor(Name);
131 return Visitor.getNamedDecl();
141 return std::string(Buff.data(), Buff.size());
Defines the clang::ASTContext interface.
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Decl - This represents one declaration (or definition), e.g.
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
A wrapper class around RecursiveASTVisitor that visits each occurrences of a named symbol...
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 preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Encodes a location in the source.
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceManager & getSourceManager()
TranslationUnitDecl * getTranslationUnitDecl() const
std::string getQualifiedNameAsString() const
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
This represents a decl that may have a name.
This class handles loading and caching of source files into memory.