simplify known variable: don't simplify this ';i++;'
This commit is contained in:
parent
c991aded12
commit
6b7b27a2e8
|
@ -1687,17 +1687,24 @@ bool Tokenizer::simplifyKnownVariables()
|
|||
if (Token::Match(tok3->next(), "%varid% ++|--", varid))
|
||||
{
|
||||
tok3 = tok3->next();
|
||||
const std::string op(tok3->strAt(1));
|
||||
if (!Token::Match(tok3->previous(), "; %any% %any% ;"))
|
||||
{
|
||||
tok3->str(value.c_str());
|
||||
incdec(value, tok3->strAt(1));
|
||||
tok3->deleteNext();
|
||||
}
|
||||
incdec(value, op);
|
||||
}
|
||||
|
||||
if (Token::Match(tok3->next(), "++|-- %varid%", varid))
|
||||
{
|
||||
incdec(value, tok3->strAt(1));
|
||||
if (!Token::Match(tok3, "; %any% %any% ;"))
|
||||
{
|
||||
tok3->deleteNext();
|
||||
tok3->next()->str(value.c_str());
|
||||
}
|
||||
tok3 = tok3->next();
|
||||
tok3->str(value.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,6 +85,7 @@ private:
|
|||
TEST_CASE(simplifyKnownVariables5);
|
||||
TEST_CASE(simplifyKnownVariables6);
|
||||
TEST_CASE(simplifyKnownVariables7);
|
||||
TEST_CASE(simplifyKnownVariables8);
|
||||
|
||||
TEST_CASE(multiCompare);
|
||||
|
||||
|
@ -557,6 +558,28 @@ private:
|
|||
ASSERT_EQUALS(std::string(" void foo ( ) { int i = 22 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; }"), ostr.str());
|
||||
}
|
||||
|
||||
void simplifyKnownVariables8()
|
||||
{
|
||||
const char code[] = "void foo()\n"
|
||||
"{\n"
|
||||
" int i = 22;\n"
|
||||
" i++;\n"
|
||||
" abc[i] = 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 foo ( ) { int i = 22 ; i ++ ; abc [ 23 ] = 0 ; }"), ostr.str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void multiCompare()
|
||||
|
|
Loading…
Reference in New Issue