Skip to content

[Clang] Correctly finds subexpressions of immediate invocations #106055

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 3 commits into from
Aug 26, 2024
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
2 changes: 1 addition & 1 deletion clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Bug Fixes to C++ Support
- Clang now rebuilds the template parameters of out-of-line declarations and specializations in the context
of the current instantiation in all cases.
- Fix evaluation of the index of dependent pack indexing expressions/types specifiers (#GH105900)

- Correctly handle subexpressions of an immediate invocation in the presence of implicit casts. (#GH105558)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
17 changes: 14 additions & 3 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17463,11 +17463,22 @@ static void RemoveNestedImmediateInvocation(
ExprResult TransformInitializer(Expr *Init, bool NotCopyInit) {
if (!Init)
return Init;

// We cannot use IgnoreImpCasts because we need to preserve
// full expressions.
while (true) {
if (auto *ICE = dyn_cast<ImplicitCastExpr>(Init))
Init = ICE->getSubExpr();
else if (auto *ICE = dyn_cast<MaterializeTemporaryExpr>(Init))
Init = ICE->getSubExpr();
else
break;
}
/// ConstantExpr are the first layer of implicit node to be removed so if
/// Init isn't a ConstantExpr, no ConstantExpr will be skipped.
if (auto *CE = dyn_cast<ConstantExpr>(Init))
if (CE->isImmediateInvocation())
RemoveImmediateInvocation(CE);
if (auto *CE = dyn_cast<ConstantExpr>(Init);
CE && CE->isImmediateInvocation())
RemoveImmediateInvocation(CE);
return Base::TransformInitializer(Init, NotCopyInit);
}
ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
Expand Down
16 changes: 5 additions & 11 deletions clang/test/AST/ByteCode/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept {

namespace cxx2a {
struct A {
int* p = new int(42); // both-note 7{{heap allocation performed here}}
int* p = new int(42); // both-note 3{{heap allocation performed here}}
consteval int ret_i() const { return p ? *p : 0; }
consteval A ret_a() const { return A{}; }
constexpr ~A() { delete p; }
Expand Down Expand Up @@ -433,9 +433,7 @@ void test() {
{ A k = to_lvalue_ref(A()); } // both-error {{'cxx2a::to_lvalue_ref' is not a constant expression}} \
// both-note {{reference to temporary is not a constant expression}} \
// both-note {{temporary created here}}
{ A k = to_lvalue_ref(A().ret_a()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
// both-note {{heap-allocated object is not a constant expression}} \
// both-error {{'cxx2a::to_lvalue_ref' is not a constant expression}} \
{ A k = to_lvalue_ref(A().ret_a()); } // both-error {{'cxx2a::to_lvalue_ref' is not a constant expression}} \
// both-note {{reference to temporary is not a constant expression}} \
// both-note {{temporary created here}}
{ int k = A().ret_a().ret_i(); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
Expand All @@ -445,19 +443,15 @@ void test() {
{ int k = const_a_ref(a); }
{ int k = rvalue_ref(A()); }
{ int k = rvalue_ref(std::move(a)); }
{ int k = const_a_ref(A().ret_a()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
// both-note {{is not a constant expression}}
{ int k = const_a_ref(to_lvalue_ref(A().ret_a())); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
// both-note {{is not a constant expression}}
{ int k = const_a_ref(A().ret_a()); }
{ int k = const_a_ref(to_lvalue_ref(A().ret_a())); }
{ int k = const_a_ref(to_lvalue_ref(std::move(a))); }
{ int k = by_value_a(A().ret_a()); }
{ int k = by_value_a(to_lvalue_ref(static_cast<const A&&>(a))); }
{ int k = (A().ret_a(), A().ret_i()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
// both-note {{is not a constant expression}} \
// both-warning {{left operand of comma operator has no effect}}
{ int k = (const_a_ref(A().ret_a()), A().ret_i()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
// both-note {{is not a constant expression}} \
// both-warning {{left operand of comma operator has no effect}}
{ int k = (const_a_ref(A().ret_a()), A().ret_i()); } // both-warning {{left operand of comma operator has no effect}}
}
}

Expand Down
37 changes: 26 additions & 11 deletions clang/test/SemaCXX/cxx2a-consteval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,9 @@ void test() {
{ A k = to_lvalue_ref(A()); } // expected-error {{is not a constant expression}}
// expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
{ A k = to_lvalue_ref(A().ret_a()); }
// expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
// expected-note@-2 {{heap-allocated object is not a constant expression}}
// expected-error@-3 {{'alloc::to_lvalue_ref' is not a constant expression}}
// expected-note@-4 {{reference to temporary is not a constant expression}}
// expected-note@-5 {{temporary created here}}
// expected-note@-1 {{reference to temporary is not a constant expression}}
// expected-error@-2 {{'alloc::to_lvalue_ref' is not a constant expression}}
// expected-note@-3 {{temporary created here}}
{ int k = A().ret_a().ret_i(); }
// expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
// expected-note@-2 {{heap-allocated object is not a constant expression}}
Expand All @@ -394,19 +392,13 @@ void test() {
{ int k = rvalue_ref(A()); }
{ int k = rvalue_ref(std::move(a)); }
{ int k = const_a_ref(A().ret_a()); }
// expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
// expected-note@-2 {{is not a constant expression}}
{ int k = const_a_ref(to_lvalue_ref(A().ret_a())); }
// expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
// expected-note@-2 {{is not a constant expression}}
{ int k = const_a_ref(to_lvalue_ref(std::move(a))); }
{ int k = by_value_a(A().ret_a()); }
{ int k = by_value_a(to_lvalue_ref(static_cast<const A&&>(a))); }
{ int k = (A().ret_a(), A().ret_i()); }// expected-error {{is not a constant expression}}
// expected-note@-1 {{is not a constant expression}}
{ int k = (const_a_ref(A().ret_a()), A().ret_i()); }
// expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
// expected-note@-2 {{is not a constant expression}}
}

}
Expand Down Expand Up @@ -1232,4 +1224,27 @@ consteval void immediate() {
}


}

namespace GH105558 {

consteval int* alloc() { return new int(0); }
consteval void f(int* p) { delete p; }
consteval void g1(int*&& p) { delete p; }
consteval void g2(const int* p) { delete p; }
consteval void g3(int*const& p) { delete p; }
struct X {
int* p;
explicit(false) constexpr X(int* p) : p(p) {}
};
consteval void g4(X x) { delete x.p; }

void test() {
f(alloc());
g1(alloc());
g2(alloc());
g3(alloc());
g4(alloc());
}

}
Loading