[cfe-dev] Missing implicit cast when parsing C++ ?

Eli Friedman eli.friedman at gmail.com
Wed Apr 6 13:51:26 PDT 2011


On Wed, Apr 6, 2011 at 9:43 AM, Abramo Bagnara <abramo.bagnara at gmail.com> wrote:
> Il 06/04/2011 16:31, Enea Zaffanella ha scritto:
>> According to the C++ standard, the operand of the logical negation
>> operator (!) is implicitly converted to type bool
>> (C++98/03 5.3.1p8, C++0x 5.3.1p9).
>>
>> However, parsing this C++ code fragment using clang
>>
>> bool foo() { return !1234; }
>>
>> we obtain the following AST, with no implicit cast:
>>
>> bool foo() (CompoundStmt 0x479b2f8 <ex.cc:1:12, col:28>
>>   (ReturnStmt 0x479b2d8 <col:14, col:22>
>>     (UnaryOperator 0x479b2b8 <col:21, col:22> '_Bool' prefix '!'
>>       (IntegerLiteral 0x479b290 <col:22> 'int' 1234))))
>>
>> Usually, clang inserts all implicit casts in its AST.
>> Is the behavior above a _meant_ exception to this general rule?
>>
>> (Side note: if the integer literal is replaced by some non-boolean
>> variable, an CK_LValueToRValue implicit cast is added, but again no cast
>> from the variable type to the Boolean type.)
>
> I've attached a candidate patch to fix this. Ok to commit?

Does this affect -O0 code generation for constructs like "if (!foo)"?

For references to the C++ standard, we generally prefer to use
"[expr.unary.op]" over "5.3.1", to make it easier to find in future
versions which might renumber the headings.

Otherwise, it should be fine.

-Eli




More information about the cfe-dev mailing list