1#ifndef THIRD_PARTY_GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_PP_H_
2#define THIRD_PARTY_GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_PP_H_
3
4#undef GMOCK_PP_INTERNAL_USE_MSVC
5#if defined(__clang__)
6#define GMOCK_PP_INTERNAL_USE_MSVC 0
7#elif defined(_MSC_VER)
8// TODO(iserna): Also verify tradional versus comformant preprocessor.
9static_assert(
10 _MSC_VER >= 1900,
11 "MSVC version not supported. There is support for MSVC 14.0 and above.");
12#define GMOCK_PP_INTERNAL_USE_MSVC 1
13#else
14#define GMOCK_PP_INTERNAL_USE_MSVC 0
15#endif
16
17// Expands and concatenates the arguments. Constructed macros reevaluate.
18#define GMOCK_PP_CAT(_1, _2) GMOCK_PP_INTERNAL_CAT(_1, _2)
19
20// Expands and stringifies the only argument.
21#define GMOCK_PP_STRINGIZE(...) GMOCK_PP_INTERNAL_STRINGIZE(__VA_ARGS__)
22
23// Returns empty. Given a variadic number of arguments.
24#define GMOCK_PP_EMPTY(...)
25
26// Returns a comma. Given a variadic number of arguments.
27#define GMOCK_PP_COMMA(...) ,
28
29// Returns the only argument.
30#define GMOCK_PP_IDENTITY(_1) _1
31
32// MSVC preprocessor collapses __VA_ARGS__ in a single argument, we use a
33// CAT-like directive to force correct evaluation. Each macro has its own.
34#if GMOCK_PP_INTERNAL_USE_MSVC
35
36// Evaluates to the number of arguments after expansion.
37//
38// #define PAIR x, y
39//
40// GMOCK_PP_NARG() => 1
41// GMOCK_PP_NARG(x) => 1
42// GMOCK_PP_NARG(x, y) => 2
43// GMOCK_PP_NARG(PAIR) => 2
44//
45// Requires: the number of arguments after expansion is at most 15.
46#define GMOCK_PP_NARG(...) \
47 GMOCK_PP_INTERNAL_NARG_CAT( \
48 GMOCK_PP_INTERNAL_INTERNAL_16TH(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, \
49 8, 7, 6, 5, 4, 3, 2, 1), )
50
51// Returns 1 if the expansion of arguments has an unprotected comma. Otherwise
52// returns 0. Requires no more than 15 unprotected commas.
53#define GMOCK_PP_HAS_COMMA(...) \
54 GMOCK_PP_INTERNAL_HAS_COMMA_CAT( \
55 GMOCK_PP_INTERNAL_INTERNAL_16TH(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
56 1, 1, 1, 1, 1, 0), )
57// Returns the first argument.
58#define GMOCK_PP_HEAD(...) \
59 GMOCK_PP_INTERNAL_HEAD_CAT(GMOCK_PP_INTERNAL_HEAD(__VA_ARGS__), )
60
61// Returns the tail. A variadic list of all arguments minus the first. Requires
62// at least one argument.
63#define GMOCK_PP_TAIL(...) \
64 GMOCK_PP_INTERNAL_TAIL_CAT(GMOCK_PP_INTERNAL_TAIL(__VA_ARGS__), )
65
66// Calls CAT(_Macro, NARG(__VA_ARGS__))(__VA_ARGS__)
67#define GMOCK_PP_VARIADIC_CALL(_Macro, ...) \
68 GMOCK_PP_INTERNAL_VARIADIC_CALL_CAT( \
69 GMOCK_PP_CAT(_Macro, GMOCK_PP_NARG(__VA_ARGS__))(__VA_ARGS__), )
70
71#else // GMOCK_PP_INTERNAL_USE_MSVC
72
73#define GMOCK_PP_NARG(...) \
74 GMOCK_PP_INTERNAL_INTERNAL_16TH(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, \
75 7, 6, 5, 4, 3, 2, 1)
76#define GMOCK_PP_HAS_COMMA(...) \
77 GMOCK_PP_INTERNAL_INTERNAL_16TH(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
78 1, 1, 1, 1, 0)
79#define GMOCK_PP_HEAD(...) GMOCK_PP_INTERNAL_HEAD(__VA_ARGS__)
80#define GMOCK_PP_TAIL(...) GMOCK_PP_INTERNAL_TAIL(__VA_ARGS__)
81#define GMOCK_PP_VARIADIC_CALL(_Macro, ...) \
82 GMOCK_PP_CAT(_Macro, GMOCK_PP_NARG(__VA_ARGS__))(__VA_ARGS__)
83
84#endif // GMOCK_PP_INTERNAL_USE_MSVC
85
86// If the arguments after expansion have no tokens, evaluates to `1`. Otherwise
87// evaluates to `0`.
88//
89// Requires: * the number of arguments after expansion is at most 15.
90// * If the argument is a macro, it must be able to be called with one
91// argument.
92//
93// Implementation details:
94//
95// There is one case when it generates a compile error: if the argument is macro
96// that cannot be called with one argument.
97//
98// #define M(a, b) // it doesn't matter what it expands to
99//
100// // Expected: expands to `0`.
101// // Actual: compile error.
102// GMOCK_PP_IS_EMPTY(M)
103//
104// There are 4 cases tested:
105//
106// * __VA_ARGS__ possible expansion has no unparen'd commas. Expected 0.
107// * __VA_ARGS__ possible expansion is not enclosed in parenthesis. Expected 0.
108// * __VA_ARGS__ possible expansion is not a macro that ()-evaluates to a comma.
109// Expected 0
110// * __VA_ARGS__ is empty, or has unparen'd commas, or is enclosed in
111// parenthesis, or is a macro that ()-evaluates to comma. Expected 1.
112//
113// We trigger detection on '0001', i.e. on empty.
114#define GMOCK_PP_IS_EMPTY(...) \
115 GMOCK_PP_INTERNAL_IS_EMPTY(GMOCK_PP_HAS_COMMA(__VA_ARGS__), \
116 GMOCK_PP_HAS_COMMA(GMOCK_PP_COMMA __VA_ARGS__), \
117 GMOCK_PP_HAS_COMMA(__VA_ARGS__()), \
118 GMOCK_PP_HAS_COMMA(GMOCK_PP_COMMA __VA_ARGS__()))
119
120// Evaluates to _Then if _Cond is 1 and _Else if _Cond is 0.
121#define GMOCK_PP_IF(_Cond, _Then, _Else) \
122 GMOCK_PP_CAT(GMOCK_PP_INTERNAL_IF_, _Cond)(_Then, _Else)
123
124// Evaluates to the number of arguments after expansion. Identifies 'empty' as
125// 0.
126//
127// #define PAIR x, y
128//
129// GMOCK_PP_NARG0() => 0
130// GMOCK_PP_NARG0(x) => 1
131// GMOCK_PP_NARG0(x, y) => 2
132// GMOCK_PP_NARG0(PAIR) => 2
133//
134// Requires: * the number of arguments after expansion is at most 15.
135// * If the argument is a macro, it must be able to be called with one
136// argument.
137#define GMOCK_PP_NARG0(...) \
138 GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(__VA_ARGS__), 0, GMOCK_PP_NARG(__VA_ARGS__))
139
140// Expands to 1 if the first argument starts with something in parentheses,
141// otherwise to 0.
142#define GMOCK_PP_IS_BEGIN_PARENS(...) \
143 GMOCK_PP_INTERNAL_ALTERNATE_HEAD( \
144 GMOCK_PP_CAT(GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_, \
145 GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C __VA_ARGS__))
146
147// Expands to 1 is there is only one argument and it is enclosed in parentheses.
148#define GMOCK_PP_IS_ENCLOSED_PARENS(...) \
149 GMOCK_PP_IF(GMOCK_PP_IS_BEGIN_PARENS(__VA_ARGS__), \
150 GMOCK_PP_IS_EMPTY(GMOCK_PP_EMPTY __VA_ARGS__), 0)
151
152// Remove the parens, requires GMOCK_PP_IS_ENCLOSED_PARENS(args) => 1.
153#define GMOCK_PP_REMOVE_PARENS(...) GMOCK_PP_INTERNAL_REMOVE_PARENS __VA_ARGS__
154
155// Expands to _Macro(0, _Data, e1) _Macro(1, _Data, e2) ... _Macro(K -1, _Data,
156// eK) as many of GMOCK_INTERNAL_NARG0 _Tuple.
157// Requires: * |_Macro| can be called with 3 arguments.
158// * |_Tuple| expansion has no more than 15 elements.
159#define GMOCK_PP_FOR_EACH(_Macro, _Data, _Tuple) \
160 GMOCK_PP_CAT(GMOCK_PP_INTERNAL_FOR_EACH_IMPL_, GMOCK_PP_NARG0 _Tuple) \
161 (0, _Macro, _Data, _Tuple)
162
163// Expands to _Macro(0, _Data, ) _Macro(1, _Data, ) ... _Macro(K - 1, _Data, )
164// Empty if _K = 0.
165// Requires: * |_Macro| can be called with 3 arguments.
166// * |_K| literal between 0 and 15
167#define GMOCK_PP_REPEAT(_Macro, _Data, _N) \
168 GMOCK_PP_CAT(GMOCK_PP_INTERNAL_FOR_EACH_IMPL_, _N) \
169 (0, _Macro, _Data, GMOCK_PP_INTENRAL_EMPTY_TUPLE)
170
171// Increments the argument, requires the argument to be between 0 and 15.
172#define GMOCK_PP_INC(_i) GMOCK_PP_CAT(GMOCK_PP_INTERNAL_INC_, _i)
173
174// Returns comma if _i != 0. Requires _i to be between 0 and 15.
175#define GMOCK_PP_COMMA_IF(_i) GMOCK_PP_CAT(GMOCK_PP_INTERNAL_COMMA_IF_, _i)
176
177// Internal details follow. Do not use any of these symbols outside of this
178// file or we will break your code.
179#define GMOCK_PP_INTENRAL_EMPTY_TUPLE (, , , , , , , , , , , , , , , )
180#define GMOCK_PP_INTERNAL_CAT(_1, _2) _1##_2
181#define GMOCK_PP_INTERNAL_STRINGIZE(...) #__VA_ARGS__
182#define GMOCK_PP_INTERNAL_INTERNAL_16TH(_1, _2, _3, _4, _5, _6, _7, _8, _9, \
183 _10, _11, _12, _13, _14, _15, _16, \
184 ...) \
185 _16
186#define GMOCK_PP_INTERNAL_CAT_5(_1, _2, _3, _4, _5) _1##_2##_3##_4##_5
187#define GMOCK_PP_INTERNAL_IS_EMPTY(_1, _2, _3, _4) \
188 GMOCK_PP_HAS_COMMA(GMOCK_PP_INTERNAL_CAT_5(GMOCK_PP_INTERNAL_IS_EMPTY_CASE_, \
189 _1, _2, _3, _4))
190#define GMOCK_PP_INTERNAL_IS_EMPTY_CASE_0001 ,
191#define GMOCK_PP_INTERNAL_IF_1(_Then, _Else) _Then
192#define GMOCK_PP_INTERNAL_IF_0(_Then, _Else) _Else
193#define GMOCK_PP_INTERNAL_HEAD(_1, ...) _1
194#define GMOCK_PP_INTERNAL_TAIL(_1, ...) __VA_ARGS__
195
196#if GMOCK_PP_INTERNAL_USE_MSVC
197#define GMOCK_PP_INTERNAL_NARG_CAT(_1, _2) GMOCK_PP_INTERNAL_NARG_CAT_I(_1, _2)
198#define GMOCK_PP_INTERNAL_HEAD_CAT(_1, _2) GMOCK_PP_INTERNAL_HEAD_CAT_I(_1, _2)
199#define GMOCK_PP_INTERNAL_HAS_COMMA_CAT(_1, _2) \
200 GMOCK_PP_INTERNAL_HAS_COMMA_CAT_I(_1, _2)
201#define GMOCK_PP_INTERNAL_TAIL_CAT(_1, _2) GMOCK_PP_INTERNAL_TAIL_CAT_I(_1, _2)
202#define GMOCK_PP_INTERNAL_VARIADIC_CALL_CAT(_1, _2) \
203 GMOCK_PP_INTERNAL_VARIADIC_CALL_CAT_I(_1, _2)
204#define GMOCK_PP_INTERNAL_NARG_CAT_I(_1, _2) _1##_2
205#define GMOCK_PP_INTERNAL_HEAD_CAT_I(_1, _2) _1##_2
206#define GMOCK_PP_INTERNAL_HAS_COMMA_CAT_I(_1, _2) _1##_2
207#define GMOCK_PP_INTERNAL_TAIL_CAT_I(_1, _2) _1##_2
208#define GMOCK_PP_INTERNAL_VARIADIC_CALL_CAT_I(_1, _2) _1##_2
209#define GMOCK_PP_INTERNAL_ALTERNATE_HEAD(...) \
210 GMOCK_PP_INTERNAL_ALTERNATE_HEAD_CAT(GMOCK_PP_HEAD(__VA_ARGS__), )
211#define GMOCK_PP_INTERNAL_ALTERNATE_HEAD_CAT(_1, _2) \
212 GMOCK_PP_INTERNAL_ALTERNATE_HEAD_CAT_I(_1, _2)
213#define GMOCK_PP_INTERNAL_ALTERNATE_HEAD_CAT_I(_1, _2) _1##_2
214#else // GMOCK_PP_INTERNAL_USE_MSVC
215#define GMOCK_PP_INTERNAL_ALTERNATE_HEAD(...) GMOCK_PP_HEAD(__VA_ARGS__)
216#endif // GMOCK_PP_INTERNAL_USE_MSVC
217
218#define GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C(...) 1 _
219#define GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_1 1,
220#define GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C \
221 0,
222#define GMOCK_PP_INTERNAL_REMOVE_PARENS(...) __VA_ARGS__
223#define GMOCK_PP_INTERNAL_INC_0 1
224#define GMOCK_PP_INTERNAL_INC_1 2
225#define GMOCK_PP_INTERNAL_INC_2 3
226#define GMOCK_PP_INTERNAL_INC_3 4
227#define GMOCK_PP_INTERNAL_INC_4 5
228#define GMOCK_PP_INTERNAL_INC_5 6
229#define GMOCK_PP_INTERNAL_INC_6 7
230#define GMOCK_PP_INTERNAL_INC_7 8
231#define GMOCK_PP_INTERNAL_INC_8 9
232#define GMOCK_PP_INTERNAL_INC_9 10
233#define GMOCK_PP_INTERNAL_INC_10 11
234#define GMOCK_PP_INTERNAL_INC_11 12
235#define GMOCK_PP_INTERNAL_INC_12 13
236#define GMOCK_PP_INTERNAL_INC_13 14
237#define GMOCK_PP_INTERNAL_INC_14 15
238#define GMOCK_PP_INTERNAL_INC_15 16
239#define GMOCK_PP_INTERNAL_COMMA_IF_0
240#define GMOCK_PP_INTERNAL_COMMA_IF_1 ,
241#define GMOCK_PP_INTERNAL_COMMA_IF_2 ,
242#define GMOCK_PP_INTERNAL_COMMA_IF_3 ,
243#define GMOCK_PP_INTERNAL_COMMA_IF_4 ,
244#define GMOCK_PP_INTERNAL_COMMA_IF_5 ,
245#define GMOCK_PP_INTERNAL_COMMA_IF_6 ,
246#define GMOCK_PP_INTERNAL_COMMA_IF_7 ,
247#define GMOCK_PP_INTERNAL_COMMA_IF_8 ,
248#define GMOCK_PP_INTERNAL_COMMA_IF_9 ,
249#define GMOCK_PP_INTERNAL_COMMA_IF_10 ,
250#define GMOCK_PP_INTERNAL_COMMA_IF_11 ,
251#define GMOCK_PP_INTERNAL_COMMA_IF_12 ,
252#define GMOCK_PP_INTERNAL_COMMA_IF_13 ,
253#define GMOCK_PP_INTERNAL_COMMA_IF_14 ,
254#define GMOCK_PP_INTERNAL_COMMA_IF_15 ,
255#define GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, _element) \
256 _Macro(_i, _Data, _element)
257#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_0(_i, _Macro, _Data, _Tuple)
258#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_1(_i, _Macro, _Data, _Tuple) \
259 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple)
260#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_2(_i, _Macro, _Data, _Tuple) \
261 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
262 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_1(GMOCK_PP_INC(_i), _Macro, _Data, \
263 (GMOCK_PP_TAIL _Tuple))
264#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_3(_i, _Macro, _Data, _Tuple) \
265 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
266 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_2(GMOCK_PP_INC(_i), _Macro, _Data, \
267 (GMOCK_PP_TAIL _Tuple))
268#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_4(_i, _Macro, _Data, _Tuple) \
269 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
270 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_3(GMOCK_PP_INC(_i), _Macro, _Data, \
271 (GMOCK_PP_TAIL _Tuple))
272#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_5(_i, _Macro, _Data, _Tuple) \
273 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
274 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_4(GMOCK_PP_INC(_i), _Macro, _Data, \
275 (GMOCK_PP_TAIL _Tuple))
276#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_6(_i, _Macro, _Data, _Tuple) \
277 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
278 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_5(GMOCK_PP_INC(_i), _Macro, _Data, \
279 (GMOCK_PP_TAIL _Tuple))
280#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_7(_i, _Macro, _Data, _Tuple) \
281 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
282 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_6(GMOCK_PP_INC(_i), _Macro, _Data, \
283 (GMOCK_PP_TAIL _Tuple))
284#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_8(_i, _Macro, _Data, _Tuple) \
285 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
286 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_7(GMOCK_PP_INC(_i), _Macro, _Data, \
287 (GMOCK_PP_TAIL _Tuple))
288#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_9(_i, _Macro, _Data, _Tuple) \
289 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
290 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_8(GMOCK_PP_INC(_i), _Macro, _Data, \
291 (GMOCK_PP_TAIL _Tuple))
292#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_10(_i, _Macro, _Data, _Tuple) \
293 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
294 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_9(GMOCK_PP_INC(_i), _Macro, _Data, \
295 (GMOCK_PP_TAIL _Tuple))
296#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_11(_i, _Macro, _Data, _Tuple) \
297 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
298 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_10(GMOCK_PP_INC(_i), _Macro, _Data, \
299 (GMOCK_PP_TAIL _Tuple))
300#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_12(_i, _Macro, _Data, _Tuple) \
301 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
302 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_11(GMOCK_PP_INC(_i), _Macro, _Data, \
303 (GMOCK_PP_TAIL _Tuple))
304#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_13(_i, _Macro, _Data, _Tuple) \
305 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
306 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_12(GMOCK_PP_INC(_i), _Macro, _Data, \
307 (GMOCK_PP_TAIL _Tuple))
308#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_14(_i, _Macro, _Data, _Tuple) \
309 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
310 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_13(GMOCK_PP_INC(_i), _Macro, _Data, \
311 (GMOCK_PP_TAIL _Tuple))
312#define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_15(_i, _Macro, _Data, _Tuple) \
313 GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
314 GMOCK_PP_INTERNAL_FOR_EACH_IMPL_14(GMOCK_PP_INC(_i), _Macro, _Data, \
315 (GMOCK_PP_TAIL _Tuple))
316
317#endif // THIRD_PARTY_GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PP_H_
318