1 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
2 # vim: set filetype=python:
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 "--enable-warnings-as-errors",
9 env="MOZ_ENABLE_WARNINGS_AS_ERRORS",
10 default=depends("MOZ_AUTOMATION")(lambda x: bool(x)),
11 help="{Enable|Disable} treating warnings as errors",
15 @depends("--enable-warnings-as-errors")
16 def warnings_as_errors(warnings_as_errors):
17 if not warnings_as_errors:
23 set_config("WARNINGS_AS_ERRORS", warnings_as_errors)
25 not_clang_cl = depends(c_compiler)(lambda c: c.type != "clang-cl")
28 # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
29 # https://clang.llvm.org/docs/DiagnosticsReference.html
31 # Lots of useful warnings
32 add_warning("-Wall", when=not_clang_cl)
33 # In clang-cl, -Wall actually means -Weverything. -W3 does mean -Wall.
34 add_warning("-W3", when=depends(c_compiler)(lambda c: c.type == "clang-cl"))
36 # catch implicit truncation of enum values assigned to smaller bit fields
37 check_and_add_warning("-Wbitfield-enum-conversion")
39 # catches deprecated implicit capture of `this` in lambdas.
40 check_and_add_warning("-Wdeprecated-this-capture", cxx_compiler)
42 # catches bugs, e.g. "if (c); foo();", few false positives
43 add_warning("-Wempty-body")
45 # catches mismatched printf integer sizes.
46 check_and_add_warning("-Wformat-type-confusion")
48 # catches return types with qualifiers like const
49 add_warning("-Wignored-qualifiers")
51 # catches pointer arithmetic using NULL or sizeof(void)
52 add_warning("-Wpointer-arith")
54 # catch modifying constructor parameter that shadows member variable
55 check_and_add_warning("-Wshadow-field-in-constructor-modified")
57 # catches comparing signed/unsigned ints
58 add_warning("-Wsign-compare")
60 # catches comparisons of values and sized types are always true or false
61 check_and_add_warning("-Wtautological-constant-in-range-compare")
63 # catches overflow bugs, few false positives
64 add_warning("-Wtype-limits")
66 # This can be triggered by certain patterns used deliberately in portable code
67 check_and_add_warning("-Wno-error=tautological-type-limit-compare")
69 # catches some dead code
70 add_warning("-Wunreachable-code")
71 check_and_add_warning("-Wunreachable-code-return")
73 # catches parameters that are set but not read
74 # Only enable on clang because gcc reports false positives.
75 check_and_add_warning(
76 "-Wunused-but-set-parameter",
77 when=depends(c_compiler)(lambda c: c.type in ("clang", "clang-cl")),
80 # turned on by -Wall, but we use offsetof on non-POD types frequently
81 add_warning("-Wno-invalid-offsetof", cxx_compiler)
83 # catches objects passed by value to variadic functions.
84 check_and_add_warning("-Wclass-varargs")
86 # catches empty if/switch/for initialization statements that have no effect
87 check_and_add_warning("-Wempty-init-stmt", cxx_compiler)
89 # catches some implicit conversion of floats to ints
90 check_and_add_warning("-Wfloat-overflow-conversion")
91 check_and_add_warning("-Wfloat-zero-conversion")
93 # catches issues around loops
94 check_and_add_warning("-Wloop-analysis")
95 # But, disable range-loop-analysis because it can raise unhelpful false
97 check_and_add_warning("-Wno-range-loop-analysis")
99 # Enable some C++20 compat warnings. We can remove these flags after we compile
100 # as C++20 (bug 1768116), because they will be enabled by default:
101 check_and_add_warning("-Wc++2a-compat", cxx_compiler)
102 check_and_add_warning("-Wcomma-subscript", cxx_compiler)
103 check_and_add_warning("-Wenum-compare-conditional")
104 check_and_add_warning("-Wenum-float-conversion")
105 check_and_add_warning("-Wvolatile", cxx_compiler)
107 # Downgrade some C++20 warnings-as-errors to warnings that we can fix after we
108 # compile as C++20 (bug 1768116). They don't need to block upgrading to C++20.
109 check_and_add_warning("-Wno-error=deprecated", cxx_compiler)
110 check_and_add_warning("-Wno-error=deprecated-anon-enum-enum-conversion", cxx_compiler)
111 check_and_add_warning("-Wno-error=deprecated-enum-enum-conversion", cxx_compiler)
112 check_and_add_warning("-Wno-error=deprecated-pragma", cxx_compiler)
113 check_and_add_warning("-Wno-error=deprecated-this-capture", cxx_compiler)
115 # catches possible misuse of the comma operator
116 check_and_add_warning("-Wcomma", cxx_compiler)
118 # catches duplicated conditions in if-else-if chains
119 check_and_add_warning("-Wduplicated-cond")
121 # catches unintentional switch case fallthroughs
122 check_and_add_warning("-Wimplicit-fallthrough", cxx_compiler)
124 # Warn about suspicious uses of logical operators in expressions.
125 check_and_add_warning("-Wlogical-op")
127 # Enable some ObjC diagnostics that are only relevant when targeting macOS:
128 with only_when(depends(target)(lambda t: t.kernel == "Darwin")):
129 # catch redeclaration of ObjC method parameter name
130 check_and_add_warning("-Wduplicate-method-arg")
132 # catch multiple declarations of ObjC method found
133 check_and_add_warning("-Wduplicate-method-match")
135 # catch ObjC method with no return type specified
136 check_and_add_warning("-Wmissing-method-return-type")
138 # catch implicit conversions between ObjC BOOL and int
139 check_and_add_warning("-Wobjc-signed-char-bool")
141 # catch semicolon before ObjC method body
142 check_and_add_warning("-Wsemicolon-before-method-body")
144 # catch ObjC method parameter type not matching super class method
145 check_and_add_warning("-Wsuper-class-method-mismatch")
147 # catches string literals used in boolean expressions
148 check_and_add_warning("-Wstring-conversion")
150 # we inline 'new' and 'delete' in mozalloc
151 check_and_add_warning("-Wno-inline-new-delete", cxx_compiler)
153 # Prevent the following GCC warnings from being treated as errors:
154 # too many false positives
155 check_and_add_warning("-Wno-error=maybe-uninitialized")
157 # we don't want our builds held hostage when a platform-specific API
158 # becomes deprecated.
159 check_and_add_warning("-Wno-error=deprecated-declarations")
161 # false positives depending on optimization
162 check_and_add_warning("-Wno-error=array-bounds")
164 # false positives depending on optimizations
165 check_and_add_warning("-Wno-error=free-nonheap-object")
167 # Would be a pain to fix all occurrences, for very little gain
168 check_and_add_warning("-Wno-multistatement-macros")
170 # Disable the -Werror for -Wclass-memaccess as we have a long
171 # tail of issues to fix
172 check_and_add_warning("-Wno-error=class-memaccess")
174 # -Watomic-alignment is a new warning in clang 7 that seems way too broad.
175 # https://bugs.llvm.org/show_bug.cgi?id=38593
176 check_and_add_warning("-Wno-error=atomic-alignment")
178 # New warning with clang 15. Catches uses of deprecated builtins in abseil-cpp.
179 # https://bugzilla.mozilla.org/show_bug.cgi?id=1779528
180 check_and_add_warning("-Wno-error=deprecated-builtins")
182 # catches format/argument mismatches with printf
183 c_format_warning, cxx_format_warning = check_and_add_warning(
184 "-Wformat", when=depends(target)(lambda t: t.kernel != "WINNT")
187 # Add compile-time warnings for unprotected functions and format functions
188 # that represent possible security problems. Enable this only when -Wformat
189 # is enabled, otherwise it is an error
190 check_and_add_warning("-Wformat-security", when=c_format_warning & cxx_format_warning)
191 check_and_add_warning("-Wformat-overflow=2", when=c_format_warning & cxx_format_warning)
193 # Other Windows specific things
194 with only_when(target_is_windows):
195 # When compiling for Windows with gcc, we encounter lots of "#pragma warning"'s
196 # which is an MSVC-only pragma that GCC does not recognize.
197 # With clang-cl, as it claims to be MSVC it would be difficult to add
198 # #if defined(_MSC_VER) && !defined(__clang__) everywhere we use such pragmas,
199 # so just ignore them.
200 check_and_add_warning("-Wno-unknown-pragmas")
202 with only_when(depends(c_compiler)(lambda c: c.type == "clang-cl")):
203 # We get errors about various #pragma intrinsic directives from
204 # clang-cl, and we don't need to hear about those.
205 check_and_add_warning("-Wno-ignored-pragmas")
207 # clang-cl's Intrin.h marks things like _ReadWriteBarrier as
208 # __attribute((__deprecated__)). This is nice to know, but since we don't
209 # get the equivalent warning from MSVC, let's just ignore it.
210 check_and_add_warning("-Wno-deprecated-declarations")
212 # This warns for reasonable things like:
213 # enum { X = 0xffffffffU };
214 # which is annoying for IDL headers.
215 check_and_add_warning("-Wno-microsoft-enum-value", cxx_compiler)
217 # This warns for cases that would be reached by the Microsoft
218 # #include rules, but also currently warns on cases that would
219 # *also* be reached by standard C++ include rules. That
220 # behavior doesn't seem useful, so we turn it off.
221 check_and_add_warning("-Wno-microsoft-include", cxx_compiler)
223 # We use a function like:
224 # __declspec(noreturn) __inline void f() {}
225 # which -Winvalid-noreturn complains about. Again, MSVC seems
226 # OK with it, so let's silence the warning.
227 check_and_add_warning("-Wno-invalid-noreturn")
229 # Missing |override| on virtual function declarations isn't
230 # something that MSVC currently warns about.
231 check_and_add_warning("-Wno-inconsistent-missing-override", cxx_compiler)
233 # We use -DHAS_EXCEPTIONS=0, which removes the |throw()|
234 # declaration on |operator delete(void*)|. However, clang-cl
235 # must internally declare |operator delete(void*)| differently,
236 # which causes this warning for virtually every file in the
237 # tree. clang-cl doesn't support -fno-exceptions or equivalent,
238 # so there doesn't seem to be any way to convince clang-cl to
239 # declare |delete| differently. Therefore, suppress this
241 check_and_add_warning("-Wno-implicit-exception-spec-mismatch", cxx_compiler)
243 # Macros like STDMETHOD() and IFACEMETHOD() can declare
244 # __attribute__((nothrow)) on their respective method declarations,
245 # while the definitions are left without the matching attribute.
246 check_and_add_warning("-Wno-microsoft-exception-spec", cxx_compiler)
248 # At least one MSVC header and several headers in-tree have
249 # unused typedefs, so turn this on.
250 check_and_add_warning("-Wno-unused-local-typedef", cxx_compiler)
252 # jemalloc uses __declspec(allocator) as a profiler hint,
253 # which clang-cl doesn't understand.
254 check_and_add_warning("-Wno-ignored-attributes", cxx_compiler)
256 # __attribute__((unused)) really means "might be unused" and
257 # we use it to avoid warnings about things that are unused
258 # in some compilation units, but used in many others. This
259 # warning insists on complaining about the latter case, which
260 # is annoying, and rather noisy.
261 check_and_add_warning("-Wno-used-but-marked-unused", cxx_compiler)
263 with only_when(depends(c_compiler)(lambda c: c.type != "clang-cl")):
264 # When compiling for Windows with gcc, gcc throws false positives and true
265 # positives where the callsite is ifdef-ed out
266 check_and_add_warning("-Wno-unused-function")
268 # When compiling for Windows with gcc, gcc cannot produce this warning
269 # correctly: it mistakes DWORD_PTR and ULONG_PTR as types you cannot
270 # give NULL to. (You can in fact do that.)
271 check_and_add_warning("-Wno-conversion-null")
273 # Throughout the codebase we regularly have switch statements off of enums
274 # without covering every value in the enum. We don't care about these warnings.
275 check_and_add_warning("-Wno-switch")
277 # Another code pattern we have is using start and end constants in enums of
278 # different types. We do this for safety, but then when comparing it throws
279 # an error, which we would like to ignore. This seems to only affect the MinGW
280 # build, but we're not sure why.
281 check_and_add_warning("-Wno-enum-compare")
283 # Make it an error to be missing function declarations for C code.
284 check_and_add_warning("-Werror=implicit-function-declaration", c_compiler)
286 # New in clang 11. We can't really do anything about this warning.
287 check_and_add_warning("-Wno-psabi")
289 # Disable broken missing-braces warning on old clang versions
290 check_and_add_warning(
291 "-Wno-missing-braces",
292 when=depends(c_compiler)(lambda c: c.type == "clang" and c.version < "6.0"),
295 # Turn on clang thread-safety analysis
296 # Older clangs don't support AutoUnlock, and have other issues
297 check_and_add_warning(
299 when=depends(c_compiler)(
300 lambda c: c.type in ("clang", "clang-cl") and c.version >= "8.0"
304 # Warn if APIs are used without available() checks on macOS.
305 check_and_add_warning("-Werror=unguarded-availability-new", when=target_is_osx)
307 # clang 17 warns about builtins being redefined and... well, we do that in
308 # multiple places, some of which are third-party. Until the situation is
309 # fixed, disable the new warning.
310 check_and_add_warning("-Wno-error=builtin-macro-redefined")
312 # Please keep the following last in this file
314 # Avoid requiring complicated logic for extra warning flags in moz.build files.
315 check_and_add_warning("-Wno-unknown-warning-option")
317 set_config("WARNINGS_CFLAGS", warnings_flags.cflags)
318 set_config("WARNINGS_CXXFLAGS", warnings_flags.cxxflags)
319 set_config("WARNINGS_HOST_CFLAGS", warnings_flags.host_cflags)
320 set_config("WARNINGS_HOST_CXXFLAGS", warnings_flags.host_cxxflags)