summaryrefslogtreecommitdiffstats
path: root/build/moz.configure/warnings.configure
blob: f4c195732fb9a8ba88d0771ba70d466bfecfc011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# -*- 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 http://mozilla.org/MPL/2.0/.

option(
    "--enable-warnings-as-errors",
    env="MOZ_ENABLE_WARNINGS_AS_ERRORS",
    default=depends("MOZ_AUTOMATION")(lambda x: bool(x)),
    help="{Enable|Disable} treating warnings as errors",
)


@depends("--enable-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:
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
# https://clang.llvm.org/docs/DiagnosticsReference.html

# 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
check_and_add_warning("-Wbitfield-enum-conversion")

# catches deprecated implicit capture of `this` in lambdas.
check_and_add_warning("-Wdeprecated-this-capture", cxx_compiler)

# catches bugs, e.g. "if (c); foo();", few false positives
add_warning("-Wempty-body")

# catches mismatched printf integer sizes.
check_and_add_warning("-Wformat-type-confusion")

# catches return types with qualifiers like const
add_warning("-Wignored-qualifiers")

# catches pointer arithmetic using NULL or sizeof(void)
add_warning("-Wpointer-arith")

# catch modifying constructor parameter that shadows member variable
check_and_add_warning("-Wshadow-field-in-constructor-modified")

# catches comparing signed/unsigned ints
add_warning("-Wsign-compare")

# catches comparisons of values and sized types are always true or false
check_and_add_warning("-Wtautological-constant-in-range-compare")

# catches overflow bugs, few false positives
add_warning("-Wtype-limits")

# This can be triggered by certain patterns used deliberately in portable code
check_and_add_warning("-Wno-error=tautological-type-limit-compare")

# catches some dead code
add_warning("-Wunreachable-code")
check_and_add_warning("-Wunreachable-code-return")

# catches parameters that are set but not read
# Only enable on clang because gcc reports false positives.
check_and_add_warning(
    "-Wunused-but-set-parameter",
    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.
check_and_add_warning("-Wclass-varargs")

# 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
check_and_add_warning("-Wfloat-overflow-conversion")
check_and_add_warning("-Wfloat-zero-conversion")

# catches issues around loops
check_and_add_warning("-Wloop-analysis")
# But, disable range-loop-analysis because it can raise unhelpful false
# positives.
check_and_add_warning("-Wno-range-loop-analysis")

# 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("-Wc++2a-compat", cxx_compiler)
check_and_add_warning("-Wcomma-subscript", cxx_compiler)
check_and_add_warning("-Wenum-compare-conditional")
check_and_add_warning("-Wenum-float-conversion")
check_and_add_warning("-Wvolatile", cxx_compiler)

# Downgrade some C++20 warnings-as-errors to warnings that we can fix after we
# compile as C++20 (bug 1768116). They don't need to block upgrading to C++20.
check_and_add_warning("-Wno-error=deprecated", cxx_compiler)
check_and_add_warning("-Wno-error=deprecated-anon-enum-enum-conversion", cxx_compiler)
check_and_add_warning("-Wno-error=deprecated-enum-enum-conversion", cxx_compiler)
check_and_add_warning("-Wno-error=deprecated-pragma", cxx_compiler)
check_and_add_warning("-Wno-error=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
check_and_add_warning("-Wduplicated-cond")

# catches unintentional switch case fallthroughs
check_and_add_warning("-Wimplicit-fallthrough", cxx_compiler)

# Warn about suspicious uses of logical operators in expressions.
check_and_add_warning("-Wlogical-op")

# 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
    check_and_add_warning("-Wduplicate-method-arg")

    # catch multiple declarations of ObjC method found
    check_and_add_warning("-Wduplicate-method-match")

    # catch ObjC method with no return type specified
    check_and_add_warning("-Wmissing-method-return-type")

    # catch implicit conversions between ObjC BOOL and int
    check_and_add_warning("-Wobjc-signed-char-bool")

    # catch semicolon before ObjC method body
    check_and_add_warning("-Wsemicolon-before-method-body")

    # catch ObjC method parameter type not matching super class method
    check_and_add_warning("-Wsuper-class-method-mismatch")

# catches string literals used in boolean expressions
check_and_add_warning("-Wstring-conversion")

# 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
check_and_add_warning("-Wno-error=maybe-uninitialized")

# we don't want our builds held hostage when a platform-specific API
# becomes deprecated.
check_and_add_warning("-Wno-error=deprecated-declarations")

# false positives depending on optimization
check_and_add_warning("-Wno-error=array-bounds")

# false positives depending on optimizations
check_and_add_warning("-Wno-error=free-nonheap-object")

# Would be a pain to fix all occurrences, for very little gain
check_and_add_warning("-Wno-multistatement-macros")

# Disable the -Werror for -Wclass-memaccess as we have a long
# tail of issues to fix
check_and_add_warning("-Wno-error=class-memaccess")

# -Watomic-alignment is a new warning in clang 7 that seems way too broad.
# https://bugs.llvm.org/show_bug.cgi?id=38593
check_and_add_warning("-Wno-error=atomic-alignment")

# New warning with clang 15. Catches uses of deprecated builtins in abseil-cpp.
# https://bugzilla.mozilla.org/show_bug.cgi?id=1779528
check_and_add_warning("-Wno-error=deprecated-builtins")

# 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.
    check_and_add_warning("-Wno-unknown-pragmas")

    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.
        check_and_add_warning("-Wno-ignored-pragmas")

        # 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.
        check_and_add_warning("-Wno-deprecated-declarations")

        # 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.
        check_and_add_warning("-Wno-invalid-noreturn")

        # 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
        check_and_add_warning("-Wno-unused-function")

        # 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.)
        check_and_add_warning("-Wno-conversion-null")

        # 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.
        check_and_add_warning("-Wno-switch")

        # 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.
        check_and_add_warning("-Wno-enum-compare")

# 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.
check_and_add_warning("-Wno-psabi")

# Disable broken missing-braces warning on old clang versions
check_and_add_warning(
    "-Wno-missing-braces",
    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
check_and_add_warning(
    "-Wthread-safety",
    when=depends(c_compiler)(
        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.
check_and_add_warning("-Wno-error=builtin-macro-redefined")

# Please keep the following last in this file

# Avoid requiring complicated logic for extra warning flags in moz.build files.
check_and_add_warning("-Wno-unknown-warning-option")

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)