gcc_attribute.c revision 1.10 1 1.10 rillig /* $NetBSD: gcc_attribute.c,v 1.10 2021/07/15 21:00:05 rillig Exp $ */
2 1.1 rillig # 3 "gcc_attribute.c"
3 1.1 rillig
4 1.1 rillig /*
5 1.1 rillig * Tests for the various attributes for functions, types, statements that are
6 1.1 rillig * provided by GCC.
7 1.1 rillig *
8 1.1 rillig * https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
9 1.1 rillig */
10 1.1 rillig
11 1.1 rillig void __attribute__((noinline))
12 1.1 rillig do_not_inline(void)
13 1.1 rillig {
14 1.1 rillig }
15 1.1 rillig
16 1.2 rillig /* All pointer arguments must be nonnull. */
17 1.2 rillig void __attribute__((nonnull))
18 1.2 rillig function_nonnull(void *, const void *, int);
19 1.2 rillig
20 1.3 rillig /*
21 1.3 rillig * The documentation suggests that the argument list of nonnull be nonempty,
22 1.3 rillig * but GCC 9.3.0 accepts an empty list as well, treating all parameters as
23 1.3 rillig * nonnull.
24 1.3 rillig */
25 1.3 rillig void __attribute__((nonnull()))
26 1.3 rillig function_nonnull_list(void *, const void *, int);
27 1.3 rillig
28 1.2 rillig /* Arguments 1 and 2 must be nonnull. */
29 1.1 rillig void __attribute__((nonnull(1, 2)))
30 1.2 rillig function_nonnull_list(void *, const void *, int);
31 1.1 rillig
32 1.1 rillig /* expect+1: syntax error 'unknown_attribute' */
33 1.1 rillig void __attribute__((unknown_attribute))
34 1.1 rillig function_with_unknown_attribute(void);
35 1.4 rillig
36 1.4 rillig /*
37 1.4 rillig * There is an attribute called 'pcs', but that attribute must not prevent an
38 1.5 rillig * ordinary variable from being named the same. Starting with scan.l 1.77
39 1.5 rillig * from 2017-01-07, that variable name generated a syntax error. Fixed in
40 1.5 rillig * lex.c 1.33 from 2021-05-03.
41 1.4 rillig *
42 1.4 rillig * Seen in yds.c, function yds_allocate_slots.
43 1.4 rillig */
44 1.5 rillig int
45 1.4 rillig local_variable_pcs(void)
46 1.4 rillig {
47 1.5 rillig int pcs = 3;
48 1.4 rillig return pcs;
49 1.4 rillig }
50 1.6 rillig
51 1.6 rillig /*
52 1.6 rillig * FIXME: The attributes are handled by different grammar rules even though
53 1.6 rillig * they occur in the same syntactical position.
54 1.6 rillig *
55 1.6 rillig * Grammar rule abstract_decl_param_list handles the first attribute.
56 1.6 rillig *
57 1.6 rillig * Grammar rule direct_abstract_declarator handles all remaining attributes.
58 1.6 rillig *
59 1.6 rillig * Since abstract_decl_param_list contains type_attribute_opt, this could be
60 1.6 rillig * the source of the many shift/reduce conflicts in the grammar.
61 1.6 rillig */
62 1.6 rillig int
63 1.6 rillig func(
64 1.6 rillig int(int)
65 1.6 rillig __attribute__((__noreturn__))
66 1.6 rillig __attribute__((__noreturn__))
67 1.6 rillig );
68 1.8 rillig
69 1.8 rillig /*
70 1.8 rillig * https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html says that the
71 1.8 rillig * attribute-list is a "possibly empty comma-separated sequence of
72 1.8 rillig * attributes".
73 1.8 rillig *
74 1.8 rillig * No matter whether this particular example is interpreted as an empty list
75 1.8 rillig * or a list containing a single empty attribute, the result is the same in
76 1.8 rillig * both cases.
77 1.8 rillig */
78 1.8 rillig void one_empty_attribute(void)
79 1.8 rillig __attribute__((/* none */));
80 1.8 rillig
81 1.8 rillig /*
82 1.8 rillig * https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html further says that
83 1.8 rillig * each individual attribute may be "Empty. Empty attributes are ignored".
84 1.8 rillig */
85 1.8 rillig void two_empty_attributes(void)
86 1.8 rillig __attribute__((/* none */, /* still none */));
87 1.9 rillig
88 1.9 rillig /*
89 1.9 rillig * Ensure that __attribute__ can be specified everywhere in a declaration.
90 1.9 rillig * This is the simplest possible requirement that covers all valid code.
91 1.9 rillig * It accepts invalid code as well, but these cases are covered by GCC and
92 1.9 rillig * Clang already.
93 1.9 rillig *
94 1.9 rillig * Since lint only parses the attributes but doesn't really relate them to
95 1.9 rillig * identifiers or other entities, ensuring that valid code can be parsed is
96 1.9 rillig * enough for now.
97 1.9 rillig *
98 1.9 rillig * To really associate __attribute__ with the corresponding entity, the
99 1.9 rillig * grammar needs to be rewritten, see the example with __noreturn__ above.
100 1.9 rillig */
101 1.9 rillig __attribute__((deprecated("d1")))
102 1.9 rillig const
103 1.9 rillig __attribute__((deprecated("d2")))
104 1.9 rillig int
105 1.9 rillig __attribute__((deprecated("d3")))
106 1.9 rillig *
107 1.9 rillig // The below line would produce a syntax error.
108 1.9 rillig // __attribute__((deprecated("d3")))
109 1.9 rillig const
110 1.9 rillig __attribute__((deprecated("d4")))
111 1.9 rillig identifier
112 1.9 rillig __attribute__((deprecated("d5")))
113 1.9 rillig (
114 1.9 rillig __attribute__((deprecated("d6")))
115 1.9 rillig void
116 1.9 rillig __attribute__((deprecated("d7")))
117 1.9 rillig )
118 1.9 rillig __attribute__((deprecated("d8")))
119 1.9 rillig ;
120 1.10 rillig
121 1.10 rillig /*
122 1.10 rillig * The attribute 'const' provides stronger guarantees than 'pure', and
123 1.10 rillig * 'volatile' is not defined. To keep the grammar simple, any T_QUAL is
124 1.10 rillig * allowed at this point, but only syntactically.
125 1.10 rillig */
126 1.10 rillig int const_function(int) __attribute__((const));
127 1.10 rillig /* cover 'gcc_attribute_spec: T_QUAL' */
128 1.10 rillig /* expect+1: syntax error 'volatile' [249] */
129 1.10 rillig int volatile_function(int) __attribute__((volatile));
130