// \brief Finds the NamedDecl for a name in the source.
// \param Name the fully qualified name.
explicit NamedDeclFindingASTVisitor(const SourceManager &SourceMgr,
- const std::string &Name)
- : Result(nullptr), SourceMgr(SourceMgr), Name(Name) {}
+ const std::string &Name,
+ const ASTContext *Context)
+ : Result(nullptr), SourceMgr(SourceMgr), Name(Name), Context(Context) {}
// Declaration visitors:
bool VisitTypeLoc(const TypeLoc Loc) {
const auto TypeBeginLoc = Loc.getBeginLoc();
const auto TypeEndLoc = Lexer::getLocForEndOfToken(
- TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
- return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc,
- TypeEndLoc);
+ TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
+ if (auto *RD = Loc.getType()->getAsCXXRecordDecl())
+ return setResult(RD, TypeBeginLoc, TypeEndLoc);
+ return true;
}
// Other:
const NamedDecl *getNamedDeclFor(const ASTContext &Context,
const std::string &Name) {
const auto &SourceMgr = Context.getSourceManager();
- NamedDeclFindingASTVisitor Visitor(SourceMgr, Name);
+ NamedDeclFindingASTVisitor Visitor(SourceMgr, Name, &Context);
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
return Visitor.getNamedDecl();