simplify known variables: variable used as array index

This commit is contained in:
Daniel Marjamäki 2009-02-20 17:27:57 +00:00
parent 385be6d0d9
commit 439b8c4051
2 changed files with 25 additions and 1 deletions

View File

@ -1638,7 +1638,7 @@ bool Tokenizer::simplifyKnownVariables()
}
// Variable is used in calculation..
if (Token::Match(tok3, "[=+-*/] %varid% [+-*/;]", varid))
if (Token::Match(tok3, "[=+-*/[] %varid% [+-*/;]]", varid))
{
tok3 = tok3->next();
tok3->str(tok2->strAt(2));

View File

@ -83,6 +83,7 @@ private:
TEST_CASE(simplifyKnownVariables3);
TEST_CASE(simplifyKnownVariables4);
TEST_CASE(simplifyKnownVariables5);
TEST_CASE(simplifyKnownVariables6);
TEST_CASE(multiCompare);
@ -510,6 +511,29 @@ private:
ASSERT_EQUALS(std::string(" void f ( ) { int a = 4 ; if ( a = 5 ) ; }"), ostr.str());
}
void simplifyKnownVariables6()
{
const char code[] = "void f()\n"
"{\n"
" char str[2];"
" int a = 4;\n"
" str[a] = 0;\n"
"}\n";
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyKnownVariables();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void f ( ) { char str [ 2 ] ; int a = 4 ; str [ 4 ] = 0 ; }"), ostr.str());
}
void multiCompare()
{
// Test for found