Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Clang, can I access the SourceManager when writing a custom ASTMatcher?

I'm trying to upgrade my AutoFFI project by making it more elegant and use Clang's ASTMatchers more extensively. I'd like to create a matcher that filters on the file path that was specified. Is it possible to do such a thing, or do I need to add custom logic outside of the matcher for this to work? As far as I can see, there's no way to get the SourceManager and use it to create a FullSourceLoc, but maybe I'm missing something.

Some relevant links:

  • https://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html
  • https://github.com/llvm-mirror/clang/blob/f3b7928366f63b51ffc97e74f8afcff497c57e8d/include/clang/ASTMatchers/ASTMatchersMacros.h#L28

If someone could tell me whether this is a limitation to Clang's ASTMatcher API or not I'd be very grateful!

like image 430
samvv Avatar asked Nov 18 '25 16:11

samvv


1 Answers

Never mind, I've found the answer by looking at the source of isExpansionInMainFile:

AST_POLYMORPHIC_MATCHER(isExpansionInMainFile,
                        AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
  auto &SourceManager = Finder->getASTContext().getSourceManager();
  return SourceManager.isInMainFile(
      SourceManager.getExpansionLoc(Node.getBeginLoc()));
}

Turns out I missed the getASTContext in MatchFinder, which holds on to the source manager.

like image 148
samvv Avatar answered Nov 20 '25 05:11

samvv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!