[cfe-dev] Assignment expression in C++ returns its right operand (A possible bug)

John McCall rjmccall at apple.com
Fri Feb 17 02:26:27 PST 2012

On Feb 17, 2012, at 2:16 AM, Abramo Bagnara wrote:
> Il 17/02/2012 10:52, John McCall ha scritto:
>> On Feb 16, 2012, at 2:37 PM, Xiaolong Tang wrote:
>>> Note that the register value denoted by %tmp is returned, but %tmp is
>>> the value of the second parameter. (This does not agree with the c++
>>> semantics.) 
>>> Again I am using clang version 3.0. And the revision is git-svn-id:
>>> https://llvm.org/svn/llvm-project/cfe/trunk@137823
>>> If I am correct and the above problem has been fixed, can someone
>>> point to me the commit where the problem is fixed? If I am somehow
>>> wrong, can someone give me any explanation? 
>> As David says, it's not a problem.  You are correct that C++ semantics
>> say that the RHS is stored to the LHS and then the result of the
>> expression is the same l-value as the original LHS.  However, when
>> that l-value is non-volatile, we are not strictly bound by those
>> semantics.  In this case, it is clear that the object identified by the LHS
>> is not modified by this thread between the store and the load, and
>> any other thread modifying it would be a forbidden race condition.
>> Therefore, if the result of this assignment is needed as an r-value,
>> we can simply forward the original value (suitably truncated if the
>> l-value is a bitfield).
> BTW: John, wouldn't it be a very good thing to have a
> BitfieldTruncateExpr AST node (or an extension of ImplicitCastExpr)?

I don't see this as tremendously useful;  it's not particularly difficult to
detect that there's a potential bitfield truncation occurring as part of an
assignment.  The bigger problem, though, is that this doesn't really
model the language;  the truncation happens as part of the assignment,
not as part of the evaluation of the right-hand side.  That distinction
matters a lot when the operation is a compound assignment or when
(and many sarcastic thanks go to C++ for this wonderful feature) the
LHS is a conditional bitfield l-value.


More information about the cfe-dev mailing list