[cfe-dev] A bug in PresumedLoc?

James Dennett james.dennett at gmail.com
Tue Dec 3 00:18:08 PST 2013


On Mon, Dec 2, 2013 at 11:58 PM, maxs <xtxwy.ustc at gmail.com> wrote:
>
>>     I use the following code to get source location:
>> =================================
>>
>>     SourceManager &SM = context.getSourceManager();
>>     int lineStart = SM.getPresumedLoc(SR->getBegin()).getLine();
>>     int lineEnd = SM.getPresumedLoc(SR->getEnd()).getLine();
>>     int colStart = SM.getPresumedLoc(SR->getBegin()).getColumn();
>>     int colEnd = SM.getPresumedLoc(SR->getEnd()).getColumn();
>>     llvm::outs() << "For:" << lineStart << "  " << colStart << "  " <<
>> lineEnd << "  " << colEnd << " \n";
>>
>> =================================
>>
>> The test case is:
>>
>> =================================
>> (1)
>>
>> for(i = 0 ; i<100;i++)
>> sum++;
>>
>> (2)
>>
>> for(i = 0 ; i<100;i++)
>> {sum++;}
>>
>> =================================
>>
>> The result is:
>>
>> =================================
>> (1)
>>
>> 44  1  45  4   =>  incorrect, should be 44  1  45  4
>
> sorry,   the  "44  1  45  4   =>  incorrect, should be 44  1  45 4 "
>             should be "44  1  45  4   =>  incorrect, should be 44  1  45  6"
>
>>
>> (2)
>>
>> 44  1  45  8  => correct
>>
>> =================================
>>
>> So, is it a bug?  or I misunderstand it?

A little of both, maybe.

Clang's "statement" doesn't include the semicolon (";") character, and
the end of a range is (generally) the first character of the last
token, leaving clients to do some work to add on the token length to
find the actual end position, and then to hunt for the semi-colon, if
the statement does in fact end in a semi-colon.

It might be nice to fix the range of statements to include the
semi-colon when that more accurately reflects the grammar, but doing
that systematically would run afoul of the Expr is-a Stmt decision,
and fixing that one is a somewhat large task.

-- James



More information about the cfe-dev mailing list