[cfe-dev] Likely nasty bug with enum constants type

Abramo Bagnara abramo.bagnara at gmail.com
Mon Feb 1 07:53:43 PST 2010


Il 01/02/2010 15:55, Eli Friedman ha scritto:
> On Mon, Feb 1, 2010 at 3:17 AM, Abramo Bagnara <abramo.bagnara at gmail.com> wrote:
>> $ cat bug.c
>>
>> enum {
>>  a = 1U,
>>  b = a-2
>> };
>>
>> int x[b];
>> $ gcc -S -std=c99 -pedantic -W -Wall bug.c
>> bug.c:7: error: size of array ‘x’ is negative
>> $ ~/llvm/Debug/bin/clang -S -std=c99 -pedantic -W -Wall bug.c
>> $
>>
>> This typescript show that while gcc (as mandated by c99 standard)
>> correctly gives int as the type of the enum constant, clang gives it
>> unsigned type.
>>
>> Do you confirm the bug?
> 
> Yes; see http://llvm.org/bugs/show_bug.cgi?id=4515 .

We have found this bug investigating about a failed assertion on clang
that perhaps it's a different bug, I don't know.

The fact is that after the type adapting phases of the enum constants to
the finally decided underlying enum type, the DeclRefExpr referring an
enum constant found inside another enum constant initializer expressions
retains the original type (that is incongruent with newly assigned
EnumConstantDecl type).

This means that if we call e.g. Expr::isIntegerConstantExpr on enum
constant init expression we get an assert violation about incongruent
signedness.

$ cat bug.c
enum {
  a = 1U,
  b = a,
  c = -1
};

$ ~/llvm/Debug/bin/clang -cc1 -ast-dump  bug.c
typedef char *__builtin_va_list;
enum  {
    a = (ImplicitCastExpr 0x98d65f8 <bug.c:2:7> 'int' <IntegralCast>
  (IntegerLiteral 0x98cfb20 <col:7> 'unsigned int' 1))
,
    b = (ImplicitCastExpr 0x98d6618 <bug.c:3:7> 'int' <IntegralCast>
  (DeclRefExpr 0x98d6520 <col:7> 'unsigned int' EnumConstant='a' 0x98be568))
,
    c = (UnaryOperator 0x98d65a0 <bug.c:4:7, col:8> 'int' prefix '-'
  (IntegerLiteral 0x98d6578 <col:8> 'int' 1))

};

As you can see above the enum constant a have int type, but DeclRefExpr
inside b initialization has still unsigned int type and the constant
expression evaluator crashes on such things.


-- 
Abramo Bagnara

Opera Unica                          Phone: +39.0546.656023
Via Borghesi, 16
48014 Castel Bolognese (RA) - Italy



More information about the cfe-dev mailing list