Tokenizer: improved simplifyKnownVariables

This commit is contained in:
Daniel Marjamäki 2010-11-06 19:42:38 +01:00
parent 53fc0ca5e6
commit 5be12a1d27
2 changed files with 44 additions and 0 deletions

View File

@ -5973,6 +5973,11 @@ bool Tokenizer::simplifyKnownVariables()
// Variable is used somehow in a non-defined pattern => bail out
if (tok3->varId() == varid)
{
// calling member function.. bail out because this might
// have different effects to the variable.
if (Token::Match(tok3->next(), ". %var% ("))
break;
// This is a really generic bailout so let's try to avoid this.
// There might be lots of false negatives.
if (_settings && _settings->debugwarnings)
@ -6029,6 +6034,15 @@ bool Tokenizer::simplifyKnownVariables()
}
}
// array usage
if (Token::Match(tok3, "( %varid% [", varid))
{
tok3 = tok3->next();
tok3->str(value);
tok3->varId(valueVarId);
ret = true;
}
// Variable is used in calculation..
if (((tok3->previous()->varId() > 0) && Token::Match(tok3, "& %varid%", varid)) ||
Token::Match(tok3, "[=+-*/[] %varid% [=?+-*/;])]", varid) ||

View File

@ -121,6 +121,8 @@ private:
TEST_CASE(simplifyKnownVariables28);
TEST_CASE(simplifyKnownVariables29); // ticket #1811
TEST_CASE(simplifyKnownVariables30);
TEST_CASE(simplifyKnownVariables31);
TEST_CASE(simplifyKnownVariablesBailOut1);
TEST_CASE(varid1);
TEST_CASE(varid2);
@ -1838,6 +1840,34 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariables31()
{
const char code[] = "void foo(const char str[]) {\n"
" const char *p = str;\n"
" if (p[0] == 0) {\n"
" }\n"
"}\n";
const char expected[] = "void foo ( const char str [ ] ) {\n"
"const char * p ; p = str ;\n"
"if ( str [ 0 ] == 0 ) {\n"
"}\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariablesBailOut1()
{
const char code[] = "void foo(obj a) {\n"
" obj b = a;\n"
" b.f();\n"
"}\n";
const char expected[] = "void foo ( obj a ) {\n"
"obj b ; b = a ;\n"
"b . f ( ) ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
std::string tokenizeDebugListing(const std::string &code, bool simplify = false)
{
Tokenizer tokenizer;