1 /* $NetBSD: lsym_unary_op.c,v 1.12 2023/06/17 22:09:24 rillig Exp $ */
2
3 /*
4 * Tests for the token lsym_unary_op, which represents a unary operator.
5 *
6 * In an expression, a unary operator is written without blank next to its
7 * argument.
8 *
9 * In a type name, the "unary operator" '*' represents the derivation of a
10 * pointer type.
11 *
12 * See also:
13 * lsym_binary_op.c
14 * lsym_postfix_op.c
15 * C11 6.4.6 "Punctuators"
16 * C11 6.5 "Expressions"
17 */
18
19 //indent input
20 void
unary_operators(void)21 unary_operators(void)
22 {
23 /* In the order of appearance in C11 6.5. */
24 function(a++, a--, ++a, --a, &a, *a, +a, -a, ~a, !a);
25 }
26 //indent end
27
28 //indent run-equals-input
29
30
31 /*
32 * The unary operators '+' and '-' can occur in long chains. In these chains,
33 * adjacent '+' must not be merged to '++' since that would be a different
34 * token. The same applies to '&', but that case is irrelevant in practice
35 * since the address of an address cannot be taken.
36 */
37 //indent input
38 int var=+3;
39 int mixed=+-+-+-+-+-+-+-+-+-+-+-+-+-3;
40 int count=~-~-~-~-~-~-~-~-~-~-~-~-~-3;
41 int same = + + + + + - - - - - 3;
42 //indent end
43
44 //indent run -di0
45 int var = +3;
46 int mixed = +-+-+-+-+-+-+-+-+-+-+-+-+-3;
47 int count = ~-~-~-~-~-~-~-~-~-~-~-~-~-3;
48 int same = + + + + +- - - - -3;
49 //indent end
50
51
52 /*
53 * A special kind of unary operator is '->', which additionally suppresses the
54 * next space.
55 */
56 //indent input
57 int var = p -> member;
58 //indent end
59
60 //indent run -di0
61 int var = p->member;
62 //indent end
63
64
65 //indent input
66 void
unary_operators(void)67 unary_operators(void)
68 {
69 ++prefix_increment;
70 --prefix_decrement;
71 int *address = &lvalue;
72 int dereferenced = *address;
73 int positive = +number;
74 int negative = -number;
75 bool negated = !condition;
76 }
77 //indent end
78
79 //indent run-equals-input -di0
80
81
82 /*
83 * Ensure that a '*' is not interpreted as unary operator in situations that
84 * may look like a cast expression.
85 */
86 //indent input
87 {
88 sbuf_t *sb = *(sbuf_t **)sp;
89 return (int)(a * (float)b);
90 a = (2 * b == c);
91 }
92 //indent end
93
94 //indent run-equals-input -di0
95
96
97 /* All asterisks from a pointer type are merged into a single token. */
98 //indent input
99 {
100 char*
101 *
102 *
103 *x;
104 char
105 *
106 *
107 *
108 *x;
109 }
110 //indent end
111
112 //indent run
113 {
114 char ****x;
115 char
116 ****x;
117 }
118 //indent end
119