[cfe-dev] Constant expressions in clang

Eli Friedman eli.friedman at gmail.com
Wed Feb 13 00:51:59 PST 2008


The current checking for constant expressions in clang is extremely
incomplete; it accepts things that are clearly not constant, and
rejects things that are.  I've been working on a patch to fix
isConstantExpr; while doing this, I ran into an issue that needs to be
resolved.

My patch currently rejects "int a = (int)&a;", where currently both
clang and gcc accept it.  The standard doesn't require this to be
allowed with good reason: it can only possibly work on platforms where
sizeof(int)>=sizeof(intptr_t) (like x86, but not x86-64) or platforms
that have a really fancy linker (like lli, which can evaluate any
llvm::Constant with a defined value in an initializer).  gcc has a
funny extension here where it allows "(int)&a" as a constant
expression, but later rejects it if the target platform can't actually
compute it in the given context.  For example, gcc rejects "struct
{int i : 5} x = {(int)&x};".  I'm not sure if we need to support this
extension, and if we do, how I should go about adding it in a way that
doesn't generate errors when we run clang output through llc.  Maybe
we need to change the signature of isConstantExpr to
"isConstantExpr(ASTContext &Ctx, SourceLocation *Loc, unsigned
DestBitWidth)"?

(In relaxed mode, comeau c apparently accepts "struct {int i : 5} x =
{(int)&x};"!  No clue how it manages to pull that off; maybe a bug?)

-Eli



More information about the cfe-dev mailing list