Source code

Revision control

Copy as Markdown

Other Tools

# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at
default=depends("MOZ_AUTOMATION")(lambda x: bool(x)),
help="{Enable|Disable} treating warnings as errors",
def warnings_as_errors(warnings_as_errors):
if not warnings_as_errors:
return ""
return "-Werror"
set_config("WARNINGS_AS_ERRORS", warnings_as_errors)
not_clang_cl = depends(c_compiler)(lambda c: c.type != "clang-cl")
# GCC/Clang warnings:
# Lots of useful warnings
add_warning("-Wall", when=not_clang_cl)
# In clang-cl, -Wall actually means -Weverything. -W3 does mean -Wall.
add_warning("-W3", when=depends(c_compiler)(lambda c: c.type == "clang-cl"))
# catch implicit truncation of enum values assigned to smaller bit fields
# catches bugs, e.g. "if (c); foo();", few false positives
# catches mismatched printf integer sizes.
# catches return types with qualifiers like const
# catches pointer arithmetic using NULL or sizeof(void)
# catch modifying constructor parameter that shadows member variable
# catches comparing signed/unsigned ints
# catches comparisons of values and sized types are always true or false
# catches overflow bugs, few false positives
# This can be triggered by certain patterns used deliberately in portable code
# catches some dead code
# catches parameters that are set but not read
# Only enable on clang because gcc reports false positives.
when=depends(c_compiler)(lambda c: c.type in ("clang", "clang-cl")),
# turned on by -Wall, but we use offsetof on non-POD types frequently
add_warning("-Wno-invalid-offsetof", cxx_compiler)
# catches objects passed by value to variadic functions.
# catches empty if/switch/for initialization statements that have no effect
check_and_add_warning("-Wempty-init-stmt", cxx_compiler)
# catches some implicit conversion of floats to ints
# catches issues around loops
# But, disable range-loop-analysis because it can raise unhelpful false
# positives.
# Enable some C++20 compat warnings. We can remove these flags after we compile
# as C++20 (bug 1768116), because they will be enabled by default:
check_and_add_warning("-Wcomma-subscript", cxx_compiler)
check_and_add_warning("-Wvolatile", cxx_compiler)
# Disable some C++20 errors to be fixed in bugs 1791958, 1791955, and 1775161.
check_and_add_warning("-Wno-deprecated-anon-enum-enum-conversion", cxx_compiler)
check_and_add_warning("-Wno-deprecated-enum-enum-conversion", cxx_compiler)
check_and_add_warning("-Wno-deprecated-this-capture", cxx_compiler)
# catches possible misuse of the comma operator
check_and_add_warning("-Wcomma", cxx_compiler)
# catches duplicated conditions in if-else-if chains
# catches unintentional switch case fallthroughs
check_and_add_warning("-Wimplicit-fallthrough", cxx_compiler)
# Warn about suspicious uses of logical operators in expressions.
# Enable some ObjC diagnostics that are only relevant when targeting macOS:
with only_when(depends(target)(lambda t: t.kernel == "Darwin")):
# catch redeclaration of ObjC method parameter name
# catch multiple declarations of ObjC method found
# catch ObjC method with no return type specified
# catch implicit conversions between ObjC BOOL and int
# catch semicolon before ObjC method body
# catch ObjC method parameter type not matching super class method
# catches string literals used in boolean expressions
# we inline 'new' and 'delete' in mozalloc
check_and_add_warning("-Wno-inline-new-delete", cxx_compiler)
# Prevent the following GCC warnings from being treated as errors:
# too many false positives
# we don't want our builds held hostage when a platform-specific API
# becomes deprecated.
# false positives depending on optimization
# false positives depending on optimizations
# Would be a pain to fix all occurrences, for very little gain
# Disable the -Werror for -Wclass-memaccess as we have a long
# tail of issues to fix
# -Watomic-alignment is a new warning in clang 7 that seems way too broad.
# New warning with clang 15. Catches uses of deprecated builtins in abseil-cpp.
# catches format/argument mismatches with printf
c_format_warning, cxx_format_warning = check_and_add_warning(
"-Wformat", when=depends(target)(lambda t: t.kernel != "WINNT")
# Add compile-time warnings for unprotected functions and format functions
# that represent possible security problems. Enable this only when -Wformat
# is enabled, otherwise it is an error
check_and_add_warning("-Wformat-security", when=c_format_warning & cxx_format_warning)
check_and_add_warning("-Wformat-overflow=2", when=c_format_warning & cxx_format_warning)
# Other Windows specific things
with only_when(target_is_windows):
# When compiling for Windows with gcc, we encounter lots of "#pragma warning"'s
# which is an MSVC-only pragma that GCC does not recognize.
# With clang-cl, as it claims to be MSVC it would be difficult to add
# #if defined(_MSC_VER) && !defined(__clang__) everywhere we use such pragmas,
# so just ignore them.
with only_when(depends(c_compiler)(lambda c: c.type == "clang-cl")):
# We get errors about various #pragma intrinsic directives from
# clang-cl, and we don't need to hear about those.
# clang-cl's Intrin.h marks things like _ReadWriteBarrier as
# __attribute((__deprecated__)). This is nice to know, but since we don't
# get the equivalent warning from MSVC, let's just ignore it.
# This warns for reasonable things like:
# enum { X = 0xffffffffU };
# which is annoying for IDL headers.
check_and_add_warning("-Wno-microsoft-enum-value", cxx_compiler)
# This warns for cases that would be reached by the Microsoft
# #include rules, but also currently warns on cases that would
# *also* be reached by standard C++ include rules. That
# behavior doesn't seem useful, so we turn it off.
check_and_add_warning("-Wno-microsoft-include", cxx_compiler)
# We use a function like:
# __declspec(noreturn) __inline void f() {}
# which -Winvalid-noreturn complains about. Again, MSVC seems
# OK with it, so let's silence the warning.
# Missing |override| on virtual function declarations isn't
# something that MSVC currently warns about.
check_and_add_warning("-Wno-inconsistent-missing-override", cxx_compiler)
# We use -DHAS_EXCEPTIONS=0, which removes the |throw()|
# declaration on |operator delete(void*)|. However, clang-cl
# must internally declare |operator delete(void*)| differently,
# which causes this warning for virtually every file in the
# tree. clang-cl doesn't support -fno-exceptions or equivalent,
# so there doesn't seem to be any way to convince clang-cl to
# declare |delete| differently. Therefore, suppress this
# warning.
check_and_add_warning("-Wno-implicit-exception-spec-mismatch", cxx_compiler)
# Macros like STDMETHOD() and IFACEMETHOD() can declare
# __attribute__((nothrow)) on their respective method declarations,
# while the definitions are left without the matching attribute.
check_and_add_warning("-Wno-microsoft-exception-spec", cxx_compiler)
# At least one MSVC header and several headers in-tree have
# unused typedefs, so turn this on.
check_and_add_warning("-Wno-unused-local-typedef", cxx_compiler)
# jemalloc uses __declspec(allocator) as a profiler hint,
# which clang-cl doesn't understand.
check_and_add_warning("-Wno-ignored-attributes", cxx_compiler)
# __attribute__((unused)) really means "might be unused" and
# we use it to avoid warnings about things that are unused
# in some compilation units, but used in many others. This
# warning insists on complaining about the latter case, which
# is annoying, and rather noisy.
check_and_add_warning("-Wno-used-but-marked-unused", cxx_compiler)
with only_when(depends(c_compiler)(lambda c: c.type != "clang-cl")):
# When compiling for Windows with gcc, gcc throws false positives and true
# positives where the callsite is ifdef-ed out
# When compiling for Windows with gcc, gcc cannot produce this warning
# correctly: it mistakes DWORD_PTR and ULONG_PTR as types you cannot
# give NULL to. (You can in fact do that.)
# Throughout the codebase we regularly have switch statements off of enums
# without covering every value in the enum. We don't care about these warnings.
# Another code pattern we have is using start and end constants in enums of
# different types. We do this for safety, but then when comparing it throws
# an error, which we would like to ignore. This seems to only affect the MinGW
# build, but we're not sure why.
# Make it an error to be missing function declarations for C code.
check_and_add_warning("-Werror=implicit-function-declaration", c_compiler)
# New in clang 11. We can't really do anything about this warning.
# Disable broken missing-braces warning on old clang versions
when=depends(c_compiler)(lambda c: c.type == "clang" and c.version < "6.0"),
# Turn on clang thread-safety analysis
# Older clangs don't support AutoUnlock, and have other issues
lambda c: c.type in ("clang", "clang-cl") and c.version >= "8.0"
# Warn if APIs are used without available() checks on macOS.
check_and_add_warning("-Werror=unguarded-availability-new", when=target_is_osx)
# clang 17 warns about builtins being redefined and... well, we do that in
# multiple places, some of which are third-party. Until the situation is
# fixed, disable the new warning.
# clang 18 has a new warning about VLAs being an extension in C++, but we
# have a number of them.
check_and_add_warning("-Wno-vla-cxx-extension", cxx_compiler)
# Please keep the following last in this file
# Avoid requiring complicated logic for extra warning flags in files.
set_config("WARNINGS_CFLAGS", warnings_flags.cflags)
set_config("WARNINGS_CXXFLAGS", warnings_flags.cxxflags)
set_config("WARNINGS_HOST_CFLAGS", warnings_flags.host_cflags)
set_config("WARNINGS_HOST_CXXFLAGS", warnings_flags.host_cxxflags)