22#include "llvm/ADT/SmallVector.h"
32class 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);
85 for (
auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) {
89 SM.isBeforeInTranslationUnit(StartLoc, Point) !=
90 SM.isBeforeInTranslationUnit(EndLoc, Point))
91 Visitor.TraverseDecl(CurrDecl);
94 return Visitor.getNamedDecl();
101class 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);
131 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
132 return Visitor.getNamedDecl();
142 return std::string(Buff);
Defines the clang::ASTContext interface.
A wrapper class around RecursiveASTVisitor that visits each occurrences of a named symbol.
Defines the SourceManager interface.
Methods for determining the USR of a symbol at a location in source code.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Decl - This represents one declaration (or definition), e.g.
This represents a decl that may have a name.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.