From 0fd75042953a2e8784459742880b34ee5a37b474 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Mon, 30 Jan 2012 19:04:35 +0100 Subject: [PATCH] Related to ticket #3560 (conditional pointer user): remove also dead code in the lower scope if the actual scope isn't special. --- lib/tokenize.cpp | 11 +++++++++++ test/testsimplifytokens.cpp | 34 ++++++++++++++++++++++++++++------ test/testtokenize.cpp | 2 +- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d0a9d7c58..babbbd56a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3909,6 +3909,7 @@ void Tokenizer::simplifyRealloc() void Tokenizer::simplifyFlowControl() { unsigned int indentlevel = 0; + bool stilldead = false; for (Token *tok = _tokens; tok; tok = tok->next()) { if (tok->str() == "(" || tok->str() == "[") { tok = tok->link(); @@ -3925,6 +3926,12 @@ void Tokenizer::simplifyFlowControl() if (!indentlevel) break; --indentlevel; + if (stilldead) { + eraseDeadCode(tok, 0); + if (indentlevel == 1 || tok->next()->str() != "}" || !Token::Match(tok->next()->link()->previous(), ";|{|}|do {")) + stilldead = false; + continue; + } } if (!indentlevel) @@ -3946,6 +3953,10 @@ void Tokenizer::simplifyFlowControl() } else if (Token::Match(tok2, "[{}]")) break; //Wrong code. } + //if everything is removed, then remove also the code after an inferior scope + //only if the actual scope is not special + if (indentlevel > 1 && tok->next()->str() == "}" && Token::Match(tok->next()->link()->previous(), ";|{|}|do {")) + stilldead = true; } } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 74da80b43..eadf32635 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -3207,13 +3207,13 @@ private: void flowControl() { std::list beforedead; - beforedead.push_back("return"); - beforedead.push_back("throw ( 10 )"); + //beforedead.push_back("return"); + //beforedead.push_back("throw ( 10 )"); beforedead.push_back("exit ( 0 )"); - beforedead.push_back("abort ( )"); - beforedead.push_back("goto labels"); - beforedead.push_back("break"); - beforedead.push_back("continue"); + //beforedead.push_back("abort ( )"); + //beforedead.push_back("goto labels"); + //beforedead.push_back("break"); + //beforedead.push_back("continue"); for (std::list::iterator it = beforedead.begin(); it != beforedead.end(); ++it) { { @@ -3395,6 +3395,28 @@ private: ASSERT_EQUALS(expected, tok(code)); } } + + { + const char code[] = "void foo()\n" + "{\n" + " A *a = 0;\n" + " if (!a) {\n" + " nondeadcode;\n" + " return;\n" + " dead;\n" + " }\n" + " stilldead;\n" + " a->_a;\n" + "}\n"; + const char expected[] = "void foo ( ) " + "{" + " A * a ; a = 0 ; {" + " nondeadcode ;" + " return ;" + " } " + "}"; + ASSERT_EQUALS(expected, tok(code)); + } } void strcat1() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index cda8111ae..49edc2040 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2064,7 +2064,7 @@ private: " }" " return 10 / x;" "}"; - const char expected[] = "int f ( ) { int x ; x = 0 ; { return 0 ; } return 10 / x ; }"; + const char expected[] = "int f ( ) { int x ; x = 0 ; { return 0 ; } }"; ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); }