2 * Turn compiler diagnostic messages on and off.
4 * From FreeRADIUS build.h.
6 * @copyright 2013 The FreeRADIUS server project
8 * That project is covered by the GPLv2, so:
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #ifndef __WS_DIAG_CONTROL_H__
14 #define __WS_DIAG_CONTROL_H__
16 #include "ws_compiler_tests.h"
22 #define XSTRINGIFY(x) #x
25 * Macros for controlling warnings in various compilers.
27 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
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.
35 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
37 #if defined(__clang__)
39 * Clang, so we'd use _Pragma("clang diagnostic XXX"), if it's
42 #if WS_IS_AT_LEAST_CLANG_VERSION(2,8)
44 * This is Clang 2.8 or later: we can use "clang diagnostic ignored -Wxxx"
45 * and "clang diagnostic push/pop".
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)
53 * Not all versions of Clang understand -Wpedantic. Clang 4.0 appears
54 * to be the first version to do so.
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)
60 #define DIAG_OFF_PEDANTIC
61 #define DIAG_ON_PEDANTIC
63 #elif defined(__GNUC__)
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.
69 #if WS_IS_AT_LEAST_GNUC_VERSION(4,8)
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().
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)
82 * We assume GCC 4.8 and later understand -Wpedantic.
84 #define DIAG_OFF_PEDANTIC DIAG_OFF(pedantic)
85 #define DIAG_ON_PEDANTIC DIAG_ON(pedantic)
87 #define DIAG_OFF_PEDANTIC
88 #define DIAG_ON_PEDANTIC
94 * This is none of the above; we don't have any way to turn diagnostics
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.
103 #define DIAG_OFF_PEDANTIC
104 #define DIAG_ON_PEDANTIC
107 /* Use for clang specific pragmas, so we can keep -Wpragmas enabled */
109 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
110 # define DIAG_ON_CLANG(x) DIAG_ON(x)
112 # define DIAG_OFF_CLANG(x)
113 # define DIAG_ON_CLANG(x)
117 * Suppress complaints about narrowing converstions and about signed vs.
118 * unsigned comparison.
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?
125 #if defined(_MSC_VER)
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
133 * as well as Visual Studio Code Analyzer warnings:
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
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))
154 * XXX - is there an issue with shadowed definitions with MSVC if
155 * somebody were to happen to use Berkeley YACC rather than Bison?
157 #define DIAG_OFF_BYACC
158 #define DIAG_ON_BYACC
163 * -Wsigned-compare warnings
164 * -Wshorten-64-to-32 warnings, if the compiler *has* -Wshorten-64-to-32
165 * -Wunreachable-code warnings
167 * We use DIAG_OFF() and DIAG_ON(), so we only use features that the
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.
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?)
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_ON(unreachable-code) \
186 DIAG_ON(shorten-64-to-32) \
187 DIAG_ON(sign-compare)
189 #define DIAG_OFF_FLEX \
190 DIAG_OFF(sign-compare)
191 #define DIAG_ON_FLEX \
192 DIAG_ON(sign-compare)
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.
203 * That causes a warning due to the local declaration in the parser
204 * shadowing the global declaration.
206 * So, if we have _Pragma, and have pragmas to suppress diagnostics,
207 * we use it to turn off -Wshadow warnings.
209 * XXX - do this for Bison only in versions of Bison with this
212 #define DIAG_OFF_BYACC \
214 #define DIAG_ON_BYACC \
219 * For dealing with APIs which are only deprecated in macOS (like the
220 * OpenSSL and MIT/Heimdal Kerberos APIs).
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.
228 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
229 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
231 # define USES_APPLE_DEPRECATED_API
232 # define USES_APPLE_RST
238 #endif /* __WS_DIAG_CONTROL_H__ */