xref: /llvm-project/clang/test/C/drs/dr3xx.c (revision 997ffce43c6d2d3f647eb091c732665049b1f47f)
1 /* RUN: %clang_cc1 -std=c89 -fsyntax-only -Wvla -verify=expected,c89only,c17andearlier -pedantic -Wno-c11-extensions %s
2    RUN: %clang_cc1 -std=c99 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic -Wno-c11-extensions %s
3    RUN: %clang_cc1 -std=c11 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic %s
4    RUN: %clang_cc1 -std=c17 -fsyntax-only -Wvla -verify=expected,c99andup,c17andearlier -pedantic %s
5    RUN: %clang_cc1 -std=c2x -fsyntax-only -Wvla -verify=expected,c99andup,c23andup -pedantic %s
6  */
7 
8 /* The following are DRs which do not require tests to demonstrate
9  * conformance or nonconformance.
10  *
11  * WG14 DR300: yes
12  * Translation-time expresssion evaluation
13  *
14  * WG14 DR301: yes
15  * Meaning of FE_* macros in <fenv.h>
16  *
17  * WG14 DR303: yes
18  * Breaking up the very long sentence describing preprocessing directive
19  *
20  * WG14 DR307: yes
21  * Clarifiying arguments vs. parameters
22  *
23  * WG14 DR308: yes
24  * Clarify that source files et al. need not be "files"
25  *
26  * WG14 DR310: yes
27  * Add non-corner case example of trigraphs
28  *
29  * WG14 DR312: yes
30  * Meaning of "known constant size"
31  *
32  * WG14 DR333: yes
33  * Missing Predefined Macro Name
34  *
35  * WG14 DR342: dup 340
36  * VLAs and conditional expressions
37  */
38 
39 
40 /* WG14 DR302: yes
41  * Adding underscore to portable include file name character set
42  */
43 #include "./abc_123.h"
44 #ifndef WE_SUPPORT_DR302
45 #error "Oh no, we don't support DR302 after all!"
46 #endif
47 
48 /* WG14 DR304: yes
49  * Clarifying illegal tokens in #if directives
50  */
51 /* expected-error@+3 {{invalid token at start of a preprocessor expression}}
52    expected-warning@+3 {{missing terminating ' character}}
53  */
54 #if 'test
55 #endif
56 
57 /* WG14 DR305: yes
58  * Clarifying handling of keywords in #if directives
59  */
60 #if int
61 #error "We definitely should not have gotten here"
62 #endif
63 
64 /* WG14 DR306: yes
65  * Clarifying that rescanning applies to object-like macros
66  */
67 #define REPLACE 1
68 #define THIS REPLACE
69 #if THIS != 1
70 #error "We definitely should not have gotten here"
71 #endif
72 
73 /* WG14 DR311: yes
74  * Definition of variably modified types
75  */
dr311(int x)76 void dr311(int x) {
77   typedef int vla[x]; /* expected-warning {{variable length array}} */
78 
79   /* Ensure that a constant array of variable-length arrays are still
80    * considered a variable-length array.
81    */
82   vla y[3]; /* expected-warning {{variable length array}} */
83 }
84 
85 /* WG14 DR313: yes
86  * Incomplete arrays of VLAs
87  */
88 void dr313(int i) {
89   int c[][i] = { 0 }; /* expected-error {{variable-sized object may not be initialized}}
90                          expected-warning {{variable length array}}
91                        */
92 }
93 
94 /* WG14 DR315: yes
95  * Implementation-defined bit-field types
96  */
97 struct dr315_t {
98   unsigned long long a : 37; /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
99   unsigned long long b : 37; /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
100 
101   short c : 8;
102   short d : 8;
103 } dr315;
104 _Static_assert(sizeof(dr315.a + dr315.b) == sizeof(unsigned long long), ""); /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
105 /* Demonstrate that integer promotions still happen when less than the width of
106  * an int.
107  */
108 _Static_assert(sizeof(dr315.c + dr315.d) == sizeof(int), "");
109 
110 #if __STDC_VERSION__ < 202311L
111 /* WG14 DR316: yes
112  * Unprototyped function types
113  */
114 void dr316_1(a) int a; {}  /* expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} */
115 void (*dr316_1_ptr)(int, int, int) = dr316_1;
116 
117 /* WG14 DR317: yes
118  * Function definitions with empty parentheses
119  *
120  * Despite the function with empty parens being a definition, this does not
121  * provide a prototype for the function. However, calling the function with
122  * arguments is undefined behavior, so it is defensible for us to warn the user
123  * about it. They key point to this DR is that we give the "without a
124  * prototype" warnings to demonstrate we don't give this function a prototype.
125  */
126 void dr317_1() {}  /* expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} */
dr317_2(void)127 void dr317_2(void) {
128   if (0)
129     dr317_1(1); /* expected-warning {{too many arguments in call to 'dr317_1'}}
130                    expected-warning {{passing arguments to 'dr317_1' without a prototype is deprecated in all versions of C and is not supported in C23}}
131                  */
132 }
133 #endif /* __STDC_VERSION__ < 202311L */
134 
135 /* WG14 DR320: yes
136  * Scope of variably modified type
137  */
138 int dr320_v;
139 typedef int dr320_t[dr320_v]; /* c89only-warning {{variable length arrays are a C99 feature}}
140                                  expected-error {{variable length array declaration not allowed at file scope}}
141                                  c99andup-warning {{variable length array used}}
142                                */
dr320(int okay[dr320_v])143 void dr320(int okay[dr320_v]) { /* c89only-warning {{variable length arrays are a C99 feature}}
144                                    c99andup-warning {{variable length array used}}
145                                  */
146   typedef int type[dr320_v]; /* c89only-warning {{variable length arrays are a C99 feature}}
147                                 c99andup-warning {{variable length array used}}
148                               */
149   extern type bad;  /* expected-error {{variable length array declaration cannot have 'extern' linkage}} */
150 
151   /* C99 6.7.5.2p2, second sentence. */
152   static type fine; /* expected-error {{variable length array declaration cannot have 'static' storage duration}} */
153 }
154 
155 /* WG14 DR321: yes
156  * Wide character code values for members of the basic character set
157  */
158 #define DR321 (\
159     ' ' == L' ' && '\t' == L'\t' && '\v' == L'\v' && '\r' == L'\r' &&           \
160     '\n' == L'\n' &&                                                            \
161     'a' == L'a' && 'b' == L'b' && 'c' == L'c' && 'd' == L'd' && 'e' == L'e' &&  \
162     'f' == L'f' && 'g' == L'g' && 'h' == L'h' && 'i' == L'i' && 'j' == L'j' &&  \
163     'k' == L'k' && 'l' == L'l' && 'm' == L'm' && 'n' == L'n' && 'o' == L'o' &&  \
164     'p' == L'p' && 'q' == L'q' && 'r' == L'r' && 's' == L's' && 't' == L't' &&  \
165     'u' == L'u' && 'v' == L'v' && 'w' == L'w' && 'x' == L'x' && 'y' == L'y' &&  \
166     'z' == L'z' &&                                                              \
167     'A' == L'A' && 'B' == L'B' && 'C' == L'C' && 'D' == L'D' && 'E' == L'E' &&  \
168     'F' == L'F' && 'G' == L'G' && 'H' == L'H' && 'I' == L'I' && 'J' == L'J' &&  \
169     'K' == L'K' && 'L' == L'L' && 'M' == L'M' && 'N' == L'N' && 'O' == L'O' &&  \
170     'P' == L'P' && 'Q' == L'Q' && 'R' == L'R' && 'S' == L'S' && 'T' == L'T' &&  \
171     'U' == L'U' && 'V' == L'V' && 'W' == L'W' && 'X' == L'X' && 'Y' == L'Y' &&  \
172     'Z' == L'Z' &&                                                              \
173     '0' == L'0' && '1' == L'1' && '2' == L'2' && '3' == L'3' && '4' == L'4' &&  \
174     '5' == L'5' && '6' == L'6' && '7' == L'7' && '8' == L'8' &&                 \
175     '9' == L'9' &&                                                              \
176     '_' == L'_' && '{' == L'{' && '}' == L'}' && '[' == L'[' && ']' == L']' &&  \
177     '#' == L'#' && '(' == L'(' && ')' == L')' && '<' == L'<' && '>' == L'>' &&  \
178     '%' == L'%' && ':' == L':' && ';' == L';' && '.' == L'.' && '?' == L'?' &&  \
179     '*' == L'*' && '+' == L'+' && '-' == L'-' && '/' == L'/' && '^' == L'^' &&  \
180     '&' == L'&' && '|' == L'|' && '~' == L'~' && '!' == L'!' && '=' == L'=' &&  \
181     ',' == L',' && '\\' == L'\\' && '"' == L'"' && '\'' == L'\''                \
182   )
183 #if __STDC_MB_MIGHT_NEQ_WC__
184 #ifndef __FreeBSD__ /* PR22208, FreeBSD expects us to give a bad (but conforming) answer here. */
185 _Static_assert(!DR321, "__STDC_MB_MIGHT_NEQ_WC__ but all basic source characters have same representation");
186 #endif
187 #else
188 _Static_assert(DR321, "!__STDC_MB_MIGHT_NEQ_WC__ but some character differs");
189 #endif
190 
191 /* WG14 DR328: partial
192  * String literals in compound literal initialization
193  *
194  * DR328 is implemented properly in terms of allowing string literals, but is
195  * not implemented. See DR339 (marked as a duplicate of this one) for details.
196  */
197 const char *dr328_v = (const char *){"this is a string literal"}; /* c89only-warning {{compound literals are a C99-specific feature}} */
dr328(void)198 void dr328(void) {
199   const char *val = (const char *){"also a string literal"}; /* c89only-warning {{compound literals are a C99-specific feature}} */
200 }
201 
202 /* WG14 DR335: yes
203  * _Bool bit-fields
204  *
205  * See dr335.c also, which tests the runtime behavior of the part of the DR
206  * which will compile.
207  */
dr335(void)208 void dr335(void) {
209   struct bits_ {
210     _Bool bbf3 : 3; /* expected-error {{width of bit-field 'bbf3' (3 bits) exceeds the width of its type (1 bit)}}
211                        c89only-warning {{'_Bool' is a C99 extension}}
212                      */
213   };
214 }
215 
216 /* WG14 DR339: dup 328
217  * Variably modified compound literals
218  *
219  * This DR is marked as a duplicate of DR328, see that DR for further
220  * details.
221  *
222  * FIXME: we should be diagnosing this compound literal as creating a variably-
223  * modified type at file scope, as we would do for a file scope variable.
224  */
225 extern int dr339_v;
226 void *dr339 = &(int (*)[dr339_v]){ 0 }; /* c89only-warning {{variable length arrays are a C99 feature}}
227                                            c99andup-warning {{variable length array used}}
228                                            c89only-warning {{compound literals are a C99-specific feature}}
229                                          */
230 
231 /* WG14 DR340: yes
232  * Composite types for variable-length arrays
233  *
234  * The DR made this behavior undefined because implementations disagreed on the
235  * behavior. For this DR, Clang accepts the code and GCC rejects it. It's
236  * unclear whether the Clang behavior is intentional, but because the code is
237  * UB, any behavior is acceptable.
238  */
239 #if __STDC_VERSION__ < 202311L
dr340(int x,int y)240 void dr340(int x, int y) {
241   typedef void (*T1)(int);
242   typedef void (*T2)(); /* expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} */
243 
244   T1 (*a)[] = 0;
245   T2 (*b)[x] = 0;       /* c89only-warning {{variable length arrays are a C99 feature}}
246                            c99andup-warning {{variable length array used}}
247                          */
248   (y ? a : b)[0][0]();
249 }
250 #endif /* __STDC_VERSION__ < 202311L */
251 
252 /* WG14 DR341: yes
253  * [*] in abstract declarators
254  */
255 void dr341_1(int (*)[*]);                  /* c89only-warning {{variable length arrays are a C99 feature}}
256                                               c99andup-warning {{variable length array used}}
257                                             */
258 void dr341_2(int (*)[sizeof(int (*)[*])]); /* expected-error {{star modifier used outside of function prototype}} */
259 
260 /* WG14 DR343: yes
261  * Initializing qualified wchar_t arrays
262  */
dr343(void)263 void dr343(void) {
264   const __WCHAR_TYPE__ x[] = L"foo";
265 }
266 
267 /* WG14 DR344: yes
268  * Casts in preprocessor conditional expressions
269  *
270  * Note: this DR removed a constraint about not containing casts because there
271  * are no keywords, therefore no types to cast to, so casts simply don't exist
272  * as a construct during preprocessing.
273  */
274 #if (int)+0
275 #error "this should not be an error, we shouldn't get here"
276 #else
277 /* expected-error@+1 {{"reached"}} */
278 #error "reached"
279 #endif
280 
281 /* WG14 DR345: yes
282  * Where does parameter scope start?
283  */
f(long double f,char (** a)[10* sizeof f])284 void f(long double f,
285        char (**a)[10 * sizeof f]) {
286   _Static_assert(sizeof **a == sizeof(long double) * 10, "");
287 }
288 
289 /* WG14 DR309: yes
290  * Clarifying trigraph substitution
291  */
292 int dr309??(1??) = { 1 }; /* c17andearlier-warning {{trigraph converted to '[' character}}
293                              c17andearlier-warning {{trigraph converted to ']' character}}
294                              c23andup-warning 2 {{trigraph ignored}}
295                              c23andup-error {{expected ';' after top level declarator}}
296                            */
297 
298 /* NOTE: Due to interactions with the diagnostic system, dr309 should be the
299  * last test case in this file because subsequent diagnostics may not be
300  * generated as expected.
301  */
302