Simplify tokens: add a ";" after case and default

This commit is contained in:
Daniel Marjamäki 2009-01-07 17:49:21 +00:00
parent de7de91091
commit 8f86a941aa
4 changed files with 30 additions and 25 deletions

View File

@ -324,12 +324,6 @@ void CheckMemoryLeakClass::MemoryLeak(const Token *tok, const char varname[], Al
}
//---------------------------------------------------------------------------
void CheckMemoryLeakClass::instoken(Token *tok, const char str[])
{
tok->insertToken(str);
}
//---------------------------------------------------------------------------
bool CheckMemoryLeakClass::notvar(const Token *tok, const char *varnames[])
{
std::string varname;
@ -544,7 +538,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
if ((tok->str() == "default"))
{
addtoken("case");
addtoken("default");
addtoken(";");
}
@ -1088,15 +1082,16 @@ void CheckMemoryLeakClass::simplifycode(Token *tok)
if (first)
{
first = false;
instoken(tok2, "{");
tok2->insertToken("{");
}
else
{
// Insert "else [if] {
instoken(tok2, "{");
tok2->insertToken("{");
if (! def)
instoken(tok2, "if");
instoken(tok2, "else");
tok2->insertToken("if");
tok2->insertToken("else");
tok2 = tok2->next();
}
while (tok2 && tok2->str() != "}" && ! Token::Match(tok2, "break ;"))
tok2 = tok2->next();

View File

@ -89,7 +89,6 @@ private:
*/
Token *getcode(const Token *tok, std::list<const Token *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype);
bool notvar(const Token *tok, const char *varnames[]);
void instoken(Token *tok, const char str[]);
void MemoryLeak(const Token *tok, const char varname[], AllocType alloctype);
void MismatchError(const Token *Tok1, const std::list<const Token *> &callstack, const char varname[]);
const char * call_func(const Token *tok, std::list<const Token *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype);

View File

@ -1040,6 +1040,14 @@ void Tokenizer::simplifyTokenList()
simplifyIfAddBraces();
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "case %any% : %var%"))
tok->next()->next()->insertToken(";");
if (Token::Match(tok, "default : %var%"))
tok->next()->insertToken(";");
}
bool modified = true;
while (modified)
{

View File

@ -787,19 +787,22 @@ private:
void switch2()
{
check("void f()\n"
"{\n"
" char *str = new char[10];\n"
" switch (abc)\n"
" {\n"
" case 1:\n"
" delete [] str;\n"
" break;\n"
" default:\n"
" break;\n"
" };\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:12]: Memory leak: str\n"), errout.str());
const std::string code("void f()\n"
"{\n"
" char *str = new char[10];\n"
" switch (abc)\n"
" {\n"
" case 1:\n"
" delete [] str;\n"
" break;\n"
" default:\n"
" break;\n"
" };\n"
"}\n");
check(code.c_str(), false);
ASSERT_EQUALS("", errout.str());
check(code.c_str(), true);
ASSERT_EQUALS("[test.cpp:12]: Memory leak: str\n", errout.str());
}