From aba1ef0d0e0a4ca4e6b1b13028d9fe0fbf1ddf32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 31 Aug 2008 07:42:54 +0000 Subject: [PATCH] memleak: uncommented and fixed the handling of switch --- CheckMemoryLeak.cpp | 34 ++++++++++++++++++++-------------- testmemleak.cpp | 4 ++-- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index bc41d4fab..9bd822a92 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -146,14 +146,14 @@ static void MemoryLeak( const TOKEN *tok, const char varname[] ) ReportErr( errmsg.str() ); } //--------------------------------------------------------------------------- -/* + static void instoken(TOKEN *tok, const char str[]) { TOKEN *newtok = new TOKEN; memcpy( newtok, tok, sizeof(TOKEN) ); newtok->str = strdup(str); tok->next = newtok; -}*/ +} //--------------------------------------------------------------------------- @@ -453,29 +453,35 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] erase(tok2, gettok(tok2,3)); done = false; } -/* + // Replace switch with if (if not complicated) if (Match(tok2, "switch {")) { // Right now, I just handle if there are a few case and perhaps a default. - bool valid = true; + bool valid = false; bool incase = false; for ( const TOKEN * _tok = gettok(tok2,2); valid && _tok; _tok = _tok->next ) { if ( _tok->str[0] == '{' ) - valid = false; - - else if ( _tok->str[0] == '}' ) break; - else if (strncmp(_tok->str,"if",2)==0) - valid = false; + else if ( _tok->str[0] == '}' ) + { + valid = true; + break; + } - else if (strncmp(_tok->str,"loop",2)==0) - valid = false; + else if (strncmp(_tok->str,"if",2)==0) + break; + + else if (strcmp(_tok->str,"switch")==0) + break; + + else if (strcmp(_tok->str,"loop")==0) + break; else if (incase && Match(_tok,"case")) - valid = false; + break; incase |= Match(_tok,"case"); incase &= !Match(_tok,"break"); @@ -514,8 +520,8 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] tok2 = tok2->next->next; } } - } -*/ + } + } } diff --git a/testmemleak.cpp b/testmemleak.cpp index 111731522..0ce861814 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -49,8 +49,8 @@ public: TEST_CASE( forwhile3 ); TEST_CASE( forwhile4 ); - //TEST_CASE( switch1 ); - //TEST_CASE( switch2 ); + TEST_CASE( switch1 ); + TEST_CASE( switch2 ); TEST_CASE( mismatch1 );