Declarations for a single file, not complete translation unit?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Declarations for a single file, not complete translation unit?

Jacob Carlborg
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.

--
/Jacob Carlborg

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Declarations for a single file, not complete translation unit?

Douglas Gregor

On Dec 27, 2010, at 12:21 PM, Jacob Carlborg wrote:

> 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
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Declarations for a single file, not complete translation unit?

Jacob Carlborg
On 2010-12-29 21:51, Douglas Gregor wrote:

>
> On Dec 27, 2010, at 12:21 PM, Jacob Carlborg wrote:
>
>> 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

--
/Jacob Carlborg

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev