Surprising cast

$ cat p.c
enum {
  a = 1
};
$ clang -cc1 -ast-dump p.c
typedef char *__builtin_va_list;
enum {
    a = (ImplicitCastExpr 0xb9e11f0 <p.c:2:7> 'int' <IntegralCast>
  (IntegerLiteral 0xb9e11a0 <col:7> 'int' 1))

};

The implicit cast from int to int is deliberate?

Seems unnecessary to me; probably something is just making an explicit expression instead of using ImpCastExprToType (which would be appropriate here, I think).

John.

The mess is done near the end of Sema::ActOnEnumBody: there is no check
that NewTy is different from current InitExpr type and and an implicit
cast is added unconditionally.

Fixed in r122056.

A similar problem is this one:

$ cat p.cc

void foo() {
  short s = (short) 1.3;
  short t = (short) 2;
}
$ clang++ -cc1 -ast-dump p.cc
typedef char *__builtin_va_list;
void foo() (CompoundStmt 0xc00ae08 <p.cc:2:12, line:5:1>
  (DeclStmt 0xc00ad30 <line:3:3, col:24>
    0xc00aca0 "short s =
      (CStyleCastExpr 0xc00ad10 <col:13, col:21> 'short' <NoOp>
        (ImplicitCastExpr 0xc00ad00 <col:21> 'short' <FloatingToIntegral>
          (FloatingLiteral 0xc00ace0 <col:21> 'double' 1.300000e+00)))")
  (DeclStmt 0xc00adf0 <line:4:3, col:22>
    0xc00ad60 "short t =
      (CStyleCastExpr 0xc00add0 <col:13, col:21> 'short' <NoOp>
        (ImplicitCastExpr 0xc00adc0 <col:21> 'short' <IntegralCast>
          (IntegerLiteral 0xc00ada0 <col:21> 'int' 2)))"))

For unknown reasons the CStyleCastExpr is always converted to a NoOp and
an unneeded ImplicitCastExpr is added...

This happens only for C++... it is very weird IMHO.

What do you think about that?

It's an artifact of how we do type-checking for casts in C++. Basically, we end up computing the casts deep in the initialization code as ImplicitCastExprs, and don't have the information around at that point to build the appropriate CStyleCastExpr. So, we wrap the "implicit" cast we get back in a CStyleCastExpr at the outer level.

  - Doug

It should be considered a bug?

Yes. We'd love to avoid generating that extra cast expression.

  - Doug