Fixed #3587 (Wrong tokenizer simplification - variable assigned by operator>>)

This commit is contained in:
Daniel Marjamäki 2012-02-07 08:05:50 +01:00
parent 47c7e346aa
commit bbb707c460
2 changed files with 37 additions and 0 deletions

View File

@ -6504,6 +6504,16 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
ret = true;
}
// The >> operator is sometimes used to assign a variable in C++
if (isCPP() && Token::Match(tok3, (">> " + structname + " %varid%").c_str(), varid)) {
// bailout for such code: ; std :: cin >> i ;
const Token *prev = tok3->previous();
while (prev && prev->str() != "return" && (prev->isName() || prev->str() == "::"))
prev = prev->previous();
if (Token::Match(prev, "[;{}]"))
break;
}
// Variable is used in calculation..
if (((tok3->previous()->varId() > 0) && Token::Match(tok3, ("& " + structname + " %varid%").c_str(), varid)) ||
Token::Match(tok3, ("[=+-*/%^|[] " + structname + " %varid% [=?+-*/%^|;])]").c_str(), varid) ||

View File

@ -146,6 +146,7 @@ private:
TEST_CASE(simplifyKnownVariables43);
TEST_CASE(simplifyKnownVariables44); // ticket #3117 - don't simplify static variables
TEST_CASE(simplifyKnownVariables45); // ticket #3281 - static constant variable not simplified
TEST_CASE(simplifyKnownVariables46); // ticket #3587 - >>
TEST_CASE(simplifyKnownVariablesBailOutAssign1);
TEST_CASE(simplifyKnownVariablesBailOutAssign2);
TEST_CASE(simplifyKnownVariablesBailOutFor1);
@ -2264,6 +2265,32 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariables46() {
const char code[] = "void f() {\n"
" int x = 0;\n"
" cin >> x;\n"
" return x;\n"
"}";
{
const char expected[] = "void f ( ) {\n"
"int x ; x = 0 ;\n"
"cin >> x ;\n"
"return x ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.cpp"));
}
{
const char expected[] = "void f ( ) {\n"
"\n"
"cin >> 0 ;\n"
"return 0 ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
}
}
void simplifyKnownVariablesBailOutAssign1() {
const char code[] = "int foo() {\n"
" int i; i = 0;\n"