Simplify tokens: add a ";" after case and default
This commit is contained in:
parent
de7de91091
commit
8f86a941aa
|
@ -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[])
|
bool CheckMemoryLeakClass::notvar(const Token *tok, const char *varnames[])
|
||||||
{
|
{
|
||||||
std::string varname;
|
std::string varname;
|
||||||
|
@ -544,7 +538,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
||||||
|
|
||||||
if ((tok->str() == "default"))
|
if ((tok->str() == "default"))
|
||||||
{
|
{
|
||||||
addtoken("case");
|
addtoken("default");
|
||||||
addtoken(";");
|
addtoken(";");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1088,15 +1082,16 @@ void CheckMemoryLeakClass::simplifycode(Token *tok)
|
||||||
if (first)
|
if (first)
|
||||||
{
|
{
|
||||||
first = false;
|
first = false;
|
||||||
instoken(tok2, "{");
|
tok2->insertToken("{");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Insert "else [if] {
|
// Insert "else [if] {
|
||||||
instoken(tok2, "{");
|
tok2->insertToken("{");
|
||||||
if (! def)
|
if (! def)
|
||||||
instoken(tok2, "if");
|
tok2->insertToken("if");
|
||||||
instoken(tok2, "else");
|
tok2->insertToken("else");
|
||||||
|
tok2 = tok2->next();
|
||||||
}
|
}
|
||||||
while (tok2 && tok2->str() != "}" && ! Token::Match(tok2, "break ;"))
|
while (tok2 && tok2->str() != "}" && ! Token::Match(tok2, "break ;"))
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
|
|
|
@ -89,7 +89,6 @@ private:
|
||||||
*/
|
*/
|
||||||
Token *getcode(const Token *tok, std::list<const Token *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype);
|
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[]);
|
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 MemoryLeak(const Token *tok, const char varname[], AllocType alloctype);
|
||||||
void MismatchError(const Token *Tok1, const std::list<const Token *> &callstack, const char varname[]);
|
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);
|
const char * call_func(const Token *tok, std::list<const Token *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype);
|
||||||
|
|
|
@ -1040,6 +1040,14 @@ void Tokenizer::simplifyTokenList()
|
||||||
|
|
||||||
simplifyIfAddBraces();
|
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;
|
bool modified = true;
|
||||||
while (modified)
|
while (modified)
|
||||||
{
|
{
|
||||||
|
|
|
@ -787,19 +787,22 @@ private:
|
||||||
|
|
||||||
void switch2()
|
void switch2()
|
||||||
{
|
{
|
||||||
check("void f()\n"
|
const std::string code("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char *str = new char[10];\n"
|
" char *str = new char[10];\n"
|
||||||
" switch (abc)\n"
|
" switch (abc)\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
" case 1:\n"
|
" case 1:\n"
|
||||||
" delete [] str;\n"
|
" delete [] str;\n"
|
||||||
" break;\n"
|
" break;\n"
|
||||||
" default:\n"
|
" default:\n"
|
||||||
" break;\n"
|
" break;\n"
|
||||||
" };\n"
|
" };\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS(std::string("[test.cpp:12]: Memory leak: str\n"), errout.str());
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue