Bug 80922 - #pragma diagnostic ignored not honoured with -flto
Summary: #pragma diagnostic ignored not honoured with -flto
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 7.0.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: diagnostic, lto
: 109916 (view as bug list)
Depends on:
Blocks: Wfree-nonheap-object 106823 107936
  Show dependency treegraph
Reported: 2017-05-30 22:10 UTC by Thiago Macieira
Modified: 2024-07-11 19:29 UTC (History)
9 users (show)

See Also:
Host: 96407
Known to work:
Known to fail: 10.2.0, 11.0, 7.3.0, 8.3.0, 9.2.0
Last reconfirmed: 2021-02-14 00:00:00


Note You need to log in before you can comment on or make changes to this bug.
Description Thiago Macieira 2017-05-30 22:10:46 UTC
$ cat f1.cpp
#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
void myfree(void *ptr)

$ cat f2.cpp
void myfree(void *);

static char c;
int main()

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]
Comment 1 Richard Biener 2017-05-31 07:21:18 UTC
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.
Comment 2 Romain Geissler 2020-02-20 13:48:04 UTC

I am hitting this issue when trying to leverage lto in many libraries (on the exact same warning class by the way, but this affects all warnings).

Since the last entry in this bug 3 years ago, is there now updated plans to stream/support warning flags/pragma diagnostics in the LTO sections in gcc 11 ?

Comment 3 Richard Biener 2020-02-20 14:31:19 UTC
ISTR Martin made -W options streamed per function for GCC 10?
Comment 4 Martin Liška 2020-02-20 15:16:14 UTC
No, it's still not supported with LTO, it's not so easy to implement that. I can return to it in the next stage.
Comment 5 Martin Sebor 2021-02-14 23:24:40 UTC
No change in GCC 11.  FWIW, I tried the patch for pr98465 but it doesn't help:
https://gcc.gnu.org/pipermail/gcc-patches/2021-January/564060.html here.
Comment 6 Martin Sebor 2021-02-14 23:28:15 UTC
We should make sure the suppression works the same way with and without -flto.
Comment 7 Martin Liška 2021-02-15 08:16:40 UTC
I can give it another try in the next stage1. It's not easy though.
Comment 8 pefoley2 2023-01-21 05:59:17 UTC
Note that this appears to be affecting the gcc source tree.

Attempting a build with --with-build-config=bootstrap-lto --enable-werror errors out with:
In function ‘release’,
    inlined from ‘release’ at ../../gcc/vec.h:1915:0,
    inlined from ‘__dt_base ’ at ../../gcc/vec.h:1574:0,
    inlined from ‘visit_reference_op_call.isra’ at ../../gcc/tree-ssa-sccvn.cc:5562:0:
../../gcc/vec.h:316: error: ‘free’ called on unallocated object ‘accesses’ [-Werror=free-nonheap-object]
  316 |   ::free (v);
../../gcc/tree-ssa-sccvn.cc: In function ‘visit_reference_op_call.isra’:
../../gcc/tree-ssa-sccvn.cc:5457: note: declared here
 5457 |       auto_vec<ao_ref, accesses_limit> accesses;
lto1: all warnings being treated as errors

Due to this pragma being ignored:
Comment 9 Andrew Pinski 2023-01-21 06:17:42 UTC
(In reply to pefoley2 from comment #8)
> Note that this appears to be affecting the gcc source tree.
> Attempting a build with --with-build-config=bootstrap-lto --enable-werror
> errors out with:
 Yes but lto bootstrap disables werror on purpose so you adding --enable-werror is not supported.
Comment 10 pefoley2 2023-01-23 04:33:33 UTC
It does? I wasn't aware of that.
My read of the configure options is that the two options are tangential.
And from a quick skim, I couldn't find anything that made enabling lto suppress Werror.
Besides, regardless of whether it's supported or not, it's another example of this issue in the wild.
Comment 11 Alexander Monakov 2023-05-20 11:08:01 UTC
*** Bug 109916 has been marked as a duplicate of this bug. ***
Comment 12 Andrea Bocci 2024-07-10 17:34:24 UTC
Another example of this bug, where 

#pragma GCC diagnostic ignored "-Warray-bounds" 

does not work with LTO:
  - https://github.com/cms-sw/cmssw/pull/45340#issuecomment-2217442378
  - https://github.com/cms-sw/cmssw/issues/45179

The only workaround seems to be to remove -Warray-bounds from the compiler flags in all places where the affected header file is included.