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)
51 #elif defined(__GNUC__)
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.
57 #if WS_IS_AT_LEAST_GNUC_VERSION(4,8)
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().
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)
73 * This is none of the above; we don't have any way to turn diagnostics
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.
84 /* Use for clang specific pragmas, so we can keep -Wpragmas enabled */
86 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
87 # define DIAG_ON_CLANG(x) DIAG_ON(x)
89 # define DIAG_OFF_CLANG(x)
90 # define DIAG_ON_CLANG(x)
94 * Suppress complaints about narrowing converstions and about signed vs.
95 * unsigned comparison.
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?
102 #if defined(_MSC_VER)
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
110 * as well as Visual Studio Code Analyzer warnings:
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
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))
131 * XXX - is there an issue with shadowed definitions with MSVC if
132 * somebody were to happen to use Berkeley YACC rather than Bison?
134 #define DIAG_OFF_BYACC
135 #define DIAG_ON_BYACC
140 * -Wsigned-compare warnings
141 * -Wshorten-64-to-32 warnings, if the compiler *has* -Wshorten-64-to-32
142 * -Wunreachable-code warnings
144 * We use DIAG_OFF() and DIAG_ON(), so we only use features that the
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.
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?)
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)
166 #define DIAG_OFF_FLEX \
167 DIAG_OFF(sign-compare)
168 #define DIAG_ON_FLEX \
169 DIAG_ON(sign-compare)
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.
178 * That causes a warning due to the local declaration in the parser
179 * shadowing the global declaration.
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.
185 #define DIAG_OFF_BYACC \
187 #define DIAG_ON_BYACC \
190 #define DIAG_OFF_BYACC
191 #define DIAG_ON_BYACC
196 * For dealing with APIs which are only deprecated in macOS (like the
197 * OpenSSL and MIT/Heimdal Kerberos APIs).
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.
205 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
206 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
208 # define USES_APPLE_DEPRECATED_API
209 # define USES_APPLE_RST
215 #endif /* __WS_DIAG_CONTROL_H__ */