Search code examples
c++clangcode-analysisllvm-clang

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:

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


Solution

  • 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.