From bb1a9a07e41055ce11b238d9d43834ed541b1f81 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Wed, 30 Sep 2009 14:30:53 +0300 Subject: [PATCH] Fix #760 (Tokenizer: Goto not simplified) http://sourceforge.net/apps/trac/cppcheck/ticket/760 --- src/tokenize.cpp | 31 ++++++++++++++++++++++++++----- test/testsimplifytokens.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 7995878bd..ac8558c9a 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -3388,15 +3388,24 @@ void Tokenizer::simplifyGoto() { // Is this label at the end.. bool end = false; + int lev = 0; for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { if (tok2->str() == "}") { - end = true; - break; + --lev; + if (lev < 0) + { + end = true; + break; + } + } + else if (tok2->str() == "{") + { + ++lev; } - if (tok2->str() == "{" || Token::Match(tok2, "%var% :")) + if (Token::Match(tok2, "%var% :")) { break; } @@ -3435,11 +3444,23 @@ void Tokenizer::simplifyGoto() bool ret = false; std::list links; + int lev = 0; for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) { if (tok2->str() == "}") - break; - if (tok2->str() == "return") + { + --lev; + if (lev < 0) + break; + + continue; + } + if (tok2->str() == "{") + { + ++lev; + continue; + } + else if (tok2->str() == "return") ret = true; token->insertToken(tok2->str().c_str()); token = token->next(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 14d8558be..6968e16fb 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -1506,6 +1506,35 @@ private: ASSERT_EQUALS(expect, tok(code)); } + { + const char code[] = "void foo()\n" + "{\n" + " if (a())\n" + " goto out;\n" + " b();\n" + "out:\n" + " if (c())\n" + " {\n" + " d();\n" + " }\n" + "}"; + + const char expect[] = "void foo ( ) " + "{ " + "if ( a ( ) ) " + "{ " + "if ( c ( ) ) " + "{ d ( ) ; } " + "return ; " + "} " + "b ( ) ; " + "if ( c ( ) ) " + "{ d ( ) ; } " + "}"; + + ASSERT_EQUALS(expect, tok(code)); + } + { const char code[] = "class NoLabels { bool varOne : 1 ; bool varTwo : 1 ; } ;"; ASSERT_EQUALS(code, tok(code));