Started making simplifyKnownVariables(), but it is very much unfinished. Two test cases added for it.

This commit is contained in:
Reijo Tomperi 2008-12-13 19:44:56 +00:00
parent ff3a1714f2
commit 538f259911
3 changed files with 73 additions and 1 deletions

View File

@ -46,7 +46,9 @@ private:
TEST_CASE( const_and_volatile_functions );
TEST_CASE( numeric_true_condition );
TEST_CASE( numeric_true_condition );
TEST_CASE( simplify_known_variables );
TEST_CASE( multi_compare );
@ -216,6 +218,51 @@ private:
ostr << " " << tok->str();
ASSERT_EQUALS( std::string(" void f ( ) { if ( true ) ; }"), ostr.str() );
}
void simplify_known_variables()
{
{
const char code[] = "void f()\n"
"{\n"
" int a = 10;\n"
" if (a);\n"
"}\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
// TODO, the actual string should be " void f ( ) { int a ; a = 10 ; if ( true ) ; }"
ASSERT_EQUALS( std::string(" void f ( ) { int a ; a = 10 ; if ( a ) ; }"), ostr.str() );
}
{
const char code[] = "void f()\n"
"{\n"
" int a = 10;\n"
" a = g();\n"
" if (a);\n"
"}\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS( std::string(" void f ( ) { int a ; a = 10 ; a = g ( ) ; if ( a ) ; }"), ostr.str() );
}
}
void multi_compare()
{

View File

@ -1175,8 +1175,23 @@ bool Tokenizer::simplifyFunctionReturn()
return ret;
}
bool Tokenizer::simplifyKnownVariables()
{
// TODO, this function should be called from simplifyTokenList()
// after the implementation is done.
// TODO, this functions needs to be implemented.
// TODO, test
bool ret = false;
for ( const TOKEN *tok = tokens(); tok; tok = tok->next() )
{
if ( TOKEN::Match(tok, "%var% = %num% ;") )
{
}
}
return ret;
}
//---------------------------------------------------------------------------
// Helper functions for handling the tokens list

View File

@ -112,6 +112,16 @@ private:
* false if nothing is done.
*/
bool simplifyFunctionReturn();
/**
* A simplify function that replaces a variable with its value in cases
* when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)"
*
* @param token The token list to check and modify.
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
bool simplifyKnownVariables();
TOKEN *_gettok(TOKEN *tok, int index);