uaudp: removed old/unused code (#if 0...#endif)
[metze/wireshark/wip.git] / ws_diag_control.h
1 /* ws_diag_control.h
2  * Turn compiler diagnostic messages on and off.
3  *
4  * From FreeRADIUS build.h.
5  *
6  * @copyright 2013 The FreeRADIUS server project
7  *
8  * That project is covered by the GPLv2, so:
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #ifndef __WS_DIAG_CONTROL_H__
14 #define __WS_DIAG_CONTROL_H__
15
16 #include "ws_compiler_tests.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 #define XSTRINGIFY(x) #x
23
24 /*
25  *      Macros for controlling warnings in various compilers.
26  */
27 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
28
29 /*
30  * XXX - this is only for GCC or GCC-compatible compilers, and we only use
31  * it to have a macro that takes a warning as an argument and turns it
32  * off in the appropriate fashion for Clang and GCC; it should only be
33  * used internally in this header.
34  */
35 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
36
37 #if defined(__clang__)
38   /*
39    * Clang, so we'd use _Pragma("clang diagnostic XXX"), if it's
40    * supported.
41    */
42   #if WS_IS_AT_LEAST_CLANG_VERSION(2,8)
43     /*
44      * This is Clang 2.8 or later: we can use "clang diagnostic ignored -Wxxx"
45      * and "clang diagnostic push/pop".
46      */
47     #define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
48     #define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
49     #define DIAG_ON(x) DIAG_PRAGMA(pop)
50   #endif
51
52   /*
53    * Not all versions of Clang understand -Wpedantic.  Clang 4.0 appears
54    * to be the first version to do so.
55    */
56   #if WS_IS_AT_LEAST_CLANG_VERSION(4,0)
57     #define DIAG_OFF_PEDANTIC DIAG_OFF(pedantic)
58     #define DIAG_ON_PEDANTIC DIAG_ON(pedantic)
59   #else
60     #define DIAG_OFF_PEDANTIC
61     #define DIAG_ON_PEDANTIC
62   #endif
63 #elif defined(__GNUC__)
64   /*
65    * GCC, or a compiler (other than Clang) that claims to be GCC.
66    * We assume that the compiler accepts _Pragma("GCC diagnostic xxx")
67    * even if it's only claiming to be GCC.
68    */
69   #if WS_IS_AT_LEAST_GNUC_VERSION(4,8)
70     /*
71      * This is GCC 4.8 or later, or a compiler claiming to be that.
72      * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2)
73      * and "GCC diagnostic push/pop" (introduced in 4.6), *and* gcc
74      * supports "-Wpedantic" (introduced in 4.8), allowing us to
75      * turn off pedantic warnings with DIAG_OFF().
76      */
77     #define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
78     #define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
79     #define DIAG_ON(x) DIAG_PRAGMA(pop)
80
81     /*
82      * We assume GCC 4.8 and later understand -Wpedantic.
83      */
84     #define DIAG_OFF_PEDANTIC DIAG_OFF(pedantic)
85     #define DIAG_ON_PEDANTIC DIAG_ON(pedantic)
86   #else
87     #define DIAG_OFF_PEDANTIC
88     #define DIAG_ON_PEDANTIC
89   #endif
90 #endif
91
92 #ifndef DIAG_OFF
93   /*
94    * This is none of the above; we don't have any way to turn diagnostics
95    * on or off.
96    *
97    * XXX - you can do that in MSVC, but it's done differently; we'd
98    * have to have macros for *particular* diagnostics, using the
99    * warning flag for GCC and Clang and the error number for MSVC.
100    */
101   #define DIAG_OFF(x)
102   #define DIAG_ON(x)
103   #define DIAG_OFF_PEDANTIC
104   #define DIAG_ON_PEDANTIC
105 #endif
106
107 /* Use for clang specific pragmas, so we can keep -Wpragmas enabled */
108 #ifdef __clang__
109 #  define DIAG_OFF_CLANG(x) DIAG_OFF(x)
110 #  define DIAG_ON_CLANG(x)  DIAG_ON(x)
111 #else
112 #  define DIAG_OFF_CLANG(x)
113 #  define DIAG_ON_CLANG(x)
114 #endif
115
116 /*
117  * Suppress complaints about narrowing converstions and about signed vs.
118  * unsigned comparison.
119  *
120  * XXX - this is done solely to squelch complaints from code generated
121  * by Flex, but newer versions of Flex might fix the code; can we
122  * check the version of Flex and suppress only the checks that code
123  * generated by that version of Flex triggers?
124  */
125 #if defined(_MSC_VER)
126   /*
127    * Suppress:
128    *
129    *   warning C4018: signed/unsigned mismatch
130    *   warning C4244: 'initializing' : conversion from '__int64' to 'int', possible loss of data
131    *   warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
132    *
133    * as well as Visual Studio Code Analyzer warnings:
134    *
135    *   warning C6011: Dereferencing NULL pointer
136    *   warning C6308: 'realloc' might return null pointer
137    *   warning C6386: Buffer overrun
138    *   warning C6387: 'XXX' could be '0'
139    *   warning C28182: Dereferencing NULL pointer
140    */
141   #define DIAG_OFF_FLEX \
142     __pragma(warning(push)) \
143     __pragma(warning(disable:4018)) \
144     __pragma(warning(disable:4244)) \
145     __pragma(warning(disable:4267)) \
146     __pragma(warning(disable:6011)) \
147     __pragma(warning(disable:6308)) \
148     __pragma(warning(disable:6386)) \
149     __pragma(warning(disable:6387)) \
150     __pragma(warning(disable:28182))
151   #define DIAG_ON_FLEX  __pragma(warning(pop))
152
153   /*
154    * XXX - is there an issue with shadowed definitions with MSVC if
155    * somebody were to happen to use Berkeley YACC rather than Bison?
156    */
157   #define DIAG_OFF_BYACC
158   #define DIAG_ON_BYACC
159 #else
160   /*
161    * Suppress:
162    *
163    *   -Wsigned-compare warnings
164    *   -Wshorten-64-to-32 warnings, if the compiler *has* -Wshorten-64-to-32
165    *   -Wunreachable-code warnings
166    *
167    * We use DIAG_OFF() and DIAG_ON(), so we only use features that the
168    * compiler supports.
169    *
170    * We disable -Wshorten-64-to-32 if we're using Clang, or if __APPLE__
171    * is defined; that option was originally added to an Apple version of
172    * GCC, and at least some versions of Clang support it - given that
173    * the Clang work started at Apple, it may be in all versions of Clang.
174    *
175    * (Does no version of GCC or Clang support the same generic "you're
176    * narrowing a value, and you didn't throw in a cast to assert that
177    * you know what you're doing" warning that MSVC does?)
178    */
179   #if defined(__clang__) || defined(__APPLE__)
180     #define DIAG_OFF_FLEX \
181       DIAG_OFF(sign-compare) \
182       DIAG_OFF(shorten-64-to-32) \
183       DIAG_OFF(unreachable-code)
184     #define DIAG_ON_FLEX \
185       DIAG_OFF(unreachable-code) \
186       DIAG_ON(shorten-64-to-32) \
187       DIAG_ON(sign-compare)
188   #else
189     #define DIAG_OFF_FLEX \
190       DIAG_OFF(sign-compare)
191     #define DIAG_ON_FLEX \
192       DIAG_ON(sign-compare)
193   #endif
194
195   /*
196    * Berkeley YACC and, apparently, some versions of Bison, such as the
197    * one in Fedora 21, generate a global declaration of yylval, or the
198    * appropriately prefixed version of yylval, in grammar.h, *even
199    * though it's been told to generate a pure parser, meaning it
200    * doesn't have any global variables*.  Other versions of Bison, such
201    * as the one in macOS Sierra don't do that.
202    *
203    * That causes a warning due to the local declaration in the parser
204    * shadowing the global declaration.
205    *
206    * So, if we have _Pragma, and have pragmas to suppress diagnostics,
207    * we use it to turn off -Wshadow warnings.
208    *
209    * XXX - do this for Bison only in versions of Bison with this
210    * problem?
211    */
212   #define DIAG_OFF_BYACC \
213     DIAG_OFF(shadow)
214   #define DIAG_ON_BYACC \
215     DIAG_ON(shadow)
216 #endif
217
218 /*
219  *      For dealing with APIs which are only deprecated in macOS (like the
220  *      OpenSSL and MIT/Heimdal Kerberos APIs).
221  *
222  *      Dear Apple: this is a cross-platform program, and we're not
223  *      going to use your Shiny New Frameworks on macOS unless there's
224  *      a sufficiently clear benefit to make it worth our while to have
225  *      both macOS and non-macOS versions of the code.
226  */
227 #ifdef __APPLE__
228 #  define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
229 #  define USES_APPLE_RST DIAG_ON(deprecated-declarations)
230 #else
231 #  define USES_APPLE_DEPRECATED_API
232 #  define USES_APPLE_RST
233 #endif
234
235 #ifdef __cplusplus
236 }
237 #endif
238 #endif /* __WS_DIAG_CONTROL_H__ */