This is a very reduced example from some real code where we are using -Werror -Wstack-usage=5000.
We want most functions to have limited stack usage, but in some cases we have carefully examined functions to ensure their stack usage isn't unbounded - but it's hard to prove it - so we have disabled the warning for those functions.
--- test.c ---
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage="
test (int i)
memset (str, 0, sizeof str);
#pragma GCC diagnostic pop
--- main.c ---
extern int test (int i);
main (int argc, char *argv)
int j = test (argc);
printf ("%d\n", j);
$ gcc -O2 -flto main.c test.c -Werror -Wstack-usage=5000 -o a.out
main.c: In function ‘main’:
main.c:6:1: error: stack usage might be unbounded [-Werror=stack-usage=]
6 | main (int argc, char *argv)
lto1: all warnings being treated as errors
Note that if you combine these two functions into a single file, it
does *not* warn/error, even though presumably it can easily inline the
test() function. So LTO seems to be causing the difference.
Same for using -Wstack-usage= on the command line. It is one of the workarounds needed for building elfutils with LTO enabled:
This version of elfutils handles debuginfo generated with GCC LTO
better and it can finally be build with GCC LTO itself:
export AR=gcc-ar RANLIB=gcc-ranlib NM=gcc-nm
./configure CFLAGS="-O2 -g -flto=auto -flto-partition=none \
CXXFLAGS="-O2 -g -flto=auto -flto-partition=none"
Note the two workaround. -flto-partition=none is needed to preserve the
symbol versioning. -Wno-error=stack-usage= is needed because LTO will
combine objects build with and without a -Wstack-usage limit.