Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / support / variant_test_helpers.h
blob78b3978dde484bc4b209d88c31232e348e86ddee
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_VARIANT_TEST_HELPERS_H
11 #define SUPPORT_VARIANT_TEST_HELPERS_H
13 #include <type_traits>
14 #include <utility>
15 #include <cassert>
17 #include "test_macros.h"
18 #include "type_id.h"
20 #if TEST_STD_VER <= 14
21 #error This file requires C++17
22 #endif
24 // FIXME: Currently the variant<T&> tests are disabled using this macro.
25 #define TEST_VARIANT_HAS_NO_REFERENCES
26 #ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
27 # define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
28 #endif
30 #ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
31 constexpr bool VariantAllowsNarrowingConversions = true;
32 #else
33 constexpr bool VariantAllowsNarrowingConversions = false;
34 #endif
36 #ifndef TEST_HAS_NO_EXCEPTIONS
37 struct CopyThrows {
38 CopyThrows() = default;
39 CopyThrows(CopyThrows const&) { throw 42; }
40 CopyThrows& operator=(CopyThrows const&) { throw 42; }
43 struct MoveThrows {
44 static int alive;
45 MoveThrows() { ++alive; }
46 MoveThrows(MoveThrows const&) {++alive;}
47 MoveThrows(MoveThrows&&) { throw 42; }
48 MoveThrows& operator=(MoveThrows const&) { return *this; }
49 MoveThrows& operator=(MoveThrows&&) { throw 42; }
50 ~MoveThrows() { --alive; }
53 int MoveThrows::alive = 0;
55 struct MakeEmptyT {
56 static int alive;
57 MakeEmptyT() { ++alive; }
58 MakeEmptyT(MakeEmptyT const&) {
59 ++alive;
60 // Don't throw from the copy constructor since variant's assignment
61 // operator performs a copy before committing to the assignment.
63 MakeEmptyT(MakeEmptyT &&) {
64 throw 42;
66 MakeEmptyT& operator=(MakeEmptyT const&) {
67 throw 42;
69 MakeEmptyT& operator=(MakeEmptyT&&) {
70 throw 42;
72 ~MakeEmptyT() { --alive; }
74 static_assert(std::is_swappable_v<MakeEmptyT>, ""); // required for test
76 int MakeEmptyT::alive = 0;
78 template <class Variant>
79 void makeEmpty(Variant& v) {
80 Variant v2(std::in_place_type<MakeEmptyT>);
81 try {
82 v = std::move(v2);
83 assert(false);
84 } catch (...) {
85 assert(v.valueless_by_exception());
88 #endif // TEST_HAS_NO_EXCEPTIONS
90 enum CallType : unsigned {
91 CT_None,
92 CT_NonConst = 1,
93 CT_Const = 2,
94 CT_LValue = 4,
95 CT_RValue = 8
98 inline constexpr CallType operator|(CallType LHS, CallType RHS) {
99 return static_cast<CallType>(static_cast<unsigned>(LHS) |
100 static_cast<unsigned>(RHS));
103 struct ForwardingCallObject {
105 template <class... Args>
106 ForwardingCallObject& operator()(Args&&...) & {
107 set_call<Args &&...>(CT_NonConst | CT_LValue);
108 return *this;
111 template <class... Args>
112 const ForwardingCallObject& operator()(Args&&...) const & {
113 set_call<Args &&...>(CT_Const | CT_LValue);
114 return *this;
117 template <class... Args>
118 ForwardingCallObject&& operator()(Args&&...) && {
119 set_call<Args &&...>(CT_NonConst | CT_RValue);
120 return std::move(*this);
123 template <class... Args>
124 const ForwardingCallObject&& operator()(Args&&...) const && {
125 set_call<Args &&...>(CT_Const | CT_RValue);
126 return std::move(*this);
129 template <class... Args> static void set_call(CallType type) {
130 assert(last_call_type == CT_None);
131 assert(last_call_args == nullptr);
132 last_call_type = type;
133 last_call_args = std::addressof(makeArgumentID<Args...>());
136 template <class... Args> static bool check_call(CallType type) {
137 bool result = last_call_type == type && last_call_args &&
138 *last_call_args == makeArgumentID<Args...>();
139 last_call_type = CT_None;
140 last_call_args = nullptr;
141 return result;
144 // To check explicit return type for visit<R>
145 constexpr operator int() const
147 return 0;
150 static CallType last_call_type;
151 static const TypeID *last_call_args;
154 CallType ForwardingCallObject::last_call_type = CT_None;
155 const TypeID *ForwardingCallObject::last_call_args = nullptr;
157 struct ReturnFirst {
158 template <class... Args> constexpr int operator()(int f, Args &&...) const {
159 return f;
163 struct ReturnArity {
164 template <class... Args> constexpr int operator()(Args &&...) const {
165 return sizeof...(Args);
169 #endif // SUPPORT_VARIANT_TEST_HELPERS_H