Uninitialized variables; Try to remove heuristics for assignment with overloaded <<. That is non-standard behavior for the << operator. Let's see how much FPs we get.

This commit is contained in:
Daniel Marjamäki 2021-05-24 13:17:50 +02:00
parent 3a498de306
commit a2e709a652
2 changed files with 9 additions and 30 deletions

View File

@ -1242,21 +1242,6 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer,
if (valueExpr->valueType() && valueExpr->valueType()->type == ValueType::Type::VOID)
return nullptr;
// overloaded << operator to initialize variable?
if (Token::simpleMatch(valueExpr->astParent(), "<<") && !valueExpr->astParent()->astParent()) {
if (astIsLhs(valueExpr))
return nullptr;
const Token *lhs = valueExpr->astParent()->astOperand1();
if (Token::simpleMatch(lhs, "<<"))
return valueExpr;
if (Token::simpleMatch(lhs->previous(), "std ::"))
return valueExpr;
const Variable *var = lhs->variable();
if (var && (var->typeStartToken()->isStandardType() || var->typeStartToken()->isEnumType() || Token::simpleMatch(var->typeStartToken(), "std ::")))
return valueExpr;
return nullptr;
}
}
if (astIsRhs(derefValue) && isLikelyStreamRead(mTokenizer->isCPP(), derefValue->astParent()))
return nullptr;

View File

@ -450,10 +450,10 @@ private:
checkUninitVar("int a(FArchive &arc) {\n" // #3060 (initialization through operator<<)
" int *p;\n"
" arc << p;\n"
" arc << p;\n" // <- TODO initialization?
" return *p;\n"
"}");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
checkUninitVar("void a() {\n"
" int ret;\n"
@ -462,13 +462,13 @@ private:
"test.c");
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
// #4320
// #4320 says this is a FP. << is overloaded.
checkUninitVar("int f() {\n"
" int a;\n"
" a << 1;\n"
" a << 1;\n" // <- TODO initialization?
" return a;\n"
"}");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
// #4673
checkUninitVar("void f() {\n"
@ -2871,13 +2871,6 @@ private:
" char a = c << 2;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", errout.str());
// #4320
checkUninitVar("void f() {\n"
" int a;\n"
" a << 1;\n" // there might be a operator<<
"}");
ASSERT_EQUALS("", errout.str());
}
void uninitvar8() {
@ -4476,10 +4469,11 @@ private:
valueFlowUninit("int a(FArchive &arc) {\n" // #3060 (initialization through operator<<)
" int *p;\n"
" arc << p;\n"
" return *p;\n" // fp: should not warn
" arc << p;\n" // <- TODO initialization?
" return *p;\n"
"}");
TODO_ASSERT_EQUALS("", "[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n"
"[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
// #4320
valueFlowUninit("void f() {\n"