Fixed #6542 (FP: Variable 'lcount' is not assigned a value - using address of integer array)

This commit is contained in:
Daniel Marjamäki 2017-07-01 11:31:51 +02:00
parent db01ea1408
commit d6f066482e
2 changed files with 20 additions and 1 deletions

View File

@ -1147,6 +1147,16 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.use(tok->varId(), tok); // use = read + write variables.use(tok->varId(), tok); // use = read + write
} }
else if (Token::Match(tok, "[?:]")) {
// TODO: This is to avoid FP for 'p = c ? buf1 : buf2;'.
// Check if the address is taken. If not this is just a read.
// maybe handle struct members better
if (tok->astOperand1())
variables.use(tok->astOperand1()->varId(), tok->astOperand1());
if (tok->astOperand2())
variables.use(tok->astOperand2()->varId(), tok->astOperand2());
}
else if (tok->isExtendedOp() && tok->next() && tok->next()->varId() && tok->strAt(2) != "=") { else if (tok->isExtendedOp() && tok->next() && tok->next()->varId() && tok->strAt(2) != "=") {
variables.readAll(tok->next()->varId(), tok); variables.readAll(tok->next()->varId(), tok);
} }

View File

@ -2019,7 +2019,8 @@ private:
ASSERT_EQUALS("[test.cpp:16]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:16]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str());
} }
void localvar50() { // #6261 void localvar50() { // #6261, #6542
// #6261 - ternary operator in function call
functionVariableUsage("void foo() {\n" functionVariableUsage("void foo() {\n"
" char buf1[10];\n" " char buf1[10];\n"
" dostuff(cond?buf1:buf2);\n" " dostuff(cond?buf1:buf2);\n"
@ -2031,6 +2032,14 @@ private:
" dostuff(cond?buf2:buf1);\n" " dostuff(cond?buf2:buf1);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #6542 - ternary operator
functionVariableUsage("void foo(int c) {\n"
" char buf1[10], buf2[10];\n"
" char *p = c ? buf1 : buf2;\n"
" dostuff(p);\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void localvaralias1() { void localvaralias1() {