Getting ASTContext within an AST Matcher

Hi all,

I'm writing an AST Matcher that operates on Expr. I'd like to use isNullPointerConstant() but it requires an ASTContext. In the past I've been able to get an ASTContext from a Decl subclassed object using Decl::getASTContext() but that clearly won't work with Expr. So how does one get access to the ASTContext in my situation? Must the matcher mechanism be modified to provide the ASTContext to matchers? How global is an ASTContext? Given the name it seems to imply there's one context for one particular AST the matchers are being run on. If so it should be pretty easy to modify the matcher mechanism to provide this info, perhaps through MatchFinder.

Hi all,

I'm writing an AST Matcher that operates on Expr. I'd like to use
isNullPointerConstant() but it requires an ASTContext. In the past I've
been able to get an ASTContext from a Decl subclassed object using
Decl::getASTContext() but that clearly won't work with Expr. So how does
one get access to the ASTContext in my situation? Must the matcher
mechanism be modified to provide the ASTContext to matchers?

The MatchFinder::MatchResult has a field Context.

How global is an ASTContext? Given the name it seems to imply there's one
context for one particular AST the matchers are being run on. If so it
should be pretty easy to modify the matcher mechanism to provide this info,
perhaps through MatchFinder.

Assessment correct :slight_smile:

Cheers,
/Manuel

But MatchFinder is only available to a Callback that’s called as the result of a match. I’m writing a custom matcher and all I have access to is an ASTMatchFinder, the AST Node being considered at the time and the BoundNodesTreeBuilder.

But MatchFinder is only available to a Callback that’s called as the
result of a match. I’m writing a custom matcher and all I have access to is
an ASTMatchFinder, the AST Node being considered at the time and the
BoundNodesTreeBuilder.

Ah, ok, now I understand... I think we should add a method getASTContext to
the ASTMatchFinder interface (in ASTMatchersInternal.h). Do you want to
take a stab at it?

Cheers,
/Manuel

I sure do! Patch forthcoming…