[cfe-commits] r146842 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/SemaCXX/constant-expression.cpp

Richard Smith richard at metafoo.co.uk
Sun Dec 18 14:31:58 PST 2011


On Sun, December 18, 2011 10:33, Abramo Bagnara wrote:
> Il 18/12/2011 03:33, Richard Smith ha scritto:
>> Author: rsmith
>> Date: Sat Dec 17 20:33:09 2011
>> New Revision: 146842
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=146842&view=rev
>> Log:
>> PR11604: don't allow floating-literal-to-integer casts in ICEs if the
>> (truncated)
>> floating literal value does not fit into the destination type. Such casts
>> have undefined behavior at translation time; treating them as non-ICE
>> matches the behavior of modern gcc versions.
>
> Just curious... do you think that the aim of the standard is to disallow
> the last and to allow the second?
>
> enum {a = (int) 2.0f}; enum {b = (int) (2.0f)}; enum {c = (int) -2.0f};

The wording of the C90, C94, and C99 standards as regards integer constant
expressions, taken literally, rules out both the second and third cases
(allowing only "floating constants that are the immediate operands of casts").
What 'immediate' means here seems reasonably clear (that the subexpression in
the cast-expression must be a floating-constant) but no compiler I have tested
interprets the standard this way (all of clang, EDG and GCC allow the extra
parentheses, but none of them accept the negation). I don't think it would be
beneficial for us to pedantically follow the standard here.

The only suggestion I can find for the rationale here
[http://c0x.coding-guidelines.com/6.6.pdf] is to avoid an implementation
needing a general compile-time mechanism for converting floating-point values
to integers -- instead, an implementation could build an integer value
directly when lexing a floating constant which is the operand of a cast.

For integral constant expressions in C++98 and C++03, the rule is that
"floating literals can appear only if they are cast to integral or enumeration
types"), which could be read as allowing parentheses but not negation -- and,
right or wrong, that is what all of clang, EDG and GCC implement.

For completeness, in C++11, all three are ICEs, and clang and GCC treat them
as such (I don't have access to a sufficiently modern version of EDG to test
it).

- Richard




More information about the cfe-commits mailing list