Lexical scope for macro expansion

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Lexical scope for macro expansion

Abramo Bagnara

Is there any way from MacroExpands callback to reach current lexical scope?

What I'm trying to do is to detect if some identifiers in the macro body
is a known typedef id.

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

Re: Lexical scope for macro expansion

Chris Lattner

On Dec 10, 2009, at 8:56 AM, Abramo Bagnara wrote:

>
> Is there any way from MacroExpands callback to reach current lexical scope?
>
> What I'm trying to do is to detect if some identifiers in the macro body
> is a known typedef id.

Nope, there is no way to do this, because the lexer/preprocessor lives at a lower level (and macros can be used and defined in multiple contexts, and don't follow the grammar of the language).  However, given a typedef, you can tell if it came from a macro.

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

Re: Lexical scope for macro expansion

Abramo Bagnara
Il 10/12/2009 18:37, Chris Lattner ha scritto:

>
> On Dec 10, 2009, at 8:56 AM, Abramo Bagnara wrote:
>
>>
>> Is there any way from MacroExpands callback to reach current lexical scope?
>>
>> What I'm trying to do is to detect if some identifiers in the macro body
>> is a known typedef id.
>
> Nope, there is no way to do this, because the lexer/preprocessor
> lives at a lower level (and macros can be used and defined in
> multiple contexts, and don't follow the grammar of the language).
> However, given a typedef, you can tell if it came from a macro.

Let see if I've understand correctly:

1) I collect macro bodies using MacroExpands callback

2) I collect the spelling locations for the NameLoc of each encountered
TypedefLoc

3) I scan the previously collected tokens of the macros body: if one of
them has the same location of one of that collected in step 2 I know for
sure that the macro body contains a typedef name

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

Re: Lexical scope for macro expansion

Chris Lattner

On Dec 10, 2009, at 10:25 AM, Abramo Bagnara wrote:

> Il 10/12/2009 18:37, Chris Lattner ha scritto:
>>
>> On Dec 10, 2009, at 8:56 AM, Abramo Bagnara wrote:
>>
>>>
>>> Is there any way from MacroExpands callback to reach current lexical scope?
>>>
>>> What I'm trying to do is to detect if some identifiers in the macro body
>>> is a known typedef id.
>>
>> Nope, there is no way to do this, because the lexer/preprocessor
>> lives at a lower level (and macros can be used and defined in
>> multiple contexts, and don't follow the grammar of the language).
>> However, given a typedef, you can tell if it came from a macro.
>
> Let see if I've understand correctly:
>
> 1) I collect macro bodies using MacroExpands callback
>
> 2) I collect the spelling locations for the NameLoc of each encountered
> TypedefLoc
>
> 3) I scan the previously collected tokens of the macros body: if one of
> them has the same location of one of that collected in step 2 I know for
> sure that the macro body contains a typedef name
>
> Right?

Sounds like it could work!  I don't know the real use case you're going for here though.

-Chris


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

Re: Lexical scope for macro expansion

Abramo Bagnara
Il 11/12/2009 00:47, Chris Lattner ha scritto:

>
> On Dec 10, 2009, at 10:25 AM, Abramo Bagnara wrote:
>
>> Il 10/12/2009 18:37, Chris Lattner ha scritto:
>>>
>>> On Dec 10, 2009, at 8:56 AM, Abramo Bagnara wrote:
>>>
>>>>
>>>> Is there any way from MacroExpands callback to reach current lexical scope?
>>>>
>>>> What I'm trying to do is to detect if some identifiers in the macro body
>>>> is a known typedef id.
>>>
>>> Nope, there is no way to do this, because the lexer/preprocessor
>>> lives at a lower level (and macros can be used and defined in
>>> multiple contexts, and don't follow the grammar of the language).
>>> However, given a typedef, you can tell if it came from a macro.
>>
>> Let see if I've understand correctly:
>>
>> 1) I collect macro bodies using MacroExpands callback
>>
>> 2) I collect the spelling locations for the NameLoc of each encountered
>> TypedefLoc
>>
>> 3) I scan the previously collected tokens of the macros body: if one of
>> them has the same location of one of that collected in step 2 I know for
>> sure that the macro body contains a typedef name
>>
>> Right?
>
> Sounds like it could work!  I don't know the real use case you're going for here though.

Just suppose we want something that warn the user that:

typedef intptr_t Handle;

is preferred to:

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