c11_generic_expression.c revision 1.1 1 /* $NetBSD: c11_generic_expression.c,v 1.1 2021/06/27 18:48:45 rillig Exp $ */
2 # 3 "c11_generic_expression.c"
3
4 /*
5 * C99 added support for type-generic macros, but these were limited to the
6 * header <tgmath.h>. C11 made this feature generally available.
7 *
8 * The generic selection is typically used with macros, but since lint1 works
9 * on the preprocessed source, the test cases look a bit strange.
10 *
11 * C99 6.5.1.1 "Generic selection"
12 */
13
14 /* lint1-extra-flags: -Ac11 */
15
16 /*
17 * The type of 'var' is not compatible with any of the types from the
18 * generic-association. This is a compile-time error.
19 */
20 const char *
21 classify_integer_without_default(double var)
22 {
23 return _Generic(var,
24 long double: "long double",
25 long long: "long long",
26 unsigned: "unsigned"
27 );
28 /* expect-7: argument 'var' unused */
29 /* expect-2: type mismatch (pointer to const char) and (double) *//* FIXME */
30 }
31
32 /*
33 * In this case, the 'default' expression is selected.
34 */
35 const char *
36 classify_integer_with_default(double var)
37 {
38 return _Generic(var,
39 long double: "long double",
40 long long: "long long",
41 unsigned: "unsigned",
42 default: "unknown"
43 );
44 /* expect-8: argument 'var' unused */
45 /* expect-2: type mismatch (pointer to const char) and (double) *//* FIXME */
46 }
47