warnings.configure (15312B)
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 not_clang = depends(c_compiler)(lambda c: c.type != "clang") 27 not_clang_based = depends(c_compiler)(lambda c: c.type not in ("clang", "clang-cl")) 28 29 # GCC/Clang warnings: 30 # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html 31 # https://clang.llvm.org/docs/DiagnosticsReference.html 32 33 # Lots of useful warnings 34 add_warning("-Wall", when=not_clang_cl) 35 # In clang-cl, -Wall actually means -Weverything. -W3 does mean -Wall. 36 add_warning("-W3", when=depends(c_compiler)(lambda c: c.type == "clang-cl")) 37 38 # catch implicit truncation of enum values assigned to smaller bit fields 39 check_and_add_warning("-Wbitfield-enum-conversion", min_clang_version="5.0.0") 40 41 # catches bugs, e.g. "if (c); foo();", few false positives 42 add_warning("-Wempty-body") 43 44 # catches mismatched printf integer sizes. 45 check_and_add_warning("-Wformat-type-confusion", min_clang_version="10.0.0") 46 47 # catches return types with qualifiers like const 48 add_warning("-Wignored-qualifiers") 49 50 # catches pointer arithmetic using NULL or sizeof(void) 51 add_warning("-Wpointer-arith") 52 53 # catch modifying constructor parameter that shadows member variable 54 check_and_add_warning( 55 "-Wshadow-field-in-constructor-modified", min_clang_version="4.0.0" 56 ) 57 58 # catches comparing signed/unsigned ints 59 add_warning("-Wsign-compare") 60 61 # catches comparisons of values and sized types are always true or false 62 check_and_add_warning( 63 "-Wtautological-constant-in-range-compare", min_clang_version="6.0.0" 64 ) 65 66 # catches overflow bugs, few false positives 67 add_warning("-Wtype-limits") 68 69 # This can be triggered by certain patterns used deliberately in portable code 70 check_and_add_warning( 71 "-Wno-error=tautological-type-limit-compare", min_clang_version="6.0.0" 72 ) 73 74 # catches some dead code 75 add_warning("-Wunreachable-code") 76 check_and_add_warning("-Wunreachable-code-return", min_clang_version="4.0.0") 77 78 # catches parameters that are set but not read 79 # Only enable on clang because gcc reports false positives. 80 check_and_add_warning( 81 "-Wunused-but-set-parameter", 82 min_clang_version="13.0.0", 83 when=depends(c_compiler)(lambda c: c.type in ("clang", "clang-cl")), 84 ) 85 86 # turned on by -Wall, but we use offsetof on non-POD types frequently 87 add_warning("-Wno-invalid-offsetof", cxx_compiler) 88 89 # catches objects passed by value to variadic functions. 90 check_and_add_warning("-Wclass-varargs", min_clang_version="4.0.0") 91 92 # catches empty if/switch/for initialization statements that have no effect 93 check_and_add_warning("-Wempty-init-stmt", cxx_compiler, min_clang_version="8.0.0") 94 95 # catches some implicit conversion of floats to ints 96 check_and_add_warning("-Wfloat-overflow-conversion", min_clang_version="4.0.0") 97 check_and_add_warning("-Wfloat-zero-conversion", min_clang_version="4.0.0") 98 99 # catches issues around loops 100 check_and_add_warning("-Wloop-analysis", min_clang_version="4.0.0") 101 # But, disable range-loop-analysis because it can raise unhelpful false 102 # positives. 103 check_and_add_warning("-Wno-range-loop-analysis", min_clang_version="4.0.0") 104 105 # Suppress some C++20 enum conversion warnings to be fixed later. These 106 # warnings will become hard errors in C++26 and must be fixed before updating. 107 # Disable some C++20 errors to be fixed in bugs 1791958, 1791955, and 1775161. 108 check_and_add_warning( 109 "-Wno-deprecated-anon-enum-enum-conversion", # Bug 1791958 110 cxx_compiler, 111 min_clang_version="10.0.0", 112 ) 113 check_and_add_warning( 114 "-Wno-deprecated-enum-enum-conversion", # Bug 1791955 115 cxx_compiler, 116 min_clang_version="10.0.0", 117 ) 118 # Suppress C++20 warnings about implicit capture of `this` until they are fixed 119 # in bug 1775161. gcc doesn't have a -Wdeprecated-this-capture flag like clang, 120 # so temporarily downgrade all gcc -Wdeprecated errors to warnings. 121 check_and_add_warning( 122 "-Wno-deprecated-this-capture", cxx_compiler, min_clang_version="6.0.0" 123 ) 124 check_and_add_warning("-Wno-error=deprecated", when=building_with_gcc) 125 126 # Enable some C++23 compat warnings. We can remove these flags after we compile 127 # as C++23 (bug 1880762) because they will be enabled by default: 128 check_and_add_warning("-Wdeprecated-literal-operator", cxx_compiler) 129 check_and_add_warning("-Winvalid-utf8") 130 131 # catches possible misuse of the comma operator 132 check_and_add_warning("-Wcomma", cxx_compiler, min_clang_version="4.0.0") 133 134 # catches duplicated conditions in if-else-if chains 135 check_and_add_warning("-Wduplicated-cond", when=not_clang_based) 136 137 # catches unintentional switch case fallthroughs 138 check_and_add_warning("-Wimplicit-fallthrough", cxx_compiler, min_clang_version="4.0.0") 139 140 # Warn about suspicious uses of logical operators in expressions. 141 check_and_add_warning("-Wlogical-op", when=not_clang_based) 142 143 # Enable some ObjC diagnostics that are only relevant when targeting macOS: 144 with only_when(depends(target)(lambda t: t.kernel == "Darwin")): 145 # catch redeclaration of ObjC method parameter name 146 check_and_add_warning("-Wduplicate-method-arg") 147 148 # catch multiple declarations of ObjC method found 149 check_and_add_warning("-Wduplicate-method-match") 150 151 # catch ObjC method with no return type specified 152 check_and_add_warning("-Wmissing-method-return-type") 153 154 # catch implicit conversions between ObjC BOOL and int 155 check_and_add_warning("-Wobjc-signed-char-bool") 156 157 # catch semicolon before ObjC method body 158 check_and_add_warning("-Wsemicolon-before-method-body") 159 160 # catch ObjC method parameter type not matching super class method 161 check_and_add_warning("-Wsuper-class-method-mismatch") 162 163 # catches string literals used in boolean expressions 164 check_and_add_warning("-Wstring-conversion", min_clang_version="4.0.0") 165 166 # we inline 'new' and 'delete' in mozalloc 167 check_and_add_warning("-Wno-inline-new-delete", cxx_compiler, min_clang_version="4.0.0") 168 169 # Prevent the following GCC warnings from being treated as errors: 170 # too many false positives 171 check_and_add_warning("-Wno-error=maybe-uninitialized", when=not_clang_based) 172 173 # we don't want our builds held hostage when a platform-specific API 174 # becomes deprecated. 175 check_and_add_warning("-Wno-error=deprecated-declarations", min_clang_version="4.0.0") 176 177 # false positives depending on optimization 178 check_and_add_warning("-Wno-error=array-bounds", min_clang_version="4.0.0") 179 180 # false positives depending on optimizations 181 check_and_add_warning("-Wno-error=free-nonheap-object", min_clang_version="12.0.0") 182 183 # Would be a pain to fix all occurrences, for very little gain 184 check_and_add_warning("-Wno-multistatement-macros", when=not_clang_based) 185 186 # Disable the -Werror for -Wclass-memaccess as we have a long 187 # tail of issues to fix 188 check_and_add_warning("-Wno-error=class-memaccess", when=not_clang_based) 189 190 # -Watomic-alignment is a new warning in clang 7 that seems way too broad. 191 # https://bugs.llvm.org/show_bug.cgi?id=38593 192 check_and_add_warning("-Wno-error=atomic-alignment", min_clang_version="7.0.0") 193 194 # New warning with clang 15. Catches uses of deprecated builtins in abseil-cpp. 195 # https://bugzilla.mozilla.org/show_bug.cgi?id=1779528 196 check_and_add_warning("-Wno-error=deprecated-builtins", min_clang_version="15.0.0") 197 198 # catches format/argument mismatches with printf 199 c_format_warning, cxx_format_warning = check_and_add_warning( 200 "-Wformat", when=depends(target)(lambda t: t.kernel != "WINNT") 201 ) 202 203 # Add compile-time warnings for unprotected functions and format functions 204 # that represent possible security problems. Enable this only when -Wformat 205 # is enabled, otherwise it is an error 206 check_and_add_warning( 207 "-Wformat-security", 208 when=c_format_warning & cxx_format_warning, 209 min_clang_version="4.0.0", 210 ) 211 check_and_add_warning( 212 "-Wformat-overflow=2", when=c_format_warning & cxx_format_warning & not_clang_based 213 ) 214 215 # Other Windows specific things 216 with only_when(target_is_windows): 217 # When compiling for Windows with gcc, we encounter lots of "#pragma warning"'s 218 # which is an MSVC-only pragma that GCC does not recognize. 219 # With clang-cl, as it claims to be MSVC it would be difficult to add 220 # #if defined(_MSC_VER) && !defined(__clang__) everywhere we use such pragmas, 221 # so just ignore them. 222 check_and_add_warning("-Wno-unknown-pragmas") 223 224 with only_when(depends(c_compiler)(lambda c: c.type == "clang-cl")): 225 # We get errors about various #pragma intrinsic directives from 226 # clang-cl, and we don't need to hear about those. 227 check_and_add_warning("-Wno-ignored-pragmas") 228 229 # clang-cl's Intrin.h marks things like _ReadWriteBarrier as 230 # __attribute((__deprecated__)). This is nice to know, but since we don't 231 # get the equivalent warning from MSVC, let's just ignore it. 232 check_and_add_warning("-Wno-deprecated-declarations") 233 234 # This warns for reasonable things like: 235 # enum { X = 0xffffffffU }; 236 # which is annoying for IDL headers. 237 check_and_add_warning("-Wno-microsoft-enum-value", cxx_compiler) 238 239 # This warns for cases that would be reached by the Microsoft 240 # #include rules, but also currently warns on cases that would 241 # *also* be reached by standard C++ include rules. That 242 # behavior doesn't seem useful, so we turn it off. 243 check_and_add_warning("-Wno-microsoft-include", cxx_compiler) 244 245 # We use a function like: 246 # __declspec(noreturn) __inline void f() {} 247 # which -Winvalid-noreturn complains about. Again, MSVC seems 248 # OK with it, so let's silence the warning. 249 check_and_add_warning("-Wno-invalid-noreturn") 250 251 # Missing |override| on virtual function declarations isn't 252 # something that MSVC currently warns about. 253 check_and_add_warning("-Wno-inconsistent-missing-override", cxx_compiler) 254 255 # We use -DHAS_EXCEPTIONS=0, which removes the |throw()| 256 # declaration on |operator delete(void*)|. However, clang-cl 257 # must internally declare |operator delete(void*)| differently, 258 # which causes this warning for virtually every file in the 259 # tree. clang-cl doesn't support -fno-exceptions or equivalent, 260 # so there doesn't seem to be any way to convince clang-cl to 261 # declare |delete| differently. Therefore, suppress this 262 # warning. 263 check_and_add_warning("-Wno-implicit-exception-spec-mismatch", cxx_compiler) 264 265 # Macros like STDMETHOD() and IFACEMETHOD() can declare 266 # __attribute__((nothrow)) on their respective method declarations, 267 # while the definitions are left without the matching attribute. 268 check_and_add_warning("-Wno-microsoft-exception-spec", cxx_compiler) 269 270 # At least one MSVC header and several headers in-tree have 271 # unused typedefs, so turn this on. 272 check_and_add_warning("-Wno-unused-local-typedef", cxx_compiler) 273 274 # jemalloc uses __declspec(allocator) as a profiler hint, 275 # which clang-cl doesn't understand. 276 check_and_add_warning("-Wno-ignored-attributes", cxx_compiler) 277 278 # __attribute__((unused)) really means "might be unused" and 279 # we use it to avoid warnings about things that are unused 280 # in some compilation units, but used in many others. This 281 # warning insists on complaining about the latter case, which 282 # is annoying, and rather noisy. 283 check_and_add_warning("-Wno-used-but-marked-unused", cxx_compiler) 284 285 with only_when(depends(c_compiler)(lambda c: c.type != "clang-cl")): 286 # When compiling for Windows with gcc, gcc throws false positives and true 287 # positives where the callsite is ifdef-ed out 288 check_and_add_warning("-Wno-unused-function") 289 290 # When compiling for Windows with gcc, gcc cannot produce this warning 291 # correctly: it mistakes DWORD_PTR and ULONG_PTR as types you cannot 292 # give NULL to. (You can in fact do that.) 293 check_and_add_warning("-Wno-conversion-null") 294 295 # Throughout the codebase we regularly have switch statements off of enums 296 # without covering every value in the enum. We don't care about these warnings. 297 check_and_add_warning("-Wno-switch") 298 299 # Another code pattern we have is using start and end constants in enums of 300 # different types. We do this for safety, but then when comparing it throws 301 # an error, which we would like to ignore. This seems to only affect the MinGW 302 # build, but we're not sure why. 303 check_and_add_warning("-Wno-enum-compare") 304 305 # Make it an error to be missing function declarations for C code. 306 check_and_add_warning( 307 "-Werror=implicit-function-declaration", c_compiler, min_clang_version="4.0.0" 308 ) 309 310 # New in clang 11. We can't really do anything about this warning. 311 check_and_add_warning("-Wno-psabi", min_clang_version="11.0.0") 312 313 # Disable broken missing-braces warning on old clang versions 314 check_and_add_warning( 315 "-Wno-missing-braces", 316 when=depends(c_compiler)(lambda c: c.type == "clang" and c.version < "6.0"), 317 min_clang_version="4.0.0", 318 ) 319 320 # Turn on clang thread-safety analysis 321 # Older clangs don't support AutoUnlock, and have other issues 322 check_and_add_warning( 323 "-Wthread-safety", 324 when=depends(c_compiler)( 325 lambda c: c.type in ("clang", "clang-cl") and c.version >= "8.0" 326 ), 327 min_clang_version="4.0.0", 328 ) 329 330 # Warn if APIs are used without available() checks on macOS. 331 check_and_add_warning( 332 "-Werror=unguarded-availability-new", when=target_is_osx, min_clang_version="5.0.0" 333 ) 334 335 # clang 17 warns about builtins being redefined and... well, we do that in 336 # multiple places, some of which are third-party. Until the situation is 337 # fixed, disable the new warning. 338 check_and_add_warning("-Wno-error=builtin-macro-redefined", min_clang_version="4.0.0") 339 340 # clang 18 has a new warning about VLAs being an extension in C++, but we 341 # have a number of them. 342 check_and_add_warning( 343 "-Wno-vla-cxx-extension", cxx_compiler, min_clang_version="18.0.0" 344 ) 345 346 # Avoid requiring complicated logic for extra warning flags in moz.build files. 347 check_and_add_warning("-Wno-unknown-warning-option", min_clang_version="4.0.0") 348 349 # clang 21 warns about charN_t conversions, and we have a lot that need a careful 350 # audit rather than plainly adding static_casts. 351 check_and_add_warning("-Wno-character-conversion", min_clang_version="21.0.0") 352 353 # Please keep the following last in this file 354 355 set_config("WARNINGS_CFLAGS", warnings_flags.cflags) 356 set_config("WARNINGS_CXXFLAGS", warnings_flags.cxxflags) 357 set_config("WARNINGS_HOST_CFLAGS", warnings_flags.host_cflags) 358 set_config("WARNINGS_HOST_CXXFLAGS", warnings_flags.host_cxxflags)