On 09/12/2020 11:00, Jakub Jelinek wrote: > On Wed, Dec 09, 2020 at 10:50:22AM +0100, David Brown wrote: >> I'd say that it makes sense to have such a warning as a natural >> enhancement to the existing "-Wunused-but-set-variable" warning. But I > > That is not really possible. > The -Wunused-but-set-* warning works by having two bits for the DECL, > TREE_USED and DECL_READ_P, where any uses mark the var TREE_USED and > (conservatively) what can read the value marks it DECL_READ_P > and -Wunused-but-set-* is then variables that are TREE_USED and > !DECL_READ_P. All this needs to be done early in the FE. > For the static vars, the optimization to remove them altogether is done > much later, and at that point the compiler doesn't know if it isn't used > because all the reads in the program have been optimized away vs. there were > none. > > Jakub >
That's what I thought might be the case. I've seen this before in situations where it might seem to the layman that it is "obvious" that there should be a warning here, or that "the compiler can optimise here, surely it can also issue a warning". If it were easy to implement a warning in a situation like this, I guess the gcc developers would have implemented it already! I hope this gives the OP the information he is looking for. David