1*5b5ce594Srillig /* $NetBSD: c11_generic_expression.c,v 1.18 2023/07/09 11:18:55 rillig Exp $ */
21a74119aSrillig # 3 "c11_generic_expression.c"
31a74119aSrillig
44175df94Srillig /* lint1-extra-flags: -X 351 */
54175df94Srillig
61a74119aSrillig /*
71a74119aSrillig * C99 added support for type-generic macros, but these were limited to the
81a74119aSrillig * header <tgmath.h>. C11 made this feature generally available.
91a74119aSrillig *
101a74119aSrillig * The generic selection is typically used with macros, but since lint1 works
111a74119aSrillig * on the preprocessed source, the test cases look a bit strange.
121a74119aSrillig *
13c85a3d2eSrillig * C11 6.5.1.1 "Generic selection"
141a74119aSrillig */
151a74119aSrillig
1628cce118Srillig /* lint1-extra-flags: -Ac11 */
171a74119aSrillig
181a74119aSrillig /*
191a74119aSrillig * The type of 'var' is not compatible with any of the types from the
201a74119aSrillig * generic-association. This is a compile-time error.
211a74119aSrillig */
221a74119aSrillig const char *
classify_type_without_default(double var)23925438b6Srillig classify_type_without_default(double var)
241a74119aSrillig {
25*5b5ce594Srillig /* expect-2: warning: parameter 'var' unused in function 'classify_type_without_default' [231] */
269db77d62Srillig
271a74119aSrillig return _Generic(var,
281a74119aSrillig long double: "long double",
291a74119aSrillig long long: "long long",
301a74119aSrillig unsigned: "unsigned"
311a74119aSrillig );
3253ed6e86Srillig /* expect-1: warning: function 'classify_type_without_default' expects to return value [214] */
331a74119aSrillig }
341a74119aSrillig
351a74119aSrillig /*
361a74119aSrillig * In this case, the 'default' expression is selected.
371a74119aSrillig */
381a74119aSrillig const char *
classify_type_with_default(double var)39925438b6Srillig classify_type_with_default(double var)
401a74119aSrillig {
41*5b5ce594Srillig /* expect-2: warning: parameter 'var' unused in function 'classify_type_with_default' [231] */
429db77d62Srillig
431a74119aSrillig return _Generic(var,
441a74119aSrillig long double: "long double",
451a74119aSrillig long long: "long long",
461a74119aSrillig unsigned: "unsigned",
471a74119aSrillig default: "unknown"
481a74119aSrillig );
491a74119aSrillig }
50925438b6Srillig
51925438b6Srillig /*
52925438b6Srillig * The type of a _Generic expression is the one from the selected association.
53925438b6Srillig */
54925438b6Srillig const char *
classify_char(char c)55925438b6Srillig classify_char(char c)
56925438b6Srillig {
57*5b5ce594Srillig /* expect-2: warning: parameter 'c' unused in function 'classify_char' [231] */
589db77d62Srillig
59925438b6Srillig return _Generic(c,
60925438b6Srillig char: "yes",
61925438b6Srillig default: 0.0
62925438b6Srillig );
63925438b6Srillig }
64518e17a2Srillig
65518e17a2Srillig /*
66518e17a2Srillig * Before cgram.y 1.238 from 2021-06-27, lint accepted a comma-expression,
67518e17a2Srillig * which looked as if _Generic would accept multiple arguments before the
68518e17a2Srillig * selection.
69518e17a2Srillig */
70518e17a2Srillig /* ARGSUSED */
71518e17a2Srillig const int *
comma_expression(char first,double second)72518e17a2Srillig comma_expression(char first, double second)
73518e17a2Srillig {
747cdb3475Srillig /* expect+1: error: syntax error 'second' [249] */
757cdb3475Srillig return _Generic(first, second,
76518e17a2Srillig char: "first",
77518e17a2Srillig double: 2.0
78518e17a2Srillig );
79884b0e6aSrillig /* expect+1: warning: function 'comma_expression' falls off bottom without returning value [217] */
80518e17a2Srillig }
816e36a2b9Srillig
826e36a2b9Srillig /*
836e36a2b9Srillig * Ensure that assignment-expressions are accepted by the grammar, as
846e36a2b9Srillig * opposed to comma-expressions.
856e36a2b9Srillig */
866e36a2b9Srillig /* ARGSUSED */
876e36a2b9Srillig int
assignment_expression(int first,int second)886e36a2b9Srillig assignment_expression(int first, int second)
896e36a2b9Srillig {
906e36a2b9Srillig return _Generic(first = second,
916e36a2b9Srillig int: second = first
926e36a2b9Srillig );
936e36a2b9Srillig }
940f733df6Srillig
950f733df6Srillig int
primary_expression(void)960f733df6Srillig primary_expression(void)
970f733df6Srillig {
980f733df6Srillig return _Generic(0, int: assignment_expression)(0, 0);
990f733df6Srillig }
1008f1fb703Srillig
1018f1fb703Srillig /*
1028f1fb703Srillig * The types don't match, therefore build_generic_selection returns NULL,
1038f1fb703Srillig * which is then silently ignored by init_expr. This situation is already
1048f1fb703Srillig * covered by the compilers, so there is no need for lint to double-check it.
1058f1fb703Srillig */
1068f1fb703Srillig const char *x = _Generic(
107dd2691b0Srillig 1ULL + 1.0f,
1088f1fb703Srillig int: 1
1098f1fb703Srillig );
110