UninitVar: Fix --experimental-fast issues

This commit is contained in:
Daniel Marjamäki 2019-02-10 19:00:01 +01:00
parent 6717f49f20
commit 6ca1aba4a7
3 changed files with 22 additions and 18 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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());
}