[Clang] Switch from TransformExpr to TransformInitializer in places we need to revert initializer to it syntactic form for Sema

In some cases we are using TransformExpr instead of
TransformInitializer, this results in ExprWithCleanups being dropped and
we are not emitting a destructor as a result.

This fixes: https://github.com/llvm/llvm-project/issues/62818

Differential Revision: https://reviews.llvm.org/D151235
This commit is contained in:
Shafik Yaghmour 2023-05-25 09:44:10 -07:00
parent 047e7ff253
commit 2a23de01e5
3 changed files with 38 additions and 15 deletions

View File

@ -11402,7 +11402,8 @@ TreeTransform<Derived>::TransformBinaryOperator(BinaryOperator *E) {
if (LHS.isInvalid())
return ExprError();
ExprResult RHS = getDerived().TransformExpr(E->getRHS());
ExprResult RHS =
getDerived().TransformInitializer(E->getRHS(), /*NotCopyInit=*/false);
if (RHS.isInvalid())
return ExprError();
@ -11950,7 +11951,8 @@ TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
ExprResult Second;
if (E->getNumArgs() == 2) {
Second = getDerived().TransformExpr(E->getArg(1));
Second =
getDerived().TransformInitializer(E->getArg(1), /*NotCopyInit=*/false);
if (Second.isInvalid())
return ExprError();
}

View File

@ -7,8 +7,6 @@ void clang_analyzer_eval(bool);
int global;
namespace variant_0 {
// This variant of the code works correctly. Function foo() is not a template
// function. Note that there are two destructors within foo().
class A {
public:
@ -46,9 +44,6 @@ void bar() {
} // end namespace variant_0
namespace variant_1 {
// Suddenly, if we turn foo() into a template, we are missing a
// CXXBindTemporaryExpr in the AST, and therefore we're missing a
// temporary destructor in the CFG.
class A {
public:
@ -59,8 +54,6 @@ class B {
A a;
};
// FIXME: Find the construction context for {} and enforce the temporary
// destructor.
// CHECK: template<> void foo<int>(int)
// CHECK: [B1]
// CHECK-NEXT: 1: (CXXConstructExpr, [B1.2], B)
@ -68,10 +61,12 @@ class B {
// CHECK-NEXT: 3: operator=
// CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, B &(*)(B &&) noexcept)
// CHECK-NEXT: 5: i
// CHECK-NEXT: 6: {} (CXXConstructExpr, B)
// CHECK-NEXT: 7: [B1.6]
// CHECK-NEXT: 8: [B1.5] = [B1.7] (OperatorCall)
// CHECK-NEXT: 9: [B1.2].~B() (Implicit destructor)
// CHECK-NEXT: 6: {} (CXXConstructExpr, [B1.7], [B1.8], B)
// CHECK-NEXT: 7: [B1.6] (BindTemporary)
// CHECK-NEXT: 8: [B1.7]
// CHECK-NEXT: 9: [B1.5] = [B1.8] (OperatorCall)
// CHECK-NEXT: 10: ~B() (Temporary object destructor)
// CHECK-NEXT: 11: [B1.2].~B() (Implicit destructor)
template <typename T> void foo(T) {
B i;
i = {};
@ -80,8 +75,7 @@ template <typename T> void foo(T) {
void bar() {
global = 0;
foo(1);
// FIXME: Should be TRUE, i.e. we should call (and inline) two destructors.
clang_analyzer_eval(global == 2); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(global == 2); // expected-warning{{TRUE [debug.ExprInspection]}}
}
} // end namespace variant_1

View File

@ -0,0 +1,27 @@
// RUN: %clang_cc1 -std=c++17 -emit-llvm -triple x86_64-linux-gnu -o - %s | FileCheck %s
void doSomething();
struct A {
A() {};
~A() noexcept {
doSomething();
}
A & operator=(A a) & noexcept {
return *this;
}
};
template<typename T>
struct B {
void test() {a = {};}
// CHECK: define linkonce_odr void @_ZN1BIiE4testEv
// CHECK: call void @_ZN1AC1Ev(ptr noundef nonnull align 1 dereferenceable(1)
// CHECK: [[CALL:%.*]] = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNR1AaSES_
// CHECK: call void @_ZN1AD2Ev(ptr noundef nonnull align 1 dereferenceable(1)
A a;
};
void client(B<int> &f) {f.test();}