From 6ca1aba4a72eb727b961c497270e6cae3a8aef5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 10 Feb 2019 19:00:01 +0100 Subject: [PATCH] UninitVar: Fix --experimental-fast issues --- lib/checkuninitvar.cpp | 20 +++++++++++++------- lib/tokenlist.cpp | 1 + test/testuninitvar.cpp | 19 ++++++++----------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 947924ad9..c9c0d750b 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -657,12 +657,15 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var // variable is seen.. if (tok->varId() == var.declarationId()) { // calling function that returns uninit data through pointer.. - if (var.isPointer() && - Token::Match(tok->next(), "= %name% (") && - Token::simpleMatch(tok->linkAt(3), ") ;") && - mSettings->library.returnuninitdata.count(tok->strAt(2)) > 0U) { - *alloc = NO_CTOR_CALL; - continue; + if (var.isPointer() && Token::simpleMatch(tok->next(), "=")) { + const Token *rhs = tok->next()->astOperand2(); + while (rhs && rhs->isCast()) + rhs = rhs->astOperand1(); + if (rhs && Token::Match(rhs->previous(), "%name% (") && + mSettings->library.returnuninitdata.count(rhs->previous()->str()) > 0U) { + *alloc = NO_CTOR_CALL; + continue; + } } if (var.isPointer() && (var.typeStartToken()->isStandardType() || var.typeStartToken()->isEnumType() || (var.type() && var.type()->needInitialization == Type::True)) && Token::simpleMatch(tok->next(), "= new")) { *alloc = CTOR_CALL; @@ -983,7 +986,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al } } - if (alloc == NO_ALLOC && Token::Match(vartok->previous(), "= %name% ;|%cop%")) { + if (alloc == NO_ALLOC && Token::Match(vartok->previous(), "%assign% %name% %cop%|;|)")) { // taking reference? const Token *prev = vartok->tokAt(-2); while (Token::Match(prev, "%name%|*")) @@ -1031,6 +1034,9 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al if (alloc == NO_ALLOC && vartok->next() && vartok->next()->isOp() && !vartok->next()->isAssignmentOp()) return true; + if (alloc == NO_ALLOC && vartok->next()->isAssignmentOp() && vartok->next()->str() != "=") + return true; + if (vartok->strAt(1) == "]") return true; diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index f0a3243a4..d68fc47fd 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -765,6 +765,7 @@ static void compilePrecedence3(Token *&tok, AST_state& state) compileUnaryOp(tok, state, compilePrecedence3); } else if (tok->str() == "(" && iscast(tok)) { Token* castTok = tok; + castTok->isCast(true); tok = tok->link()->next(); compilePrecedence3(tok, state); compileUnaryOp(castTok, state, nullptr); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index c541822b5..1ef08d12a 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -100,8 +100,6 @@ private: std::istringstream istr(code); tokenizer.tokenize(istr, fname); - tokenizer.simplifyTokenList2(); - // Check for redundant code.. CheckUninitVar checkuninitvar(&tokenizer, &settings, this); checkuninitvar.check(); @@ -320,13 +318,13 @@ private: " int b = 1;\n" " (b += a) = 1;\n" "}"); - TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n","", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); checkUninitVar("int f() {\n" " int a,b,c;\n" " a = b = c;\n" "}", "test.cpp", /*verify=*/ false); - TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", errout.str()); checkUninitVar("static void foo()\n" "{\n" @@ -2672,13 +2670,12 @@ private: "}"); ASSERT_EQUALS("", errout.str()); - checkUninitVar("void getLibraryContainer() {\n" + checkUninitVar("void foo() {\n" " void* x;\n" - " Reference< XStorageBasedLibraryContainer >(*Factory)(const Reference< XComponentContext >&, const Reference< XStorageBasedDocument >&)\n" - " = x;\n" - " rxContainer.set((*Factory)(m_aContext, xDocument));\n" - "}", "test.cpp", false); - ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str()); + " int (*f)(int, int) = x;\n" + " dostuff((*f)(a,b));\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); checkUninitVar("void getLibraryContainer() {\n" " Reference< XStorageBasedLibraryContainer >(*Factory)(const Reference< XComponentContext >&, const Reference< XStorageBasedDocument >&);\n" @@ -3867,7 +3864,7 @@ private: " dp=(char *)d; \n" " init(dp); \n" "}", "test.c"); - TODO_ASSERT_EQUALS("", "[test.c:4]: (error) Uninitialized variable: d\n", errout.str()); + ASSERT_EQUALS("", errout.str()); }