[PATCH] D39562: [CodeGen][ObjC] Fix an assertion failure caused by copy elision

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 2 12:45:19 PDT 2017


ahatanak created this revision.
Herald added subscribers: kristof.beyls, aemerson.

The assertion failure occurs when a setter is called using the dot syntax to set a property of a class type that doesn't have trivial copy/move constructors or assignment operators. It happens only when clang is compiling for c++1z and copy elision allows avoiding copying a temporary to the argument passed to the setter method in Sema.

For example:

  struct S2 {
    id f1;
  };
  
  @interface C
  @property S2 f;
  @end
  @implementation C
  @end
  
  void test(C *c) {
    c.f = S2();
  }

When IRGen visits the ObjCMessageExpr for the call to the setter, it tries to emit a copy of an S2 object that has been constructed (this happens in AggExprEmitter::VisitOpaqueValueExpr) and the assertion in CodeGenFunction::EmitAggregateCopy fails because S2 doesn't have the trivial special functions needed to do the copy.

This is the AST of the ObjCMessageExpr:

  `-ObjCMessageExpr 0x7fe03c06b730 <col:5> 'void' selector=setF:
  |         |-OpaqueValueExpr 0x7fe03c06b698 <col:3> 'C *'
  |         | `-ImplicitCastExpr 0x7fe03c06b5e0 <col:3> 'C *' <LValueToRValue>
  |         |   `-DeclRefExpr 0x7fe03c06b5b8 <col:3> 'C *__strong' lvalue ParmVar 0x7fe03c06b408 'c' 'C *__strong'
  |         `-OpaqueValueExpr 0x7fe03c06b6e8 <col:9, col:12> 'struct S2'
  |           `-CXXBindTemporaryExpr 0x7fe03c06b678 <col:9, col:12> 'struct S2' (CXXTemporary 0x7fe03c06b670)
  |             `-CXXTemporaryObjectExpr 0x7fe03c06b638 <col:9, col:12> 'struct S2' 'void (void)'

To avoid the crash, I modified CodeGenFunction::EmitAnyExprToTemp to return the value for the OpaqueValueExpr (which must have already been evaluated when it's visited, I think) so that it doesn't have to call EmitAggregateCopy to make a copy.

I'm actually unsure whether we should fix Sema and change the AST representation or fix IRGen as I did in this patch. I'm open to suggestions if anyone has a better idea to fix the crash.


https://reviews.llvm.org/D39562

Files:
  lib/CodeGen/CGExpr.cpp
  test/CodeGenObjCXX/property-dot-copy-elision.mm
  test/CodeGenObjCXX/property-objects.mm

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39562.121345.patch
Type: text/x-patch
Size: 3309 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171102/8d4876d0/attachment.bin>


More information about the cfe-commits mailing list