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) || PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
41  /*
42   * Clang and GCC both support this way of putting pragmas into #defines.
43   * We use it only if we have a compiler that supports it; see below
44   * for the code that uses it and the #defines that control whether
45   * that code is used.
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 PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
90  /*
91   * This is Clang 2.8 or later; we can use "clang diagnostic
92   * ignored -Wxxx" and "clang diagnostic push/pop".
93   *
94   * Suppress -Wdocumentation warnings; GCC doesn't support -Wdocumentation,
95   * at least according to the GCC 7.3 documentation.  Apparently, Flex
96   * generates code that upsets at least some versions of Clang's
97   * -Wdocumentation.
98   *
99   * (This could be clang-cl, which defines _MSC_VER, so test this
100   * before testing _MSC_VER.)
101   */
102  #define DIAG_OFF_FLEX \
103    PCAP_DO_PRAGMA(clang diagnostic push) \
104    PCAP_DO_PRAGMA(clang diagnostic ignored "-Wsign-compare") \
105    PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdocumentation") \
106    PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32") \
107    PCAP_DO_PRAGMA(clang diagnostic ignored "-Wmissing-noreturn") \
108    PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunused-parameter") \
109    PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
110  #define DIAG_ON_FLEX \
111    PCAP_DO_PRAGMA(clang diagnostic pop)
112
113  /*
114   * Suppress the only narrowing warnings you get from Clang.
115   */
116  #define DIAG_OFF_NARROWING \
117    PCAP_DO_PRAGMA(clang diagnostic push) \
118    PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32")
119
120  #define DIAG_ON_NARROWING \
121    PCAP_DO_PRAGMA(clang diagnostic pop)
122
123  /*
124   * Suppress deprecation warnings.
125   */
126  #define DIAG_OFF_DEPRECATION \
127    PCAP_DO_PRAGMA(clang diagnostic push) \
128    PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdeprecated-declarations")
129  #define DIAG_ON_DEPRECATION \
130    PCAP_DO_PRAGMA(clang diagnostic pop)
131  #define DIAG_OFF_FORMAT_TRUNCATION
132  #define DIAG_ON_FORMAT_TRUNCATION
133#elif defined(_MSC_VER)
134  /*
135   * This is Microsoft Visual Studio; we can use __pragma(warning(disable:XXXX))
136   * and __pragma(warning(push/pop)).
137   *
138   * Suppress signed-vs-unsigned comparison, narrowing, and unreachable
139   * code warnings.
140   */
141  #define DIAG_OFF_FLEX \
142    __pragma(warning(push)) \
143    __pragma(warning(disable:4127)) \
144    __pragma(warning(disable:4242)) \
145    __pragma(warning(disable:4244)) \
146    __pragma(warning(disable:4702))
147  #define DIAG_ON_FLEX \
148    __pragma(warning(pop))
149
150  /*
151   * Suppress narrowing warnings.
152   */
153  #define DIAG_OFF_NARROWING \
154    __pragma(warning(push)) \
155    __pragma(warning(disable:4242)) \
156    __pragma(warning(disable:4311))
157  #define DIAG_ON_NARROWING \
158    __pragma(warning(pop))
159
160  /*
161   * Suppress deprecation warnings.
162   */
163  #define DIAG_OFF_DEPRECATION \
164    __pragma(warning(push)) \
165    __pragma(warning(disable:4996))
166  #define DIAG_ON_DEPRECATION \
167    __pragma(warning(pop))
168  #define DIAG_OFF_FORMAT_TRUNCATION
169  #define DIAG_ON_FORMAT_TRUNCATION
170#elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
171  /*
172   * This is GCC 4.6 or later, or a compiler claiming to be that.
173   * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2)
174   * and "GCC diagnostic push/pop" (introduced in 4.6).
175   */
176  #define DIAG_OFF_FLEX \
177    PCAP_DO_PRAGMA(GCC diagnostic push) \
178    PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wsign-compare") \
179    PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-parameter") \
180    PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
181  #define DIAG_ON_FLEX \
182    PCAP_DO_PRAGMA(GCC diagnostic pop)
183
184  /*
185   * GCC currently doesn't issue any narrowing warnings.
186   */
187  #define DIAG_OFF_NARROWING
188  #define DIAG_ON_NARROWING
189
190  /*
191   * Suppress deprecation warnings.
192   */
193  #define DIAG_OFF_DEPRECATION \
194    PCAP_DO_PRAGMA(GCC diagnostic push) \
195    PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
196  #define DIAG_ON_DEPRECATION \
197    PCAP_DO_PRAGMA(GCC diagnostic pop)
198
199  /*
200   * Suppress format-truncation= warnings.
201   * GCC 7.1 had introduced this warning option. Earlier versions (at least
202   * one particular copy of GCC 4.6.4) treat the request as a warning.
203   */
204  #if PCAP_IS_AT_LEAST_GNUC_VERSION(7,1)
205    #define DIAG_OFF_FORMAT_TRUNCATION \
206      PCAP_DO_PRAGMA(GCC diagnostic push) \
207      PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wformat-truncation=")
208    #define DIAG_ON_FORMAT_TRUNCATION \
209      PCAP_DO_PRAGMA(GCC diagnostic pop)
210  #else
211   #define DIAG_OFF_FORMAT_TRUNCATION
212   #define DIAG_ON_FORMAT_TRUNCATION
213  #endif
214#else
215  /*
216   * Neither Visual Studio, nor Clang 2.8 or later, nor GCC 4.6 or later
217   * or a compiler claiming to be that; there's nothing we know of that
218   * we can do.
219   */
220  #define DIAG_OFF_FLEX
221  #define DIAG_ON_FLEX
222  #define DIAG_OFF_NARROWING
223  #define DIAG_ON_NARROWING
224  #define DIAG_OFF_DEPRECATION
225  #define DIAG_ON_DEPRECATION
226  #define DIAG_OFF_FORMAT_TRUNCATION
227  #define DIAG_ON_FORMAT_TRUNCATION
228#endif
229
230#ifdef YYBYACC
231  /*
232   * Berkeley YACC.
233   *
234   * It generates a global declaration of yylval, or the appropriately
235   * prefixed version of yylval, in grammar.h, *even though it's been
236   * told to generate a pure parser, meaning it doesn't have any global
237   * variables*.  Bison doesn't do this.
238   *
239   * That causes a warning due to the local declaration in the parser
240   * shadowing the global declaration.
241   *
242   * So, if the compiler warns about that, we turn off -Wshadow warnings.
243   *
244   * In addition, the generated code may have functions with unreachable
245   * code, so suppress warnings about those.
246   */
247  #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
248    /*
249     * This is Clang 2.8 or later (including clang-cl, so test this
250     * before _MSC_VER); we can use "clang diagnostic ignored -Wxxx".
251     */
252    #define DIAG_OFF_BISON_BYACC \
253      PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshadow") \
254      PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
255  #elif defined(_MSC_VER)
256    /*
257     * This is Microsoft Visual Studio; we can use
258     * __pragma(warning(disable:XXXX)).
259     */
260    #define DIAG_OFF_BISON_BYACC \
261      __pragma(warning(disable:4702))
262  #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
263    /*
264     * This is GCC 4.6 or later, or a compiler claiming to be that.
265     * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
266     * but it may not actually work very well prior to 4.6).
267     */
268    #define DIAG_OFF_BISON_BYACC \
269      PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wshadow") \
270      PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
271  #else
272    /*
273     * Neither Clang 2.8 or later nor GCC 4.6 or later or a compiler
274     * claiming to be that; there's nothing we know of that we can do.
275     */
276    #define DIAG_OFF_BISON_BYACC
277  #endif
278#else
279  /*
280   * Bison.
281   *
282   * The generated code may have functions with unreachable code and
283   * switches with only a default case, so suppress warnings about those.
284   */
285  #if PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
286    /*
287     * This is Clang 2.8 or later (including clang-cl, so test this
288     * before _MSC_VER); we can use "clang diagnostic ignored -Wxxx".
289     */
290    #define DIAG_OFF_BISON_BYACC \
291      PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
292  #elif defined(_MSC_VER)
293    /*
294     * This is Microsoft Visual Studio; we can use
295     * __pragma(warning(disable:XXXX)).
296     *
297     * Suppress some /Wall warnings.
298     */
299    #define DIAG_OFF_BISON_BYACC \
300      __pragma(warning(disable:4065)) \
301      __pragma(warning(disable:4127)) \
302      __pragma(warning(disable:4242)) \
303      __pragma(warning(disable:4244)) \
304      __pragma(warning(disable:4702))
305  #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
306    /*
307     * This is GCC 4.6 or later, or a compiler claiming to be that.
308     * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
309     * but it may not actually work very well prior to 4.6).
310     */
311    #define DIAG_OFF_BISON_BYACC \
312      PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
313  #else
314    /*
315     * Neither Clang 2.8 or later nor GCC 4.6 or later or a compiler
316     * claiming to be that; there's nothing we know of that we can do.
317     */
318    #define DIAG_OFF_BISON_BYACC
319  #endif
320#endif
321
322/*
323 * GCC needs this on AIX for longjmp().
324 */
325#if PCAP_IS_AT_LEAST_GNUC_VERSION(5,1)
326  /*
327   * Beware that the effect of this builtin is more than just squelching the
328   * warning! GCC trusts it enough for the process to segfault if the control
329   * flow reaches the builtin (an infinite empty loop in the same context would
330   * squelch the warning and ruin the process too, albeit in a different way).
331   * So please remember to use this very carefully.
332   */
333  #define PCAP_UNREACHABLE __builtin_unreachable();
334#else
335  #define PCAP_UNREACHABLE
336#endif
337
338#endif /* _diag_control_h */
339