[cfe-dev] End SourceLocation for multi-line Tokens?

Chris Lattner clattner at apple.com
Thu Apr 24 09:56:29 PDT 2008


On Apr 24, 2008, at 9:42 AM, Emerson Murphy-Hill wrote:
>> Same thing works for those.  An example s HTMLRewriter.cpp,  
>> html::SyntaxHighlight:
>>
>>    unsigned TokOffs = SourceMgr.getFullFilePos(Tok.getLocation());
>>    unsigned TokLen = Tok.getLength();
>> ...
>>    case tok::comment:
>>      HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
>>                     "<span class='comment'>", "</span>");
>
> I did find this code, and my code works properly for single line  
> comments. I still don't see how this technique works for multiline  
> comments (but I could just be being dense [well, more so than  
> usual]).  How about this; fill in the question marks:
>
> SourceLocation startLoc = token.getLocation();
> int startLine = _manager->getLogicalLineNumber(startLoc);
> int startCol = _manager->getLogicalColumnNumber(startLoc);
> int endLine = ?;//"startLine" is wrong for multi-line comments
> int endCol = ?;//"startCol + token.getLength()" is wrong too

To get the end position, you need to get a SourceLocation for the end  
of the token, then ask SM the line/col of that *location*.  This will  
properly handle the various craziness that C includes.

See this post for info on getting the source location for the end of a  
token:
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-April/001501.html

Incidentally, improvements to the internals document are very welcome :)

-Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080424/80b2c057/attachment.html>


More information about the cfe-dev mailing list