c++ - Is there a way to detect inline function ODR violations? -
so have code in 2 separate translation units:
// a.cpp #include <stdio.h> inline int func() { return 5; } int proxy(); int main() { printf("%d", func() + proxy()); } // b.cpp inline int func() { return 6; } int proxy() { return func(); }
when compiled result 10
. when compiled -o3 (inlining on) 11
.
i have done odr violation func()
.
it showed when started merging sources of different dll's fewer dll's.
i have tried:
- gcc 5.1
-wodr
(which requires-flto
) - gold linker
-detect-odr-violations
- setting
asan_options=detect_odr_violation=1
before running instrumented binary address sanitizer.
asan can supposedly catch other odr violations (global vars different types or that...)
this nasty c++ issue , amazed there isn't reliable tooling detecting it.
pherhaps have misused 1 of tools tried? or there different tool this?
edit:
the problem remains unnoticed when make 2 implementations of func()
drastically different don't compiled same amount of instructions.
this affects class methods defined inside class body - implicitly inline.
// a.cpp struct { int data; a() : data(5){} }; // b.cpp struct { int data; a() : data(6){} };
legacy code lots of copy/paste + minor modifications after joy.
the simplest way detect such concerns copy functions single compilation unit (create 1 temporarily if needed). c++ compiler able detect , report duplicate definitions when compiling file.
Comments
Post a Comment