From: Ted Kremenek Date: Tue, 9 Nov 2010 02:11:40 +0000 (+0000) Subject: Fix InitListExpr::getSourceRange() to work in the case of no locations for '(' and... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3;p=clang Fix InitListExpr::getSourceRange() to work in the case of no locations for '(' and ')'. This can happen in the case of transparent unions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@118472 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 3ba0667efc..d7e8fd8d90 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -2934,9 +2934,8 @@ public: HadArrayRangeDesignator = ARD; } - virtual SourceRange getSourceRange() const { - return SourceRange(LBraceLoc, RBraceLoc); - } + virtual SourceRange getSourceRange() const; + static bool classof(const Stmt *T) { return T->getStmtClass() == InitListExprClass; } diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 078bd7c8a1..7d05bdb264 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -1028,6 +1028,35 @@ Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) { return Result; } +SourceRange InitListExpr::getSourceRange() const { + if (SyntacticForm) + return SyntacticForm->getSourceRange(); + SourceLocation Beg = LBraceLoc, End = RBraceLoc; + if (Beg.isInvalid()) { + // Find the first non-null initializer. + for (InitExprsTy::const_iterator I = InitExprs.begin(), + E = InitExprs.end(); + I != E; ++I) { + if (Stmt *S = *I) { + Beg = S->getLocStart(); + break; + } + } + } + if (End.isInvalid()) { + // Find the first non-null initializer from the end. + for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(), + E = InitExprs.rend(); + I != E; ++I) { + if (Stmt *S = *I) { + End = S->getSourceRange().getEnd(); + break; + } + } + } + return SourceRange(Beg, End); +} + /// getFunctionType - Return the underlying function type for this block. /// const FunctionType *BlockExpr::getFunctionType() const {