From 24a2a48bc2e042ca9684d5dfb71e4a28a12a32b0 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Sun, 3 Feb 2019 03:44:31 +0000 Subject: [PATCH] Fix handling of usual deallocation functions in various configuratios. Clang allows users to enable or disable various types of allocation and deallocation regardless of the C++ dialect. When extended new/delete overloads are enabled in older dialects, we need to treat them as if they're usual. Also, disabling one usual deallocation form shouldn't disable any others. For example, disabling aligned allocation in C++2a should have no effect on destroying delete. llvm-svn: 352980 --- clang/lib/AST/DeclCXX.cpp | 9 ++- .../test/SemaCXX/cxx2a-destroying-delete.cpp | 13 ++-- .../extended-usual-deallocation-functions.cpp | 69 +++++++++++++++++++ 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 clang/test/SemaCXX/extended-usual-deallocation-functions.cpp diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp index 22ec400571eb..cb908e511733 100644 --- a/clang/lib/AST/DeclCXX.cpp +++ b/clang/lib/AST/DeclCXX.cpp @@ -2091,8 +2091,13 @@ bool CXXMethodDecl::isUsualDeallocationFunction( return false; // In C++17 onwards, all potential usual deallocation functions are actual - // usual deallocation functions. - if (Context.getLangOpts().AlignedAllocation) + // usual deallocation functions. Honor this behavior when post-C++14 + // deallocation functions are offered as extensions too. + // FIXME(EricWF): Destrying Delete should be a language option. How do we + // handle when destroying delete is used prior to C++17? + if (Context.getLangOpts().CPlusPlus17 || + Context.getLangOpts().AlignedAllocation || + isDestroyingOperatorDelete()) return true; // This function is a usual deallocation function if there are no diff --git a/clang/test/SemaCXX/cxx2a-destroying-delete.cpp b/clang/test/SemaCXX/cxx2a-destroying-delete.cpp index 6115774e3836..553b7a7080fe 100644 --- a/clang/test/SemaCXX/cxx2a-destroying-delete.cpp +++ b/clang/test/SemaCXX/cxx2a-destroying-delete.cpp @@ -1,4 +1,5 @@ -// RUN: %clang_cc1 -std=c++2a -verify %s +// RUN: %clang_cc1 -std=c++2a -fexceptions -verify %s +// RUN: %clang_cc1 -std=c++2a -verify %s namespace std { using size_t = decltype(sizeof(0)); @@ -58,11 +59,13 @@ namespace delete_selection { C(); void *operator new(std::size_t); void operator delete(void*) = delete; - void operator delete(C *, std::destroying_delete_t) = delete; + void operator delete(C *, std::destroying_delete_t) = delete; // expected-note 0-1 {{deleted here}} }; - // FIXME: This should be ill-formed, but we incorrectly decide that overload - // resolution failed (because it selected a deleted function) and thus no - // 'operator delete' should be called. + // TODO: We only diagnose the use of a deleted operator delete when exceptions + // are enabled. Otherwise we don't bother doing the lookup. +#ifdef __EXCEPTIONS + // expected-error@+2 {{attempt to use a deleted function}} +#endif C *new_C() { return new C; } struct D { diff --git a/clang/test/SemaCXX/extended-usual-deallocation-functions.cpp b/clang/test/SemaCXX/extended-usual-deallocation-functions.cpp new file mode 100644 index 000000000000..87b68d3a8a5a --- /dev/null +++ b/clang/test/SemaCXX/extended-usual-deallocation-functions.cpp @@ -0,0 +1,69 @@ +// RUN: %clang_cc1 -fexceptions -std=c++2a -fsized-deallocation -fno-aligned-allocation -verify %s +// RUN: %clang_cc1 -fexceptions -std=c++17 -fsized-deallocation -fno-aligned-allocation -verify %s +// RUN: %clang_cc1 -fexceptions -std=c++14 -fsized-deallocation -faligned-allocation -DHAS_ALIGN -verify %s +// RUN: %clang_cc1 -fexceptions -std=c++11 -fsized-deallocation -faligned-allocation -DHAS_ALIGN -verify %s + +// Test that we handle aligned deallocation, sized deallocation, and destroying +// delete as usual deallocation functions even if they are used as extensions +// prior to C++17. + +namespace std { +using size_t = decltype(sizeof(0)); +enum class align_val_t : size_t; + +struct destroying_delete_t { + struct __construct { explicit __construct() = default; }; + explicit destroying_delete_t(__construct) {} +}; + +inline constexpr destroying_delete_t destroying_delete(destroying_delete_t::__construct()); +} + +// FIXME: Should destroying delete really be on in all dialects by default? +struct A { + void operator delete(void*) = delete; + void operator delete(A*, std::destroying_delete_t) = delete; // expected-note {{deleted}} +}; +void ATest(A* a) { delete a; } // expected-error {{deleted}} + +struct B { + void operator delete(void*) = delete; // expected-note {{deleted}} + void operator delete(void*, std::size_t) = delete; +}; +void BTest(B *b) { delete b; }// expected-error {{deleted}} + + +struct alignas(32) C { +#ifndef HAS_ALIGN + // expected-note@+2 {{deleted}} +#endif + void operator delete(void*) = delete; +#ifdef HAS_ALIGN + // expected-note@+2 {{deleted}} +#endif + void operator delete(void*, std::align_val_t) = delete; +}; +void CTest(C *c) { delete c; } // expected-error {{deleted}} + +struct D { + void operator delete(void*) = delete; + void operator delete(D*, std::destroying_delete_t) = delete; // expected-note {{deleted}} + void operator delete(D*, std::destroying_delete_t, std::size_t) = delete; + void operator delete(D*, std::destroying_delete_t, std::align_val_t) = delete; + void operator delete(D*, std::destroying_delete_t, std::size_t, std::align_val_t) = delete; +}; +void DTest(D *d) { delete d; } // expected-error {{deleted}} + +struct alignas(64) E { + void operator delete(void*) = delete; + void operator delete(E*, std::destroying_delete_t) = delete; + void operator delete(E*, std::destroying_delete_t, std::size_t) = delete; + void operator delete(E*, std::destroying_delete_t, std::align_val_t) = delete; + void operator delete(E*, std::destroying_delete_t, std::size_t, std::align_val_t) = delete; +#ifdef HAS_ALIGN + // expected-note@-3 {{deleted}} +#else + // expected-note@-7 {{deleted}} +#endif +}; +void ETest(E *e) { delete e; } // expected-error {{deleted}}