simplify known variables: variable used as array index
This commit is contained in:
parent
385be6d0d9
commit
439b8c4051
|
@ -1638,7 +1638,7 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variable is used in calculation..
|
// Variable is used in calculation..
|
||||||
if (Token::Match(tok3, "[=+-*/] %varid% [+-*/;]", varid))
|
if (Token::Match(tok3, "[=+-*/[] %varid% [+-*/;]]", varid))
|
||||||
{
|
{
|
||||||
tok3 = tok3->next();
|
tok3 = tok3->next();
|
||||||
tok3->str(tok2->strAt(2));
|
tok3->str(tok2->strAt(2));
|
||||||
|
|
|
@ -83,6 +83,7 @@ private:
|
||||||
TEST_CASE(simplifyKnownVariables3);
|
TEST_CASE(simplifyKnownVariables3);
|
||||||
TEST_CASE(simplifyKnownVariables4);
|
TEST_CASE(simplifyKnownVariables4);
|
||||||
TEST_CASE(simplifyKnownVariables5);
|
TEST_CASE(simplifyKnownVariables5);
|
||||||
|
TEST_CASE(simplifyKnownVariables6);
|
||||||
|
|
||||||
TEST_CASE(multiCompare);
|
TEST_CASE(multiCompare);
|
||||||
|
|
||||||
|
@ -510,6 +511,29 @@ private:
|
||||||
ASSERT_EQUALS(std::string(" void f ( ) { int a = 4 ; if ( a = 5 ) ; }"), ostr.str());
|
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()
|
void multiCompare()
|
||||||
{
|
{
|
||||||
// Test for found
|
// Test for found
|
||||||
|
|
Loading…
Reference in New Issue