xref: /llvm-project/clang/test/Sema/warn-missing-prototypes.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -fsyntax-only -Wdocumentation -Wmissing-prototypes -Wno-deprecated-non-prototype -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -Wdocumentation -Wmissing-prototypes -Wno-deprecated-non-prototype -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
3 
4 int f(); // expected-note{{this declaration is not a prototype; add parameter declarations to make it one}}
5 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:{{.*}}-[[@LINE-1]]:{{.*}}}:"{{.*}}"
6 
f(int x)7 int f(int x) { return x; } // expected-warning{{no previous prototype for function 'f'}}
8 
g(int x)9 static int g(int x) { return x; }
10 
h(int x)11 int h(int x) { return x; } // expected-warning{{no previous prototype for function 'h'}}
12 // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
13 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
14 
15 static int g2();
16 
g2(int x)17 int g2(int x) { return x; }
18 
g3(int x)19 extern int g3(int x) { return x; } // expected-warning{{no previous prototype for function 'g3'}}
20 // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
21 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:{{.*}}-[[@LINE-2]]:{{.*}}}:"{{.*}}"
22 
23 void test(void);
24 
25 int h3(); // expected-note{{this declaration is not a prototype; add parameter declarations to make it one}}
26 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:{{.*}}-[[@LINE-1]]:{{.*}}}:"{{.*}}"
27 int h4(int);
28 int h4();
29 
test(void)30 void test(void) {
31   int h2(int x);
32   int h3(int x);
33   int h4();
34 }
35 
h2(int x)36 int h2(int x) { return x; } // expected-warning{{no previous prototype for function 'h2'}}
37 // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
h3(int x)38 int h3(int x) { return x; } // expected-warning{{no previous prototype for function 'h3'}}
h4(int x)39 int h4(int x) { return x; }
40 
41 int f2(int);
42 int f2();
43 
f2(int x)44 int f2(int x) { return x; }
45 
main(void)46 int main(void) { return 0; }
47 
48 void not_a_prototype_test(); // expected-note{{this declaration is not a prototype; add 'void' to make it a prototype for a zero-parameter function}}
49 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:27-[[@LINE-1]]:27}:"void"
not_a_prototype_test()50 void not_a_prototype_test() { } // expected-warning{{no previous prototype for function 'not_a_prototype_test'}}
51 
get_const()52 const int *get_const() { // expected-warning{{no previous prototype for function 'get_const'}}
53   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
54   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
55   return (void *)0;
56 }
57 
58 struct MyStruct {};
59 
get_struct()60 const struct MyStruct get_struct() { // expected-warning{{no previous prototype for function 'get_struct'}}
61   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
62   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
63   struct MyStruct ret;
64   return ret;
65 }
66 
67 // Turn off clang-format for white-space dependent testing.
68 // clang-format off
69 // Two spaces between cost and struct
get_struct_2()70 const  struct MyStruct get_struct_2() {  // expected-warning{{no previous prototype for function 'get_struct_2'}}
71   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
72   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
73   struct MyStruct ret;
74   return ret;
75 }
76 
77 // Two spaces bewteen const and struct
get_struct_ptr()78 const  struct MyStruct* get_struct_ptr() {  // expected-warning{{no previous prototype for function 'get_struct_ptr'}}
79   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
80   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
81   return (void*)0;
82 }
83 
84 // Random comment before const.
get_struct_3()85 /*some randome comment*/const  struct MyStruct* get_struct_3() {  // expected-warning{{no previous prototype for function 'get_struct_3'}}
86   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
87   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:"static "
88   return (void*)0;
89 }
90 
91 // Random comment after const.
get_struct_4()92 const/*some randome comment*/ struct MyStruct* get_struct_4() {  // expected-warning{{no previous prototype for function 'get_struct_4'}}
93   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
94   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
95   return (void*)0;
96 }
97 // clang-format on
98 
99 #define MY_CONST const
100 
101 // Since we can't easily understand what MY_CONST means while preparing the
102 // diagnostic, the fix-it suggests to add 'static' in a non-idiomatic place.
get_struct_nyi()103 MY_CONST struct MyStruct *get_struct_nyi() { // expected-warning{{no previous prototype for function 'get_struct_nyi'}}
104   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
105   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"static "
106   return (void *)0;
107 }
108