It took me forever to track down that there was a bug in my code being triggered by /OPT:ICF
:
Because /OPT:ICF can cause the same address to be assigned to different functions or read-only data members (const variables compiled by using /Gy), it can break a program that depends on unique addresses for functions or read-only data members.
(I had been storing and comparing function pointers for equality, which breaks when the linker throws away identical functions.)
Now I need to find every place where I might have done such a thing.
The test case is of course trivial:
//MSVC: /Gy /link /OPT:ICF
int test1(void) { return 0; }
int test2(void) { return 0; }
int main(void) { return test1 == test2; }
I've tried -Wall
, -Wextra
, -Weverything
, -pedantic
, etc. but none of them generate warnings.
Is there any compiler option or tool (whether part of Visual C++, GCC, Clang, or other) that can analyze my code and tell me where I'm comparing function pointers with each other, like in the code above?
/OPT:REF
. – Tollgate/OPT:NOICF
. – Chrysolite/OPT:ICF
anymore. – Tollgate/OPT:ICF
does. I just didn't realize it might be the culprit because I didn't think I was comparing function pointers anywhere, so it took me forever to realize I should try toggling it (and after that it was "merely" the pain of isolating the code). I can't imagine what another poor soul who doesn't know about the flag's behavior will have to go through to figure it out... – Tollgate/OPT:NOICF
which is the default; you can't blame Microsoft for adding a fast-but-dangerous option. Same with fast floating point math - if you don't understand the trade-off, stick with the safe default. – Humanism