From: Steve Naroff Date: Wed, 9 Jan 2008 20:58:06 +0000 (+0000) Subject: Teach Sema::ActOnCompoundLiteral about constraint C99 6.5.2.5p3. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=58d1821279b0ffd002e675d211faca3b5de067e2;p=clang Teach Sema::ActOnCompoundLiteral about constraint C99 6.5.2.5p3. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45782 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp index 7d224c5e80..897432578b 100644 --- a/Sema/SemaExpr.cpp +++ b/Sema/SemaExpr.cpp @@ -664,8 +664,9 @@ ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, Expr *literalExpr = static_cast(InitExpr); // FIXME: add more semantic analysis (C99 6.5.2.5). - if (CheckInitializer(literalExpr, literalType, false)) - return 0; + bool requireConstantExprs = !CurFunctionDecl && !CurMethodDecl; + if (CheckInitializer(literalExpr, literalType, requireConstantExprs)) + return true; return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr); } diff --git a/test/Sema/compound-literal.c b/test/Sema/compound-literal.c index 2c9595c50e..12ee9f9300 100644 --- a/test/Sema/compound-literal.c +++ b/test/Sema/compound-literal.c @@ -1,4 +1,4 @@ -// RUN: clang -fsyntax-only -verify %s +// RUN: clang -fsyntax-only -verify -pedantic %s struct foo { int a, b; }; @@ -8,9 +8,13 @@ static struct foo t3 = t2; // -expected-error {{initializer element is not const static int *p = (int []){2,4}; static int x = (int){1}; // -expected-error {{initializer element is not constant}} -expected-warning{{braces around scalar initializer}} +static int *p2 = (int []){2,x}; // -expected-error {{initializer element is not constant}} +static int *p3 = (int []){2,"x"}; // -expected-warning {{incompatible pointer to integer conversion initializing 'char *', expected 'int'}} + extern void fooFunc(struct foo *pfoo); int main(int argc, char **argv) { + int *l = (int []){x, *p, *p2}; fooFunc(&(struct foo){ 1, 2 }); }