[cfe-dev] Constant expression checking rewrite

Eli Friedman eli.friedman at gmail.com
Sun May 18 10:09:49 PDT 2008

On Sat, May 17, 2008 at 9:44 PM, Ted Kremenek <kremenek at apple.com> wrote:
> I understand that there is an issue of cleanly implementing the checking of
> constant expressions in Sema so that we can report diagnostics (as done in
> your patch), and that keeping isConstantExpr in Expr makes this a little
> more difficult to do cleanly.  Is this the main issue?

Essentially, yes; that's the biggest issue.  Implementing this stuff
outside of Sema would require a complicated return convention for
reasonable diagnostics. It might end up being the best approach, but
it's a lot more complicated.

Also, there's really a few different kinds of constant expressions.
First, there are integer constant expressions, which are precisely
defined in the C99 standard to be essentially integers and integer

Then, there are general constant expressions, also defined in the C99
standard, which are essentially either arithmetic constants (computed
with integer/floating-point arithmetic), or an easily computable
address (the details are slightly more complicated).  This is what my
patch implements.

Then, there is what Expr::isConstantExpr returns, which isn't exactly
clear; it's currently buggy, but it apparently tries to compute
whether an expression is constant in the sense that it doesn't depend
on the values of any variables/globals.  This could potentially be
useful, but it's not acceptable for the Sema checking; we cannot
accept all expressions in this category as constant expressions
because some of them might not be computable by the linker.

One issue with my patch I haven't mentioned: it doesn't try to check
whether an expression has a defined result when doing
constant-expression checking.  Unfortunately, this will significantly
complicate the code, and I'm not sure what the best approach is yet
(maybe leaving it to an analysis pass would be best).  I wouldn't
normally worry about that sort of thing, but division by zero in a
global currently crashes llc, so it would be best if we emitted a hard


More information about the cfe-dev mailing list