Debian Codegraph @

gcc/testsuite/g++.dg/template/builtin-atomic-overloads7.C

raw
1 /* Various atomic builtin errors not emitted when in SFINAE context. */
2 // { dg-do compile { target c++17 } }
3 // { dg-additional-options "-Wno-pedantic" }
4 #include <type_traits>
5 #ifdef __ARM_FEATURE_SVE
6 #include <arm_sve.h>
7 #endif
8
9 /*
10 Covering all if clauses, *not* all possible errors.
11 E.g. load, store, exchange, compare_exchange all go through
12 get_atomic_generic_size. I ensure I test all if clauses in that function
13 but do not ensure each clause is hit when using each of the different
14 builtins.
15
16 This is the stuff that is not handled by
17 builtin-atomic-overloads{1,2,3,4,5}.C */
18
19 class X{};
20 /* Want a zero-sized type in order to trigger one of the error messages.
21 Don't want the error message about creating a zero sized type.
22 However, *do* want to see any pedantic error messages coming from the rest
23 of the testcase (shouldn't be any, but would like to be alerted if there
24 are). */
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wpedantic"
27 class Zero {
28 unsigned int trailing[0];
29 };
30 #pragma GCC diagnostic pop
31 class Large { public: int arr[10]; };
32 class Incomplete;
33 /* If there are other non-constant size types that I can use in a template
34 would appreciate hearing about it (especially if they work on all targets).
35 AFAIK VLA's are the other canonical example and have not managed to trigger
36 the same error with those due to scoping limitations. */
37 #ifdef __ARM_FEATURE_SVE
38 typedef __SVUint32_t NonConstant;
39 #else
40 class NonConstant { };
41 #endif
42 typedef __UINT64_TYPE__ uint64_t;
43 typedef __UINT32_TYPE__ uint32_t;
44 // typedef _BitInt(12) Bitint;
45
46
47 #define INCORRECT_NUMBER_ARGUMENTS(X) \
48 X(load, (std::declval<T>(), int(), int(), int()), 0)
49
50 #define NONPOINTER_FIRST_ARG(X) \
51 X(load, (std::declval<std::remove_pointer_t<T>>(), int(), int()), 1)
52 #define INCOMPLETE_FIRST_ARG(X) \
53 X(load, (std::declval<Incomplete*>(), std::declval<T>(), int()), 2)
54 /* This won't trigger relevant fail when not using nonconstant sized type. */
55 #define NONCONST_SIZE_FIRST_ARG(X) \
56 X(load, (std::declval<NonConstant*>(), std::declval<T>(), int()), 3)
57 #define ZEROSIZE_FIRST_ARG(X) \
58 X(load, (std::declval<Zero*>(), std::declval<T>(), int()), 4)
59
60 // Errors triggered by a bad type in the first position not yet triggered by
61 // builtin-atomic-overloads5.C.
62 // these are already checked to *not* give an error in the SFINAE context by
63 // builtin-atomic-overloads1.C.
64 #define FIRST_ARGS_BADTYPE(X) \
65 ZEROSIZE_FIRST_ARG(X) \
66 NONCONST_SIZE_FIRST_ARG(X) \
67 INCOMPLETE_FIRST_ARG(X) \
68 NONPOINTER_FIRST_ARG(X)
69
70 #define NONPOINTER_OTHER_ARG(X) \
71 X(load, (std::declval<T>(), int(), int()), 5)
72 /* This won't trigger relevant fail when not using nonconstant sized type. */
73 #define NONCONST_SIZE_OTHER_ARG(X) \
74 X(load, (std::declval<T>(), std::declval<NonConstant*>(), int()), 6)
75 #define FUNCTIONPTR_OTHER_ARG(X) \
76 X(load, (std::declval<T>(), std::declval<int(*)()>(), int()), 7)
77 #define SIZE_MISMATCH(X) \
78 X(load, (std::declval<T>(), std::declval<uint64_t*>(), int()), 8)
79 #define OUTPUT_CONST(X) \
80 X(load, (std::declval<T>(), std::declval<const int*>(), int()), 9)
81 #define SECOND_VOLATILE(X) \
82 X(load, (std::declval<T>(), std::declval<volatile int*>(), int()), 10)
83
84 #define OTHER_ARG_BADTYPE(X) \
85 NONPOINTER_OTHER_ARG(X) \
86 SECOND_VOLATILE(X) \
87 OUTPUT_CONST(X) \
88 SIZE_MISMATCH(X) \
89 FUNCTIONPTR_OTHER_ARG(X) \
90 NONCONST_SIZE_OTHER_ARG(X)
91
92 #define MEMMODEL_BADTYPE(X) \
93 X(load, (std::declval<T>(), std::declval<int*>(), float()), 11)
94 #define MEMMODEL_TOOLARGE(X) \
95 X(load, (std::declval<T>(), std::declval<int*>(), 100), 12)
96
97 #define MEMMODEL_BAD(X) \
98 MEMMODEL_BADTYPE(X) \
99
100 #define GET_ATOMIC_GENERIC_ERRS(X) \
101 INCORRECT_NUMBER_ARGUMENTS(X) \
102 FIRST_ARGS_BADTYPE(X) \
103 OTHER_ARG_BADTYPE(X) \
104 MEMMODEL_BAD(X)
105
106 /* Can't trigger this error in SFINAE context since in order to trigger error
107 need zero arguments, but that means type is fully specified.
108
109 #define SYNC_SIZE_TOOFEW(X) \
110 X(load_n, (), 0)
111 */
112 #define SYNC_SIZE_TOOFEW(X)
113 #define SYNC_SIZE_INCOMPATIBLE(X) \
114 X(load_n, (int(), std::declval<T>()), 1)
115 #define SYNC_SIZE_ERRS(X) \
116 SYNC_SIZE_TOOFEW(X) \
117 SYNC_SIZE_INCOMPATIBLE(X)
118
119 #define SYNC_PARM_TOOFEW(X) \
120 X(load_n, (std::declval<T>()), 2)
121 #define SYNC_PARM_TOOMANY(X) \
122 X(load_n, (std::declval<T>(), int(), int()), 3)
123 #define SYNC_PARM_ERRS(X) \
124 SYNC_PARM_TOOFEW(X) \
125 SYNC_PARM_TOOMANY(X)
126
127 /*
128 No Bitint in C++. Hence can't check for this error.
129 #define BITINT_FETCHCAS_TOOFEW(X) \
130 X(add_fetch, (std::declval<Bitint*>(), std::declval<Bitint>()))
131 #define BITINT_FETCHCAS_TOOMANY(X) \
132 X(add_fetch, (std::declval<Bitint*>(), std::declval<Bitint>(), int(), int()))
133 #define BITINT_FETCHCAS_ERRS(X) \
134 BITINT_FETCHCAS_TOOFEW(X) \
135 BITINT_FETCHCAS_TOOMANY(X)
136 */
137 #define BITINT_FETCHCAS_ERRS(X)
138
139 #define ALL_ERRS(X) \
140 GET_ATOMIC_GENERIC_ERRS(X) \
141 SYNC_SIZE_ERRS(X) \
142 SYNC_PARM_ERRS(X) \
143 MEMMODEL_TOOLARGE(X) \
144 BITINT_FETCHCAS_ERRS(X)
145
146 #define SFINAE_TYPE_CHECK(NAME, PARAMS, COUNTER) \
147 template <typename T, typename = void> \
148 struct is_##NAME##_available_##COUNTER : std::false_type {}; \
149 template <typename T> \
150 struct is_##NAME##_available_##COUNTER<T, \
151 std::void_t<decltype(__atomic_##NAME PARAMS) >> \
152 : std::true_type {}; \
153
154 ALL_ERRS(SFINAE_TYPE_CHECK)
155
156 #define ASSERT(NAME, PARAMS, COUNTER) \
157 static_assert(is_##NAME##_available_##COUNTER<int*>::value == false);
158
159 #define ASSERT_TRUE(NAME, PARAMS, COUNTER) \
160 static_assert(is_##NAME##_available_##COUNTER<int*>::value == true);
161
162 int foo() {
163 ALL_ERRS(ASSERT)
164 return 1;
165 }
166