[cfe-commits] r55607 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/Sema/array-init.c

Eli Friedman eli.friedman at gmail.com
Mon Sep 1 14:22:13 PDT 2008


On Mon, Sep 1, 2008 at 7:47 AM, Nuno Lopes <nunoplopes at sapo.pt> wrote:
> +    if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
> +      QualType Ty = VD->getType();
> +      if (Ty->isPointerLikeType() || Ty->isArrayType())
> +        return false;
> +    }

This is wrong; it's declaring that anything with a pointer or array
type is a constant expression!  For example:
int a = (int)(char*)a;
passes with this test.  Not to mention that nothing with a
pointer/array type is supposed to get here!

>     Diag(Init->getExprLoc(),
>          diag::err_init_element_not_constant, Init->getSourceRange());
>     return true;
> @@ -1098,6 +1105,8 @@
>       Diag(Init->getExprLoc(),
>            diag::err_init_element_not_constant, Init->getSourceRange());
>       return true;
> +    case UnaryOperator::AddrOf:
> +      return false;

Also wrong; consider something like the following:
int a = (int)*&(char*)a;

>     case UnaryOperator::SizeOf:
>     case UnaryOperator::AlignOf:
>     case UnaryOperator::OffsetOf:
> @@ -1160,12 +1169,7 @@
>   case Expr::ImplicitCastExprClass:
>   case Expr::ExplicitCastExprClass: {
>     const Expr *SubExpr = cast<CastExpr>(Init)->getSubExpr();
> -    if (SubExpr->getType()->isArithmeticType())
> -      return CheckArithmeticConstantExpression(SubExpr);
> -
> -    Diag(Init->getExprLoc(),
> -         diag::err_init_element_not_constant, Init->getSourceRange());
> -    return true;
> +    return CheckArithmeticConstantExpression(SubExpr);

The original version was intentional; the code only expects arithmetic
casts.  We can't deal with pointers in general here; we can't compile
stuff like the following:

int a,b, c = ((unsigned)&a)*((unsigned)&b);

> Modified: cfe/trunk/test/Sema/array-init.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/array-init.c?rev=55607&r1=55606&r2=55607&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Sema/array-init.c (original)
> +++ cfe/trunk/test/Sema/array-init.c Mon Sep  1 09:47:06 2008
> @@ -222,3 +222,16 @@
>  struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
>  int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
>
> +
> +// ptrs are constant
> +struct soft_segment_descriptor {
> +       int ssd_base;
> +};
> +static int dblfault_tss;
> +
> +union uniao { int ola; } xpto[1];
> +
> +struct soft_segment_descriptor gdt_segs[] = {
> +       {(int) &dblfault_tss},
> +       { (int)xpto},
> +};

Thank you for the testcase. However, the fix isn't correct.  I'll file
a bug momentarily for this issue, and I'll generally try to be
responsive for issues like this.

(Ultimately, the real fix for issues like this is to complete the
tryEvaluate code, and use that for determining whether we can evaluate
an expression... that said, it's a significant chunk of work to finish
that.)

-Eli



More information about the cfe-commits mailing list