Get filename from ASTVisitor

I am using a RecursiveASTVisitor to get the declaration statements of my
I took inspiration from the example shown here:

I extended the example so to include "included" files in the AST (this is
necessary to be able to identify variable declarations that use non-basic
data types).

Now I want to filter all variable declarations that are not part of the file
that I want to analyze.
How can I do this?

Thanks a lot,


You can get the SourceLocation of the Decl using getLocStart(), getLocEnd() or getLocation(). Then you can use SourceManager :: getBufferName() to get the name of the file containing the SourceLocation.

Hope this helps.


Thank you for your reply.

llvm::errs() << "Filename " <<
SourceManager::getBufferName(v->getLocation()) << "\n";

leads to
error: call to non-static member function without an object argument

Yes, that’s because you need to use an object instance of the SourceManager class. You should have an instance available, if you were able to create a RecursiveASTVisitor.

Also, before asking questions in the mail-list, please try reading the documentation and doing some Google searches. It’s unlikely that you’ll get a reply to such RTFM question in the future. (Not to mention that the C++ compiler actually stated the problem accurately, so you could have figured this out by yourself if you had tried.)

If you only care about parts of the AST that come from the source file and not the included files, SourceManager provides isFromMainFile().