Fixed #3910 (False positive: Variable is not assigned a value (pointerArray alias))

This commit is contained in:
Daniel Marjamäki 2012-06-24 16:54:37 +02:00
parent be464b43b3
commit 25b24d149f
2 changed files with 27 additions and 2 deletions

View File

@ -430,7 +430,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
Variables::VariableUsage* var2 = variables.find(varid2);
if (var2) { // local variable (alias or read it)
if (var1->_type == Variables::pointer) {
if (var1->_type == Variables::pointer || var1->_type == Variables::pointerArray) {
if (dereference)
variables.read(varid2);
else {
@ -439,8 +439,12 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
var2->_type == Variables::pointer) {
bool replace = true;
// pointerArray => don't replace
if (var1->_type == Variables::pointerArray)
replace = false;
// check if variable declared in same scope
if (scope == var1->_scope)
else if (scope == var1->_scope)
replace = true;
// not in same scope as declaration
@ -839,6 +843,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
} else if (var->_type == Variables::pointer || var->_type == Variables::reference) {
variables.read(varid);
variables.writeAliases(varid);
} else if (var->_type == Variables::pointerArray) {
tok = doAssignment(variables, tok, false, scope);
} else
variables.writeAll(varid);
}

View File

@ -1185,6 +1185,25 @@ private:
" *(++ptr) = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #3910
functionVariableUsage("int foo() {\n"
" char buf[5];\n"
" char *data[2];\n"
" data[0] = buf;\n"
" do_something(data);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("int foo() {\n"
" char buf1[5];\n"
" char buf2[5];\n"
" char *data[2];\n"
" data[0] = buf1;\n"
" data[1] = buf2;\n"
" do_something(data);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvar17() { // ticket #1720