Surprising cast

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Surprising cast

Abramo Bagnara-2
$ 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?
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Surprising cast

John McCall
On Dec 11, 2010, at 9:21 AM, Abramo Bagnara wrote:

> $ 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.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Surprising cast

Abramo Bagnara-2
Il 11/12/2010 23:33, John McCall ha scritto:

> On Dec 11, 2010, at 9:21 AM, Abramo Bagnara wrote:
>> $ 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).

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.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Surprising cast

Abramo Bagnara-2
In reply to this post by John McCall
Il 11/12/2010 23:33, John McCall ha scritto:

> On Dec 11, 2010, at 9:21 AM, Abramo Bagnara wrote:
>> $ 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).

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?

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Surprising cast

Douglas Gregor

On Dec 17, 2010, at 4:28 AM, Abramo Bagnara wrote:

> Il 11/12/2010 23:33, John McCall ha scritto:
>> On Dec 11, 2010, at 9:21 AM, Abramo Bagnara wrote:
>>> $ 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).
>
> 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
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Surprising cast

Abramo Bagnara-2
Il 17/12/2010 16:47, Douglas Gregor ha scritto:

>
> On Dec 17, 2010, at 4:28 AM, Abramo Bagnara wrote:
>
>> Il 11/12/2010 23:33, John McCall ha scritto:
>>> On Dec 11, 2010, at 9:21 AM, Abramo Bagnara wrote:
>>>> $ 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).
>>
>> 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.

It should be considered a bug?
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Surprising cast

Douglas Gregor

On Dec 17, 2010, at 7:54 AM, Abramo Bagnara wrote:

> Il 17/12/2010 16:47, Douglas Gregor ha scritto:
>>
>> On Dec 17, 2010, at 4:28 AM, Abramo Bagnara wrote:
>>
>>> Il 11/12/2010 23:33, John McCall ha scritto:
>>>> On Dec 11, 2010, at 9:21 AM, Abramo Bagnara wrote:
>>>>> $ 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).
>>>
>>> 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.
>
> It should be considered a bug?


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

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev