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,8 +173,12 @@ 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;
const Library::AllocFunc *allocFunc = mSettings->library.getAllocFuncInfo(tok->tokAt(3));
if (!allocFunc || allocFunc->initData)
continue;
if (arg.typeStartToken()->strAt(-1) == "struct" || (arg.type() && arg.type()->isStructType())) if (arg.typeStartToken()->strAt(-1) == "struct" || (arg.type() && arg.type()->isStructType()))
checkStruct(tok, arg); checkStruct(tok, arg);
else if (arg.typeStartToken()->isStandardType() || arg.typeStartToken()->isEnumType()) { else if (arg.typeStartToken()->isStandardType() || arg.typeStartToken()->isEnumType()) {
@ -187,7 +191,6 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
} }
} }
} }
}
void CheckUninitVar::checkStruct(const Token *tok, const Variable &structvar) void CheckUninitVar::checkStruct(const Token *tok, const Variable &structvar)
{ {
@ -716,12 +719,14 @@ 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());
if (allocFunc && !allocFunc->initData) {
*alloc = NO_CTOR_CALL; *alloc = NO_CTOR_CALL;
continue; 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")) {
*alloc = CTOR_CALL; *alloc = CTOR_CALL;

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 {