1 //===--- funcptrs.cc - test input file for iwyu ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // Tests that function pointers make the right claims for involved types.
11 // Function pointer expressions come in three flavors:
12 //
13 // 1) Assignments: int (*fptr)(int) = function;
14 // 2) Calls: FunctionThatTakesFptr(function);
15 // 3) Naked expressions: &function;
16 //
17 // A 'function' can be a free function, a static member function, a member
18 // function, or any template instantiation of the above.
19
20 #include "tests/cxx/funcptrs-d1.h"
21
22
23 // Functions to drive call-syntax.
24
25 // IWYU: Class needs a declaration
26 // IWYU: Enum is...*funcptrs-i1.h
27 void FunctionThatTakesFptr(Enum (*fptr)(Class*));
28
29 // IWYU: Class needs a declaration
30 void FunctionThatTakesFptr(int (*fptr)(Class*));
31
32 void FunctionThatTakesFptr(int (*fptr)());
33
34 // IWYU: Class needs a declaration
35 void FunctionThatTakesMptr(int (Class::*mptr)() const);
36
37 // IWYU: ClassTemplate needs a declaration
38 // IWYU: Class needs a declaration
39 void FunctionThatTakesMptr(int (ClassTemplate<Class>::*mptr)() const);
40
41
42 // Test cases below.
43 // Note that we test primarily the diagnostics from IWYU for the individual
44 // constructs, not which header is chosen -- all relevant types are in
45 // funcptrs-i1.h anyway.
46 //
47 // Each test creates function pointers to a plain function and a template
48 // instantiation, and for classes similarly for instance member functions.
49
FreeFunctions()50 void FreeFunctions() {
51 // Assignment of function pointer to function and template instantiation.
52 // IWYU: Class needs a declaration
53 // IWYU: Enum is...*funcptrs-i1.h
54 // IWYU: Function is...*funcptrs-i1.h
55 Enum (*fptr)(Class*) = &Function;
56
57 // IWYU: Class needs a declaration
58 // IWYU: Retval needs a declaration
59 // IWYU: Retval is...*funcptrs-i1.h
60 // IWYU: FunctionTemplate is...*funcptrs-i1.h
61 int (*template_fptr)(Class*) = &FunctionTemplate<Retval>;
62
63 // Call with function pointer to function and template instantiation.
64 // IWYU: Function is...*funcptrs-i1.h
65 FunctionThatTakesFptr(Function);
66
67 // IWYU: Retval needs a declaration
68 // IWYU: Retval is...*funcptrs-i1.h
69 // IWYU: FunctionTemplate is...*funcptrs-i1.h
70 FunctionThatTakesFptr(FunctionTemplate<Retval>);
71
72 // Naked function pointer expressions
73 // IWYU: Function is...*funcptrs-i1.h
74 &Function;
75
76 // IWYU: Retval needs a declaration
77 // IWYU: Retval is...*funcptrs-i1.h
78 // IWYU: FunctionTemplate is...*funcptrs-i1.h
79 &FunctionTemplate<Retval>;
80 }
81
ClassMembers()82 void ClassMembers() {
83 // IWYU: Class is...*funcptrs-i1.h
84 int (*static_method_ptr)() = &Class::StaticMemberFunction;
85
86 // IWYU: Class is...*funcptrs-i1.h
87 // IWYU: Retval needs a declaration
88 // IWYU: Retval is...*funcptrs-i1.h
89 int (*static_template_method_ptr)() = &Class::StaticMemberTemplate<Retval>;
90
91 // IWYU: Class is...*funcptrs-i1.h
92 int (Class::*method_ptr)() const = &Class::MemberFunction;
93
94 // IWYU: Class is...*funcptrs-i1.h
95 // IWYU: Retval needs a declaration
96 // IWYU: Retval is...*funcptrs-i1.h
97 int (Class::*template_method_ptr)() const = &Class::MemberTemplate<Retval>;
98
99 // Call with pointers to static member function and template instantiation.
100 // IWYU: Class is...*funcptrs-i1.h
101 FunctionThatTakesFptr(Class::StaticMemberFunction);
102
103 // IWYU: Retval needs a declaration
104 // IWYU: Retval is...*funcptrs-i1.h
105 // IWYU: Class is...*funcptrs-i1.h
106 FunctionThatTakesFptr(Class::StaticMemberTemplate<Retval>);
107
108 // Call with pointers to instance member function and template instantiation.
109 // IWYU: Class is...*funcptrs-i1.h
110 FunctionThatTakesMptr(&Class::MemberFunction);
111
112 // IWYU: Retval needs a declaration
113 // IWYU: Retval is...*funcptrs-i1.h
114 // IWYU: Class is...*funcptrs-i1.h
115 FunctionThatTakesMptr(&Class::MemberTemplate<Retval>);
116
117 // Naked function pointer expressions
118 // IWYU: Class is...*funcptrs-i1.h
119 &Class::StaticMemberFunction;
120
121 // IWYU: Retval needs a declaration
122 // IWYU: Retval is...*funcptrs-i1.h
123 // IWYU: Class is...*funcptrs-i1.h
124 &Class::StaticMemberTemplate<Retval>;
125
126 // IWYU: Class is...*funcptrs-i1.h
127 &Class::MemberFunction;
128
129 // IWYU: Retval needs a declaration
130 // IWYU: Retval is...*funcptrs-i1.h
131 // IWYU: Class is...*funcptrs-i1.h
132 &Class::MemberTemplate<Retval>;
133 }
134
ClassTemplateMembers()135 void ClassTemplateMembers() {
136 // IWYU: ClassTemplate is...*funcptrs-i1.h
137 // IWYU: Class needs a declaration
138 int (*static_method_ptr)() = &ClassTemplate<Class>::StaticMemberFunction;
139
140 int (*static_template_method_ptr)() =
141 // IWYU: ClassTemplate is...*funcptrs-i1.h
142 // IWYU: Class needs a declaration
143 // IWYU: Retval needs a declaration
144 // IWYU: Retval is...*funcptrs-i1.h
145 &ClassTemplate<Class>::StaticMemberTemplate<Retval>;
146
147 // IWYU: ClassTemplate is...*funcptrs-i1.h
148 // IWYU: Class needs a declaration
149 int (ClassTemplate<Class>::*method_ptr)() const =
150 // IWYU: ClassTemplate is...*funcptrs-i1.h
151 // IWYU: Class needs a declaration
152 &ClassTemplate<Class>::MemberFunction;
153
154 // IWYU: ClassTemplate is...*funcptrs-i1.h
155 // IWYU: Class needs a declaration
156 int (ClassTemplate<Class>::*template_method_ptr)() const =
157 // IWYU: ClassTemplate is...*funcptrs-i1.h
158 // IWYU: Class needs a declaration
159 // IWYU: Retval needs a declaration
160 // IWYU: Retval is...*funcptrs-i1.h
161 &ClassTemplate<Class>::MemberTemplate<Retval>;
162
163 // Call with pointers to static member function and template instantiation.
164 // IWYU: ClassTemplate is...*funcptrs-i1.h
165 // IWYU: Class needs a declaration
166 FunctionThatTakesFptr(ClassTemplate<Class>::StaticMemberFunction);
167
168 // IWYU: Retval needs a declaration
169 // IWYU: Retval is...*funcptrs-i1.h
170 // IWYU: ClassTemplate is...*funcptrs-i1.h
171 // IWYU: Class needs a declaration
172 FunctionThatTakesFptr(ClassTemplate<Class>::StaticMemberTemplate<Retval>);
173
174 // Call with pointers to instance member function and template instantiation.
175 // IWYU: ClassTemplate is...*funcptrs-i1.h
176 // IWYU: Class needs a declaration
177 FunctionThatTakesMptr(&ClassTemplate<Class>::MemberFunction);
178
179 // IWYU: Retval needs a declaration
180 // IWYU: Retval is...*funcptrs-i1.h
181 // IWYU: ClassTemplate is...*funcptrs-i1.h
182 // IWYU: Class needs a declaration
183 FunctionThatTakesMptr(&ClassTemplate<Class>::MemberTemplate<Retval>);
184
185 // Naked class template member function pointer expressions
186 // IWYU: ClassTemplate is...*funcptrs-i1.h
187 // IWYU: Class needs a declaration
188 &ClassTemplate<Class>::StaticMemberFunction;
189
190 // IWYU: Retval needs a declaration
191 // IWYU: Retval is...*funcptrs-i1.h
192 // IWYU: ClassTemplate is...*funcptrs-i1.h
193 // IWYU: Class needs a declaration
194 &ClassTemplate<Class>::StaticMemberTemplate<Retval>;
195
196 // IWYU: ClassTemplate is...*funcptrs-i1.h
197 // IWYU: Class needs a declaration
198 &ClassTemplate<Class>::MemberFunction;
199
200 // IWYU: Retval needs a declaration
201 // IWYU: Retval is...*funcptrs-i1.h
202 // IWYU: ClassTemplate is...*funcptrs-i1.h
203 // IWYU: Class needs a declaration
204 &ClassTemplate<Class>::MemberTemplate<Retval>;
205 }
206
207 /**** IWYU_SUMMARY
208
209 tests/cxx/funcptrs.cc should add these lines:
210 #include "tests/cxx/funcptrs-i1.h"
211
212 tests/cxx/funcptrs.cc should remove these lines:
213 - #include "tests/cxx/funcptrs-d1.h" // lines XX-XX
214
215 The full include-list for tests/cxx/funcptrs.cc:
216 #include "tests/cxx/funcptrs-i1.h" // for Class, ClassTemplate, Enum, Function, FunctionTemplate, Retval
217
218 ***** IWYU_SUMMARY */
219