19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/RecursiveASTVisitor.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Index/USRGeneration.h"
23 #include "llvm/ADT/SmallVector.h"
33 class USRLocFindingASTVisitor
34 :
public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> {
36 explicit USRLocFindingASTVisitor(
const std::string
USR) : USR(USR) {
41 bool VisitNamedDecl(
const NamedDecl *Decl) {
50 bool VisitDeclRefExpr(
const DeclRefExpr *Expr) {
51 const auto *Decl = Expr->getFoundDecl();
53 checkNestedNameSpecifierLoc(Expr->getQualifierLoc());
61 bool VisitMemberExpr(
const MemberExpr *Expr) {
62 const auto *Decl = Expr->getFoundDecl().getDecl();
73 const std::vector<clang::SourceLocation> &getLocationsFound()
const {
79 void checkNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
81 const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
84 NameLoc = NameLoc.getPrefix();
89 const std::string
USR;
96 USRLocFindingASTVisitor visitor(USR);
98 visitor.TraverseDecl(Decl);
99 return visitor.getLocationsFound();
std::string getUSRForDecl(const Decl *Decl)
Methods for determining the USR of a symbol at a location in source code.
Provides functionality for finding all instances of a USR in a given AST.
std::vector< clang::SourceLocation > LocationsFound
std::vector< SourceLocation > getLocationsOfUSR(const std::string USR, Decl *Decl)