| 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/. |
| 6 |
|
| 7 |
option( |
| 8 |
"--enable-warnings-as-errors", |
| 9 |
env="MOZ_ENABLE_WARNINGS_AS_ERRORS", |
| 10 |
default=moz_automation, |
| 11 |
help="{Enable|Disable} treating warnings as errors", |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
@depends("--enable-warnings-as-errors") |
| 16 |
def warnings_as_errors(warnings_as_errors): |
| 17 |
if not warnings_as_errors: |
| 18 |
return "" |
| 19 |
|
| 20 |
return "-Werror" |
| 21 |
|
| 22 |
|
| 23 |
set_config("WARNINGS_AS_ERRORS", warnings_as_errors) |
| 24 |
|
| 25 |
not_clang_cl = depends(c_compiler)(lambda c: c.type != "clang-cl") |
| 26 |
|
| 27 |
# GCC/Clang warnings: |
| 28 |
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html |
| 29 |
# https://clang.llvm.org/docs/DiagnosticsReference.html |
| 30 |
|
| 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")) |
| 35 |
|
| 36 |
# catch implicit truncation of enum values assigned to smaller bit fields |
| 37 |
check_and_add_warning("-Wbitfield-enum-conversion") |
| 38 |
|
| 39 |
# catches bugs, e.g. "if (c); foo();", few false positives |
| 40 |
add_warning("-Wempty-body") |
| 41 |
|
| 42 |
# catches mismatched printf integer sizes. |
| 43 |
check_and_add_warning("-Wformat-type-confusion") |
| 44 |
|
| 45 |
# catches return types with qualifiers like const |
| 46 |
add_warning("-Wignored-qualifiers") |
| 47 |
|
| 48 |
# catches pointer arithmetic using NULL or sizeof(void) |
| 49 |
add_warning("-Wpointer-arith") |
| 50 |
|
| 51 |
# catch modifying constructor parameter that shadows member variable |
| 52 |
check_and_add_warning("-Wshadow-field-in-constructor-modified") |
| 53 |
|
| 54 |
# catches comparing signed/unsigned ints |
| 55 |
add_warning("-Wsign-compare") |
| 56 |
|
| 57 |
# catches comparisons of values and sized types are always true or false |
| 58 |
check_and_add_warning("-Wtautological-constant-in-range-compare") |
| 59 |
|
| 60 |
# catches overflow bugs, few false positives |
| 61 |
add_warning("-Wtype-limits") |
| 62 |
|
| 63 |
# This can be triggered by certain patterns used deliberately in portable code |
| 64 |
check_and_add_warning("-Wno-error=tautological-type-limit-compare") |
| 65 |
|
| 66 |
# catches some dead code |
| 67 |
add_warning("-Wunreachable-code") |
| 68 |
check_and_add_warning("-Wunreachable-code-return") |
| 69 |
|
| 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")), |
| 75 |
) |
| 76 |
|
| 77 |
# turned on by -Wall, but we use offsetof on non-POD types frequently |
| 78 |
add_warning("-Wno-invalid-offsetof", cxx_compiler) |
| 79 |
|
| 80 |
# catches objects passed by value to variadic functions. |
| 81 |
check_and_add_warning("-Wclass-varargs") |
| 82 |
|
| 83 |
# catches empty if/switch/for initialization statements that have no effect |
| 84 |
check_and_add_warning("-Wempty-init-stmt", cxx_compiler) |
| 85 |
|
| 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") |
| 89 |
|
| 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") |
| 95 |
|
| 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) |
| 102 |
|
| 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) |
| 107 |
|
| 108 |
# catches possible misuse of the comma operator |
| 109 |
check_and_add_warning("-Wcomma", cxx_compiler) |
| 110 |
|
| 111 |
# catches duplicated conditions in if-else-if chains |
| 112 |
check_and_add_warning("-Wduplicated-cond") |
| 113 |
|
| 114 |
# catches unintentional switch case fallthroughs |
| 115 |
check_and_add_warning("-Wimplicit-fallthrough", cxx_compiler) |
| 116 |
|
| 117 |
# Warn about suspicious uses of logical operators in expressions. |
| 118 |
check_and_add_warning("-Wlogical-op") |
| 119 |
|
| 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") |
| 124 |
|
| 125 |
# catch multiple declarations of ObjC method found |
| 126 |
check_and_add_warning("-Wduplicate-method-match") |
| 127 |
|
| 128 |
# catch ObjC method with no return type specified |
| 129 |
check_and_add_warning("-Wmissing-method-return-type") |
| 130 |
|
| 131 |
# catch implicit conversions between ObjC BOOL and int |
| 132 |
check_and_add_warning("-Wobjc-signed-char-bool") |
| 133 |
|
| 134 |
# catch semicolon before ObjC method body |
| 135 |
check_and_add_warning("-Wsemicolon-before-method-body") |
| 136 |
|
| 137 |
# catch ObjC method parameter type not matching super class method |
| 138 |
check_and_add_warning("-Wsuper-class-method-mismatch") |
| 139 |
|
| 140 |
# catches string literals used in boolean expressions |
| 141 |
check_and_add_warning("-Wstring-conversion") |
| 142 |
|
| 143 |
# we inline 'new' and 'delete' in mozalloc |
| 144 |
check_and_add_warning("-Wno-inline-new-delete", cxx_compiler) |
| 145 |
|
| 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") |
| 149 |
|
| 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") |
| 153 |
|
| 154 |
# false positives depending on optimization |
| 155 |
check_and_add_warning("-Wno-error=array-bounds") |
| 156 |
|
| 157 |
# false positives depending on optimizations |
| 158 |
check_and_add_warning("-Wno-error=free-nonheap-object") |
| 159 |
|
| 160 |
# Would be a pain to fix all occurrences, for very little gain |
| 161 |
check_and_add_warning("-Wno-multistatement-macros") |
| 162 |
|
| 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") |
| 166 |
|
| 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") |
| 170 |
|
| 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") |
| 174 |
|
| 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") |
| 178 |
) |
| 179 |
|
| 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) |
| 185 |
|
| 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") |
| 194 |
|
| 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") |
| 199 |
|
| 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") |
| 204 |
|
| 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) |
| 209 |
|
| 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) |
| 215 |
|
| 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") |
| 221 |
|
| 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) |
| 225 |
|
| 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) |
| 235 |
|
| 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) |
| 240 |
|
| 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) |
| 244 |
|
| 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) |
| 248 |
|
| 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) |
| 255 |
|
| 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") |
| 260 |
|
| 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") |
| 265 |
|
| 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") |
| 269 |
|
| 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") |
| 275 |
|
| 276 |
# Make it an error to be missing function declarations for C code. |
| 277 |
check_and_add_warning("-Werror=implicit-function-declaration", c_compiler) |
| 278 |
|
| 279 |
# New in clang 11. We can't really do anything about this warning. |
| 280 |
check_and_add_warning("-Wno-psabi") |
| 281 |
|
| 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"), |
| 286 |
) |
| 287 |
|
| 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 |
), |
| 295 |
) |
| 296 |
|
| 297 |
# Warn if APIs are used without available() checks on macOS. |
| 298 |
check_and_add_warning("-Werror=unguarded-availability-new", when=target_is_osx) |
| 299 |
|
| 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") |
| 304 |
|
| 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) |
| 308 |
|
| 309 |
# Please keep the following last in this file |
| 310 |
|
| 311 |
# Avoid requiring complicated logic for extra warning flags in moz.build files. |
| 312 |
check_and_add_warning("-Wno-unknown-warning-option") |
| 313 |
|
| 314 |
# clang 21 warns about charN_t conversions, and we have a lot that need a careful |
| 315 |
# audit rather than plainly adding static_casts. |
| 316 |
check_and_add_warning("-Wno-character-conversion") |
| 317 |
|
| 318 |
set_config("WARNINGS_CFLAGS", warnings_flags.cflags) |
| 319 |
set_config("WARNINGS_CXXFLAGS", warnings_flags.cxxflags) |
| 320 |
set_config("WARNINGS_HOST_CFLAGS", warnings_flags.host_cflags) |
| 321 |
set_config("WARNINGS_HOST_CXXFLAGS", warnings_flags.host_cxxflags) |