From 8f86a941aabba7777409ac2198018bc6669524cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 7 Jan 2009 17:49:21 +0000 Subject: [PATCH] Simplify tokens: add a ";" after case and default --- src/checkmemoryleak.cpp | 17 ++++++----------- src/checkmemoryleak.h | 1 - src/tokenize.cpp | 8 ++++++++ test/testmemleak.cpp | 29 ++++++++++++++++------------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index f44e6f7bd..d4f0e3569 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -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 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(); diff --git a/src/checkmemoryleak.h b/src/checkmemoryleak.h index b2c53d435..e4cf61f6d 100644 --- a/src/checkmemoryleak.h +++ b/src/checkmemoryleak.h @@ -89,7 +89,6 @@ private: */ Token *getcode(const Token *tok, std::list 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 &callstack, const char varname[]); const char * call_func(const Token *tok, std::list callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype); diff --git a/src/tokenize.cpp b/src/tokenize.cpp index b936ed11f..1491cc54e 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -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) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 605d313e5..52e88bb1e 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -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()); }