Skip to content

Commit c498d63

Browse files
committed
Suppress spurious clang -Wbraced-scalar-init
As reported in <llvm/llvm-project#54702>, clang warns about use of braces in contexts where they are useful to explicitly disallow narrowing conversions. Such braces should not be removed, as it would allow a future change to accidentally introduce a narrowing conversion and not receive a diagnostic. As of this commit, the LLVM issue has seen no activity and there is no indication the spurious warning will be fixed. Add an SConf test to detect if the compiler warns for these cases, and suppress it if so.
1 parent ab8f421 commit c498d63

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

SConstruct

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,42 @@ constexpr literal_as_type<T, v...> operator""_literal_as_type();
26212621
''', msg='whether compiler accepts string literal operator templates'):
26222622
self.successful_flags['CXXFLAGS'].append('-Wno-gnu-string-literal-operator-template')
26232623
2624+
@_custom_test
2625+
def check_compiler_overzealous_braced_scalar_init(self,context,_text='''
2626+
/* clang-16 issues a spurious -Wbraced-scalar-init warning if the result
2627+
* is returned anonymously.
2628+
2629+
```
2630+
error: braces around scalar initializer [-Werror,-Wbraced-scalar-init]
2631+
```
2632+
2633+
* Removing the braces allows an implicit narrowing of the result, which would
2634+
* be a trap for a future maintenance programmer.
2635+
*
2636+
* gcc accepts the anonymous brace-initialized result.
2637+
*/
2638+
unsigned f(unsigned i);
2639+
unsigned f(unsigned i)
2640+
{
2641+
return {i + 1};
2642+
}
2643+
2644+
unsigned g(unsigned i);
2645+
unsigned g(unsigned i)
2646+
{
2647+
++i;
2648+
return {i};
2649+
}
2650+
''',_main='''
2651+
f({5});
2652+
g({6});
2653+
''',successflags={'CXXFLAGS' : ['-Wno-braced-scalar-init']}):
2654+
if self.Compile(context, text=_text, main=_main, msg='whether compiler accepts braced variables as function arguments and return values'):
2655+
return
2656+
if self.Compile(context, text=_text, main=_main, msg='whether compiler accepts -Wno-braced-scalar-init', successflags=successflags):
2657+
return
2658+
raise SCons.Errors.StopError("C++ compiler rejects braced scalar initialization, even with `-Wno-braced-scalar-init`.")
2659+
26242660
@_custom_test
26252661
def check_have_std_ranges(self,context,_testflags={'CPPDEFINES' : ['_LIBCPP_ENABLE_EXPERIMENTAL']}):
26262662
text = '''

0 commit comments

Comments
 (0)