$ cat f1.cpp
#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
void myfree(void *ptr)
$ cat f2.cpp
void myfree(void *);
static char c;
This code is intentionally bogus just to trigger the warning. The situation that caused this was correct code, with a false positive warning I was trying to suppress.
$ gcc -O2 -include f1.cpp f2.cpp
[no warning, as expected]
$ gcc -O2 -flto f1.cpp f2.cpp
In function ‘myfree.constprop’,
inlined from ‘main’ at f2.cpp:6:11:
f1.cpp:4:19: warning: attempt to free a non-heap object ‘c’ [-Wfree-nonheap-object]
Confirmed. We do not track warning options (and thus optimize pragmas / attributes) across LTO because they are not saved in the function specific
optimization flag section.