summaryrefslogtreecommitdiff
path: root/tests/usr.bin/xlint/lint1/c11_generic_expression.c
blob: da2a5ebc54855bcc33e6d1651938c90ade5f4e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*	$NetBSD: c11_generic_expression.c,v 1.17 2023/07/07 19:45:22 rillig Exp $	*/
# 3 "c11_generic_expression.c"

/* lint1-extra-flags: -X 351 */

/*
 * C99 added support for type-generic macros, but these were limited to the
 * header <tgmath.h>.  C11 made this feature generally available.
 *
 * The generic selection is typically used with macros, but since lint1 works
 * on the preprocessed source, the test cases look a bit strange.
 *
 * C11 6.5.1.1 "Generic selection"
 */

/* lint1-extra-flags: -Ac11 */

/*
 * The type of 'var' is not compatible with any of the types from the
 * generic-association.  This is a compile-time error.
 */
const char *
classify_type_without_default(double var)
{
	/* expect-2: warning: argument 'var' unused in function 'classify_type_without_default' [231] */

	return _Generic(var,
	    long double: "long double",
	    long long: "long long",
	    unsigned: "unsigned"
	);
	/* expect-1: warning: function 'classify_type_without_default' expects to return value [214] */
}

/*
 * In this case, the 'default' expression is selected.
 */
const char *
classify_type_with_default(double var)
{
	/* expect-2: warning: argument 'var' unused in function 'classify_type_with_default' [231] */

	return _Generic(var,
	    long double: "long double",
	    long long: "long long",
	    unsigned: "unsigned",
	    default: "unknown"
	);
}

/*
 * The type of a _Generic expression is the one from the selected association.
 */
const char *
classify_char(char c)
{
	/* expect-2: warning: argument 'c' unused in function 'classify_char' [231] */

	return _Generic(c,
	    char: "yes",
	    default: 0.0
	);
}

/*
 * Before cgram.y 1.238 from 2021-06-27, lint accepted a comma-expression,
 * which looked as if _Generic would accept multiple arguments before the
 * selection.
 */
/* ARGSUSED */
const int *
comma_expression(char first, double second)
{
	/* expect+1: error: syntax error 'second' [249] */
	return _Generic(first, second,
	    char: "first",
	    double: 2.0
	);
	/* expect+1: warning: function 'comma_expression' falls off bottom without returning value [217] */
}

/*
 * Ensure that assignment-expressions are accepted by the grammar, as
 * opposed to comma-expressions.
 */
/* ARGSUSED */
int
assignment_expression(int first, int second)
{
	return _Generic(first = second,
	    int: second = first
	);
}

int
primary_expression(void)
{
	return _Generic(0, int: assignment_expression)(0, 0);
}

/*
 * The types don't match, therefore build_generic_selection returns NULL,
 * which is then silently ignored by init_expr.  This situation is already
 * covered by the compilers, so there is no need for lint to double-check it.
 */
const char *x = _Generic(
    1ULL + 1.0f,
    int: 1
);