Tokenizer : Simplify function calls for functions that only returns a constant value

This commit is contained in:
Daniel Marjamäki 2008-12-13 17:57:36 +00:00
parent 3a1196e5f9
commit 1e9863d671
3 changed files with 52 additions and 7 deletions

View File

@ -117,7 +117,7 @@ private:
TEST_CASE( func6 ); TEST_CASE( func6 );
// TODO TEST_CASE( func7 ); // TODO TEST_CASE( func7 );
TEST_CASE( func8 ); // Using callback TEST_CASE( func8 ); // Using callback
// TODO TEST_CASE( func9 ); // Embedding the function call in a if-condition TEST_CASE( func9 ); // Embedding the function call in a if-condition
TEST_CASE( class1 ); TEST_CASE( class1 );
TEST_CASE( class2 ); TEST_CASE( class2 );

View File

@ -1012,11 +1012,14 @@ void Tokenizer::simplifyTokenList()
} }
for ( bool done = false; !done; done = true) bool done = false;
while ( ! done )
{ {
done = true;
done &= simplifyConditions(); done &= simplifyConditions();
done &= simplifyCasts(); done &= simplifyCasts();
}; done &= simplifyFunctionReturn();
}
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -1131,6 +1134,43 @@ bool Tokenizer::simplifyCasts()
} }
bool Tokenizer::simplifyFunctionReturn()
{
bool ret = true;
int indentlevel = 0;
for ( const TOKEN *tok = tokens(); tok; tok = tok->next() )
{
if ( tok->str() == "{" )
++indentlevel;
else if ( tok->str() == "}" )
--indentlevel;
else if ( indentlevel == 0 && TOKEN::Match(tok, "%var% ( ) { return %num% ; }") )
{
std::ostringstream pattern;
pattern << "[(=+-*/] " << tok->str() << " ( ) [;)+-*/]";
for ( TOKEN *tok2 = _tokens; tok2; tok2 = tok2->next() )
{
if ( TOKEN::Match(tok2, pattern.str().c_str()) )
{
tok2 = tok2->next();
tok2->setstr( tok->strAt(5) );
tok2->deleteNext();
tok2->deleteNext();
ret = false;
}
}
}
}
return ret;
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Helper functions for handling the tokens list // Helper functions for handling the tokens list
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -95,10 +95,15 @@ private:
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno); void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);
/** Simplify conditions */
bool simplifyConditions(); bool simplifyConditions();
/** Simplify casts */
bool simplifyCasts(); bool simplifyCasts();
/** Simplify function calls - constant return value */
bool simplifyFunctionReturn();
TOKEN *_gettok(TOKEN *tok, int index); TOKEN *_gettok(TOKEN *tok, int index);
void InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n); void InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n);