5 ms·
"Here it's clear that "NeverCalled" is never called so it's clear that Do is never assigned." I don't think that is clear. If the given file is linked with ano
by culturedsystems 9y ago
"Here it's clear that "NeverCalled" is never called so it's clear that Do is never assigned."
I don't think that is clear. If the given file is linked with another file that initializes a static variable with a function that calls NeverCalled, the initialization will be done before main is called, and so NeverCalled will be called. The compiler can't tell from just analysing the one file shown here whether or not NeverCalled is called.
- alkonaut 9y ago> I don't think that is clear Right. I missed that the uncalled function was public. So the issue is not clear until you link I suppose, so only detected "globally" whether the function is called. is it fair to say that that kind of global analysis is rare in the compiler+linker toolchain world for C, and usually left to static analyzers instead? > The compiler can't tell from just analysing the one file shown here whether or not NeverCalled is called. Right, and making it static you get the -Wunused-function. Great. This warning is about the NeverCalled function being unused though, and not about Do being unassigned before it's called. Curious, is there any way to get a warning for this? This is UB, correct? And the assignment is now known not to happen? typedef int (*Function)(); static Function Do; void main() { Do(); }
- tom_mellior 9y ago> another file that initializes a static variable with a function that calls NeverCalled How would that work in actual code? Static initialization must be done with constant expressions. C++ has constexpr functions, but they cannot call NeverCalled unless it is also marked constexpr, can they?