1 // RUN: %clang_cc1 -fsyntax-only -Wdocumentation -Wmissing-prototypes -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -Wdocumentation -Wmissing-prototypes -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 
46 // rdar://6759522
main(void)47 int main(void) { return 0; }
48 
49 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}}
50 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:27-[[@LINE-1]]:27}:"void"
not_a_prototype_test()51 void not_a_prototype_test() { } // expected-warning{{no previous prototype for function 'not_a_prototype_test'}}
52 
get_const()53 const int *get_const() { // expected-warning{{no previous prototype for function 'get_const'}}
54   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
55   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
56   return (void *)0;
57 }
58 
59 struct MyStruct {};
60 
get_struct()61 const struct MyStruct get_struct() { // expected-warning{{no previous prototype for function 'get_struct'}}
62   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
63   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
64   struct MyStruct ret;
65   return ret;
66 }
67 
68 // Turn off clang-format for white-space dependent testing.
69 // clang-format off
70 // Two spaces between cost and struct
get_struct_2()71 const  struct MyStruct get_struct_2() {  // expected-warning{{no previous prototype for function 'get_struct_2'}}
72   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
73   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
74   struct MyStruct ret;
75   return ret;
76 }
77 
78 // Two spaces bewteen const and struct
get_struct_ptr()79 const  struct MyStruct* get_struct_ptr() {  // expected-warning{{no previous prototype for function 'get_struct_ptr'}}
80   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
81   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
82   return (void*)0;
83 }
84 
85 // Random comment before const.
get_struct_3()86 /*some randome comment*/const  struct MyStruct* get_struct_3() {  // expected-warning{{no previous prototype for function 'get_struct_3'}}
87   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
88   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:"static "
89   return (void*)0;
90 }
91 
92 // Random comment after const.
get_struct_4()93 const/*some randome comment*/ struct MyStruct* get_struct_4() {  // expected-warning{{no previous prototype for function 'get_struct_4'}}
94   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
95   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:1-[[@LINE-2]]:1}:"static "
96   return (void*)0;
97 }
98 // clang-format on
99 
100 #define MY_CONST const
101 
102 // Since we can't easily understand what MY_CONST means while preparing the
103 // diagnostic, the fix-it suggests to add 'static' in a non-idiomatic place.
get_struct_nyi()104 MY_CONST struct MyStruct *get_struct_nyi() { // expected-warning{{no previous prototype for function 'get_struct_nyi'}}
105   // expected-note@-1{{declare 'static' if the function is not intended to be used outside of this translation unit}}
106   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"static "
107   return (void *)0;
108 }
109