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 );
// TODO TEST_CASE( func7 );
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( class2 );

View File

@ -1011,12 +1011,15 @@ void Tokenizer::simplifyTokenList()
}
}
for ( bool done = false; !done; done = true)
{
bool done = false;
while ( ! done )
{
done = true;
done &= simplifyConditions();
done &= simplifyCasts();
};
done &= simplifyCasts();
done &= simplifyFunctionReturn();
}
}
//---------------------------------------------------------------------------
@ -1130,6 +1133,43 @@ bool Tokenizer::simplifyCasts()
return ret;
}
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

View File

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