[cfe-dev] How to get the name of a translation unit when using ast matchers?

Gábor Márton martongabesz at gmail.com
Sun Jan 25 12:11:34 PST 2015


Hi,

I am a newby here ... and I have a quesiton, hoping you can help :)

I am writing a tool based on the ast matchers tutorial
(http://clang.llvm.org/docs/LibASTMatchersTutorial.html).

I would like to have a callback (or similar) to print the filename of
each and every translation unit which have been processed. I need this
because I'd like to track the progress of my tool.

For now I've been trying with this:
struct TuFileHandler : public MatchFinder::MatchCallback {
  virtual void run(const MatchFinder::MatchResult &Result) {
    if (const Decl *D = Result.Nodes.getNodeAs<Decl>("decl")) {
      if (const TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(D)) {
        auto loc = TUD->getLocation();
        llvm::outs() << loc.printToString(*Result.SourceManager) << "\n";
        llvm::outs().flush();
      }
    }
  }
};
...
TuFileHandler FileHandler;
auto const TuMatcher = decl().bind("decl");
Finder.addMatcher(TuMatcher, &FileHandler);
But the TranslationUnitDecl has invalid source location. I might be
trying with some cumbersome solutions to match different Decls which
has valid source locations, but that would be overkill. I am pretty
sure there might be an easier way...
Is there a way to somehow get this info from the CangTool class?


Many Thanks,
Gabor



More information about the cfe-dev mailing list