/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2015 Microsoft Corporation. All rights reserved. // // This code is licensed under the MIT License (MIT). // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // /////////////////////////////////////////////////////////////////////////////// #include // for not_null, operator<, operator<=, operator> #include #include // for declval #include "deathTestCommon.h" using namespace gsl; #if __cplusplus >= 201703l using std::void_t; #else // __cplusplus >= 201703l template using void_t = void; #endif // __cplusplus < 201703l // stand-in for a user-defined ref-counted class template struct RefCounted { RefCounted(T* p) : p_(p) {} operator T*() { return p_; } T* p_; }; namespace { // clang-format off GSL_SUPPRESS(f.4) // NO-FORMAT: attribute // clang-format on bool helper(not_null p) { return *p == 12; } // clang-format off GSL_SUPPRESS(f.4) // NO-FORMAT: attribute // clang-format on bool helper_const(not_null p) { return *p == 12; } // clang-format off GSL_SUPPRESS(f.4) // NO-FORMAT: attribute // clang-format on bool strict_helper(strict_not_null p) { return *p == 12; } // clang-format off GSL_SUPPRESS(f.4) // NO-FORMAT: attribute // clang-format on bool strict_helper_const(strict_not_null p) { return *p == 12; } int* return_pointer() { return nullptr; } } // namespace template static constexpr bool CtorCompilesFor_A = false; template static constexpr bool CtorCompilesFor_A{std::declval()})>> = true; template static constexpr bool CtorCompilesFor_B = false; template static constexpr bool CtorCompilesFor_B{N})>> = true; template static constexpr bool DefaultCtorCompilesFor = false; template static constexpr bool DefaultCtorCompilesFor{})>> = true; template static constexpr bool CtorCompilesFor_C = false; template static constexpr bool CtorCompilesFor_C< U, void_t{std::declval>()})>> = true; TEST(strict_notnull_tests, TestStrictNotNullConstructors) { { static_assert(CtorCompilesFor_A, "CtorCompilesFor_A"); static_assert(!CtorCompilesFor_A, "!CtorCompilesFor_A"); static_assert(!CtorCompilesFor_B, "!CtorCompilesFor_B"); static_assert(!DefaultCtorCompilesFor, "!DefaultCtorCompilesFor"); static_assert(!CtorCompilesFor_C, "CtorCompilesFor_C"); #ifdef CONFIRM_COMPILATION_ERRORS // Forbid non-nullptr assignable types strict_not_null> f(std::vector{1}); strict_not_null z(10); strict_not_null> y({1, 2}); #endif } const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. TestNotNullConstructors"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { // from shared pointer int i = 12; auto rp = RefCounted(&i); strict_not_null p(rp); EXPECT_TRUE(p.get() == &i); strict_not_null> x( std::make_shared(10)); // shared_ptr is nullptr assignable int* pi = nullptr; EXPECT_DEATH((strict_not_null(pi)), expected); } { // from unique pointer strict_not_null> x( std::make_unique(10)); // unique_ptr is nullptr assignable EXPECT_DEATH((strict_not_null>(std::unique_ptr{})), expected); } { // from pointer to local int t = 42; strict_not_null x{&t}; helper(&t); helper_const(&t); EXPECT_TRUE(*x == 42); } { // from raw pointer // from strict_not_null pointer int t = 42; int* p = &t; strict_not_null x{p}; helper(p); helper_const(p); helper(x); helper_const(x); EXPECT_TRUE(*x == 42); } { // from raw const pointer // from strict_not_null const pointer int t = 42; const int* cp = &t; strict_not_null x{cp}; helper_const(cp); helper_const(x); EXPECT_TRUE(*x == 42); } { // from strict_not_null const pointer, using auto int t = 42; const int* cp = &t; auto x = strict_not_null{cp}; EXPECT_TRUE(*x == 42); } { // from returned pointer EXPECT_DEATH(helper(return_pointer()), expected); EXPECT_DEATH(helper_const(return_pointer()), expected); } } template static constexpr bool StrictHelperCompilesFor = false; template static constexpr bool StrictHelperCompilesFor()))>> = true; template static constexpr bool StrictHelperConstCompilesFor = false; template static constexpr bool StrictHelperConstCompilesFor()))>> = true; template static constexpr bool HelperCompilesFor = false; template static constexpr bool HelperCompilesFor()))>> = true; TEST(strict_notnull_tests, TestStrictNotNull) { { // raw ptr <-> strict_not_null int x = 42; #ifdef CONFIRM_COMPILATION_ERRORS strict_not_null snn = &x; #endif static_assert(!StrictHelperCompilesFor, "!StrictHelperCompilesFor"); static_assert(!StrictHelperConstCompilesFor, "!StrictHelperCompilesFor"); const strict_not_null snn1{&x}; static_assert(StrictHelperCompilesFor>, "StrictHelperCompilesFor>"); helper(snn1); helper_const(snn1); EXPECT_TRUE(*snn1 == 42); } { // raw ptr <-> strict_not_null const int x = 42; #ifdef CONFIRM_COMPILATION_ERRORS strict_not_null snn = &x; #endif static_assert(!StrictHelperCompilesFor, "!StrictHelperFor"); static_assert(!StrictHelperConstCompilesFor, "!StrictHelperCompilesFor"); const strict_not_null snn1{&x}; static_assert(!HelperCompilesFor>, "!HelperCompilesFor>"); static_assert(StrictHelperConstCompilesFor>, "StrictHelperCompilesFor>"); helper_const(snn1); EXPECT_TRUE(*snn1 == 42); } { // strict_not_null -> strict_not_null int x = 42; strict_not_null snn1{&x}; const strict_not_null snn2{&x}; strict_helper(snn1); strict_helper_const(snn1); strict_helper_const(snn2); EXPECT_TRUE(snn1 == snn2); } { // strict_not_null -> strict_not_null const int x = 42; strict_not_null snn1{&x}; const strict_not_null snn2{&x}; static_assert(!StrictHelperCompilesFor>, "!StrictHelperCompilesFor>"); strict_helper_const(snn1); strict_helper_const(snn2); EXPECT_TRUE(snn1 == snn2); } { // strict_not_null -> not_null int x = 42; strict_not_null snn{&x}; const not_null nn1 = snn; const not_null nn2{snn}; helper(snn); helper_const(snn); EXPECT_TRUE(snn == nn1); EXPECT_TRUE(snn == nn2); } { // strict_not_null -> not_null const int x = 42; strict_not_null snn{&x}; const not_null nn1 = snn; const not_null nn2{snn}; static_assert(!HelperCompilesFor>, "!HelperCompilesFor>"); helper_const(snn); EXPECT_TRUE(snn == nn1); EXPECT_TRUE(snn == nn2); } { // not_null -> strict_not_null int x = 42; not_null nn{&x}; const strict_not_null snn1{nn}; const strict_not_null snn2{nn}; strict_helper(nn); strict_helper_const(nn); EXPECT_TRUE(snn1 == nn); EXPECT_TRUE(snn2 == nn); std::hash> hash_snn; std::hash> hash_nn; EXPECT_TRUE(hash_nn(snn1) == hash_nn(nn)); EXPECT_TRUE(hash_snn(snn1) == hash_nn(nn)); EXPECT_TRUE(hash_nn(snn1) == hash_nn(snn2)); EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn)); } { // not_null -> strict_not_null const int x = 42; not_null nn{&x}; const strict_not_null snn1{nn}; const strict_not_null snn2{nn}; static_assert(!StrictHelperCompilesFor>, "!StrictHelperCompilesFor>"); strict_helper_const(nn); EXPECT_TRUE(snn1 == nn); EXPECT_TRUE(snn2 == nn); std::hash> hash_snn; std::hash> hash_nn; EXPECT_TRUE(hash_nn(snn1) == hash_nn(nn)); EXPECT_TRUE(hash_snn(snn1) == hash_nn(nn)); EXPECT_TRUE(hash_nn(snn1) == hash_nn(snn2)); EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn)); } } #if defined(__cplusplus) && (__cplusplus >= 201703L) TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. TestStrictNotNullConstructorTypeDeduction"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { int i = 42; strict_not_null x{&i}; helper(strict_not_null{&i}); helper_const(strict_not_null{&i}); EXPECT_TRUE(*x == 42); } { const int i = 42; strict_not_null x{&i}; static_assert(!HelperCompilesFor>, "!HelperCompilesFor>"); helper_const(strict_not_null{&i}); EXPECT_TRUE(*x == 42); } { int i = 42; int* p = &i; strict_not_null x{p}; helper(strict_not_null{p}); helper_const(strict_not_null{p}); EXPECT_TRUE(*x == 42); } { const int i = 42; const int* p = &i; strict_not_null x{p}; static_assert(!HelperCompilesFor>, "!HelperCompilesFor>"); helper_const(strict_not_null{p}); EXPECT_TRUE(*x == 42); } { auto workaround_macro = []() { int* p1 = nullptr; const strict_not_null x{p1}; }; EXPECT_DEATH(workaround_macro(), expected); } { auto workaround_macro = []() { const int* p1 = nullptr; const strict_not_null x{p1}; }; EXPECT_DEATH(workaround_macro(), expected); } { int* p = nullptr; EXPECT_DEATH(helper(strict_not_null{p}), expected); EXPECT_DEATH(helper_const(strict_not_null{p}), expected); } #ifdef CONFIRM_COMPILATION_ERRORS { strict_not_null x{nullptr}; helper(strict_not_null{nullptr}); helper_const(strict_not_null{nullptr}); } #endif } #endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)