22 #include "llvm/ADT/SmallVector.h" 32 class NamedDeclOccurrenceFindingVisitor
33 :
public RecursiveSymbolVisitor<NamedDeclOccurrenceFindingVisitor> {
37 explicit NamedDeclOccurrenceFindingVisitor(
const SourceLocation Point,
38 const ASTContext &Context)
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) {
64 return Point == Start || Point == End ||
65 (Context.getSourceManager().isBeforeInTranslationUnit(Start,
67 Context.getSourceManager().isBeforeInTranslationUnit(Point, End));
70 const NamedDecl *Result =
nullptr;
71 const SourceLocation Point;
72 const ASTContext &Context;
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) {
119 const NamedDecl *getNamedDecl()
const {
return Result; }
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.
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
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.