Skip to content
Browse files

Related to ticket #3560 (conditional pointer user): remove also dead …

…code in the lower scope if the actual scope isn't special.
  • Loading branch information...
1 parent 11e724d commit 0fd75042953a2e8784459742880b34ee5a37b474 @edward-san edward-san committed
Showing with 40 additions and 7 deletions.
  1. +11 −0 lib/tokenize.cpp
  2. +28 −6 test/testsimplifytokens.cpp
  3. +1 −1 test/testtokenize.cpp
View
11 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;
}
}
}
View
34 test/testsimplifytokens.cpp
@@ -3207,13 +3207,13 @@ class TestSimplifyTokens : public TestFixture {
void flowControl() {
std::list<std::string> 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<std::string>::iterator it = beforedead.begin(); it != beforedead.end(); ++it) {
{
@@ -3395,6 +3395,28 @@ class TestSimplifyTokens : public TestFixture {
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() {
View
2 test/testtokenize.cpp
@@ -2064,7 +2064,7 @@ class TestTokenizer : public TestFixture {
" }"
" 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));
}

0 comments on commit 0fd7504

Please sign in to comment.
Something went wrong with that request. Please try again.