xref: /llvm-project/clang/test/C/drs/dr4xx.c (revision af34ac4508adcb5b54e62947d7caf7dd40227b16)
1 /* RUN: %clang_cc1 -std=c89 -verify=expected,c89only,pre-c23 -pedantic -Wno-c11-extensions %s
2    RUN: %clang_cc1 -std=c99 -verify=expected,pre-c23 -pedantic -Wno-c11-extensions %s
3    RUN: %clang_cc1 -std=c11 -verify=expected,pre-c23 -pedantic %s
4    RUN: %clang_cc1 -std=c17 -verify=expected,pre-c23 -pedantic %s
5    RUN: %clang_cc1 -std=c2x -verify=expected -pedantic %s
6  */
7 
8 /* The following are DRs which do not require tests to demonstrate
9  * conformance or nonconformance.
10  *
11  * WG14 DR401: yes
12  * "happens before" can not be cyclic
13  *
14  * WG14 DR402: yes
15  * Memory model coherence is not aligned with C++11
16  *
17  * WG14 DR404: yes
18  * Joke fragment remains in a footnote
19  *
20  * WG14 DR406: yes
21  * Visible sequences of side effects are redundant
22  *
23  * WG14 DR415: yes
24  * Missing divide by zero entry in Annex J
25  *
26  * WG14 DR417: yes
27  * Annex J not updated with necessary aligned_alloc entries
28  *
29  * WG14 DR419: yes
30  * Generic Functions
31  *
32  * WG14 DR420: yes
33  * Sytax error in specification of for-statement
34  *
35  * WG14 DR425: yes
36  * No specification for the access to variables with temporary lifetime
37  *
38  * WG14 DR434: yes
39  * Possible defect report: Missing constraint w.r.t. Atomic
40  *
41  * WG14 DR435: yes
42  * Possible defect report: Missing constraint w.r.t. Imaginary
43  *
44  * WG14 DR436: yes
45  * Request for interpretation of C11 6.8.5#6
46  * Note: This is not really testable because it requires -O1 or higher for LLVM
47  * to perform its reachability analysis and -Wunreachable-code only verifies
48  * diagnostic behavior, not runtime behavior. Also, both are a matter of QoI as
49  * to what they optimize/diagnose. But if someone thinks of a way to test this,
50  * we can add a test case for it then.
51  *
52  * WG14 DR448: yes
53  * What are the semantics of a # non-directive?
54  *
55  * WG14 DR454: yes
56  * ATOMIC_VAR_INIT (issues 3 and 4)
57  *
58  * WG14 DR455: yes
59  * ATOMIC_VAR_INIT issue 5
60  *
61  * WG14 DR459: yes
62  * atomic_load missing const qualifier
63  *
64  * WG14 DR475: yes
65  * Misleading Atomic library references to atomic types
66  *
67  * WG14 DR485: yes
68  * Problem with the specification of ATOMIC_VAR_INIT
69  *
70  * WG14 DR486: yes
71  * Inconsistent specification for arithmetic on atomic objects
72  *
73  * WG14 DR490: yes
74  * Unwritten Assumptions About if-then
75  */
76 
77 /* WG14 DR412: yes
78  * #elif
79  *
80  * Note: this is testing that #elif behaves the same as #else followed by #if.
81  */
82 #if 1
83 #elif this is not a valid expression
84 #else
85   #if this is not a valid expression
86   #endif
87 #endif
88 
89 /* WG14 DR413: yes
90  * Initialization
91  */
dr413(void)92 void dr413(void) {
93   typedef struct {
94     int k;
95     int l;
96     int a[2];
97   } T;
98 
99   typedef struct {
100     int i;
101     T t;
102   } S;
103 
104   /* Ensure that explicit initialization (.t = { ... }) takes precedence over a
105    * later implicit partial initialization (.t.l = 41). The value should be 42,
106    * not 0.
107    */
108   _Static_assert((S){ /* c89only-warning {{compound literals are a C99-specific feature}}
109                          expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
110                        */
111       1,
112       .t = {          /* c89only-warning {{designated initializers are a C99 feature}} */
113         .l = 43,      /* c89only-warning {{designated initializers are a C99 feature}}
114                          expected-note {{previous initialization is here}}
115                        */
116         .k = 42,
117         .a[1] = 19,   /* expected-note {{previous initialization is here}} */
118         .a[0] = 18
119       },
120       .t.l = 41,      /* expected-warning {{initializer overrides prior initialization of this subobject}} */
121       .t.a[1] = 17    /* expected-warning {{initializer overrides prior initialization of this subobject}} */
122     }.t.k == 42, "");
123 }
124 
125 /* WG14 DR423: partial
126  * Defect Report relative to n1570: underspecification for qualified rvalues
127  */
128 
129 /* FIXME: this should pass because the qualifier on the return type should be
130  * dropped when forming the function type.
131  */
132 const int dr423_const(void);
133 int dr423_nonconst(void);
134 _Static_assert(__builtin_types_compatible_p(__typeof__(dr423_const), __typeof__(dr423_nonconst)), "fail"); /* expected-error {{fail}} */
135 
dr423_func(void)136 void dr423_func(void) {
137   const int i = 12;
138   __typeof__(i) v1 = 12; /* expected-note {{variable 'v1' declared const here}} */
139   __typeof__((const int)12) v2 = 12;
140 
141   v1 = 100; /* expected-error {{cannot assign to variable 'v1' with const-qualified type 'typeof (i)' (aka 'const int')}} */
142   v2 = 100; /* Not an error; the qualifier was stripped. */
143 }
144 
145 /* WG14 DR432: yes
146  * Possible defect report: Is 0.0 required to be a representable value?
147  *
148  * We're going to lean on the fpclassify builtin to tell us whether 0.0
149  * represents the value 0, and we'll test that adding and subtracting 0.0 does
150  * not change the value, and we'll hope that's enough to validate this DR.
151  */
152 _Static_assert(__builtin_fpclassify(0, 1, 2, 3, 4, 0.0f) == 4, "");
153 _Static_assert((1.0 / 3.0) + 0.0 == (1.0 / 3.0) - 0.0, ""); /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}} */
154 
155 /* WG14 DR444: partial
156  * Issues with alignment in C11, part 1
157  */
dr444(void)158 void dr444(void) {
159   _Alignas(int) int i;
160    _Alignas(int) struct S {
161     _Alignas(int) int i;
162   } s;
163 
164   /* FIXME: This should be accepted as per this DR. */
165   int j = (_Alignas(int) int){12}; /* expected-error {{expected expression}} */
166 
167   _Alignas(int) struct T { /* expected-warning {{'_Alignas' attribute ignored}} */
168     int i;
169   };
170 
171   struct U {
172     _Alignas(int) int bit : 1; /* expected-error {{'_Alignas' attribute cannot be applied to a bit-field}} */
173   };
174 
175   _Alignas(int) typedef int foo;  /* expected-error {{'_Alignas' attribute only applies to variables and fields}} */
176   _Alignas(int) register int bar; /* expected-error {{'_Alignas' attribute cannot be applied to a variable with 'register' storage class}} */
177   _Alignas(int) void func(void);  /* expected-error {{'_Alignas' attribute only applies to variables and fields}} */
178 
179   /* FIXME: it is correct for us to accept this per 6.7.3p5, but it seems like
180    * a situation we'd want to diagnose because the alignments are different and
181    * the user probably doesn't know which one "wins".
182    */
183   _Alignas(int) _Alignas(double) int k;
184 }
185 
186 /* WG14 DR447: yes
187  * Boolean from complex
188  *
189  * Ensure that the imaginary part contributes to the conversion to bool, not
190  * just the real part.
191  */
192 _Static_assert((_Bool)0.0 + 3.0 * (__extension__ 1.0iF), "");  /* c89only-warning {{'_Bool' is a C99 extension}}
193                                                                   expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
194                                                                 */
195 _Static_assert(!(_Bool)0.0 + 0.0 * (__extension__ 1.0iF), ""); /* c89only-warning {{'_Bool' is a C99 extension}}
196                                                                   expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
197                                                                */
198 
199 /* WG14 DR463: yes
200  * Left-shifting into the sign bit
201  *
202  * This DR was NAD and leaves shifting a bit into the high bit of a signed
203  * integer type undefined behavior, unlike in C++. Note, the diagnostic is also
204  * issued in C++ for shifting into that bit despite being well-defined because
205  * the code is questionable and should be validated by the programmer.
206  */
dr463(void)207 void dr463(void) {
208   (void)(1 << (__CHAR_BIT__ * sizeof(int))); /* expected-warning {{shift count >= width of type}} */
209   (void)(1 << ((__CHAR_BIT__ * sizeof(int)) - 1));
210 }
211 
212 /* WG14 DR478: yes
213  * Valid uses of the main function
214  */
main(void)215 int main(void) {
216   /* This DR clarifies that C explicitly allows you to call main() in a hosted
217    * environment; it is not special as it is in C++, so recursive calls are
218    * fine as well as nonrecursive direct calls.
219    */
220   main(); /* ok */
221 }
222 
dr478(void)223 void dr478(void) {
224   int (*fp)(void) = main; /* ok */
225   main(); /* ok */
226 }
227 
228 /* WG14 DR481: yes
229  * Controlling expression of _Generic primary expression
230  */
dr481(void)231 void dr481(void) {
232   /* The controlling expression undergoes lvalue to rvalue conversion, and that
233    * performs array decay and strips qualifiers.
234    */
235   (void)_Generic("bla", char *: "blu");
236   (void)_Generic((int const){ 0 }, int: "blu");  /* c89only-warning {{compound literals are a C99-specific feature}} */
237   (void)_Generic(+(int const){ 0 }, int: "blu"); /* c89only-warning {{compound literals are a C99-specific feature}} */
238 
239   (void)_Generic("bla", /* expected-error {{controlling expression type 'char *' not compatible with any generic association type}} */
240     char[4]: "blu");    /* expected-warning {{due to lvalue conversion of the controlling expression, association of type 'char[4]' will never be selected because it is of array type}} */
241 
242   (void)_Generic((int const){ 0 }, /* expected-error {{controlling expression type 'int' not compatible with any generic association type}}
243                                       c89only-warning {{compound literals are a C99-specific feature}}
244                                     */
245     int const: "blu");             /* expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const int' will never be selected because it is qualified}} */
246 
247   (void)_Generic(+(int const){ 0 }, /* expected-error {{controlling expression type 'int' not compatible with any generic association type}}
248                                        c89only-warning {{compound literals are a C99-specific feature}}
249                                      */
250     int const: "blu");              /* expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const int' will never be selected because it is qualified}} */
251 }
252 
253 /* WG14 DR489: partial
254  * Integer Constant Expression
255  *
256  * The DR is about whether unevaluated operands have to follow the same
257  * restrictions as the rest of the expression in an ICE, and according to the
258  * committee, they do.
259  */
dr489(void)260 void dr489(void) {
261   struct S {
262     int bit : 12 || 1.0f; /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}} */
263   };
264   enum E {
265     Val = 0 && 1.0f /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}} */
266   };
267 
268   int i;
269 
270   /* FIXME: mentioning the 'aligned' attribute is confusing, but also, should
271    * this be folded as an ICE as a GNU extension? GCC does not fold it.
272    */
273   _Alignas(0 ? i++ : 8) char c; /* expected-error {{'aligned' attribute requires integer constant}} */
274 
275   /* FIXME: this should get the constant folding diagnostic as this is not a
276    * valid ICE because the floating-point constants are not the immediate
277    * operand of a cast. It should then also get a diagnostic about trying to
278    * declare a VLA with static storage duration and the C99 extension warning
279    * for VLAs in C89.
280    */
281   static int vla[sizeof(1.0f + 1.0f)];
282 
283   int val[5] = { [1 ? 0 : i--] = 12  }; /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
284                                            c89only-warning {{designated initializers are a C99 feature}}
285                                          */
286 
287   /* FIXME: this should be the constant folding diagnostic as this is not a
288    * valid ICE because of the / operator.
289    */
290   _Static_assert(sizeof(0 / 0), "");
291 
292   /* FIXME: this should also get the constant folding diagnostic as this is not
293    * a valid ICE because of the = operator.
294    */
295   (void)_Generic(i = 12, int : 0); /* expected-warning {{expression with side effects has no effect in an unevaluated context}} */
296 
297   switch (i) {
298   case (int)0.0f: break;    /* okay, a valid ICE */
299 
300   /* FIXME: this should be accepted in C23 and up without a diagnostic, as C23
301    * added compound literals to the allowed list of things in an ICE. The
302    * diagnostic is correct for C17 and earlier though.
303    */
304   case (int){ 2 }: break;   /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
305                                c89only-warning {{compound literals are a C99-specific feature}}
306                              */
307   case 12 || main(): break; /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}} */
308   }
309 }
310 
311 /* WG14 DR492: yes
312  * Named Child struct-union with no Member
313  */
314 struct dr492_t {
315   union U11 {  /* expected-warning {{declaration does not declare anything}} */
316     int m11;
317     float m12;
318   };
319   int m13;
320 } dr492;
321 
322 /* WG14 DR496: yes
323  * offsetof questions
324  */
dr496(void)325 void dr496(void) {
326   struct A { int n, a [2]; };
327   struct B { struct A a; };
328   struct C { struct A a[1]; };
329 
330   /* Array access & member access expressions are now valid. */
331   _Static_assert(__builtin_offsetof(struct B, a.n) == 0, "");
332   /* First int below is for 'n' and the second int is for 'a[0]'; this presumes
333    * there is no padding involved.
334    */
335   _Static_assert(__builtin_offsetof(struct B, a.a[1]) == sizeof(int) + sizeof(int), "");
336 
337   /* However, we do not support using the -> operator to access a member, even
338    * if that would be a valid expression. FIXME: GCC accepts this, perhaps we
339    * should as well.
340    */
341   (void)__builtin_offsetof(struct C, a->n); /* expected-error {{expected ')'}} \
342                                                expected-note {{to match this '('}}
343                                              */
344 
345   /* The DR asked a question about whether defining a new type within offsetof
346    * is allowed. C23 N2350 had made this explicitly undefined behavior, but this
347    * was later overturned when C23 DE-137 was accepted, making it well-formed.
348    *
349    * Additionally, GCC and Clang both support it as an extension in pre-C23
350    * mode.
351    */
352    (void)__builtin_offsetof(struct S { int a; }, a); /* pre-c23-warning{{defining a type within '__builtin_offsetof' is a C23 extension}} */
353 }
354 
355 /* WG14 DR499: yes
356  * Anonymous structure in union behavior
357  */
dr499(void)358 void dr499(void) {
359   union U {
360     struct {
361       char B1;
362       char B2;
363       char B3;
364       char B4;
365     };
366     int word;
367   } u;
368 
369   /* Validate that B1, B2, B3, and B4 do not have overlapping storage, only the
370    * anonymous structure and 'word' overlap.
371    */
372   _Static_assert(__builtin_offsetof(union U, B1) == 0, "");
373   _Static_assert(__builtin_offsetof(union U, B2) == 1, "");
374   _Static_assert(__builtin_offsetof(union U, B3) == 2, "");
375   _Static_assert(__builtin_offsetof(union U, B4) == 3, "");
376   _Static_assert(__builtin_offsetof(union U, word) == 0, "");
377 }
378