Bug 96407 - LTO inlined functions don't inherit disabled warnings
Summary: LTO inlined functions don't inherit disabled warnings
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 10.1.1
: P3 normal
Target Milestone: ---
Assignee: Jeffrey A. Law
Depends on:
Reported: 2020-08-01 09:49 UTC by Richard W.M. Jones
Modified: 2021-05-04 12:31 UTC (History)
2 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2020-08-01 00:00:00


Note You need to log in before you can comment on or make changes to this bug.
Description Richard W.M. Jones 2020-08-01 09:49:17 UTC
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 ---
#include <string.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage="

test (int i)
  char str[i];
  memset (str, 0, sizeof str);
  return str[0]+i;

#pragma GCC diagnostic pop

--- main.c ---
#include <stdio.h>

extern int test (int i);

main (int argc, char *argv[])
  int j = test (argc);
  printf ("%d\n", j);
  return 0;

$ 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.
Comment 1 Mark Wielaard 2020-08-01 15:07:30 UTC
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 \
                      -Wno-error=stack-usage=" \
            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.