]> The Tcpdump Group git mirrors - libpcap/blob - diag-control.h
f4edc8d56a63ee592303fba809d3cc4500d16efd
[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 #elif defined(_MSC_VER)
141 /*
142 * This is Microsoft Visual Studio; we can use __pragma(warning(disable:XXXX))
143 * and __pragma(warning(push/pop)).
144 *
145 * Suppress signed-vs-unsigned comparison, narrowing, and unreachable
146 * code warnings.
147 */
148 #define DIAG_OFF_FLEX \
149 __pragma(warning(push)) \
150 __pragma(warning(disable:4127)) \
151 __pragma(warning(disable:4242)) \
152 __pragma(warning(disable:4244)) \
153 __pragma(warning(disable:4702))
154 #define DIAG_ON_FLEX \
155 __pragma(warning(pop))
156
157 /*
158 * Suppress narrowing warnings.
159 */
160 #define DIAG_OFF_NARROWING \
161 __pragma(warning(push)) \
162 __pragma(warning(disable:4242)) \
163 __pragma(warning(disable:4311))
164 #define DIAG_ON_NARROWING \
165 __pragma(warning(pop))
166
167 /*
168 * Suppress deprecation warnings.
169 */
170 #define DIAG_OFF_DEPRECATION \
171 __pragma(warning(push)) \
172 __pragma(warning(disable:4996))
173 #define DIAG_ON_DEPRECATION \
174 __pragma(warning(pop))
175 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
176 /*
177 * This is GCC 4.6 or later, or a compiler claiming to be that.
178 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2)
179 * and "GCC diagnostic push/pop" (introduced in 4.6).
180 */
181 #define DIAG_OFF_FLEX \
182 PCAP_DO_PRAGMA(GCC diagnostic push) \
183 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wsign-compare") \
184 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-parameter") \
185 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
186 #define DIAG_ON_FLEX \
187 PCAP_DO_PRAGMA(GCC diagnostic pop)
188
189 /*
190 * GCC currently doesn't issue any narrowing warnings.
191 */
192
193 /*
194 * Suppress deprecation warnings.
195 */
196 #define DIAG_OFF_DEPRECATION \
197 PCAP_DO_PRAGMA(GCC diagnostic push) \
198 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
199 #define DIAG_ON_DEPRECATION \
200 PCAP_DO_PRAGMA(GCC diagnostic pop)
201
202 /*
203 * Suppress format-truncation= warnings.
204 * GCC 7.1 had introduced this warning option. Earlier versions (at least
205 * one particular copy of GCC 4.6.4) treat the request as a warning.
206 */
207 #if PCAP_IS_AT_LEAST_GNUC_VERSION(7,1)
208 #define DIAG_OFF_FORMAT_TRUNCATION \
209 PCAP_DO_PRAGMA(GCC diagnostic push) \
210 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wformat-truncation=")
211 #define DIAG_ON_FORMAT_TRUNCATION \
212 PCAP_DO_PRAGMA(GCC diagnostic pop)
213 #endif
214 #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
215 /*
216 * Sun C compiler version 5.5 (Studio version 8) and later supports "#pragma
217 * error_messages()".
218 */
219 #define DIAG_OFF_FLEX \
220 PCAP_DO_PRAGMA(error_messages(off,E_STATEMENT_NOT_REACHED))
221 #define DIAG_ON_FLEX \
222 PCAP_DO_PRAGMA(error_messages(default,E_STATEMENT_NOT_REACHED))
223 #endif
224
225 #ifdef YYBYACC
226 /*
227 * Berkeley YACC.
228 *
229 * It generates a global declaration of yylval, or the appropriately
230 * prefixed version of yylval, in grammar.h, *even though it's been
231 * told to generate a pure parser, meaning it doesn't have any global
232 * variables*. Bison doesn't do this.
233 *
234 * That causes a warning due to the local declaration in the parser
235 * shadowing the global declaration.
236 *
237 * So, if the compiler warns about that, we turn off -Wshadow warnings.
238 *
239 * In addition, the generated code may have functions with unreachable
240 * code, so suppress warnings about those.
241 */
242 #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
243 /*
244 * This is Clang 2.8 or later (including clang-cl, so test this
245 * before _MSC_VER); we can use "clang diagnostic ignored -Wxxx".
246 */
247 #define DIAG_OFF_BISON_BYACC \
248 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshadow") \
249 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
250 #elif defined(_MSC_VER)
251 /*
252 * This is Microsoft Visual Studio; we can use
253 * __pragma(warning(disable:XXXX)).
254 */
255 #define DIAG_OFF_BISON_BYACC \
256 __pragma(warning(disable:4702))
257 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
258 /*
259 * This is GCC 4.6 or later, or a compiler claiming to be that.
260 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
261 * but it may not actually work very well prior to 4.6).
262 */
263 #define DIAG_OFF_BISON_BYACC \
264 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wshadow") \
265 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
266 #endif
267 #else
268 /*
269 * Bison.
270 *
271 * The generated code may have functions with unreachable code and
272 * switches with only a default case, so suppress warnings about those.
273 */
274 #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
275 /*
276 * This is Clang 2.8 or later (including clang-cl, so test this
277 * before _MSC_VER); we can use "clang diagnostic ignored -Wxxx".
278 */
279 #define DIAG_OFF_BISON_BYACC \
280 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
281 #elif defined(_MSC_VER)
282 /*
283 * This is Microsoft Visual Studio; we can use
284 * __pragma(warning(disable:XXXX)).
285 *
286 * Suppress some /Wall warnings.
287 */
288 #define DIAG_OFF_BISON_BYACC \
289 __pragma(warning(disable:4065)) \
290 __pragma(warning(disable:4127)) \
291 __pragma(warning(disable:4242)) \
292 __pragma(warning(disable:4244)) \
293 __pragma(warning(disable:4702))
294 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
295 /*
296 * This is GCC 4.6 or later, or a compiler claiming to be that.
297 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
298 * but it may not actually work very well prior to 4.6).
299 */
300 #define DIAG_OFF_BISON_BYACC \
301 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
302 #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
303 /*
304 * Same as for DIAG_OFF_FLEX above.
305 */
306 #define DIAG_OFF_BISON_BYACC \
307 PCAP_DO_PRAGMA(error_messages(off,E_STATEMENT_NOT_REACHED))
308 #endif
309 #endif
310
311 #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
312 /*
313 * Clang appears to let you ignore a result without a warning by
314 * casting the function result to void, so we don't appear to
315 * need this for Clang.
316 */
317 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,5)
318 /*
319 * GCC warns about unused return values if a function is marked as
320 * "warn about ignoring this function's return value".
321 */
322 #define DIAG_OFF_WARN_UNUSED_RESULT \
323 PCAP_DO_PRAGMA(GCC diagnostic push) \
324 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-result")
325 #define DIAG_ON_WARN_UNUSED_RESULT \
326 PCAP_DO_PRAGMA(GCC diagnostic pop)
327
328 /*
329 * GCC does not currently generate any -Wstrict-prototypes warnings that
330 * would need silencing as is done for Clang above.
331 */
332 #endif
333
334 /*
335 * GCC needs this on AIX for longjmp().
336 */
337 #if PCAP_IS_AT_LEAST_GNUC_VERSION(5,1)
338 /*
339 * Beware that the effect of this builtin is more than just squelching the
340 * warning! GCC trusts it enough for the process to segfault if the control
341 * flow reaches the builtin (an infinite empty loop in the same context would
342 * squelch the warning and ruin the process too, albeit in a different way).
343 * So please remember to use this very carefully.
344 */
345 #define PCAP_UNREACHABLE __builtin_unreachable();
346 #endif
347
348 #ifndef DIAG_OFF_ENUM_SWITCH
349 #define DIAG_OFF_ENUM_SWITCH
350 #endif
351 #ifndef DIAG_ON_ENUM_SWITCH
352 #define DIAG_ON_ENUM_SWITCH
353 #endif
354 #ifndef DIAG_OFF_DEFAULT_ONLY_SWITCH
355 #define DIAG_OFF_DEFAULT_ONLY_SWITCH
356 #endif
357 #ifndef DIAG_ON_DEFAULT_ONLY_SWITCH
358 #define DIAG_ON_DEFAULT_ONLY_SWITCH
359 #endif
360 #ifndef DIAG_OFF_FLEX
361 #define DIAG_OFF_FLEX
362 #endif
363 #ifndef DIAG_ON_FLEX
364 #define DIAG_ON_FLEX
365 #endif
366 #ifndef DIAG_OFF_NARROWING
367 #define DIAG_OFF_NARROWING
368 #endif
369 #ifndef DIAG_ON_NARROWING
370 #define DIAG_ON_NARROWING
371 #endif
372 #ifndef DIAG_OFF_DEPRECATION
373 #define DIAG_OFF_DEPRECATION
374 #endif
375 #ifndef DIAG_ON_DEPRECATION
376 #define DIAG_ON_DEPRECATION
377 #endif
378 #ifndef DIAG_OFF_FORMAT_TRUNCATION
379 #define DIAG_OFF_FORMAT_TRUNCATION
380 #endif
381 #ifndef DIAG_ON_FORMAT_TRUNCATION
382 #define DIAG_ON_FORMAT_TRUNCATION
383 #endif
384 #ifndef DIAG_OFF_BISON_BYACC
385 #define DIAG_OFF_BISON_BYACC
386 #endif
387 #ifndef DIAG_ON_WARN_UNUSED_RESULT
388 #define DIAG_ON_WARN_UNUSED_RESULT
389 #endif
390 #ifndef DIAG_OFF_WARN_UNUSED_RESULT
391 #define DIAG_OFF_WARN_UNUSED_RESULT
392 #endif
393 #ifndef DIAG_OFF_STRICT_PROTOTYPES
394 #define DIAG_OFF_STRICT_PROTOTYPES
395 #endif
396 #ifndef DIAG_ON_STRICT_PROTOTYPES
397 #define DIAG_ON_STRICT_PROTOTYPES
398 #endif
399 #ifndef PCAP_UNREACHABLE
400 #define PCAP_UNREACHABLE
401 #endif
402
403 #endif /* _diag_control_h */