[cfe-dev] GotoStmt::getSourceRange, bug?

Steve Naroff snaroff at apple.com
Thu Sep 6 10:34:56 PDT 2007


On Sep 6, 2007, at 10:13 AM, Ted Kremenek wrote:

>
> On Sep 6, 2007, at 9:58 AM, Steve Naroff wrote:
>
>> Not currently. It looks like we need to add a "LabelLoc" to the  
>> AST (this is already passed into the actions, so it's trivial to  
>> do). Make sense?
>>
>> Thanks for finding this,
>
> Cool.  I've committed a patch.  I'm terribly familiar with the  
> mechanics of SourceLocations, so if someone could audit the patch  
> that would be great.
>
> In particular, I'm not so certain about the following line in  
> GotoStmt:getSourceRange:
>
>   return SourceRange(GotoLoc, LabelLoc);
>
> This extent will only include from the start of the goto statement  
> to the start of the label token (and not the end of the label  
> token).  Is this what we want?

It is. It will derive the end of the label token through the magic of  
the routine below. This enables us to compute the range of  
identifiers (which are quite common) without caching  
<beginloc,endloc> pairs.

Since the range protocol is typically used for error/diagnostics,  
performance isn't a problem...

snaroff

/// GetTokenLength - Given the source location of a token, determine  
its length.
/// This is a fully general function that uses a lexer to relex the  
token.
unsigned TextDiagnosticPrinter::GetTokenLength(SourceLocation Loc) {
   // If this comes from a macro expansion, we really do want the  
macro name, not
   // the token this macro expanded to.
   Loc = SourceMgr.getLogicalLoc(Loc);
   const char *StrData = SourceMgr.getCharacterData(Loc);

   // TODO: this could be special cased for common tokens like  
identifiers, ')',
   // etc to make this faster, if it mattered.  This could use
   // Lexer::isObviouslySimpleCharacter for example.

   // Create a lexer starting at the beginning of this token.
   Lexer TheLexer(Loc, *ThePreprocessor, StrData);
   Token TheTok;
   TheLexer.LexRawToken(TheTok);
   return TheTok.getLength();
}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20070906/c0eb57e9/attachment.html>


More information about the cfe-dev mailing list