Tokenizer : Added function for simplifying casts

This commit is contained in:
Daniel Marjamäki 2008-12-13 16:42:39 +00:00
parent 4b04d38867
commit 3a1196e5f9
3 changed files with 48 additions and 6 deletions

View File

@ -135,9 +135,9 @@ private:
// TODO TEST_CASE( varid );
// TODO TEST_CASE( cast1 );
// TODO TEST_CASE( cast2 ); // Doesn't fail but for bad reasons
// TODO TEST_CASE( cast3 );
TEST_CASE( cast1 );
TEST_CASE( cast2 );
TEST_CASE( cast3 );
}
@ -1119,7 +1119,7 @@ private:
check( "void foo()\n"
"{\n"
" char *a = malloc(10);\n"
" free(reinterpret_cast<void *>(c));\n"
" free(reinterpret_cast<void *>(a));\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );

View File

@ -1014,7 +1014,8 @@ void Tokenizer::simplifyTokenList()
for ( bool done = false; !done; done = true)
{
done &= simplifyConditions();
done &= simplifyConditions();
done &= simplifyCasts();
};
}
//---------------------------------------------------------------------------
@ -1090,6 +1091,45 @@ bool Tokenizer::simplifyConditions()
return ret;
}
bool Tokenizer::simplifyCasts()
{
bool ret = true;
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
{
if ( TOKEN::Match(tok->next(), "( %type% * )") )
{
tok->deleteNext();
tok->deleteNext();
tok->deleteNext();
tok->deleteNext();
ret = false;
}
else if ( TOKEN::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast <" ) )
{
while ( tok->next() && tok->next()->str() != ">" )
tok->deleteNext();
tok->deleteNext();
tok->deleteNext();
TOKEN *tok2 = tok;
int parlevel = 0;
while ( tok2->next() && parlevel >= 0 )
{
tok2 = tok2->next();
if ( TOKEN::Match(tok2->next(), "(") )
++parlevel;
else if ( TOKEN::Match(tok2->next(), ")") )
--parlevel;
}
if (tok2->next())
tok2->deleteNext();
}
}
return ret;
}
//---------------------------------------------------------------------------
// Helper functions for handling the tokens list

View File

@ -95,7 +95,9 @@ private:
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);
bool simplifyConditions();
bool simplifyConditions();
bool simplifyCasts();
TOKEN *_gettok(TOKEN *tok, int index);