xref: /llvm-project/clang/test/Sema/warn-strict-prototypes.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -triple i386-pc-unknown -fsyntax-only -Wstrict-prototypes -Wno-implicit-function-declaration -Wno-error=implicit-int -verify %s
2 // RUN: %clang_cc1 -triple i386-pc-unknown -fsyntax-only -Wstrict-prototypes -Wno-implicit-function-declaration -Wno-error=implicit-int -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
3 
4 // function definition with 0 params, no prototype, no preceding declaration.
foo0()5 void foo0() {} // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
6 
7 // function declaration with unspecified params
8 void foo1(); // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
9              // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:11}:"void"
10 // function declaration with 0 params
11 void foo2(void);
12 
13 // function definition with 0 params, no prototype.
foo1()14 void foo1() {} // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
15 // function definition with 0 params, prototype.
foo2(void)16 void foo2(void) {}
17 
18 // function type typedef unspecified params
19 typedef void foo3(); // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
20                      // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:19-[[@LINE-1]]:19}:"void"
21 
22 // global fp unspecified params
23 void (*foo4)(); // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
24                 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:14}:"void"
25 
26 // struct member fp unspecified params
27 struct { void (*foo5)(); } s; // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
28                               // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:23-[[@LINE-1]]:23}:"void"
29 
30 // param fp unspecified params
bar2(void (* foo6)())31 void bar2(void (*foo6)()) { // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
32                             // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:24-[[@LINE-1]]:24}:"void"
33   // local fp unspecified params
34   void (*foo7)() = 0; // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
35                       // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"void"
36   // array fp unspecified params
37   void (*foo8[2])() = {0}; // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
38                            // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:19-[[@LINE-1]]:19}:"void"
39 }
40 
41 // function type cast using using an anonymous function declaration
bar3(void)42 void bar3(void) {
43   // casting function w/out prototype to unspecified params function type
44   (void)(void(*)()) foo1; // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}}
45                           // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:18-[[@LINE-1]]:18}:"void"
46   // .. specified params
47   (void)(void(*)(void)) foo1;
48 }
49 
50 // K&R function definition not preceded by full prototype
foo9(a,b)51 int foo9(a, b) // expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}}
52   int a, b;
53 {
54   return a + b;
55 }
56 
57 // Function declaration with no types
58 void foo10(); // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} \
59                  expected-warning {{a function declaration without a prototype is deprecated in all versions of C and is treated as a zero-parameter prototype in C23, conflicting with a subsequent definition}}
60               // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"void"
61 // K&R function definition with incomplete param list declared
foo10(p,p2)62 void foo10(p, p2) void *p; {} // expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} \
63                                  expected-warning {{parameter 'p2' was not declared, defaults to 'int'; ISO C99 and later do not support implicit int}}
64 
65 void foo11(int p, int p2);
foo11(p,p2)66 void foo11(p, p2) int p; int p2; {} // expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}}
67 
68 // PR31020
foo12(d)69 void __attribute__((cdecl)) foo12(d) // expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}}
70   short d;
71 {}
72 
73 // No warnings for variadic functions. Overloadable attribute is required
74 // to avoid err_ellipsis_first_param error.
75 void foo13(...) __attribute__((overloadable));
foo13(...)76 void foo13(...) __attribute__((overloadable)) {}
77 
78 // We should not generate a strict-prototype warning for an implicit
79 // declaration.  Leave that up to the implicit-function-declaration warning.
foo14(void)80 void foo14(void) {
81   foo14_call(); // no-warning
82 }
83 
84 // Ensure that redeclarations involving a typedef type work properly, even if
85 // there are function attributes involved in the declaration.
86 typedef void foo_t(unsigned val);
87 __attribute__((noreturn)) foo_t foo15;
88 foo_t foo15; // OK
89 void foo15(unsigned val); // OK
90 
91 foo_t foo16;
92 void foo16(unsigned val); // OK
93