Fixed #3280 (False positive: assigned value that is never used)

This commit is contained in:
Daniel Marjamäki 2011-11-20 12:09:07 +01:00
parent 8c091ff968
commit 1cf45a5cde
2 changed files with 31 additions and 0 deletions

View File

@ -3349,7 +3349,16 @@ void Tokenizer::setVarId()
// Set variable ids..
_varId = 0;
unsigned int executableScope = 0;
for (Token *tok = _tokens; tok; tok = tok->next()) {
if (tok->str() == "{") {
if (executableScope)
executableScope++;
else if (tok->strAt(-1) == ")" || Token::simpleMatch(tok->tokAt(-2), ") const"))
executableScope = 1;
} else if (executableScope >= 1 && tok->str() == "}")
--executableScope;
if (tok != _tokens && !Token::Match(tok, "[;{}(,] %type%") && !Token::Match(tok, "[;{}(,] ::"))
continue;
@ -3383,6 +3392,11 @@ void Tokenizer::setVarId()
!tok->previous()->isName() &&
tok->strAt(-2) != "operator")
continue;
if (executableScope && tok->str() == "(" && Token::simpleMatch(tok->link(),") ;")) {
tok = tok->link();
continue;
}
tok = tok->next();
}
@ -3529,6 +3543,9 @@ void Tokenizer::setVarId()
tok2 = tok2->next();
}
if (executableScope && Token::Match(tok2, ") ;"))
continue;
if (Token::Match(tok2 ? tok2->tokAt(-2) : 0, "class|struct %type% ;"))
continue;

View File

@ -193,6 +193,7 @@ private:
TEST_CASE(varidFunctionCall1);
TEST_CASE(varidFunctionCall2);
TEST_CASE(varidFunctionCall3);
TEST_CASE(varidFunctionCall4); // ticket #3280
TEST_CASE(varidStl);
TEST_CASE(varid_delete);
TEST_CASE(varid_functions);
@ -3071,6 +3072,19 @@ private:
ASSERT_EQUALS(expected, tokenizeDebugListing(code));
}
void varidFunctionCall4() {
// Ticket #3280
const std::string code1("void f() { int x; fun(a,b*x); }");
ASSERT_EQUALS("\n\n##file 0\n"
"1: void f ( ) { int x@1 ; fun ( a , b * x@1 ) ; }\n",
tokenizeDebugListing(code1));
const std::string code2("void f(int a) { int x; fun(a,b*x); }");
ASSERT_EQUALS("\n\n##file 0\n"
"1: void f ( int a@1 ) { int x@2 ; fun ( a@1 , b * x@2 ) ; }\n",
tokenizeDebugListing(code2));
}
void varidStl() {
const std::string actual = tokenizeDebugListing(
"list<int> ints;\n"