xref: /llvm-project/clang/test/C/drs/dr1xx.c (revision 7dd7d5749f7d9d98fec50ee88e633e8b85c6502a)
1 /* RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only -pedantic -Wno-c11-extensions %s
2    RUN: %clang_cc1 -std=c99 -fsyntax-only -verify=expected,c99untilc2x -pedantic -Wno-c11-extensions %s
3    RUN: %clang_cc1 -std=c11 -fsyntax-only -verify=expected,c99untilc2x -pedantic %s
4    RUN: %clang_cc1 -std=c17 -fsyntax-only -verify=expected,c99untilc2x -pedantic %s
5    RUN: %clang_cc1 -std=c2x -fsyntax-only -verify=expected,c2xandup -pedantic %s
6  */
7 
8 /* The following are DRs which do not require tests to demonstrate
9  * conformance or nonconformance.
10  *
11  * WG14 DR100: dup 001
12  * Defect with the return statement
13  *
14  * WG14 DR104: dup 084
15  * Incomplete tag types in a parameter list
16  *
17  * WG14 DR109: yes
18  * Are undefined values and undefined behavior the same?
19  *
20  * WG14 DR110: dup 047
21  * Formal parameters having array-of-non-object types
22  *
23  * WG14 DR117: yes
24  * Abstract semantics, sequence points, and expression evaluation
25  *
26  * WG14 DR121: yes
27  * Conversions of pointer values to integral types
28  *
29  * WG14 DR122: dup 015
30  * Conversion/widening of bit-fields
31  *
32  * WG14 DR125: yes
33  * Using things declared as 'extern (qualified) void'
34  *
35  * WG14 DR127: dup 013
36  * Composite type of an enumerated type and an integral type
37  *
38  * WG14 DR132: dup 109
39  * Can undefined behavior occur at translation time, or only at run time?
40  *
41  * WG14 DR133: yes
42  * Undefined behavior not previously listed in subclause G2
43  *
44  * WG14 DR138: yes
45  * Is there an allocated storage duration?
46  *
47  * WG14 DR139: yes
48  * Compatibility of complete and incomplete types
49  *
50  * WG14 DR146: yes
51  * Nugatory constraint
52  *
53  * WG14 DR147: yes
54  * Sequence points in library functions
55  *
56  * WG14 DR148: yes
57  * Defining library functions
58  *
59  * WG14 DR149: yes
60  * The term "variable"
61  *
62  * WG14 DR154: yes
63  * Consistency of implementation-defined values
64  *
65  * WG14 DR159: yes
66  * Consistency of the C Standard Defects exist in the way the Standard refers
67  * to itself
68  *
69  * WG14 DR161: yes
70  * Details of reserved symbols
71  *
72  * WG14 DR169: yes
73  * Trigraphs
74  */
75 
76 
77 /* WG14 DR101: yes
78  * Type qualifiers and "as if by assignment"
79  */
80 void dr101_callee(const int val);
dr101_caller(void)81 void dr101_caller(void) {
82   int val = 1;
83   dr101_callee(val); /* ok; const qualifier on the parameter doesn't prevent as-if assignment. */
84 }
85 
86 /* WG14 DR102: yes
87  * Tag redeclaration constraints
88  */
dr102(void)89 void dr102(void) {
90   struct S { int member; }; /* expected-note {{previous definition is here}} */
91   struct S { int member; }; /* expected-error {{redefinition of 'S'}} */
92 
93   union U { int member; }; /* expected-note {{previous definition is here}} */
94   union U { int member; }; /* expected-error {{redefinition of 'U'}} */
95 
96   enum E { member }; /* expected-note 2{{previous definition is here}} */
97   enum E { member }; /* expected-error {{redefinition of 'E'}}
98                         expected-error {{redefinition of enumerator 'member'}} */
99 }
100 
101 /* WG14 DR103: yes
102  * Formal parameters of incomplete type
103  */
104 void dr103_1(int arg[]); /* ok, not an incomplete type due to rewrite */
dr103_2(struct S s)105 void dr103_2(struct S s) {} /* expected-warning {{declaration of 'struct S' will not be visible outside of this function}}
106                                expected-error {{variable has incomplete type 'struct S'}}
107                                expected-note {{forward declaration of 'struct S'}} */
108 void dr103_3(struct S s);               /* expected-warning {{declaration of 'struct S' will not be visible outside of this function}}
109                                            expected-note {{previous declaration is here}} */
dr103_3(struct S{ int a; } s)110 void dr103_3(struct S { int a; } s) { } /* expected-warning {{declaration of 'struct S' will not be visible outside of this function}}
111                                            expected-error {{conflicting types for 'dr103_3'}} */
112 void dr103_4(struct S s1, struct S { int a; } s2); /* expected-warning {{declaration of 'struct S' will not be visible outside of this function}} */
113 
114 /* WG14 DR105: dup 017
115  * Precedence of requirements on compatible types
116  *
117  * NB: This is also Question 3 from DR017.
118  */
dr105(void)119 void dr105(void) {
120   /* According to C2x 6.7.6.3p14 the return type and parameter types to be
121    * compatible types, but qualifiers are dropped from the parameter type.
122    */
123   extern void func(int);
124   extern void func(const int); /* FIXME: this should be pedantically diagnosed. */
125 
126   extern void other_func(int);   /* expected-note {{previous declaration is here}} */
127   extern void other_func(int *); /* expected-error {{conflicting types for 'other_func'}} */
128 
129   extern int i;   /* expected-note {{previous declaration is here}} */
130   extern float i; /* expected-error {{redeclaration of 'i' with a different type: 'float' vs 'int'}} */
131 }
132 
133 /* WG14 DR106: yes
134  * When can you dereference a void pointer?
135  *
136  * NB: This is a partial duplicate of DR012.
137  */
dr106(void * p,int i)138 void dr106(void *p, int i) {
139   /* The behavior changed between C89 and C99. */
140   (void)&*p; /* c89only-warning {{ISO C forbids taking the address of an expression of type 'void'}}
141                 c89only-warning {{ISO C does not allow indirection on operand of type 'void *'}} */
142 
143   /* The behavior of all three of these is undefined. */
144   (void)*p; /* expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}*/
145 
146   (void)&(*p); /* c89only-warning {{ISO C forbids taking the address of an expression of type 'void'}}
147                   expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}*/
148 
149   (void)(i ? *p : *p); /* expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}
150                           expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}*/
151 
152   (void)(*p, *p); /* expected-warning {{left operand of comma operator has no effect}}
153                      expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}
154 		     expected-warning {{ISO C does not allow indirection on operand of type 'void *'}}*/
155 }
156 
157 /* WG14 DR108: yes
158  * Can a macro identifier hide a keyword?
159  */
dr108(void)160 void dr108(void) {
161 #define const
162   const int i = 12;
163 #undef const
164   const int j = 12; /* expected-note {{variable 'j' declared const here}} */
165 
166   i = 100; /* Okay, the keyword was hidden by the macro. */
167   j = 100; /* expected-error {{cannot assign to variable 'j' with const-qualified type 'const int'}} */
168 }
169 
170 /* WG14 DR111: yes
171  * Conversion of pointer-to-qualified type values to type (void*) values
172  */
dr111(const char * ccp,void * vp)173 void dr111(const char *ccp, void *vp) {
174   vp = ccp; /* expected-warning {{assigning to 'void *' from 'const char *' discards qualifiers}} */
175 }
176 
177 /* WG14 DR112: yes
178  * Null pointer constants and relational comparisons
179  */
dr112(void * vp)180 void dr112(void *vp) {
181   /* The behavior of this expression is pedantically undefined.
182    * FIXME: should we diagnose under -pedantic?
183    */
184   (void)(vp > (void*)0);
185 }
186 
187 /* WG14 DR113: yes
188  * Return expressions in functions declared to return qualified void
189  */
dr113_v(volatile void * vvp)190 volatile void dr113_v(volatile void *vvp) { /* expected-warning {{function cannot return qualified void type 'volatile void'}} */
191   return *vvp; /* expected-warning {{void function 'dr113_v' should not return void expression}}
192                   expected-warning{{ISO C does not allow indirection on operand of type 'volatile void *'}} */
193 }
dr113_c(const void * cvp)194 const void dr113_c(const void *cvp) { /* expected-warning {{function cannot return qualified void type 'const void'}} */
195   return *cvp; /* expected-warning {{void function 'dr113_c' should not return void expression}}
196                   expected-warning{{ISO C does not allow indirection on operand of type 'const void *'}} */
197 }
198 
199 /* WG14 DR114: yes
200  * Initialization of multi-dimensional char array objects
201  */
dr114(void)202 void dr114(void) {
203   char array[2][5] = { "defghi" }; /* expected-warning {{initializer-string for char array is too long}} */
204 }
205 
206 /* WG14 DR115: yes
207  * Member declarators as declarators
208  */
dr115(void)209 void dr115(void) {
210   struct { int mbr; }; /* expected-warning {{declaration does not declare anything}} */
211   union { int mbr; };  /* expected-warning {{declaration does not declare anything}} */
212 }
213 
214 /* WG14 DR116: yes
215  * Implicit unary & applied to register arrays
216  */
dr116(void)217 void dr116(void) {
218   register int array[5] = { 0, 1, 2, 3, 4 };
219   (void)array;       /* expected-error {{address of register variable requested}} */
220   (void)array[3];    /* expected-error {{address of register variable requested}} */
221   (void)(array + 3); /* expected-error {{address of register variable requested}} */
222 }
223 
224 /* WG14 DR118: yes
225  * Completion point for enumerated types
226  */
dr118(void)227 void dr118(void) {
228   enum E {
229 	/* The enum isn't a complete type until the closing }, but an
230 	 * implementation may complete the type earlier if it has sufficient type
231 	 * information to calculate size or alignment, etc.
232 	 *
233 	 * On Microsoft targets, an enum is always implicit int sized, so the type
234 	 * is sufficiently complete there. On other platforms, it is an incomplete
235 	 * type at this point.
236 	 */
237     Val = sizeof(enum E)
238     #if !defined(_WIN32) || defined(__MINGW32__)
239     /* expected-error@-2 {{invalid application of 'sizeof' to an incomplete type 'enum E'}} */
240     /* expected-note@-12 {{definition of 'enum E' is not complete until the closing '}'}} */
241     #endif
242   };
243 }
244 
245 /* WG14 DR119: yes
246  * Initialization of multi-dimensional array objects
247  */
dr119(void)248 void dr119(void) {
249   static int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; /* expected-error {{array has incomplete element type 'int[]'}} */
250 }
251 
252 /* WG14 DR120: yes
253  * Semantics of assignment to (and initialization of) bit-fields
254  */
dr120(void)255 void dr120(void) {
256   /* We could verify this one with a codegen test to ensure that the proper
257    * value is stored into bit, but the diagnostic tells us what the value is
258    * after conversion, so we can lean on that for verification.
259    */
260   struct S { unsigned bit:1; };
261   struct S object1 = { 3 }; /* expected-warning {{implicit truncation from 'int' to bit-field changes value from 3 to 1}} */
262   struct S object2;
263   object2.bit = 3; /* expected-warning {{implicit truncation from 'int' to bit-field changes value from 3 to 1}} */
264 }
265 
266 /* WG14 DR123: yes
267  * 'Type categories' and qualified types
268  */
dr123(void)269 void dr123(void) {
270   /* Both of these examples are strictly conforming. */
271   enum E1 {
272     enumerator1 = (const int) 9
273   };
274   enum E2 {
275     enumerator2 = (volatile int) 9
276   };
277 }
278 
279 /* WG14 DR124: yes
280  * Casts to 'a void type' versus casts to 'the void type'
281  */
dr124(void)282 void dr124(void) {
283   /* A cast can cast to void or any qualified version of void. */
284   (const volatile void)0;
285 }
286 
287 /* WG14 DR126:  yes
288  * What does 'synonym' mean with respect to typedef names?
289  */
dr126(void)290 void dr126(void) {
291   typedef int *IP;
292   const IP object; /* expected-note {{variable 'object' declared const here}} */
293 
294   /* The root of the DR is whether 'object' is a pointer to a const int, or a
295    * const pointer to int.
296    */
297   *object = 12; /* ok */
298   ++object; /* expected-error {{cannot assign to variable 'object' with const-qualified type 'const IP' (aka 'int *const')}} */
299 }
300 
301 /* WG14 DR128: yes
302  * Editorial issue relating to tag declarations in type specifiers
303  */
dr128(void)304 void dr128(void) {
305   {
306     struct TAG { int i; };
307   }
308   {
309     struct TAG object; /* expected-error {{variable has incomplete type 'struct TAG'}}
310                           expected-note {{forward declaration of 'struct TAG'}}
311                         */
312   }
313 }
314 
315 /* WG14 DR129: yes
316  * Tags and name spaces
317  */
318 struct dr129_t { int i; };
dr129(void)319 void dr129(void) {
320   enum dr129_t { enumerator }; /* expected-note {{previous use is here}} */
321   void *vp;
322 
323   (void)(struct dr129_t *)vp; /* expected-error {{use of 'dr129_t' with tag type that does not match previous declaration}} */
324 }
325 
326 /* WG14 DR131: yes
327  * const member qualification and assignment
328  */
dr131(void)329 void dr131(void) {
330   struct S {
331     const int i; /* expected-note {{data member 'i' declared const here}} */
332   } s1, s2;
333   s1 = s2; /* expected-error {{cannot assign to variable 's1' with const-qualified data member 'i'}} */
334 }
335 
336 /* WG14 DR142: yes
337  * Reservation of macro names
338  */
dr142(void)339 void dr142(void) {
340 #include <stddef.h>
341 /* FIXME: undefining a macro defined by the standard library is undefined
342  * behavior. We have diagnostics when declaring reserved identifiers, and we
343  * could consider extending that to undefining a macro defined in a system
344  * header. However, whether we diagnose or not, we conform.
345  */
346 #undef NULL
347 }
348 
349 /* WG14 DR144: yes
350  * Preprocessing of preprocessing directives
351  */
352 #define DR144
353 # DR144 include <stddef.h> /* expected-error {{invalid preprocessing directive}} */
354 DR144 # include <stddef.h> /* expected-error {{expected identifier or '('}} */
355 
356 /* WG14 DR145:
357  * Constant expressions
358  */
dr145(void)359 void dr145(void) {
360   static int array[10];
361   static int *ip = (int *)0;
362   /* The below is failing because some systems think this is a valid compile-
363    * time constant. Commenting the out while investigating whether we implement
364    * this DR properly or not.
365    * static int i = array[0] + array[1]; broken-expected-error {{initializer element is not a compile-time constant}}
366    */
367 }
368 
369 /* WG14 DR150: yes
370  * Initialization of a char array from a string literal
371  */
dr150(void)372 void dr150(void) {
373   /* Accept even though a string literal is not a constant expression. */
374   static char array[] = "Hello, World";
375 }
376 
377 /* WG14 DR163: yes
378  * Undeclared identifiers
379  */
dr163(void)380 void dr163(void) {
381   int i;
382   i = undeclared; /* expected-error {{use of undeclared identifier 'undeclared'}} */
383   sdfsdfsf = 1;   /* expected-error {{use of undeclared identifier 'sdfsdfsf'}} */
384   i = also_undeclared(); /* c99untilc2x-error {{call to undeclared function 'also_undeclared'; ISO C99 and later do not support implicit function declarations}}
385                             c2xandup-error {{use of undeclared identifier 'also_undeclared'}}
386                           */
387 }
388 
389 /* WG14 DR164: yes
390  * Bad declarations
391  */
dr164(void)392 void dr164(void) {
393   int a [][5];    /* expected-error {{definition of variable with array type needs an explicit size or an initializer}} */
394   int x, b [][5]; /* expected-error {{definition of variable with array type needs an explicit size or an initializer}} */
395 }
396