[cfe-dev] from source location to token?

Chris Lattner clattner at apple.com
Wed Dec 10 10:31:01 PST 2008


On Dec 10, 2008, at 6:03 AM, Paolo Bolzoni wrote:

> On Mon, 24 Nov 2008 10:23:12 -0800
> Chris Lattner <clattner at apple.com> wrote:
>
>> The way it gets the end of the fp literal is to relex the token with
>> code in Lexer::MeasureTokenLength.
>>
>> Given a SourceLocation and the length of the token (as returned by
>> MeasureTokenLength) you can get the exact original spelling  
>> (including
>> trigraphs and escaped newlines, beware).  The pointer to the start of
>> the string is obtained with:
>>
>> const char *StrData = SourceMgr.getCharacterData(Loc);
>
> I have still a problem. Consider the following program:
>
> #define N_OF_ELEM 037;
> int a = N_OF_ELEM;
>
> Using the method you suggest I have that the textual representation  
> of the
> integer literal in the right hand side of the assignment is  
> `N_OF_ELEM'.
>
> It seems Lexer::MeasureTokenLength() automatically changes the  
> SourceLocation
> using clang::SourceManager::getLogicalLoc() . In my case it shouldn't.
>
> What is the best way to obtain the token text after preprocessing?

Use "Loc = SourceManager.getPhysicalLoc(Loc)" before calling that code,

-Chris



More information about the cfe-dev mailing list