Sort the PCCC_ES values, as required in a value_string_ext.
[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 #elif defined(__GNUC__)
52   /*
53    * GCC, or a compiler (other than Clang) that claims to be GCC.
54    * We assume that the compiler accepts _Pragma("GCC diagnostic xxx")
55    * even if it's only claiming to be GCC.
56    */
57   #if WS_IS_AT_LEAST_GNUC_VERSION(4,8)
58     /*
59      * This is GCC 4.8 or later, or a compiler claiming to be that.
60      * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2)
61      * and "GCC diagnostic push/pop" (introduced in 4.6), *and* gcc
62      * supports "-Wpedantic" (introduced in 4.8), allowing us to
63      * turn off pedantic warnings with DIAG_OFF().
64      */
65     #define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
66     #define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
67     #define DIAG_ON(x) DIAG_PRAGMA(pop)
68   #endif
69 #endif
70
71 #ifndef DIAG_OFF
72   /*
73    * This is none of the above; we don't have any way to turn diagnostics
74    * on or off.
75    *
76    * XXX - you can do that in MSVC, but it's done differently; we'd
77    * have to have macros for *particular* diagnostics, using the
78    * warning flag for GCC and Clang and the error number for MSVC.
79    */
80   #define DIAG_OFF(x)
81   #define DIAG_ON(x)
82 #endif
83
84 /* Use for clang specific pragmas, so we can keep -Wpragmas enabled */
85 #ifdef __clang__
86 #  define DIAG_OFF_CLANG(x) DIAG_OFF(x)
87 #  define DIAG_ON_CLANG(x)  DIAG_ON(x)
88 #else
89 #  define DIAG_OFF_CLANG(x)
90 #  define DIAG_ON_CLANG(x)
91 #endif
92
93 /*
94  * Suppress complaints about narrowing converstions and about signed vs.
95  * unsigned comparison.
96  *
97  * XXX - this is done solely to squelch complaints from code generated
98  * by Flex, but newer versions of Flex might fix the code; can we
99  * check the version of Flex and suppress only the checks that code
100  * generated by that version of Flex triggers?
101  */
102 #if defined(_MSC_VER)
103   /*
104    * Suppress:
105    *
106    *   warning C4018: signed/unsigned mismatch
107    *   warning C4244: 'initializing' : conversion from '__int64' to 'int', possible loss of data
108    *   warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
109    *
110    * as well as Visual Studio Code Analyzer warnings:
111    *
112    *   warning C6011: Dereferencing NULL pointer
113    *   warning C6308: 'realloc' might return null pointer
114    *   warning C6386: Buffer overrun
115    *   warning C6387: 'XXX' could be '0'
116    *   warning C28182: Dereferencing NULL pointer
117    */
118   #define DIAG_OFF_FLEX \
119     __pragma(warning(push)) \
120     __pragma(warning(disable:4018)) \
121     __pragma(warning(disable:4244)) \
122     __pragma(warning(disable:4267)) \
123     __pragma(warning(disable:6011)) \
124     __pragma(warning(disable:6308)) \
125     __pragma(warning(disable:6386)) \
126     __pragma(warning(disable:6387)) \
127     __pragma(warning(disable:28182))
128   #define DIAG_ON_FLEX  __pragma(warning(pop))
129
130   /*
131    * XXX - is there an issue with shadowed definitions with MSVC if
132    * somebody were to happen to use Berkeley YACC rather than Bison?
133    */
134   #define DIAG_OFF_BYACC
135   #define DIAG_ON_BYACC
136 #else
137   /*
138    * Suppress:
139    *
140    *   -Wsigned-compare warnings
141    *   -Wshorten-64-to-32 warnings, if the compiler *has* -Wshorten-64-to-32
142    *   -Wunreachable-code warnings
143    *
144    * We use DIAG_OFF() and DIAG_ON(), so we only use features that the
145    * compiler supports.
146    *
147    * We disable -Wshorten-64-to-32 if we're using Clang, or if __APPLE__
148    * is defined; that option was originally added to an Apple version of
149    * GCC, and at least some versions of Clang support it - given that
150    * the Clang work started at Apple, it may be in all versions of Clang.
151    *
152    * (Does no version of GCC or Clang support the same generic "you're
153    * narrowing a value, and you didn't throw in a cast to assert that
154    * you know what you're doing" warning that MSVC does?)
155    */
156   #if defined(__clang__) || defined(__APPLE__)
157     #define DIAG_OFF_FLEX \
158       DIAG_OFF(sign-compare) \
159       DIAG_OFF(shorten-64-to-32) \
160       DIAG_OFF(unreachable-code)
161     #define DIAG_ON_FLEX \
162       DIAG_OFF(unreachable-code) \
163       DIAG_ON(shorten-64-to-32) \
164       DIAG_ON(sign-compare)
165   #else
166     #define DIAG_OFF_FLEX \
167       DIAG_OFF(sign-compare)
168     #define DIAG_ON_FLEX \
169       DIAG_ON(sign-compare)
170   #endif
171
172   /*
173    * Berkeley YACC generates a global declaration of yylval, or the
174    * appropriately prefixed version of yylval, in grammar.h, *even
175    * though it's been told to generate a pure parser, meaning it
176    * doesn't have any global variables*.  Bison doesn't do this.
177    *
178    * That causes a warning due to the local declaration in the parser
179    * shadowing the global declaration.
180    *
181    * So, if this is Berkeley YACC, and we have _Pragma, and have pragmas
182    * to suppress diagnostics, we use it to turn off -Wshadow warnings.
183    */
184   #ifdef YYBYACC
185     #define DIAG_OFF_BYACC \
186       DIAG_OFF(shadow)
187     #define DIAG_ON_BYACC \
188       DIAG_ON(shadow)
189   #else
190     #define DIAG_OFF_BYACC
191     #define DIAG_ON_BYACC
192   #endif
193 #endif
194
195 /*
196  *      For dealing with APIs which are only deprecated in macOS (like the
197  *      OpenSSL and MIT/Heimdal Kerberos APIs).
198  *
199  *      Dear Apple: this is a cross-platform program, and we're not
200  *      going to use your Shiny New Frameworks on macOS unless there's
201  *      a sufficiently clear benefit to make it worth our while to have
202  *      both macOS and non-macOS versions of the code.
203  */
204 #ifdef __APPLE__
205 #  define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
206 #  define USES_APPLE_RST DIAG_ON(deprecated-declarations)
207 #else
208 #  define USES_APPLE_DEPRECATED_API
209 #  define USES_APPLE_RST
210 #endif
211
212 #ifdef __cplusplus
213 }
214 #endif
215 #endif /* __WS_DIAG_CONTROL_H__ */