xref: /aosp_15_r20/external/clang/test/Sema/c89.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li /* RUN: %clang_cc1 %s -std=c89 -pedantic -fsyntax-only -verify -Wimplicit-function-declaration
2*67e74705SXin Li  */
test1()3*67e74705SXin Li void test1() {
4*67e74705SXin Li   {
5*67e74705SXin Li     int i;
6*67e74705SXin Li     i = i + 1;
7*67e74705SXin Li     int j;          /* expected-warning {{mixing declarations and code}} */
8*67e74705SXin Li   }
9*67e74705SXin Li   {
10*67e74705SXin Li     __extension__ int i;
11*67e74705SXin Li     i = i + 1;
12*67e74705SXin Li     int j;          /* expected-warning {{mixing declarations and code}} */
13*67e74705SXin Li   }
14*67e74705SXin Li   {
15*67e74705SXin Li     int i;
16*67e74705SXin Li     i = i + 1;
17*67e74705SXin Li     __extension__ int j; /* expected-warning {{mixing declarations and code}} */
18*67e74705SXin Li   }
19*67e74705SXin Li }
20*67e74705SXin Li 
21*67e74705SXin Li long long test2;   /* expected-warning {{extension}} */
22*67e74705SXin Li 
23*67e74705SXin Li 
test3(int i)24*67e74705SXin Li void test3(int i) {
25*67e74705SXin Li   int A[i];        /* expected-warning {{variable length array}} */
26*67e74705SXin Li }
27*67e74705SXin Li 
28*67e74705SXin Li int test4 = 0LL;   /* expected-warning {{long long}} */
29*67e74705SXin Li 
30*67e74705SXin Li /* PR1999 */
31*67e74705SXin Li void test5(register);
32*67e74705SXin Li 
33*67e74705SXin Li /* PR2041 */
34*67e74705SXin Li int *restrict;
35*67e74705SXin Li int *__restrict;  /* expected-error {{expected identifier}} */
36*67e74705SXin Li 
37*67e74705SXin Li 
38*67e74705SXin Li /* Implicit int, always ok */
test6()39*67e74705SXin Li test6() { return 0; }
40*67e74705SXin Li 
41*67e74705SXin Li /* PR2012 */
42*67e74705SXin Li test7;  /* expected-warning {{declaration specifier missing, defaulting to 'int'}} */
43*67e74705SXin Li 
44*67e74705SXin Li void test8(int, x);  /* expected-warning {{declaration specifier missing, defaulting to 'int'}} */
45*67e74705SXin Li 
46*67e74705SXin Li typedef int sometype;
a(sometype,y)47*67e74705SXin Li int a(sometype, y) {return 0;}  /* expected-warning {{declaration specifier missing, defaulting to 'int'}} \
48*67e74705SXin Li                                    expected-error {{parameter name omitted}}*/
49*67e74705SXin Li 
50*67e74705SXin Li 
51*67e74705SXin Li 
52*67e74705SXin Li 
53*67e74705SXin Li void bar (void *);
f11(z)54*67e74705SXin Li void f11 (z)       /* expected-error {{may not have 'void' type}} */
55*67e74705SXin Li void z;
56*67e74705SXin Li { bar (&z); }
57*67e74705SXin Li 
58*67e74705SXin Li typedef void T;
59*67e74705SXin Li void foo(T); /* typedef for void is allowed */
60*67e74705SXin Li 
foo(void)61*67e74705SXin Li void foo(void) {}
62*67e74705SXin Li 
63*67e74705SXin Li /* PR2759 */
64*67e74705SXin Li void test10 (int x[*]); /* expected-warning {{variable length arrays are a C99 feature}} */
65*67e74705SXin Li void test11 (int x[static 4]); /* expected-warning {{static array size is a C99 feature}} */
66*67e74705SXin Li 
test12(int x[const4])67*67e74705SXin Li void test12 (int x[const 4]) { /* expected-warning {{qualifier in array size is a C99 feature}} */
68*67e74705SXin Li   int Y[x[1]]; /* expected-warning {{variable length arrays are a C99 feature}} */
69*67e74705SXin Li }
70*67e74705SXin Li 
71*67e74705SXin Li /* PR4074 */
72*67e74705SXin Li struct test13 {
73*67e74705SXin Li   int X[23];
74*67e74705SXin Li } test13a();
75*67e74705SXin Li 
test13b()76*67e74705SXin Li void test13b() {
77*67e74705SXin Li   int a = test13a().X[1]; /* expected-warning {{ISO C90 does not allow subscripting non-lvalue array}} */
78*67e74705SXin Li   int b = 1[test13a().X]; /* expected-warning {{ISO C90 does not allow subscripting non-lvalue array}} */
79*67e74705SXin Li }
80*67e74705SXin Li 
81*67e74705SXin Li /* Make sure we allow *test14 as a "function designator" */
test14()82*67e74705SXin Li int test14() { return (&*test14)(); }
83*67e74705SXin Li 
84*67e74705SXin Li int test15[5] = { [2] = 1 }; /* expected-warning {{designated initializers are a C99 feature}} */
85*67e74705SXin Li 
86*67e74705SXin Li extern int printf(__const char *__restrict __format, ...);
87*67e74705SXin Li 
88*67e74705SXin Li /* Warn, but don't suggest typo correction. */
test16()89*67e74705SXin Li void test16() {
90*67e74705SXin Li   printg("Hello, world!\n"); /* expected-warning {{implicit declaration of function 'printg'}} */
91*67e74705SXin Li }
92*67e74705SXin Li 
93*67e74705SXin Li struct x { int x,y[]; }; /* expected-warning {{flexible array members are a C99 feature}} */
94*67e74705SXin Li 
95*67e74705SXin Li /* Duplicated type-qualifiers aren't allowed by C90 */
96*67e74705SXin Li const const int c_i; /* expected-warning {{duplicate 'const' declaration specifier}} */
97*67e74705SXin Li typedef volatile int vol_int;
98*67e74705SXin Li volatile vol_int volvol_i; /* expected-warning {{duplicate 'volatile' declaration specifier}} */
99*67e74705SXin Li typedef volatile vol_int volvol_int; /* expected-warning {{duplicate 'volatile' declaration specifier}} */
100*67e74705SXin Li const int * const c;
101*67e74705SXin Li 
102*67e74705SXin Li typedef const int CI;
103*67e74705SXin Li 
104*67e74705SXin Li const CI mine1[5][5]; /* expected-warning {{duplicate 'const' declaration specifier}} */
105*67e74705SXin Li 
106*67e74705SXin Li typedef CI array_of_CI[5];
107*67e74705SXin Li const array_of_CI mine2; /* expected-warning {{duplicate 'const' declaration specifier}} */
108*67e74705SXin Li 
109*67e74705SXin Li typedef CI *array_of_pointer_to_CI[5];
110*67e74705SXin Li const array_of_pointer_to_CI mine3;
111*67e74705SXin Li 
main()112*67e74705SXin Li void main() {} /* expected-error {{'main' must return 'int'}} */
113*67e74705SXin Li 
main()114*67e74705SXin Li const int main() {} /* expected-error {{'main' must return 'int'}} */
115*67e74705SXin Li 
116*67e74705SXin Li long long ll1 = /* expected-warning {{'long long' is an extension when C99 mode is not enabled}} */
117*67e74705SXin Li          -42LL; /* expected-warning {{'long long' is an extension when C99 mode is not enabled}} */
118*67e74705SXin Li unsigned long long ull1 = /* expected-warning {{'long long' is an extension when C99 mode is not enabled}} */
119*67e74705SXin Li                    42ULL; /* expected-warning {{'long long' is an extension when C99 mode is not enabled}} */
120*67e74705SXin Li 
121*67e74705SXin Li struct Test17 { int a; };
122*67e74705SXin Li struct Test17 test17_aux(void);
123*67e74705SXin Li 
test17(int v,int w)124*67e74705SXin Li void test17(int v, int w) {
125*67e74705SXin Li   int a[2] = { v, w }; /* expected-warning {{initializer for aggregate is not a compile-time constant}} */
126*67e74705SXin Li   struct Test17 t0 = { v }; /* expected-warning {{initializer for aggregate is not a compile-time constant}} */
127*67e74705SXin Li   struct Test17 t1 = test17_aux(); /* this is allowed */
128*67e74705SXin Li }
129*67e74705SXin Li 
130