• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _MSC_VER
41   /*
42    * Clang and GCC both support this way of putting pragmas into #defines.
43    * We don't use it unless we have a compiler that supports it; the
44    * warning-suppressing pragmas differ between Clang and GCC, so we test
45    * for both of those separately.
46    */
47   #define PCAP_DO_PRAGMA(x) _Pragma (#x)
48 #endif
49 
50 /*
51  * Suppress "enum value not explicitly handled in switch" warnings.
52  * We may have to build on multiple different Windows SDKs, so we
53  * may not be able to include all enum values in a switch, as they
54  * won't necessarily be defined on all the SDKs, and, unlike
55  * #defines, there's no easy way to test whether a given enum has
56  * a given value.  It *could* be done by the configure script or
57  * CMake tests.
58  */
59 #if defined(_MSC_VER)
60   #define DIAG_OFF_ENUM_SWITCH \
61     __pragma(warning(push)) \
62     __pragma(warning(disable:4061))
63   #define DIAG_ON_ENUM_SWITCH \
64     __pragma(warning(pop))
65 #else
66   #define DIAG_OFF_ENUM_SWITCH
67   #define DIAG_ON_ENUM_SWITCH
68 #endif
69 
70 /*
71  * Suppress "switch statement has only a default case" warnings.
72  * There's a switch in bpf_filter.c that only has additional
73  * cases on Linux.
74  */
75 #if defined(_MSC_VER)
76   #define DIAG_OFF_DEFAULT_ONLY_SWITCH \
77     __pragma(warning(push)) \
78     __pragma(warning(disable:4065))
79   #define DIAG_ON_DEFAULT_ONLY_SWITCH \
80     __pragma(warning(pop))
81 #else
82   #define DIAG_OFF_DEFAULT_ONLY_SWITCH
83   #define DIAG_ON_DEFAULT_ONLY_SWITCH
84 #endif
85 
86 /*
87  * Suppress Flex, narrowing, and deprecation warnings.
88  */
89 #if defined(_MSC_VER)
90   /*
91    * This is Microsoft Visual Studio; we can use __pragma(warning(disable:XXXX))
92    * and __pragma(warning(push/pop)).
93    *
94    * Suppress signed-vs-unsigned comparison, narrowing, and unreachable
95    * code warnings.
96    */
97   #define DIAG_OFF_FLEX \
98     __pragma(warning(push)) \
99     __pragma(warning(disable:4127)) \
100     __pragma(warning(disable:4242)) \
101     __pragma(warning(disable:4244)) \
102     __pragma(warning(disable:4702))
103   #define DIAG_ON_FLEX \
104     __pragma(warning(pop))
105 
106   /*
107    * Suppress narrowing warnings.
108    */
109   #define DIAG_OFF_NARROWING \
110     __pragma(warning(push)) \
111     __pragma(warning(disable:4242)) \
112     __pragma(warning(disable:4311))
113   #define DIAG_ON_NARROWING \
114     __pragma(warning(pop))
115 
116   /*
117    * Suppress deprecation warnings.
118    */
119   #define DIAG_OFF_DEPRECATION \
120     __pragma(warning(push)) \
121     __pragma(warning(disable:4996))
122   #define DIAG_ON_DEPRECATION \
123     __pragma(warning(pop))
124 #elif PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
125   /*
126    * This is Clang 2.8 or later; we can use "clang diagnostic
127    * ignored -Wxxx" and "clang diagnostic push/pop".
128    *
129    * Suppress -Wdocumentation warnings; GCC doesn't support -Wdocumentation,
130    * at least according to the GCC 7.3 documentation.  Apparently, Flex
131    * generates code that upsets at least some versions of Clang's
132    * -Wdocumentation.
133    */
134   #define DIAG_OFF_FLEX \
135     PCAP_DO_PRAGMA(clang diagnostic push) \
136     PCAP_DO_PRAGMA(clang diagnostic ignored "-Wsign-compare") \
137     PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdocumentation") \
138     PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32") \
139     PCAP_DO_PRAGMA(clang diagnostic ignored "-Wmissing-noreturn") \
140     PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunused-parameter") \
141     PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
142   #define DIAG_ON_FLEX \
143     PCAP_DO_PRAGMA(clang diagnostic pop)
144 
145   /*
146    * Suppress the only narrowing warnings you get from Clang.
147    */
148   #define DIAG_OFF_NARROWING \
149     PCAP_DO_PRAGMA(clang diagnostic push) \
150     PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32")
151 
152   #define DIAG_ON_NARROWING \
153     PCAP_DO_PRAGMA(clang diagnostic pop)
154 
155   /*
156    * Suppress deprecation warnings.
157    */
158   #define DIAG_OFF_DEPRECATION \
159     PCAP_DO_PRAGMA(clang diagnostic push) \
160     PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdeprecated-declarations")
161   #define DIAG_ON_DEPRECATION \
162     PCAP_DO_PRAGMA(clang diagnostic pop)
163 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
164   /*
165    * This is GCC 4.6 or later, or a compiler claiming to be that.
166    * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2)
167    * and "GCC diagnostic push/pop" (introduced in 4.6).
168    */
169   #define DIAG_OFF_FLEX \
170     PCAP_DO_PRAGMA(GCC diagnostic push) \
171     PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wsign-compare") \
172     PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-parameter") \
173     PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
174   #define DIAG_ON_FLEX \
175     PCAP_DO_PRAGMA(GCC diagnostic pop)
176 
177   /*
178    * GCC currently doesn't issue any narrowing warnings.
179    */
180   #define DIAG_OFF_NARROWING
181   #define DIAG_ON_NARROWING
182 
183   /*
184    * Suppress deprecation warnings.
185    */
186   #define DIAG_OFF_DEPRECATION \
187     PCAP_DO_PRAGMA(GCC diagnostic push) \
188     PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
189   #define DIAG_ON_DEPRECATION \
190     PCAP_DO_PRAGMA(GCC diagnostic pop)
191 #else
192   /*
193    * Neither Visual Studio, nor Clang 2.8 or later, nor GCC 4.6 or later
194    * or a compiler claiming to be that; there's nothing we know of that
195    * we can do.
196    */
197   #define DIAG_OFF_FLEX
198   #define DIAG_ON_FLEX
199   #define DIAG_OFF_NARROWING
200   #define DIAG_ON_NARROWING
201   #define DIAG_OFF_DEPRECATION
202   #define DIAG_ON_DEPRECATION
203 #endif
204 
205 #ifdef YYBYACC
206   /*
207    * Berkeley YACC.
208    *
209    * It generates a global declaration of yylval, or the appropriately
210    * prefixed version of yylval, in grammar.h, *even though it's been
211    * told to generate a pure parser, meaning it doesn't have any global
212    * variables*.  Bison doesn't do this.
213    *
214    * That causes a warning due to the local declaration in the parser
215    * shadowing the global declaration.
216    *
217    * So, if the compiler warns about that, we turn off -Wshadow warnings.
218    *
219    * In addition, the generated code may have functions with unreachable
220    * code, so suppress warnings about those.
221    */
222   #if defined(_MSC_VER)
223     /*
224      * This is Microsoft Visual Studio; we can use
225      * __pragma(warning(disable:XXXX)).
226      */
227     #define DIAG_OFF_BISON_BYACC \
228       __pragma(warning(disable:4702))
229   #elif PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
230     /*
231      * This is Clang 2.8 or later; we can use "clang diagnostic
232      * ignored -Wxxx".
233      */
234     #define DIAG_OFF_BISON_BYACC \
235       PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshadow") \
236       PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
237   #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
238     /*
239      * This is GCC 4.6 or later, or a compiler claiming to be that.
240      * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
241      * but it may not actually work very well prior to 4.6).
242      */
243     #define DIAG_OFF_BISON_BYACC \
244       PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wshadow") \
245       PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
246   #else
247     /*
248      * Neither Clang 2.8 or later nor GCC 4.6 or later or a compiler
249      * claiming to be that; there's nothing we know of that we can do.
250      */
251     #define DIAG_OFF_BISON_BYACC
252   #endif
253 #else
254   /*
255    * Bison.
256    *
257    * The generated code may have functions with unreachable code and
258    * switches with only a default case, so suppress warnings about those.
259    */
260   #if defined(_MSC_VER)
261     /*
262      * This is Microsoft Visual Studio; we can use
263      * __pragma(warning(disable:XXXX)).
264      *
265      * Suppress some /Wall warnings.
266      */
267     #define DIAG_OFF_BISON_BYACC \
268       __pragma(warning(disable:4065)) \
269       __pragma(warning(disable:4127)) \
270       __pragma(warning(disable:4242)) \
271       __pragma(warning(disable:4244)) \
272       __pragma(warning(disable:4702))
273   #elif PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
274     /*
275      * This is Clang 2.8 or later; we can use "clang diagnostic
276      * ignored -Wxxx".
277      */
278     #define DIAG_OFF_BISON_BYACC \
279       PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
280   #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
281     /*
282      * This is GCC 4.6 or later, or a compiler claiming to be that.
283      * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
284      * but it may not actually work very well prior to 4.6).
285      */
286     #define DIAG_OFF_BISON_BYACC \
287       PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
288   #else
289     /*
290      * Neither Clang 2.8 or later nor GCC 4.6 or later or a compiler
291      * claiming to be that; there's nothing we know of that we can do.
292      */
293     #define DIAG_OFF_BISON_BYACC
294   #endif
295 #endif
296 
297 #endif /* _diag_control_h */
298