From 47e1a571f7bfbc78e8dc48f309345531832d8c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Dec 2012 19:28:53 +0100 Subject: [PATCH] Tokenizer: Fixed goto problems --- lib/tokenize.cpp | 22 ++++++++++++---------- test/testsimplifytokens.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c71846206..c4df03341 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6697,14 +6697,12 @@ void Tokenizer::simplifyGoto() } else if (tok->str() == "}") { - if (!indentlevel) { + if (indentlevel == 0) { if (indentspecial) --indentspecial; - else - break; // break out - it seems the code is wrong } else { --indentlevel; - if (!indentlevel) { + if (indentlevel == 0) { gotos.clear(); beginfunction = 0; } @@ -6839,12 +6837,16 @@ void Tokenizer::simplifyGoto() } // goto the end of the function - while (tok) { - if (tok->str() == "{") - tok = tok->link(); - else if (tok->str() == "}") - break; - tok = tok->next(); + if (tok->str() == "{") + tok = tok->link(); + else { + while (tok) { + if (tok->str() == "{") + tok = tok->link(); + else if (tok->str() == "}") + break; + tok = tok->next(); + } } if (!tok) break; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 1718cdee5..547db3746 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -174,6 +174,7 @@ private: TEST_CASE(goto3); // #3138 TEST_CASE(goto4); // #3459 TEST_CASE(goto5); // #3705 - return ({asm("");}); + TEST_CASE(goto6); //remove dead code after flow control statements TEST_CASE(flowControl); @@ -3347,6 +3348,33 @@ private: "}", tok(code)); } + void goto6() { // from code in linux that wasn't handled well + const char code1[] = "static void a() {\n" + "unlock:\n" + "}\n" + "\n" + "static void b() {\n" + " if (c)\n" + " goto defer;\n" + "defer:\n" + "}\n"; + ASSERT_EQUALS("static void a ( ) { } " + "static void b ( ) { if ( c ) { return ; } }", tok(code1)); + + const char code2[] = "void a()\n" + "{\n" + " if (x) {}\n" + "unlock:\n" + "}\n" + "\n" + "void b()\n" + "{\n" + " { goto defer; }\n" + "defer:\n" + "}"; + ASSERT_EQUALS("void a ( ) { if ( x ) { } } void b ( ) { return ; }", tok(code2)); + } + void flowControl() { std::list beforedead; //beforedead.push_back("return");