From 6c588cc3efe4bf14f7c4d4dd503dceef8b26f16a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 28 Jun 2020 21:00:50 +0200 Subject: [PATCH] Library: Refactoring init attribute --- lib/checkuninitvar.cpp | 31 ++++++++++++++++++------------- lib/library.cpp | 4 +++- lib/library.h | 2 +- test/testlibrary.cpp | 4 +--- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 03323abfc..e46f94304 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -173,15 +173,18 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set if (arg.declarationId() && Token::Match(arg.typeStartToken(), "%type% * %name% [,)]")) { // Treat the pointer as initialized until it is assigned by malloc for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { - if (Token::Match(tok, "[;{}] %varid% = %name% (", arg.declarationId()) && - mSettings->library.returnuninitdata.count(tok->strAt(3)) == 1U) { - if (arg.typeStartToken()->strAt(-1) == "struct" || (arg.type() && arg.type()->isStructType())) - checkStruct(tok, arg); - else if (arg.typeStartToken()->isStandardType() || arg.typeStartToken()->isEnumType()) { - Alloc alloc = NO_ALLOC; - const std::map variableValue; - checkScopeForVariable(tok->next(), arg, nullptr, nullptr, &alloc, emptyString, variableValue); - } + if (!Token::Match(tok, "[;{}] %varid% = %name% (", arg.declarationId())) + 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())) + checkStruct(tok, arg); + else if (arg.typeStartToken()->isStandardType() || arg.typeStartToken()->isEnumType()) { + Alloc alloc = NO_ALLOC; + const std::map 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(); while (rhs && rhs->isCast()) rhs = rhs->astOperand2() ? rhs->astOperand2() : rhs->astOperand1(); - if (rhs && Token::Match(rhs->previous(), "%name% (") && - mSettings->library.returnuninitdata.count(rhs->previous()->str()) > 0U) { - *alloc = NO_CTOR_CALL; - continue; + if (rhs && Token::Match(rhs->previous(), "%name% (")) { + const Library::AllocFunc *allocFunc = mSettings->library.getAllocFuncInfo(rhs->astOperand1()); + if (allocFunc && !allocFunc->initData) { + *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")) { diff --git a/lib/library.cpp b/lib/library.cpp index f5192a0ca..5d4da2949 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -194,7 +194,9 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) temp.groupId = allocationId; if (memorynode->Attribute("init", "false")) - returnuninitdata.insert(memorynode->GetText()); + temp.initData = false; + else + temp.initData = true; const char *arg = memorynode->Attribute("arg"); if (arg) diff --git a/lib/library.h b/lib/library.h index f7975faa9..53bdbfe6d 100644 --- a/lib/library.h +++ b/lib/library.h @@ -78,6 +78,7 @@ public: int bufferSizeArg1; int bufferSizeArg2; int reallocArg; + bool initData; }; /** get allocation info for function */ @@ -418,7 +419,6 @@ public: return -1; } - std::set returnuninitdata; std::vector defines; // to provide some library defines std::set smartPointers; diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp index 606149599..d34d4d88f 100644 --- a/test/testlibrary.cpp +++ b/test/testlibrary.cpp @@ -686,11 +686,9 @@ private: ASSERT(library.functions.empty()); 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"); ASSERT(df && df->arg == 2); - - ASSERT(library.returnuninitdata.find("CreateX") != library.returnuninitdata.cend()); } void resource() const {