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

Ted Kremenek kremenek at apple.com
Thu Sep 6 09:42:45 PDT 2007

I think there is a bug in the current implementation of getSourceRange  
for GotoStmt:

   class GotoStmt : public Stmt {
     LabelStmt *Label;
     SourceLocation GotoLoc;
     virtual SourceRange getSourceRange() const {
       return SourceRange(GotoLoc, Label->getLocEnd());

I believe that getting the end SourceLocation using Label->getLocEnd()  
is incorrect.  While the target LabelStmt is referenced by GotoStmt,  
it isn't a substatement in the tree.  Consider the following snippet  
of code:

   l1: goto l1;

Here, the GotoStmt is a substatement of the LabelStmt for "l1".  The  
implementation of getSourceRange() in LabelStmt is as follows (notice  
the call to SubStmt->getLocEnd):

   class LabelStmt : public Stmt {
     IdentifierInfo *Label;
     Stmt *SubStmt;
     SourceLocation IdentLoc;
     virtual SourceRange getSourceRange() const {
       return SourceRange(IdentLoc, SubStmt->getLocEnd());

There is an unbounded recursion here.  While LabelStmt is correctly  
calling getLocEnd for its substatement, GotoStmt is incorrectly  
calling getLocEnd for the target LabelStmt.

The question is that do we have enough extent information recorded in  
GotoStmt to accurately recreate the full source range for a goto  

More information about the cfe-dev mailing list