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

@ -48,6 +48,8 @@ private:
TEST_CASE( numeric_true_condition ); TEST_CASE( numeric_true_condition );
TEST_CASE( simplify_known_variables );
TEST_CASE( multi_compare ); TEST_CASE( multi_compare );
TEST_CASE( match1 ); TEST_CASE( match1 );
@ -217,6 +219,51 @@ private:
ASSERT_EQUALS( std::string(" void f ( ) { if ( true ) ; }"), ostr.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() void multi_compare()
{ {
// Test for found // Test for found

View File

@ -1175,8 +1175,23 @@ bool Tokenizer::simplifyFunctionReturn()
return ret; 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 // Helper functions for handling the tokens list

View File

@ -113,6 +113,16 @@ private:
*/ */
bool simplifyFunctionReturn(); 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); TOKEN *_gettok(TOKEN *tok, int index);
void InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n); void InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n);