I'm working on compiling Cppcheck on AIX using xlC
. Every checker class is derived from a Check
class, whose constructor is responsible for registering that type of checker in a global static list.
Here's the relevant part of the code in question (filenames link to full source on Github):
check.h
class Check {
public:
Check() {
instances().push_back(this);
instances().sort();
}
static std::list<Check *> &instances() {
static std::list<Check *> _instances;
return _instances;
}
// ...
};
checkbufferoverrun.h
class CheckBufferOverrun: public Check {
// ...
};
checkbufferoverrun.cpp
// Register this check class (by creating a static instance of it)
namespace
{
CheckBufferOverrun instance;
}
Notice how the _instances
static variable is declared inside a static
function in the header file (there is no corresponding check.cpp
file). When compiled with g++
, the compiler and linker work together to ensure that there is only one implementation of the static instances()
function, and therefore only one instance of the static _instances
list. All the different checker classes instantiated in different .cpp
files get registered in the same _instances
list together.
However, under AIX's xlC
, this same code ends up creating a different instances()
function for every .cpp
file in which it is included, which each having a different static _instances
list. So there is no longer a single central _instances
list, which causes Cppcheck to not run most of its checks.
Which compiler's behaviour is correct in this case?
Update: This question is not about how to fix the problem, I've already done that. I'm curious about which behaviour is correct.