Tokenizer: Remove redundant assignments and variables

This commit is contained in:
Daniel Marjamäki 2010-01-31 09:33:57 +01:00
parent e1efad0c27
commit e15a16eb15
4 changed files with 80 additions and 15 deletions

View File

@ -2466,7 +2466,6 @@ bool Tokenizer::simplifyTokenList()
}
}
simplifyStd();
simplifyNamespaces();
@ -2759,6 +2758,8 @@ bool Tokenizer::simplifyTokenList()
}
}
removeRedundantAssignment();
simplifyComma();
if (_settings && _settings->_debug)
{
@ -2769,6 +2770,60 @@ bool Tokenizer::simplifyTokenList()
}
//---------------------------------------------------------------------------
void Tokenizer::removeRedundantAssignment()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() == "{")
tok = tok->link();
if (Token::Match(tok, ") const| {"))
{
// parse in this function..
std::set<unsigned int> localvars;
if (tok->next()->str() == "const")
tok = tok->next();
const Token * const end = tok->next()->link();
for (Token *tok2 = tok->next(); tok2 && tok2 != end; tok2 = tok2->next())
{
if (Token::Match(tok2, "[;{}] %type% * %var% ;"))
{
tok2 = tok2->tokAt(3);
localvars.insert(tok2->varId());
}
else if (Token::Match(tok2, "[;{}] %type% %var% ;") && tok2->next()->isStandardType())
{
tok2 = tok2->tokAt(2);
localvars.insert(tok2->varId());
}
else if (tok2->varId() && !Token::Match(tok2->previous(), "[;{}] %var% = %var% ;"))
{
localvars.erase(tok2->varId());
}
}
localvars.erase(0);
if (!localvars.empty())
{
for (Token *tok2 = tok->next(); tok2 && tok2 != end; tok2 = tok2->next())
{
if (Token::Match(tok2, "[;{}] %type% %var% ;") && localvars.find(tok2->tokAt(2)->varId()) != localvars.end())
{
Token::eraseTokens(tok2, tok2->tokAt(3));
}
else if (Token::Match(tok2, "[;{}] %type% * %var% ;") && localvars.find(tok2->tokAt(3)->varId()) != localvars.end())
{
Token::eraseTokens(tok2, tok2->tokAt(4));
}
else if (Token::Match(tok2, "[;{}] %var% = %var% ;") && localvars.find(tok2->next()->varId()) != localvars.end())
{
Token::eraseTokens(tok2, tok2->tokAt(4));
}
}
}
}
}
}
bool Tokenizer::removeReduntantConditions()
{
bool ret = false;

View File

@ -139,6 +139,9 @@ public:
private:
#endif
/** Remove redundant assignment */
void removeRedundantAssignment();
/**
* Replace sizeof() to appropriate size.
*/

View File

@ -292,9 +292,9 @@ private:
}
{
const char code1[] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; }";
const char code2[] = " void f() { int a; bool use = true; { a=0; }int c=1; }";
ASSERT_EQUALS(tok(code2), tok(code1));
const char code1[] = "void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; }";
const char code2[] = "void f ( ) { int a ; ; ; { a = 0 ; } int c ; c = 1 ; }";
ASSERT_EQUALS(code2, tok(code1));
}
{
@ -779,7 +779,7 @@ private:
"}\n";
std::ostringstream oss;
oss << sizeofFromTokenizer("*");
ASSERT_EQUALS("void f ( ) { char * ptrs ; int a ; a = " + oss.str() + " ; }", sizeof_(code));
ASSERT_EQUALS("void f ( ) { ; int a ; a = " + oss.str() + " ; }", sizeof_(code));
}
}
@ -956,7 +956,7 @@ private:
const char expected[] = "void f ( ) "
"{"
" int * p ;"
" ;"
" 4 ; "
"}";
@ -1122,7 +1122,7 @@ private:
"f<int>(10);";
const std::string expected("; f<int> ( 10 ) ; "
"void f<int> ( int val ) { int a ; }");
"void f<int> ( int val ) { ; }");
ASSERT_EQUALS(expected, sizeof_(code));
}
@ -1941,7 +1941,7 @@ private:
" bool x = false;\n"
" int b = x ? 44 : 3;\n"
"}\n";
ASSERT_EQUALS("void f ( ) { bool x ; x = false ; int b ; b = 3 ; }", tok(code));
ASSERT_EQUALS("void f ( ) { ; ; int b ; b = 3 ; }", tok(code));
}
{
@ -3040,7 +3040,7 @@ private:
"}\n"
"}";
ASSERT_EQUALS("void f ( ) { bool x ; x = true ; }", tok(code));
ASSERT_EQUALS("void f ( ) { ; ; }", tok(code));
}
{
@ -3053,7 +3053,7 @@ private:
"}\n"
"}";
ASSERT_EQUALS("void f ( ) { bool x ; x = false ; { g ( ) ; } }", tok(code));
ASSERT_EQUALS("void f ( ) { ; ; { g ( ) ; } }", tok(code));
}
{
@ -3107,7 +3107,7 @@ private:
const char expected[] = "void f ( ) "
"{ "
"char buf [ 100 ] ; "
"char * p ; p = buf ; "
"; ; "
"x ( buf ) ; "
"}";
@ -3159,7 +3159,7 @@ private:
const char expected[] = "int * foo ( ) "
"{ "
"int a [ 10 ] ; "
"int * b ; b = a ; "
"; ; "
"return a ; "
"}";

View File

@ -183,6 +183,8 @@ private:
TEST_CASE(switchCase);
TEST_CASE(functionpointer);
TEST_CASE(removeRedundantAssignment);
}
@ -1284,7 +1286,7 @@ private:
const std::string expected("\n\n##file 0\n"
"1: void f ( )\n"
"2: {\n"
"3: int a@1 ; int b@2 ;\n"
"3: ; ;\n"
"4: }\n");
ASSERT_EQUALS(expected, actual);
@ -1498,7 +1500,7 @@ private:
const std::string expected("\n\n##file 0\n"
"1: void f ( )\n"
"2: {\n"
"3: int a@1 ; int b@2 ;\n"
"3: int a@1 ; ;\n"
"4: a@1 = a@1 ;\n"
"5: }\n");
@ -2007,7 +2009,7 @@ private:
" int x;"
" { }"
"}";
ASSERT_EQUALS("void foo ( ) { if ( x ) { int x ; } { } }", tokenizeAndStringify(code, true));
ASSERT_EQUALS("void foo ( ) { if ( x ) { ; } { } }", tokenizeAndStringify(code, true));
}
}
@ -2866,6 +2868,11 @@ private:
ASSERT_EQUALS(" unsigned int* f;", simplifyFunctionPointers("unsigned int (*f)();"));
ASSERT_EQUALS(" unsigned int** f;", simplifyFunctionPointers("unsigned int * (*f)();"));
}
void removeRedundantAssignment()
{
ASSERT_EQUALS("void f ( ) { ; int * q ; ; }", tokenizeAndStringify("void f() { int *p, *q; p = q; }", true));
}
};
REGISTER_TEST(TestTokenizer)