Have a look at Lexer::findLocationAfterToken(stmt->getLocEnd(),<br>                                                                  tok::semi,<br>                                                                  Rewrite.getSourceMgr(),<br>
                                                                  Rewrite.getLangOpts(),<br>                                                                  true);<br><br>Robert<br><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Message: 6<br>
Date: Tue, 5 Feb 2013 10:37:07 +0900<br>
From: Antoine Trouve <<a href="mailto:trouve@isit.or.jp">trouve@isit.or.jp</a>><br>
To: James Dennett <<a href="mailto:james.dennett@gmail.com">james.dennett@gmail.com</a>><br>
Cc: clang-dev Developers <<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a>><br>
Subject: Re: [cfe-dev] Stmt.getLocEnd()<br>
Message-ID: <<a href="mailto:B4B7A695-BFF1-40F0-8E75-379D80991DA4@isit.or.jp">B4B7A695-BFF1-40F0-8E75-379D80991DA4@isit.or.jp</a>><br>
Content-Type: text/plain; charset=iso-8859-1<br>
<br>
Le H.25/02/04 ? 17:06, James Dennett <<a href="mailto:james.dennett@gmail.com">james.dennett@gmail.com</a>> a ?crit :<br>
<br>
> On Sun, Feb 3, 2013 at 11:35 PM, Antoine Trouve <<a href="mailto:trouve@isit.or.jp">trouve@isit.or.jp</a>> wrote:<br>
>> Le H.25/02/02 ? 2:28, Jordan Rose <<a href="mailto:jordan_rose@apple.com">jordan_rose@apple.com</a>> a ?crit :<br>
>><br>
>>><br>
>>> On Feb 1, 2013, at 1:02 , Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> wrote:<br>
>>><br>
>>>> On Fri, Feb 1, 2013 at 9:25 AM, Antoine Trouve <<a href="mailto:trouve@isit.or.jp">trouve@isit.or.jp</a>> wrote:<br>
>>>> Hi everybody,<br>
>>>><br>
>>>> I am now using LibTooling with the RefactoringTool to add some comments in some C codes.<br>
>>>><br>
>>>> I am now trying to add comments after a given statement (the body of a for to say the truth);<br>
>>>> in order to detect the location of the end of a statement I use "Stmt.getLocEnd()", but I seems that its output is often inaccurate.<br>
>>>><br>
>>>> Here are two examples, if I insert "/* HERE */" at the end location:<br>
>>>><br>
>>>>        for(i=0; i<N/2; i++) res +=mand(i,N/4/* HERE */);<br>
>>>>        for(i=0; i<mand(N,N); i++) res /* HERE */++;<br>
>>>><br>
>>>> I would expect the following output:<br>
>>>><br>
>>>>        for(i=0; i<N/2; i++) res +=mand(i,N/4);/* HERE */<br>
>>>>        for(i=0; i<mand(N,N); i++) res ++;/* HERE */<br>
>>>><br>
>>>> It looks like a bug. Am I right or did I miss something ?<br>
>>>><br>
>>>> I found a previous recent discussion about this matter (<a href="http://clang-developers.42468.n3.nabble.com/Statements-that-end-with-td4028311.html" target="_blank">http://clang-developers.42468.n3.nabble.com/Statements-that-end-with-td4028311.html</a>), but It looks like nothing has been done.<br>

>>>><br>
>>>> Yes, this is a known issue, and as far as I'm aware nothing has been done. We're usually working around this by looking for the next ';' token.<br>
>>>><br>
>>>> I guess it's the good old "patches welcome" answer - or wait until it becomes important enough for somebody else to solve.<br>
>>><br>
>>> The other issue here is that (by design) getLocEnd() doesn't actually return the SourceLocation just past the end of the statement, but the SourceLocation of the start of the last token in the statement. If you actually want the SourceLocation following the statement, you'll have to use Lexer::getLocForEndOfToken.<br>

>>><br>
>>> Jordan<br>
>><br>
>> I tried using "Lexer::getLoxForEndOfToken()", but that still wouldn't give the desired behaviour.<br>
>><br>
>> For instance, for<br>
>><br>
>>         for(i=0; i<mand(N,N); i++) res /* HERE */++;<br>
>><br>
>> It would now return the following location:<br>
>><br>
>>         for(i=0; i<mand(N,N); i++) res +/* HERE */+;<br>
>><br>
>> The token is only the first "+", not the whole "++".<br>
><br>
> That sounds wrong; the token is definitely "++", not "+".  What code<br>
> are you using to find that it's a single "+"?<br>
><br>
> -- James<br>
<br>
My bad, I was using the function the wrong way.<br>
<br>
But I noticed that I couldn't go through a semicolon using "Lexer::getLocForEndOfToken" if there is a space before the semicolon.<br>
<br>
For instance, let's consider this code:<br>
<br>
        for(i=0; i<mand(N,N); i++) res ++ ;<br>
<br>
Initially, the SourceLocation retreieved with "getLocEnd()" is before the "++":<br>
<br>
        for(i=0; i<mand(N,N); i++) res /*HERE*/++ ;<br>
<br>
If I call "Lexer::getLocForEndOfToken", it will point to after "++": nice:<br>
<br>
        for(i=0; i<mand(N,N); i++) res ++/*HERE*/ ;<br>
<br>
Then if I call again getLocForEndOfToken, the result will point to the exact same location (I need to call "getLocWithOffset")<br>
<br>
        for(i=0; i<mand(N,N); i++) res ++/*STILL HERE*/ ;<br>
<br>
In the case I don't have any space before the semicolon, the return value of the second call to getLocForEndOfToken will point to after it:<br>
<br>
        for(i=0; i<mand(N,N); i++) res ++;/*HERE*/<br>
        (no space before the ";")<br>
<br>
Is that the expected behaviour ? I find it pretty annoying in my very situation because I have no choice but looping with "SourceLocation::getLocWithOffset" until I find a ";".<br>
<br>
Regards,<br>
<br>
- Antoine<br>
<br></blockquote></div>