Author: cor3ntin Date: 2024-08-26T22:55:32+02:00 New Revision: c821cc3f8824e29ba1861fb1de43e90543928498
URL: https://github.com/llvm/llvm-project/commit/c821cc3f8824e29ba1861fb1de43e90543928498 DIFF: https://github.com/llvm/llvm-project/commit/c821cc3f8824e29ba1861fb1de43e90543928498.diff LOG: [Clang] Correctly finds subexpressions of immediate invocations (#106055) We were not correctly ignoring implicit casts. Fixes #105558 Added: Modified: clang/docs/ReleaseNotes.rst clang/lib/Sema/SemaExpr.cpp clang/test/AST/ByteCode/new-delete.cpp clang/test/SemaCXX/cxx2a-consteval.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 0a31953ab87005..2c29d49ba20f03 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -317,7 +317,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 ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index ea57316ad8014e..95f53dfefbcc52 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -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) { diff --git a/clang/test/AST/ByteCode/new-delete.cpp b/clang/test/AST/ByteCode/new-delete.cpp index a7be4102fd0a05..d733e3182fd59c 100644 --- a/clang/test/AST/ByteCode/new-delete.cpp +++ b/clang/test/AST/ByteCode/new-delete.cpp @@ -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; } @@ -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}} \ @@ -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}} } } diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp index 6b0609a26c5882..81923617f637e8 100644 --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/clang/test/SemaCXX/cxx2a-consteval.cpp @@ -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}} @@ -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}} } } @@ -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()); +} + } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits