]> The Tcpdump Group git mirrors - libpcap/blob - diag-control.h
Squelch Clang warnings about OpenSSL.
[libpcap] / diag-control.h
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3 * Copyright (c) 1993, 1994, 1995, 1996, 1997
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef _diag_control_h
36 #define _diag_control_h
37
38 #include "pcap/compiler-tests.h"
39
40 #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8) || \
41 PCAP_IS_AT_LEAST_GNUC_VERSION(4,6) || \
42 PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
43 /*
44 * All these compilers support this way of putting pragmas into #defines.
45 * We use it only if we have a compiler that supports it; see below
46 * for the code that uses it and the #defines that control whether
47 * that code is used.
48 */
49 #define PCAP_DO_PRAGMA(x) _Pragma (#x)
50 #endif
51
52 /*
53 * Suppress "enum value not explicitly handled in switch" warnings.
54 * We may have to build on multiple different Windows SDKs, so we
55 * may not be able to include all enum values in a switch, as they
56 * won't necessarily be defined on all the SDKs, and, unlike
57 * #defines, there's no easy way to test whether a given enum has
58 * a given value. It *could* be done by the configure script or
59 * CMake tests.
60 */
61 #if defined(_MSC_VER)
62 #define DIAG_OFF_ENUM_SWITCH \
63 __pragma(warning(push)) \
64 __pragma(warning(disable:4061))
65 #define DIAG_ON_ENUM_SWITCH \
66 __pragma(warning(pop))
67 #endif
68
69 /*
70 * Suppress "switch statement has only a default case" warnings.
71 * There's a switch in bpf_filter.c that only has additional
72 * cases on Linux.
73 */
74 #if defined(_MSC_VER)
75 #define DIAG_OFF_DEFAULT_ONLY_SWITCH \
76 __pragma(warning(push)) \
77 __pragma(warning(disable:4065))
78 #define DIAG_ON_DEFAULT_ONLY_SWITCH \
79 __pragma(warning(pop))
80 #endif
81
82 /*
83 * Suppress Flex, narrowing, and deprecation warnings.
84 */
85 #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
86 /*
87 * This is Clang 2.8 or later; we can use "clang diagnostic
88 * ignored -Wxxx" and "clang diagnostic push/pop".
89 *
90 * Suppress -Wdocumentation warnings; GCC doesn't support -Wdocumentation,
91 * at least according to the GCC 7.3 documentation. Apparently, Flex
92 * generates code that upsets at least some versions of Clang's
93 * -Wdocumentation.
94 *
95 * (This could be clang-cl, which defines _MSC_VER, so test this
96 * before testing _MSC_VER.)
97 */
98 #define DIAG_OFF_FLEX \
99 PCAP_DO_PRAGMA(clang diagnostic push) \
100 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wsign-compare") \
101 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdocumentation") \
102 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32") \
103 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wmissing-noreturn") \
104 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunused-parameter") \
105 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
106 #define DIAG_ON_FLEX \
107 PCAP_DO_PRAGMA(clang diagnostic pop)
108
109 /*
110 * Suppress the only narrowing warnings you get from Clang.
111 */
112 #define DIAG_OFF_NARROWING \
113 PCAP_DO_PRAGMA(clang diagnostic push) \
114 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32")
115
116 #define DIAG_ON_NARROWING \
117 PCAP_DO_PRAGMA(clang diagnostic pop)
118
119 /*
120 * Suppress deprecation warnings.
121 */
122 #define DIAG_OFF_DEPRECATION \
123 PCAP_DO_PRAGMA(clang diagnostic push) \
124 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdeprecated-declarations")
125 #define DIAG_ON_DEPRECATION \
126 PCAP_DO_PRAGMA(clang diagnostic pop)
127
128 /*
129 * When Clang correctly detects an old-style function prototype after
130 * preprocessing, the warning can be irrelevant to this source tree because
131 * the prototype comes from a system header macro.
132 */
133 #if PCAP_IS_AT_LEAST_CLANG_VERSION(5,0)
134 #define DIAG_OFF_STRICT_PROTOTYPES \
135 PCAP_DO_PRAGMA(clang diagnostic push) \
136 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wstrict-prototypes")
137 #define DIAG_ON_STRICT_PROTOTYPES \
138 PCAP_DO_PRAGMA(clang diagnostic pop)
139 #endif
140
141 #define DIAG_OFF_DOCUMENTATION \
142 PCAP_DO_PRAGMA(clang diagnostic push) \
143 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdocumentation")
144 #define DIAG_ON_DOCUMENTATION \
145 PCAP_DO_PRAGMA(clang diagnostic pop)
146 #elif defined(_MSC_VER)
147 /*
148 * This is Microsoft Visual Studio; we can use __pragma(warning(disable:XXXX))
149 * and __pragma(warning(push/pop)).
150 *
151 * Suppress signed-vs-unsigned comparison, narrowing, and unreachable
152 * code warnings.
153 */
154 #define DIAG_OFF_FLEX \
155 __pragma(warning(push)) \
156 __pragma(warning(disable:4127)) \
157 __pragma(warning(disable:4242)) \
158 __pragma(warning(disable:4244)) \
159 __pragma(warning(disable:4702))
160 #define DIAG_ON_FLEX \
161 __pragma(warning(pop))
162
163 /*
164 * Suppress narrowing warnings.
165 */
166 #define DIAG_OFF_NARROWING \
167 __pragma(warning(push)) \
168 __pragma(warning(disable:4242)) \
169 __pragma(warning(disable:4311))
170 #define DIAG_ON_NARROWING \
171 __pragma(warning(pop))
172
173 /*
174 * Suppress deprecation warnings.
175 */
176 #define DIAG_OFF_DEPRECATION \
177 __pragma(warning(push)) \
178 __pragma(warning(disable:4996))
179 #define DIAG_ON_DEPRECATION \
180 __pragma(warning(pop))
181 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
182 /*
183 * This is GCC 4.6 or later, or a compiler claiming to be that.
184 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2)
185 * and "GCC diagnostic push/pop" (introduced in 4.6).
186 */
187 #define DIAG_OFF_FLEX \
188 PCAP_DO_PRAGMA(GCC diagnostic push) \
189 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wsign-compare") \
190 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-parameter") \
191 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
192 #define DIAG_ON_FLEX \
193 PCAP_DO_PRAGMA(GCC diagnostic pop)
194
195 /*
196 * GCC currently doesn't issue any narrowing warnings.
197 */
198
199 /*
200 * Suppress deprecation warnings.
201 */
202 #define DIAG_OFF_DEPRECATION \
203 PCAP_DO_PRAGMA(GCC diagnostic push) \
204 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
205 #define DIAG_ON_DEPRECATION \
206 PCAP_DO_PRAGMA(GCC diagnostic pop)
207
208 /*
209 * Suppress format-truncation= warnings.
210 * GCC 7.1 had introduced this warning option. Earlier versions (at least
211 * one particular copy of GCC 4.6.4) treat the request as a warning.
212 */
213 #if PCAP_IS_AT_LEAST_GNUC_VERSION(7,1)
214 #define DIAG_OFF_FORMAT_TRUNCATION \
215 PCAP_DO_PRAGMA(GCC diagnostic push) \
216 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wformat-truncation=")
217 #define DIAG_ON_FORMAT_TRUNCATION \
218 PCAP_DO_PRAGMA(GCC diagnostic pop)
219 #endif
220 #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
221 /*
222 * Sun C compiler version 5.5 (Studio version 8) and later supports "#pragma
223 * error_messages()".
224 */
225 #define DIAG_OFF_FLEX \
226 PCAP_DO_PRAGMA(error_messages(off,E_STATEMENT_NOT_REACHED))
227 #define DIAG_ON_FLEX \
228 PCAP_DO_PRAGMA(error_messages(default,E_STATEMENT_NOT_REACHED))
229 #endif
230
231 #ifdef YYBYACC
232 /*
233 * Berkeley YACC.
234 *
235 * It generates a global declaration of yylval, or the appropriately
236 * prefixed version of yylval, in grammar.h, *even though it's been
237 * told to generate a pure parser, meaning it doesn't have any global
238 * variables*. Bison doesn't do this.
239 *
240 * That causes a warning due to the local declaration in the parser
241 * shadowing the global declaration.
242 *
243 * So, if the compiler warns about that, we turn off -Wshadow warnings.
244 *
245 * In addition, the generated code may have functions with unreachable
246 * code, so suppress warnings about those.
247 */
248 #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
249 /*
250 * This is Clang 2.8 or later (including clang-cl, so test this
251 * before _MSC_VER); we can use "clang diagnostic ignored -Wxxx".
252 */
253 #define DIAG_OFF_BISON_BYACC \
254 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshadow") \
255 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
256 #elif defined(_MSC_VER)
257 /*
258 * This is Microsoft Visual Studio; we can use
259 * __pragma(warning(disable:XXXX)).
260 */
261 #define DIAG_OFF_BISON_BYACC \
262 __pragma(warning(disable:4702))
263 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
264 /*
265 * This is GCC 4.6 or later, or a compiler claiming to be that.
266 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
267 * but it may not actually work very well prior to 4.6).
268 */
269 #define DIAG_OFF_BISON_BYACC \
270 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wshadow") \
271 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
272 #endif
273 #else
274 /*
275 * Bison.
276 *
277 * The generated code may have functions with unreachable code and
278 * switches with only a default case, so suppress warnings about those.
279 */
280 #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
281 /*
282 * This is Clang 2.8 or later (including clang-cl, so test this
283 * before _MSC_VER); we can use "clang diagnostic ignored -Wxxx".
284 */
285 #define DIAG_OFF_BISON_BYACC \
286 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
287 #elif defined(_MSC_VER)
288 /*
289 * This is Microsoft Visual Studio; we can use
290 * __pragma(warning(disable:XXXX)).
291 *
292 * Suppress some /Wall warnings.
293 */
294 #define DIAG_OFF_BISON_BYACC \
295 __pragma(warning(disable:4065)) \
296 __pragma(warning(disable:4127)) \
297 __pragma(warning(disable:4242)) \
298 __pragma(warning(disable:4244)) \
299 __pragma(warning(disable:4702))
300 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
301 /*
302 * This is GCC 4.6 or later, or a compiler claiming to be that.
303 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
304 * but it may not actually work very well prior to 4.6).
305 */
306 #define DIAG_OFF_BISON_BYACC \
307 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
308 #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
309 /*
310 * Same as for DIAG_OFF_FLEX above.
311 */
312 #define DIAG_OFF_BISON_BYACC \
313 PCAP_DO_PRAGMA(error_messages(off,E_STATEMENT_NOT_REACHED))
314 #endif
315 #endif
316
317 #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
318 /*
319 * Clang appears to let you ignore a result without a warning by
320 * casting the function result to void, so we don't appear to
321 * need this for Clang.
322 */
323 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,5)
324 /*
325 * GCC warns about unused return values if a function is marked as
326 * "warn about ignoring this function's return value".
327 */
328 #define DIAG_OFF_WARN_UNUSED_RESULT \
329 PCAP_DO_PRAGMA(GCC diagnostic push) \
330 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-result")
331 #define DIAG_ON_WARN_UNUSED_RESULT \
332 PCAP_DO_PRAGMA(GCC diagnostic pop)
333
334 /*
335 * GCC does not currently generate any -Wstrict-prototypes warnings that
336 * would need silencing as is done for Clang above.
337 */
338 #endif
339
340 /*
341 * GCC needs this on AIX for longjmp().
342 */
343 #if PCAP_IS_AT_LEAST_GNUC_VERSION(5,1)
344 /*
345 * Beware that the effect of this builtin is more than just squelching the
346 * warning! GCC trusts it enough for the process to segfault if the control
347 * flow reaches the builtin (an infinite empty loop in the same context would
348 * squelch the warning and ruin the process too, albeit in a different way).
349 * So please remember to use this very carefully.
350 */
351 #define PCAP_UNREACHABLE __builtin_unreachable();
352 #endif
353
354 #ifndef DIAG_OFF_ENUM_SWITCH
355 #define DIAG_OFF_ENUM_SWITCH
356 #endif
357 #ifndef DIAG_ON_ENUM_SWITCH
358 #define DIAG_ON_ENUM_SWITCH
359 #endif
360 #ifndef DIAG_OFF_DEFAULT_ONLY_SWITCH
361 #define DIAG_OFF_DEFAULT_ONLY_SWITCH
362 #endif
363 #ifndef DIAG_ON_DEFAULT_ONLY_SWITCH
364 #define DIAG_ON_DEFAULT_ONLY_SWITCH
365 #endif
366 #ifndef DIAG_OFF_FLEX
367 #define DIAG_OFF_FLEX
368 #endif
369 #ifndef DIAG_ON_FLEX
370 #define DIAG_ON_FLEX
371 #endif
372 #ifndef DIAG_OFF_NARROWING
373 #define DIAG_OFF_NARROWING
374 #endif
375 #ifndef DIAG_ON_NARROWING
376 #define DIAG_ON_NARROWING
377 #endif
378 #ifndef DIAG_OFF_DEPRECATION
379 #define DIAG_OFF_DEPRECATION
380 #endif
381 #ifndef DIAG_ON_DEPRECATION
382 #define DIAG_ON_DEPRECATION
383 #endif
384 #ifndef DIAG_OFF_FORMAT_TRUNCATION
385 #define DIAG_OFF_FORMAT_TRUNCATION
386 #endif
387 #ifndef DIAG_ON_FORMAT_TRUNCATION
388 #define DIAG_ON_FORMAT_TRUNCATION
389 #endif
390 #ifndef DIAG_OFF_BISON_BYACC
391 #define DIAG_OFF_BISON_BYACC
392 #endif
393 //
394 // DIAG_ON_BISON_BYACC does not need to be defined.
395 //
396 #ifndef DIAG_OFF_WARN_UNUSED_RESULT
397 #define DIAG_OFF_WARN_UNUSED_RESULT
398 #endif
399 #ifndef DIAG_ON_WARN_UNUSED_RESULT
400 #define DIAG_ON_WARN_UNUSED_RESULT
401 #endif
402 #ifndef DIAG_OFF_STRICT_PROTOTYPES
403 #define DIAG_OFF_STRICT_PROTOTYPES
404 #endif
405 #ifndef DIAG_ON_STRICT_PROTOTYPES
406 #define DIAG_ON_STRICT_PROTOTYPES
407 #endif
408 #ifndef DIAG_OFF_DOCUMENTATION
409 #define DIAG_OFF_DOCUMENTATION
410 #endif
411 #ifndef DIAG_ON_DOCUMENTATION
412 #define DIAG_ON_DOCUMENTATION
413 #endif
414 #ifndef PCAP_UNREACHABLE
415 #define PCAP_UNREACHABLE
416 #endif
417
418 #endif /* _diag_control_h */