Backed out 2 changesets (bug 1906804, bug 1882553) for causing artifact build bustage...
[gecko.git] / build / moz.configure / warnings.configure
blobb33ede9fbb3bf163bebd8606f8326c09814dfa98
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/.
7 option(
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:
18         return ""
20     return "-Werror"
23 set_config("WARNINGS_AS_ERRORS", warnings_as_errors)
25 not_clang_cl = depends(c_compiler)(lambda c: c.type != "clang-cl")
27 # GCC/Clang warnings:
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 bugs, e.g. "if (c); foo();", few false positives
40 add_warning("-Wempty-body")
42 # catches mismatched printf integer sizes.
43 check_and_add_warning("-Wformat-type-confusion")
45 # catches return types with qualifiers like const
46 add_warning("-Wignored-qualifiers")
48 # catches pointer arithmetic using NULL or sizeof(void)
49 add_warning("-Wpointer-arith")
51 # catch modifying constructor parameter that shadows member variable
52 check_and_add_warning("-Wshadow-field-in-constructor-modified")
54 # catches comparing signed/unsigned ints
55 add_warning("-Wsign-compare")
57 # catches comparisons of values and sized types are always true or false
58 check_and_add_warning("-Wtautological-constant-in-range-compare")
60 # catches overflow bugs, few false positives
61 add_warning("-Wtype-limits")
63 # This can be triggered by certain patterns used deliberately in portable code
64 check_and_add_warning("-Wno-error=tautological-type-limit-compare")
66 # catches some dead code
67 add_warning("-Wunreachable-code")
68 check_and_add_warning("-Wunreachable-code-return")
70 # catches parameters that are set but not read
71 # Only enable on clang because gcc reports false positives.
72 check_and_add_warning(
73     "-Wunused-but-set-parameter",
74     when=depends(c_compiler)(lambda c: c.type in ("clang", "clang-cl")),
77 # turned on by -Wall, but we use offsetof on non-POD types frequently
78 add_warning("-Wno-invalid-offsetof", cxx_compiler)
80 # catches objects passed by value to variadic functions.
81 check_and_add_warning("-Wclass-varargs")
83 # catches empty if/switch/for initialization statements that have no effect
84 check_and_add_warning("-Wempty-init-stmt", cxx_compiler)
86 # catches some implicit conversion of floats to ints
87 check_and_add_warning("-Wfloat-overflow-conversion")
88 check_and_add_warning("-Wfloat-zero-conversion")
90 # catches issues around loops
91 check_and_add_warning("-Wloop-analysis")
92 # But, disable range-loop-analysis because it can raise unhelpful false
93 # positives.
94 check_and_add_warning("-Wno-range-loop-analysis")
96 # Enable some C++20 compat warnings. We can remove these flags after we compile
97 # as C++20 (bug 1768116), because they will be enabled by default:
98 check_and_add_warning("-Wcomma-subscript", cxx_compiler)
99 check_and_add_warning("-Wenum-compare-conditional")
100 check_and_add_warning("-Wenum-float-conversion")
101 check_and_add_warning("-Wvolatile", cxx_compiler)
103 # Disable some C++20 errors to be fixed in bugs 1791958, 1791955, and 1775161.
104 check_and_add_warning("-Wno-deprecated-anon-enum-enum-conversion", cxx_compiler)
105 check_and_add_warning("-Wno-deprecated-enum-enum-conversion", cxx_compiler)
106 check_and_add_warning("-Wno-deprecated-this-capture", cxx_compiler)
108 # catches possible misuse of the comma operator
109 check_and_add_warning("-Wcomma", cxx_compiler)
111 # catches duplicated conditions in if-else-if chains
112 check_and_add_warning("-Wduplicated-cond")
114 # catches unintentional switch case fallthroughs
115 check_and_add_warning("-Wimplicit-fallthrough", cxx_compiler)
117 # Warn about suspicious uses of logical operators in expressions.
118 check_and_add_warning("-Wlogical-op")
120 # Enable some ObjC diagnostics that are only relevant when targeting macOS:
121 with only_when(depends(target)(lambda t: t.kernel == "Darwin")):
122     # catch redeclaration of ObjC method parameter name
123     check_and_add_warning("-Wduplicate-method-arg")
125     # catch multiple declarations of ObjC method found
126     check_and_add_warning("-Wduplicate-method-match")
128     # catch ObjC method with no return type specified
129     check_and_add_warning("-Wmissing-method-return-type")
131     # catch implicit conversions between ObjC BOOL and int
132     check_and_add_warning("-Wobjc-signed-char-bool")
134     # catch semicolon before ObjC method body
135     check_and_add_warning("-Wsemicolon-before-method-body")
137     # catch ObjC method parameter type not matching super class method
138     check_and_add_warning("-Wsuper-class-method-mismatch")
140 # catches string literals used in boolean expressions
141 check_and_add_warning("-Wstring-conversion")
143 # we inline 'new' and 'delete' in mozalloc
144 check_and_add_warning("-Wno-inline-new-delete", cxx_compiler)
146 # Prevent the following GCC warnings from being treated as errors:
147 # too many false positives
148 check_and_add_warning("-Wno-error=maybe-uninitialized")
150 # we don't want our builds held hostage when a platform-specific API
151 # becomes deprecated.
152 check_and_add_warning("-Wno-error=deprecated-declarations")
154 # false positives depending on optimization
155 check_and_add_warning("-Wno-error=array-bounds")
157 # false positives depending on optimizations
158 check_and_add_warning("-Wno-error=free-nonheap-object")
160 # Would be a pain to fix all occurrences, for very little gain
161 check_and_add_warning("-Wno-multistatement-macros")
163 # Disable the -Werror for -Wclass-memaccess as we have a long
164 # tail of issues to fix
165 check_and_add_warning("-Wno-error=class-memaccess")
167 # -Watomic-alignment is a new warning in clang 7 that seems way too broad.
168 # https://bugs.llvm.org/show_bug.cgi?id=38593
169 check_and_add_warning("-Wno-error=atomic-alignment")
171 # New warning with clang 15. Catches uses of deprecated builtins in abseil-cpp.
172 # https://bugzilla.mozilla.org/show_bug.cgi?id=1779528
173 check_and_add_warning("-Wno-error=deprecated-builtins")
175 # catches format/argument mismatches with printf
176 c_format_warning, cxx_format_warning = check_and_add_warning(
177     "-Wformat", when=depends(target)(lambda t: t.kernel != "WINNT")
180 # Add compile-time warnings for unprotected functions and format functions
181 # that represent possible security problems. Enable this only when -Wformat
182 # is enabled, otherwise it is an error
183 check_and_add_warning("-Wformat-security", when=c_format_warning & cxx_format_warning)
184 check_and_add_warning("-Wformat-overflow=2", when=c_format_warning & cxx_format_warning)
186 # Other Windows specific things
187 with only_when(target_is_windows):
188     # When compiling for Windows with gcc, we encounter lots of "#pragma warning"'s
189     # which is an MSVC-only pragma that GCC does not recognize.
190     # With clang-cl, as it claims to be MSVC it would be difficult to add
191     # #if defined(_MSC_VER) && !defined(__clang__) everywhere we use such pragmas,
192     # so just ignore them.
193     check_and_add_warning("-Wno-unknown-pragmas")
195     with only_when(depends(c_compiler)(lambda c: c.type == "clang-cl")):
196         # We get errors about various #pragma intrinsic directives from
197         # clang-cl, and we don't need to hear about those.
198         check_and_add_warning("-Wno-ignored-pragmas")
200         # clang-cl's Intrin.h marks things like _ReadWriteBarrier as
201         # __attribute((__deprecated__)).  This is nice to know, but since we don't
202         # get the equivalent warning from MSVC, let's just ignore it.
203         check_and_add_warning("-Wno-deprecated-declarations")
205         # This warns for reasonable things like:
206         #   enum { X = 0xffffffffU };
207         # which is annoying for IDL headers.
208         check_and_add_warning("-Wno-microsoft-enum-value", cxx_compiler)
210         # This warns for cases that would be reached by the Microsoft
211         # #include rules, but also currently warns on cases that would
212         # *also* be reached by standard C++ include rules.  That
213         # behavior doesn't seem useful, so we turn it off.
214         check_and_add_warning("-Wno-microsoft-include", cxx_compiler)
216         # We use a function like:
217         #   __declspec(noreturn) __inline void f() {}
218         # which -Winvalid-noreturn complains about.  Again, MSVC seems
219         # OK with it, so let's silence the warning.
220         check_and_add_warning("-Wno-invalid-noreturn")
222         # Missing |override| on virtual function declarations isn't
223         # something that MSVC currently warns about.
224         check_and_add_warning("-Wno-inconsistent-missing-override", cxx_compiler)
226         # We use -DHAS_EXCEPTIONS=0, which removes the |throw()|
227         # declaration on |operator delete(void*)|.  However, clang-cl
228         # must internally declare |operator delete(void*)| differently,
229         # which causes this warning for virtually every file in the
230         # tree.  clang-cl doesn't support -fno-exceptions or equivalent,
231         # so there doesn't seem to be any way to convince clang-cl to
232         # declare |delete| differently.  Therefore, suppress this
233         # warning.
234         check_and_add_warning("-Wno-implicit-exception-spec-mismatch", cxx_compiler)
236         # Macros like STDMETHOD() and IFACEMETHOD() can declare
237         # __attribute__((nothrow)) on their respective method declarations,
238         # while the definitions are left without the matching attribute.
239         check_and_add_warning("-Wno-microsoft-exception-spec", cxx_compiler)
241         # At least one MSVC header and several headers in-tree have
242         # unused typedefs, so turn this on.
243         check_and_add_warning("-Wno-unused-local-typedef", cxx_compiler)
245         # jemalloc uses __declspec(allocator) as a profiler hint,
246         # which clang-cl doesn't understand.
247         check_and_add_warning("-Wno-ignored-attributes", cxx_compiler)
249         # __attribute__((unused)) really means "might be unused" and
250         # we use it to avoid warnings about things that are unused
251         # in some compilation units, but used in many others.  This
252         # warning insists on complaining about the latter case, which
253         # is annoying, and rather noisy.
254         check_and_add_warning("-Wno-used-but-marked-unused", cxx_compiler)
256     with only_when(depends(c_compiler)(lambda c: c.type != "clang-cl")):
257         # When compiling for Windows with gcc, gcc throws false positives and true
258         # positives where the callsite is ifdef-ed out
259         check_and_add_warning("-Wno-unused-function")
261         # When compiling for Windows with gcc, gcc cannot produce this warning
262         # correctly: it mistakes DWORD_PTR and ULONG_PTR as types you cannot
263         # give NULL to. (You can in fact do that.)
264         check_and_add_warning("-Wno-conversion-null")
266         # Throughout the codebase we regularly have switch statements off of enums
267         # without covering every value in the enum. We don't care about these warnings.
268         check_and_add_warning("-Wno-switch")
270         # Another code pattern we have is using start and end constants in enums of
271         # different types. We do this for safety, but then when comparing it throws
272         # an error, which we would like to ignore. This seems to only affect the MinGW
273         # build, but we're not sure why.
274         check_and_add_warning("-Wno-enum-compare")
276 # Make it an error to be missing function declarations for C code.
277 check_and_add_warning("-Werror=implicit-function-declaration", c_compiler)
279 # New in clang 11. We can't really do anything about this warning.
280 check_and_add_warning("-Wno-psabi")
282 # Disable broken missing-braces warning on old clang versions
283 check_and_add_warning(
284     "-Wno-missing-braces",
285     when=depends(c_compiler)(lambda c: c.type == "clang" and c.version < "6.0"),
288 # Turn on clang thread-safety analysis
289 # Older clangs don't support AutoUnlock, and have other issues
290 check_and_add_warning(
291     "-Wthread-safety",
292     when=depends(c_compiler)(
293         lambda c: c.type in ("clang", "clang-cl") and c.version >= "8.0"
294     ),
297 # Warn if APIs are used without available() checks on macOS.
298 check_and_add_warning("-Werror=unguarded-availability-new", when=target_is_osx)
300 # clang 17 warns about builtins being redefined and... well, we do that in
301 # multiple places, some of which are third-party. Until the situation is
302 # fixed, disable the new warning.
303 check_and_add_warning("-Wno-error=builtin-macro-redefined")
305 # clang 18 has a new warning about VLAs being an extension in C++, but we
306 # have a number of them.
307 check_and_add_warning("-Wno-vla-cxx-extension", cxx_compiler)
309 # Please keep the following last in this file
311 # Avoid requiring complicated logic for extra warning flags in moz.build files.
312 check_and_add_warning("-Wno-unknown-warning-option")
314 set_config("WARNINGS_CFLAGS", warnings_flags.cflags)
315 set_config("WARNINGS_CXXFLAGS", warnings_flags.cxxflags)
316 set_config("WARNINGS_HOST_CFLAGS", warnings_flags.host_cflags)
317 set_config("WARNINGS_HOST_CXXFLAGS", warnings_flags.host_cxxflags)