Declarations for a single file, not complete translation unit?

I'm working on a source to source translator which translates Objective-C/C code into D code. I've subclassed ASTConsumer to create something similar to the Objective-C rewriter.

The problem I have is that I'm receiving declarations for a complete translation unit via HandleTopLevelDecl function. I'm wondering if it's possible somehow to receive (or filter) the declarations for a single file instead of a complete translation unit. I would like to have a one to one mapping between the original files and the translated files.

You can filter the declarations yourself based on the location of the declaration. Use Decl::getLocation() to figure out where the declaration is, and then use SourceManager's various routines to map that down to the file where the declaration was located. If it's not in a file you care about, ignore it.

  - Doug

Thanks, I've already got a reply, not posted to the mailing list. I haven't had the chance yet to post the solution here yet. This is how I ended up fixing the issue:

1. Get the location of the current declaration
2. Get the file ID of that location
3. Get the main file ID from the source manager
4. If the main file ID and the file ID of the declaration doesn't match, skip the declaration

In code:

FileID fileId = sourceManager->getFileID(declaration->getLocation());

if (sourceManager->getMainFileID() != fileId)
     // continue with next declaration