From ed9caee9a83a5a03bb2f328a79fcf79696d2789a Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Thu, 13 Apr 2017 01:11:58 +0000 Subject: Add tests that std::unique_ptr's default constructor is constexpr. std::unique_ptr's default constructor must be constexpr in order to allow constant initialization to take place for static objects; Even though we can never have a constexpr unique_ptr variable since it's not a literal type. This patch adds tests that constant initialization takes place by using the __attribute__((require_constant_initialization)) macro. llvm-svn: 300158 --- .../unique.ptr.runtime.ctor/default.pass.cpp | 50 +++++++++++++ .../unique.ptr.runtime.ctor/default01.pass.cpp | 47 ------------ .../unique.ptr.runtime.ctor/default02.pass.cpp | 87 ---------------------- .../default_allows_incomplete.pass.cpp | 80 ++++++++++++++++++++ .../unique.ptr.single.ctor/default.pass.cpp | 5 ++ 5 files changed, 135 insertions(+), 134 deletions(-) create mode 100644 libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default.pass.cpp delete mode 100644 libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default01.pass.cpp delete mode 100644 libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default02.pass.cpp create mode 100644 libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default_allows_incomplete.pass.cpp (limited to 'libcxx/test/std/utilities/memory') diff --git a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default.pass.cpp b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default.pass.cpp new file mode 100644 index 00000000000..ff59b3b9f80 --- /dev/null +++ b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default.pass.cpp @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// unique_ptr + +// Test unique_ptr default ctor + +// default unique_ptr ctor should only require default Deleter ctor + +#include +#include +#include "test_macros.h" + +#if defined(_LIBCPP_VERSION) +_LIBCPP_SAFE_STATIC std::unique_ptr global_static_unique_ptr; +#endif + +class Deleter { + int state_; + + Deleter(Deleter&); + Deleter& operator=(Deleter&); + +public: + Deleter() : state_(5) {} + + int state() const { return state_; } + + void operator()(void*) {} +}; + +int main() { + { + std::unique_ptr p; + assert(p.get() == 0); + } + { + std::unique_ptr p; + assert(p.get() == 0); + assert(p.get_deleter().state() == 5); + } +} diff --git a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default01.pass.cpp b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default01.pass.cpp deleted file mode 100644 index 0cc54382b98..00000000000 --- a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default01.pass.cpp +++ /dev/null @@ -1,47 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// - -// unique_ptr - -// Test unique_ptr default ctor - -// default unique_ptr ctor should only require default Deleter ctor - -#include -#include - -class Deleter -{ - int state_; - - Deleter(Deleter&); - Deleter& operator=(Deleter&); - -public: - Deleter() : state_(5) {} - - int state() const {return state_;} - - void operator()(void*) {} -}; - -int main() -{ - { - std::unique_ptr p; - assert(p.get() == 0); - } - { - std::unique_ptr p; - assert(p.get() == 0); - assert(p.get_deleter().state() == 5); - } -} diff --git a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default02.pass.cpp b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default02.pass.cpp deleted file mode 100644 index 3ded41c419c..00000000000 --- a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default02.pass.cpp +++ /dev/null @@ -1,87 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// - -// unique_ptr - -// Test default unique_ptr ctor - -// default unique_ptr ctor shouldn't require complete type - -#include -#include - -struct A; - -class Deleter -{ - int state_; - - Deleter(Deleter&); - Deleter& operator=(Deleter&); - -public: - Deleter() : state_(5) {} - - int state() const {return state_;} - - void operator()(A* p); -}; - -void check(int i); - -template > -struct B -{ - std::unique_ptr a_; - B(); - ~B(); - - A* get() const {return a_.get();} - D& get_deleter() {return a_.get_deleter();} -}; - -int main() -{ - { - B<> s; - assert(s.get() == 0); - } - check(0); - { - B s; - assert(s.get() == 0); - assert(s.get_deleter().state() == 5); - } - check(0); -} - -struct A -{ - static int count; - A() {++count;} - A(const A&) {++count;} - ~A() {--count;} -}; - -int A::count = 0; - -void Deleter::operator()(A* p) {delete p;} - -void check(int i) -{ - assert(A::count == i); -} - -template -B::B() {} - -template -B::~B() {} diff --git a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default_allows_incomplete.pass.cpp b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default_allows_incomplete.pass.cpp new file mode 100644 index 00000000000..f54baac5bb0 --- /dev/null +++ b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.ctor/default_allows_incomplete.pass.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// unique_ptr + +// Test default unique_ptr ctor + +// default unique_ptr ctor shouldn't require complete type + +#include +#include + +struct A; + +class Deleter { + int state_; + + Deleter(Deleter&); + Deleter& operator=(Deleter&); + +public: + Deleter() : state_(5) {} + + int state() const { return state_; } + + void operator()(A* p); +}; + +void check(int i); + +template > +struct B { + std::unique_ptr a_; + B(); + ~B(); + + A* get() const { return a_.get(); } + D& get_deleter() { return a_.get_deleter(); } +}; + +int main() { + { + B<> s; + assert(s.get() == 0); + } + check(0); + { + B s; + assert(s.get() == 0); + assert(s.get_deleter().state() == 5); + } + check(0); +} + +struct A { + static int count; + A() { ++count; } + A(const A&) { ++count; } + ~A() { --count; } +}; + +int A::count = 0; + +void Deleter::operator()(A* p) { delete p; } + +void check(int i) { assert(A::count == i); } + +template +B::B() {} + +template +B::~B() {} diff --git a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.ctor/default.pass.cpp b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.ctor/default.pass.cpp index 569daae7da0..6508d9360e5 100644 --- a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.ctor/default.pass.cpp +++ b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.ctor/default.pass.cpp @@ -25,9 +25,14 @@ #include #include +#include "test_macros.h" #include "deleter_types.h" +#if defined(_LIBCPP_VERSION) +_LIBCPP_SAFE_STATIC std::unique_ptr global_static_unique_ptr; +#endif + struct IncompleteT; void checkNumIncompleteTypeAlive(int i); -- cgit v1.2.3