Library: Refactoring <alloc> init attribute

This commit is contained in:
Daniel Marjamäki 2020-06-28 21:00:50 +02:00
parent 0e736e0c29
commit 6c588cc3ef
4 changed files with 23 additions and 18 deletions

View File

@ -173,15 +173,18 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
if (arg.declarationId() && Token::Match(arg.typeStartToken(), "%type% * %name% [,)]")) { if (arg.declarationId() && Token::Match(arg.typeStartToken(), "%type% * %name% [,)]")) {
// Treat the pointer as initialized until it is assigned by malloc // Treat the pointer as initialized until it is assigned by malloc
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %varid% = %name% (", arg.declarationId()) && if (!Token::Match(tok, "[;{}] %varid% = %name% (", arg.declarationId()))
mSettings->library.returnuninitdata.count(tok->strAt(3)) == 1U) { continue;
if (arg.typeStartToken()->strAt(-1) == "struct" || (arg.type() && arg.type()->isStructType())) const Library::AllocFunc *allocFunc = mSettings->library.getAllocFuncInfo(tok->tokAt(3));
checkStruct(tok, arg); if (!allocFunc || allocFunc->initData)
else if (arg.typeStartToken()->isStandardType() || arg.typeStartToken()->isEnumType()) { continue;
Alloc alloc = NO_ALLOC;
const std::map<int, VariableValue> variableValue; if (arg.typeStartToken()->strAt(-1) == "struct" || (arg.type() && arg.type()->isStructType()))
checkScopeForVariable(tok->next(), arg, nullptr, nullptr, &alloc, emptyString, variableValue); checkStruct(tok, arg);
} else if (arg.typeStartToken()->isStandardType() || arg.typeStartToken()->isEnumType()) {
Alloc alloc = NO_ALLOC;
const std::map<int, VariableValue> variableValue;
checkScopeForVariable(tok->next(), arg, nullptr, nullptr, &alloc, emptyString, variableValue);
} }
} }
} }
@ -716,10 +719,12 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
const Token *rhs = tok->next()->astOperand2(); const Token *rhs = tok->next()->astOperand2();
while (rhs && rhs->isCast()) while (rhs && rhs->isCast())
rhs = rhs->astOperand2() ? rhs->astOperand2() : rhs->astOperand1(); rhs = rhs->astOperand2() ? rhs->astOperand2() : rhs->astOperand1();
if (rhs && Token::Match(rhs->previous(), "%name% (") && if (rhs && Token::Match(rhs->previous(), "%name% (")) {
mSettings->library.returnuninitdata.count(rhs->previous()->str()) > 0U) { const Library::AllocFunc *allocFunc = mSettings->library.getAllocFuncInfo(rhs->astOperand1());
*alloc = NO_CTOR_CALL; if (allocFunc && !allocFunc->initData) {
continue; *alloc = NO_CTOR_CALL;
continue;
}
} }
} }
if (mTokenizer->isCPP() && var.isPointer() && (var.typeStartToken()->isStandardType() || var.typeStartToken()->isEnumType() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True)) && Token::simpleMatch(tok->next(), "= new")) { if (mTokenizer->isCPP() && var.isPointer() && (var.typeStartToken()->isStandardType() || var.typeStartToken()->isEnumType() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True)) && Token::simpleMatch(tok->next(), "= new")) {

View File

@ -194,7 +194,9 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
temp.groupId = allocationId; temp.groupId = allocationId;
if (memorynode->Attribute("init", "false")) if (memorynode->Attribute("init", "false"))
returnuninitdata.insert(memorynode->GetText()); temp.initData = false;
else
temp.initData = true;
const char *arg = memorynode->Attribute("arg"); const char *arg = memorynode->Attribute("arg");
if (arg) if (arg)

View File

@ -78,6 +78,7 @@ public:
int bufferSizeArg1; int bufferSizeArg1;
int bufferSizeArg2; int bufferSizeArg2;
int reallocArg; int reallocArg;
bool initData;
}; };
/** get allocation info for function */ /** get allocation info for function */
@ -418,7 +419,6 @@ public:
return -1; return -1;
} }
std::set<std::string> returnuninitdata;
std::vector<std::string> defines; // to provide some library defines std::vector<std::string> defines; // to provide some library defines
std::set<std::string> smartPointers; std::set<std::string> smartPointers;

View File

@ -686,11 +686,9 @@ private:
ASSERT(library.functions.empty()); ASSERT(library.functions.empty());
const Library::AllocFunc* af = library.getAllocFuncInfo("CreateX"); const Library::AllocFunc* af = library.getAllocFuncInfo("CreateX");
ASSERT(af && af->arg == 5); ASSERT(af && af->arg == 5 && !af->initData);
const Library::AllocFunc* df = library.getDeallocFuncInfo("DeleteX"); const Library::AllocFunc* df = library.getDeallocFuncInfo("DeleteX");
ASSERT(df && df->arg == 2); ASSERT(df && df->arg == 2);
ASSERT(library.returnuninitdata.find("CreateX") != library.returnuninitdata.cend());
} }
void resource() const { void resource() const {