Fix more (potential) multi-threading issues by moving static local vars (non-POD-type) to file scope

This commit is contained in:
Alexander Mai 2015-06-13 16:22:43 +02:00
parent d704e97203
commit 40d7baa6bb
2 changed files with 21 additions and 14 deletions

View File

@ -1827,6 +1827,11 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const
return false; return false;
} }
namespace {
// The container contains the STL types whose operator[] is not a const.
static const std::set<std::string> stl_containers_not_const = make_container< std::set<std::string> >() << "map" << "unordered_map";
}
bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& memberAccessed) const bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& memberAccessed) const
{ {
// if the function doesn't have any assignment nor function call, // if the function doesn't have any assignment nor function call,
@ -1856,7 +1861,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
return false; return false;
} }
const Token* jumpBackToken = 0; const Token* jumpBackToken = nullptr;
const Token *lastVarTok = tok1; const Token *lastVarTok = tok1;
const Token *end = tok1; const Token *end = tok1;
for (;;) { for (;;) {
@ -1867,9 +1872,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
} else if (end->strAt(1) == "[") { } else if (end->strAt(1) == "[") {
if (end->varId()) { if (end->varId()) {
const Variable *var = end->variable(); const Variable *var = end->variable();
// The container contains the STL types whose operator[] is not a const. if (var && var->isStlType(stl_containers_not_const))
static const std::set<std::string> stl_containers = make_container< std::set<std::string> >() << "map" << "unordered_map";
if (var && var->isStlType(stl_containers))
return false; return false;
} }
if (!jumpBackToken) if (!jumpBackToken)
@ -2109,7 +2112,7 @@ void CheckClass::checkPureVirtualFunctionCall()
std::map<const Function *, std::list<const Token *> > callsPureVirtualFunctionMap; std::map<const Function *, std::list<const Token *> > callsPureVirtualFunctionMap;
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
if (scope->function == 0 || !scope->function->hasBody() || if (scope->function == nullptr || !scope->function->hasBody() ||
!(scope->function->isConstructor() || !(scope->function->isConstructor() ||
scope->function->isDestructor())) scope->function->isDestructor()))
continue; continue;

View File

@ -136,6 +136,12 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
} }
} }
namespace {
static const std::set<std::string> stl_stream = make_container< std::set<std::string> >() <<
"fstream" << "ifstream" << "iostream" << "istream" <<
"istringstream" << "ofstream" << "ostream" << "ostringstream" <<
"stringstream" << "wistringstream" << "wostringstream" << "wstringstream";
}
/** /**
* Is there a pointer dereference? Everything that should result in * Is there a pointer dereference? Everything that should result in
@ -148,11 +154,6 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
*/ */
bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown) bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
{ {
static const std::set<std::string> stl_stream = make_container< std::set<std::string> >() <<
"fstream" << "ifstream" << "iostream" << "istream" <<
"istringstream" << "ofstream" << "ostream" << "ostringstream" <<
"stringstream" << "wistringstream" << "wostringstream" << "wstringstream";
unknown = false; unknown = false;
const Token* parent = tok->astParent(); const Token* parent = tok->astParent();
@ -373,13 +374,16 @@ void CheckNullPointer::nullPointer()
nullPointerByDeRefAndChec(); nullPointerByDeRefAndChec();
} }
namespace {
static const std::set<std::string> stl_istream = make_container< std::set<std::string> >() <<
"fstream" << "ifstream" << "iostream" << "istream" <<
"istringstream" << "stringstream" << "wistringstream" << "wstringstream";
}
/** Dereferencing null constant (simplified token list) */ /** Dereferencing null constant (simplified token list) */
void CheckNullPointer::nullConstantDereference() void CheckNullPointer::nullConstantDereference()
{ {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
static const std::set<std::string> stl_stream = make_container< std::set<std::string> >() <<
"fstream" << "ifstream" << "iostream" << "istream" <<
"istringstream" << "stringstream" << "wistringstream" << "wstringstream";
const std::size_t functions = symbolDatabase->functionScopes.size(); const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t i = 0; i < functions; ++i) {
@ -436,7 +440,7 @@ void CheckNullPointer::nullConstantDereference()
nullPointerError(tok); nullPointerError(tok);
if (tok2 && tok2->varId() != 0) { if (tok2 && tok2->varId() != 0) {
const Variable *var = tok2->variable(); const Variable *var = tok2->variable();
if (var && var->isStlType(stl_stream)) if (var && var->isStlType(stl_istream))
nullPointerError(tok); nullPointerError(tok);
} }
} }