There was no need to create a SourceManager
object. MatchFinder::MatchResult::Context
gives me the ASTContext*
on which I can call getSourceManager
to get the SourceManager
object. The rest is as we were doing previously.
class VarDeclPrinter : public MatchFinder::MatchCallback {
public:
virtual void run(const MatchFinder::MatchResult &Result) {
SourceManager &srcMgr = Result.Context->getSourceManager();
if(const VarDecl* var = Result.Nodes.getNodeAs<VarDecl>("var")) {
if(var->isFunctionOrMethodVarDecl()) {
cout << setw(20) << left << "Local Variable: " << var->getName().str() << "\t\t";
cout << ((CXXMethodDecl*)(var->getParentFunctionOrMethod()))->getQualifiedNameAsString() << "\t";
cout << "--" << srcMgr.getFilename(var->getLocation()).str();
cout << "\n";
}
if(var->hasExternalStorage()) {
cout << setw(20) << left << "External Variable: " << var->getName().str() << "\t\t";
cout << "--" << srcMgr.getFilename(var->getLocation()).str();
cout << "\n";
}
else if(var->hasGlobalStorage()) {
cout << setw(20) << left << "Global Variable: " << var->getName().str() << "\t\t";
cout << "--" << srcMgr.getFilename(var->getLocation()).str();
cout << "\n";
}
}
}
};
Thanks for your help, @Oak.