Tokenizer::simplifyKnownVariables: Simplify known variables for struct members
This commit is contained in:
parent
723570fe6c
commit
71c1ce71ce
|
@ -5918,7 +5918,6 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
if (varid == 0)
|
if (varid == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
// skip loop variable
|
// skip loop variable
|
||||||
if (Token::Match(tok2->tokAt(-2), "(|:: %type%"))
|
if (Token::Match(tok2->tokAt(-2), "(|:: %type%"))
|
||||||
{
|
{
|
||||||
|
@ -5929,6 +5928,11 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// struct name..
|
||||||
|
const std::string structname(Token::Match(tok2->tokAt(-3), "[;{}] %var% .") ?
|
||||||
|
(tok2->strAt(-2) + " .") :
|
||||||
|
std::string(""));
|
||||||
|
|
||||||
if (tok2->str() == tok2->strAt(2))
|
if (tok2->str() == tok2->strAt(2))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -6148,11 +6152,16 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using the variable in condition..
|
// Using the variable in condition..
|
||||||
if (Token::Match(tok3->previous(), "if ( %varid% ==|!=|<|<=|>|>=|)", varid) ||
|
if (Token::Match(tok3->previous(), ("if ( " + structname + " %varid% ==|!=|<|<=|>|>=|)").c_str(), varid) ||
|
||||||
Token::Match(tok3, "( %varid% ==|!=|<|<=|>|>=", varid) ||
|
Token::Match(tok3, ("( " + structname + " %varid% ==|!=|<|<=|>|>=").c_str(), varid) ||
|
||||||
Token::Match(tok3, "!|==|!=|<|<=|>|>= %varid% ==|!=|<|<=|>|>=|)", varid) ||
|
Token::Match(tok3, ("!|==|!=|<|<=|>|>= " + structname + " %varid% ==|!=|<|<=|>|>=|)").c_str(), varid) ||
|
||||||
Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid))
|
Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid))
|
||||||
{
|
{
|
||||||
|
if (!structname.empty())
|
||||||
|
{
|
||||||
|
tok3->deleteNext();
|
||||||
|
tok3->deleteNext();
|
||||||
|
}
|
||||||
tok3 = tok3->next();
|
tok3 = tok3->next();
|
||||||
tok3->str(value);
|
tok3->str(value);
|
||||||
tok3->varId(valueVarId);
|
tok3->varId(valueVarId);
|
||||||
|
@ -6160,7 +6169,7 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variable is used in function call..
|
// Variable is used in function call..
|
||||||
if (Token::Match(tok3, "%var% ( %varid% ,", varid))
|
if (Token::Match(tok3, ("%var% ( " + structname + " %varid% ,").c_str(), varid))
|
||||||
{
|
{
|
||||||
const char * const functionName[] =
|
const char * const functionName[] =
|
||||||
{
|
{
|
||||||
|
@ -6172,6 +6181,11 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
if (tok3->str() == functionName[i])
|
if (tok3->str() == functionName[i])
|
||||||
{
|
{
|
||||||
Token *par1 = tok3->next()->next();
|
Token *par1 = tok3->next()->next();
|
||||||
|
if (!structname.empty())
|
||||||
|
{
|
||||||
|
par1->deleteThis();
|
||||||
|
par1->deleteThis();
|
||||||
|
}
|
||||||
par1->str(value);
|
par1->str(value);
|
||||||
par1->varId(valueVarId);
|
par1->varId(valueVarId);
|
||||||
break;
|
break;
|
||||||
|
@ -6180,8 +6194,13 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
}
|
}
|
||||||
|
|
||||||
// array usage
|
// array usage
|
||||||
if (Token::Match(tok3, "[(,] %varid% [+-*/[]", varid))
|
if (Token::Match(tok3, ("[(,] " + structname + " %varid% [+-*/[]").c_str(), varid))
|
||||||
{
|
{
|
||||||
|
if (!structname.empty())
|
||||||
|
{
|
||||||
|
tok3->deleteNext();
|
||||||
|
tok3->deleteNext();
|
||||||
|
}
|
||||||
tok3 = tok3->next();
|
tok3 = tok3->next();
|
||||||
tok3->str(value);
|
tok3->str(value);
|
||||||
tok3->varId(valueVarId);
|
tok3->varId(valueVarId);
|
||||||
|
@ -6189,12 +6208,17 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variable is used in calculation..
|
// Variable is used in calculation..
|
||||||
if (((tok3->previous()->varId() > 0) && Token::Match(tok3, "& %varid%", varid)) ||
|
if (((tok3->previous()->varId() > 0) && Token::Match(tok3, ("& " + structname + " %varid%").c_str(), varid)) ||
|
||||||
Token::Match(tok3, "[=+-*/[] %varid% [=?+-*/;])]", varid) ||
|
Token::Match(tok3, ("[=+-*/[] " + structname + " %varid% [=?+-*/;])]").c_str(), varid) ||
|
||||||
Token::Match(tok3, "[(=+-*/[] %varid% <<|>>", varid) ||
|
Token::Match(tok3, ("[(=+-*/[] " + structname + " %varid% <<|>>").c_str(), varid) ||
|
||||||
Token::Match(tok3, "<<|>> %varid% [+-*/;])]", varid) ||
|
Token::Match(tok3, ("<<|>> " + structname + " %varid% [+-*/;])]").c_str(), varid) ||
|
||||||
Token::Match(tok3->previous(), "[=+-*/[] ( %varid%", varid))
|
Token::Match(tok3->previous(), ("[=+-*/[] ( " + structname + " %varid%").c_str(), varid))
|
||||||
{
|
{
|
||||||
|
if (!structname.empty())
|
||||||
|
{
|
||||||
|
tok3->deleteNext();
|
||||||
|
tok3->deleteNext();
|
||||||
|
}
|
||||||
tok3 = tok3->next();
|
tok3 = tok3->next();
|
||||||
tok3->str(value);
|
tok3->str(value);
|
||||||
tok3->varId(valueVarId);
|
tok3->varId(valueVarId);
|
||||||
|
|
|
@ -123,6 +123,7 @@ private:
|
||||||
TEST_CASE(simplifyKnownVariables30);
|
TEST_CASE(simplifyKnownVariables30);
|
||||||
TEST_CASE(simplifyKnownVariables31);
|
TEST_CASE(simplifyKnownVariables31);
|
||||||
TEST_CASE(simplifyKnownVariables32); // const
|
TEST_CASE(simplifyKnownVariables32); // const
|
||||||
|
TEST_CASE(simplifyKnownVariables33); // struct variable
|
||||||
TEST_CASE(simplifyKnownVariablesBailOutAssign);
|
TEST_CASE(simplifyKnownVariablesBailOutAssign);
|
||||||
TEST_CASE(simplifyKnownVariablesBailOutFor1);
|
TEST_CASE(simplifyKnownVariablesBailOutFor1);
|
||||||
TEST_CASE(simplifyKnownVariablesBailOutFor2);
|
TEST_CASE(simplifyKnownVariablesBailOutFor2);
|
||||||
|
@ -1878,6 +1879,19 @@ private:
|
||||||
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
|
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void simplifyKnownVariables33()
|
||||||
|
{
|
||||||
|
const char code[] = "static void foo(struct Foo *foo) {\n"
|
||||||
|
" foo->a = 23;\n"
|
||||||
|
" x[foo->a] = 0;\n"
|
||||||
|
"}\n";
|
||||||
|
const char expected[] = "static void foo ( struct Foo * foo ) {\n"
|
||||||
|
"foo . a = 23 ;\n"
|
||||||
|
"x [ 23 ] = 0 ;\n"
|
||||||
|
"}";
|
||||||
|
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
|
||||||
|
}
|
||||||
|
|
||||||
void simplifyKnownVariablesBailOutAssign()
|
void simplifyKnownVariablesBailOutAssign()
|
||||||
{
|
{
|
||||||
const char code[] = "int foo() {\n"
|
const char code[] = "int foo() {\n"
|
||||||
|
|
Loading…
Reference in New Issue