Skip to content

[CodeGen] Emit destructor calls to destruct non-trivial C struct temporaries created by conditional and assignment operators #2034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/lib/CodeGen/CGExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,11 @@ void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {

// Copy into the destination if the assignment isn't ignored.
EmitFinalDestCopy(E->getType(), LHS);

if (!Dest.isIgnored() && !Dest.isExternallyDestructed() &&
E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct)
CGF.pushDestroy(QualType::DK_nontrivial_c_struct, Dest.getAddress(),
E->getType());
}

void AggExprEmitter::
Expand All @@ -1231,6 +1236,11 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {

// Save whether the destination's lifetime is externally managed.
bool isExternallyDestructed = Dest.isExternallyDestructed();
bool destructNonTrivialCStruct =
!isExternallyDestructed &&
E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct;
isExternallyDestructed |= destructNonTrivialCStruct;
Dest.setExternallyDestructed(isExternallyDestructed);

eval.begin(CGF);
CGF.EmitBlock(LHSBlock);
Expand All @@ -1252,6 +1262,10 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
Visit(E->getFalseExpr());
eval.end(CGF);

if (destructNonTrivialCStruct)
CGF.pushDestroy(QualType::DK_nontrivial_c_struct, Dest.getAddress(),
E->getType());

CGF.EmitBlock(ContBlock);
}

Expand Down
46 changes: 45 additions & 1 deletion clang/test/CodeGenObjC/strong-in-c-struct.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ + (StrongSmall)getStrongSmallClass;
@end

id g0;
StrongSmall g1, g2;

// CHECK: %[[STRUCT_STRONGSMALL:.*]] = type { i32, i8* }
// CHECK: %[[STRUCT_STRONGOUTER:.*]] = type { %[[STRUCT_STRONG:.*]], i8*, double }
// CHECK: %[[STRUCT_STRONG]] = type { %[[STRUCT_TRIVIAL:.*]], i8* }
// CHECK: %[[STRUCT_TRIVIAL]] = type { [4 x i32] }
// CHECK: %[[STRUCT_BLOCK_BYREF_T:.*]] = type { i8*, %[[STRUCT_BLOCK_BYREF_T]]*, i32, i32, i8*, i8*, i8*, %[[STRUCT_STRONGOUTER]] }
// CHECK: %[[STRUCT_STRONGSMALL:.*]] = type { i32, i8* }
// CHECK: %[[STRUCT_STRONGBLOCK:.*]] = type { void ()* }
// CHECK: %[[STRUCT_BITFIELD1:.*]] = type { i8, i8, i8*, i32, i8*, [3 x i32], i8*, double, i8, i8 }

Expand Down Expand Up @@ -900,4 +901,47 @@ void test_zero_bitfield() {
a = b;
}

// CHECK-LABEL: define i8* @test_conditional0(
// CHECK: %[[TMP:.*]] = alloca %[[STRUCT_STRONGSMALL]], align 8

// CHECK: %[[V1:.*]] = bitcast %[[STRUCT_STRONGSMALL]]* %[[TMP]] to i8**
// CHECK: call void @__copy_constructor_8_8_t0w4_s8(i8** %[[V1]], i8** bitcast (%[[STRUCT_STRONGSMALL]]* @g2 to i8**))

// CHECK: %[[V2:.*]] = bitcast %[[STRUCT_STRONGSMALL]]* %[[TMP]] to i8**
// CHECK: call void @__copy_constructor_8_8_t0w4_s8(i8** %[[V2]], i8** bitcast (%[[STRUCT_STRONGSMALL]]* @g1 to i8**))

// CHECK: %[[V5:.*]] = bitcast %[[STRUCT_STRONGSMALL]]* %[[TMP]] to i8**
// CHECK: call void @__destructor_8_s8(i8** %[[V5]])
// CHECK: @llvm.objc.autoreleaseReturnValue

id test_conditional0(int c) {
return (c ? g2 : g1).f1;
}

// CHECK-LABEL: define i8* @test_conditional1(
// CHECK-NOT: call void @__destructor

id test_conditional1(int c) {
calleeStrongSmall(c ? g2 : g1);
}

// CHECK-LABEL: define i8* @test_assignment0(
// CHECK: %[[TMP:.*]] = alloca %[[STRUCT_STRONGSMALL]], align 8
// CHECK: call void @__copy_assignment_8_8_t0w4_s8(i8** bitcast (%[[STRUCT_STRONGSMALL]]* @g2 to i8**), i8** bitcast (%[[STRUCT_STRONGSMALL]]* @g1 to i8**))
// CHECK: %[[V0:.*]] = bitcast %[[STRUCT_STRONGSMALL]]* %[[TMP]] to i8**
// CHECK: call void @__copy_constructor_8_8_t0w4_s8(i8** %[[V0]], i8** bitcast (%[[STRUCT_STRONGSMALL]]* @g2 to i8**))
// CHECK: %[[V3:.*]] = bitcast %[[STRUCT_STRONGSMALL]]* %[[TMP]] to i8**
// CHECK: call void @__destructor_8_s8(i8** %[[V3]])

id test_assignment0(void) {
return (g2 = g1).f1;
}

// CHECK-LABEL: define i8* @test_assignment1(
// CHECK-NOT: call void @__destructor

id test_assignment1(void) {
calleeStrongSmall(g2 = g1);
}

#endif /* USESTRUCT */