From: Eli Friedman Date: Wed, 21 May 2008 03:39:11 +0000 (+0000) Subject: Fix a couple of bugs found by Neil Booth in the const-ness checking. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=97c0a391138d20e1066174a9cfa92860fb06e5a1;p=clang Fix a couple of bugs found by Neil Booth in the const-ness checking. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51361 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index a050ab2d4b..b0b50bab96 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1004,8 +1004,13 @@ bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) { return cast(Init)->isFileScope(); case Expr::DeclRefExprClass: { const Decl *D = cast(Init)->getDecl(); - if (const VarDecl *VD = dyn_cast(D)) - return VD->hasGlobalStorage(); + if (const VarDecl *VD = dyn_cast(D)) { + if (VD->hasGlobalStorage()) + return false; + Diag(Init->getExprLoc(), + diag::err_init_element_not_constant, Init->getSourceRange()); + return true; + } if (isa(D)) return false; Diag(Init->getExprLoc(), @@ -1032,7 +1037,7 @@ bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) { // C99 6.6p9 if (Exp->getOpcode() == UnaryOperator::Deref) - return CheckAddressConstantExpressionLValue(Exp->getSubExpr()); + return CheckAddressConstantExpression(Exp->getSubExpr()); Diag(Init->getExprLoc(), diag::err_init_element_not_constant, Init->getSourceRange()); diff --git a/test/Sema/init.c b/test/Sema/init.c index 9085dbcf0d..ffe678c5f7 100644 --- a/test/Sema/init.c +++ b/test/Sema/init.c @@ -30,3 +30,15 @@ struct cdiff_cmd commands[] = { {"OPEN", 1, &cdiff_cmd_open } }; +// PR2348 +static struct { int z; } s[2]; +int *t = &(*s).z; + +// PR2349 +short *a2(void) +{ + short int b; + static short *bp = &b; // expected-error {{initializer element is not constant}} + + return bp; +}