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