Skip to content

Commit 5f659af

Browse files
committed
ninja backend: don't hide all compiler warnings for transpiled languages
This was originally added for vala only, with the rationale that vala generates bad code that has warnings. Unfortunately, the rationale was fatally flawed. The compiler warns about a number of things, which the user can control depending on their code (or their code generator's code), but some of those things are absolutely critical to warn about. In particular, GCC 14 and clang 17 are updating their defaults to warn -- and error by default for -- invalid C code that breaks the standard, but has been silently accepted for over 20 years "because lots of people do it". The code in question is UB, and compilers will generate faulty machine code that behaves erroneously and probably has a mass of CVEs waiting to happen. Compiler warnings are NOT safe to just... universally turn off. Compiler warnings could be either: - coding style lints - threatening statements that the code is factually and behaviorally wrong There is no magic bullet to ignore the former while respecting the latter. And the very last thing we should ever do is pass `-w`, since that causes ALL warnings to be disabled, even the manually added `-Werror=XXX`. If vala generated code creates warnings, then the vala compiler can decrease the log level by generating better code, or by adding warning suppression pragmas for *specific* issues, such as unused functions.
1 parent 30184a4 commit 5f659af

File tree

7 files changed

+11
-87
lines changed

7 files changed

+11
-87
lines changed

mesonbuild/backend/backends.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ def get_no_stdlib_args(self, target: 'build.BuildTarget', compiler: 'Compiler')
986986
return compiler.get_no_stdinc_args()
987987
return []
988988

989-
def generate_basic_compiler_args(self, target: build.BuildTarget, compiler: 'Compiler', no_warn_args: bool = False) -> 'CompilerArgs':
989+
def generate_basic_compiler_args(self, target: build.BuildTarget, compiler: 'Compiler') -> 'CompilerArgs':
990990
# Create an empty commands list, and start adding arguments from
991991
# various sources in the order in which they must override each other
992992
# starting from hard-coded defaults followed by build options and so on.
@@ -999,17 +999,12 @@ def generate_basic_compiler_args(self, target: build.BuildTarget, compiler: 'Com
999999
commands += self.get_no_stdlib_args(target, compiler)
10001000
# Add things like /NOLOGO or -pipe; usually can't be overridden
10011001
commands += compiler.get_always_args()
1002-
# Only add warning-flags by default if the buildtype enables it, and if
1003-
# we weren't explicitly asked to not emit warnings (for Vala, f.ex)
1004-
if no_warn_args:
1005-
commands += compiler.get_no_warn_args()
1006-
else:
1007-
# warning_level is a string, but mypy can't determine that
1008-
commands += compiler.get_warn_args(T.cast('str', target.get_option(OptionKey('warning_level'))))
1002+
# warning_level is a string, but mypy can't determine that
1003+
commands += compiler.get_warn_args(T.cast('str', target.get_option(OptionKey('warning_level'))))
10091004
# Add -Werror if werror=true is set in the build options set on the
10101005
# command-line or default_options inside project(). This only sets the
10111006
# action to be done for warnings if/when they are emitted, so it's ok
1012-
# to set it after get_no_warn_args() or get_warn_args().
1007+
# to set it after or get_warn_args().
10131008
if target.get_option(OptionKey('werror')):
10141009
commands += compiler.get_werror_args()
10151010
# Add compile args for c_* or cpp_* build options set on the

mesonbuild/backend/ninjabackend.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ def generate_rust_target(self, target: build.BuildTarget) -> None:
19391939
if cratetype in {'bin', 'dylib'}:
19401940
args.extend(rustc.get_linker_always_args())
19411941

1942-
args += self.generate_basic_compiler_args(target, rustc, False)
1942+
args += self.generate_basic_compiler_args(target, rustc)
19431943
# Rustc replaces - with _. spaces or dots are not allowed, so we replace them with underscores
19441944
args += ['--crate-name', target.name.replace('-', '_').replace(' ', '_').replace('.', '_')]
19451945
depfile = os.path.join(target.subdir, target.name + '.d')
@@ -2804,10 +2804,9 @@ def generate_inc_dir(self, compiler: 'Compiler', d: str, basedir: str, is_system
28042804
bargs = []
28052805
return (sargs, bargs)
28062806

2807-
def _generate_single_compile(self, target: build.BuildTarget, compiler: 'Compiler',
2808-
is_generated: bool = False) -> 'CompilerArgs':
2807+
def _generate_single_compile(self, target: build.BuildTarget, compiler: Compiler) -> CompilerArgs:
28092808
commands = self._generate_single_compile_base_args(target, compiler)
2810-
commands += self._generate_single_compile_target_args(target, compiler, is_generated)
2809+
commands += self._generate_single_compile_target_args(target, compiler)
28112810
return commands
28122811

28132812
def _generate_single_compile_base_args(self, target: build.BuildTarget, compiler: 'Compiler') -> 'CompilerArgs':
@@ -2825,14 +2824,10 @@ def _generate_single_compile_base_args(self, target: build.BuildTarget, compiler
28252824
return commands
28262825

28272826
@lru_cache(maxsize=None)
2828-
def _generate_single_compile_target_args(self, target: build.BuildTarget, compiler: 'Compiler',
2829-
is_generated: bool = False) -> 'ImmutableListProtocol[str]':
2830-
# The code generated by valac is usually crap and has tons of unused
2831-
# variables and such, so disable warnings for Vala C sources.
2832-
no_warn_args = is_generated == 'vala'
2827+
def _generate_single_compile_target_args(self, target: build.BuildTarget, compiler: Compiler) -> ImmutableListProtocol[str]:
28332828
# Add compiler args and include paths from several sources; defaults,
28342829
# build options, external dependencies, etc.
2835-
commands = self.generate_basic_compiler_args(target, compiler, no_warn_args)
2830+
commands = self.generate_basic_compiler_args(target, compiler)
28362831
# Add custom target dirs as includes automatically, but before
28372832
# target-specific include directories.
28382833
if target.implicit_include_directories:
@@ -2901,7 +2896,7 @@ def generate_common_compile_args_per_src_type(self, target: build.BuildTarget) -
29012896
if use_pch and 'mw' not in compiler.id:
29022897
commands += self.get_pch_include_args(compiler, target)
29032898

2904-
commands += self._generate_single_compile_target_args(target, compiler, is_generated=False)
2899+
commands += self._generate_single_compile_target_args(target, compiler)
29052900

29062901
# Metrowerks compilers require PCH include args to come after intraprocedural analysis args
29072902
if use_pch and 'mw' in compiler.id:
@@ -2935,7 +2930,7 @@ def generate_single_compile(self, target: build.BuildTarget, src,
29352930
if use_pch and 'mw' not in compiler.id:
29362931
commands += self.get_pch_include_args(compiler, target)
29372932

2938-
commands += self._generate_single_compile_target_args(target, compiler, is_generated)
2933+
commands += self._generate_single_compile_target_args(target, compiler)
29392934

29402935
# Metrowerks compilers require PCH include args to come after intraprocedural analysis args
29412936
if use_pch and 'mw' in compiler.id:

test cases/failing build/1 vala c werror/meson.build

Lines changed: 0 additions & 10 deletions
This file was deleted.

test cases/failing build/1 vala c werror/prog.vala

Lines changed: 0 additions & 7 deletions
This file was deleted.

test cases/failing build/1 vala c werror/unused-var.c

Lines changed: 0 additions & 8 deletions
This file was deleted.

test cases/vala/5 target glib/meson.build

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
project('valatest', 'vala', 'c')
22

3-
if not meson.is_unity()
4-
add_global_arguments('-Werror', language : 'c')
5-
endif
6-
73
valadeps = [dependency('glib-2.0', version : '>=2.32'), dependency('gobject-2.0')]
84

95
e = executable('valaprog', 'GLib.Thread.vala', 'retcode.c', dependencies : valadeps)

unittests/linuxliketests.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -298,43 +298,6 @@ def test_symlink_builddir(self) -> None:
298298
self.build()
299299
self._run(self.mtest_command)
300300

301-
def test_vala_c_warnings(self):
302-
'''
303-
Test that no warnings are emitted for C code generated by Vala. This
304-
can't be an ordinary test case because we need to inspect the compiler
305-
database.
306-
https://github.com/mesonbuild/meson/issues/864
307-
'''
308-
if not shutil.which('valac'):
309-
raise SkipTest('valac not installed.')
310-
testdir = os.path.join(self.vala_test_dir, '5 target glib')
311-
self.init(testdir)
312-
compdb = self.get_compdb()
313-
vala_command = None
314-
c_command = None
315-
for each in compdb:
316-
if each['file'].endswith('GLib.Thread.c'):
317-
vala_command = each['command']
318-
elif each['file'].endswith('GLib.Thread.vala'):
319-
continue
320-
elif each['file'].endswith('retcode.c'):
321-
c_command = each['command']
322-
else:
323-
m = 'Unknown file {!r} in vala_c_warnings test'.format(each['file'])
324-
raise AssertionError(m)
325-
self.assertIsNotNone(vala_command)
326-
self.assertIsNotNone(c_command)
327-
# -w suppresses all warnings, should be there in Vala but not in C
328-
self.assertIn(" -w ", vala_command)
329-
self.assertNotIn(" -w ", c_command)
330-
# -Wall enables all warnings, should be there in C but not in Vala
331-
self.assertNotIn(" -Wall ", vala_command)
332-
self.assertIn(" -Wall ", c_command)
333-
# -Werror converts warnings to errors, should always be there since it's
334-
# injected by an unrelated piece of code and the project has werror=true
335-
self.assertIn(" -Werror ", vala_command)
336-
self.assertIn(" -Werror ", c_command)
337-
338301
@skipIfNoPkgconfig
339302
def test_qtdependency_pkgconfig_detection(self):
340303
'''

0 commit comments

Comments
 (0)