Tokenizer::removeRedundantCondition: refactoring due to various reasons.

1) Remove the dead code with 'eraseDeadCode', not with 'eraseTokens' because of the possible presence of labels.
2) It's impossible that in tokenizer there is pattern like 'if (true|false) {code;} else if (cond) {code_cond;}' because the function 'Tokenizer::elseif' adds '{' between 'else' and 'if', hence a code path is redundant, so it's removed from code.
This commit is contained in:
Edoardo Prezioso 2012-01-26 22:27:31 +01:00
parent 7cf8e5dfff
commit 0f1f3c0f5f
1 changed files with 32 additions and 69 deletions

View File

@ -3944,93 +3944,56 @@ bool Tokenizer::removeRedundantConditions()
continue; continue;
// Find matching else // Find matching else
const Token *elseTag = 0; Token *elseTag = 0;
// Find the closing "}" // Find the closing "}"
elseTag = tok->linkAt(4)->next(); elseTag = tok->linkAt(4)->next();
bool boolValue = false; bool boolValue = (tok->strAt(2) == "true");
if (tok->strAt(2) == "true")
boolValue = true;
// Handle if with else // Handle if with else
if (elseTag && elseTag->str() == "else") { if (Token::simpleMatch(elseTag, "else {")) {
if (Token::simpleMatch(elseTag->next(), "if (")) {
// Handle "else if"
if (boolValue == false) {
// Convert "if( false ) {aaa;} else if() {bbb;}" => "if() {bbb;}"
Token::eraseTokens(tok, elseTag->tokAt(2));
ret = true;
} else {
// Keep first if, remove every else if and else after it
const Token *lastTagInIf = elseTag->tokAt(2);
while (lastTagInIf) {
if (lastTagInIf->str() == "(") {
lastTagInIf = lastTagInIf->link()->next();
}
lastTagInIf = lastTagInIf->link()->next();
if (!Token::simpleMatch(lastTagInIf, "else"))
break;
lastTagInIf = lastTagInIf->next();
if (lastTagInIf->str() == "if")
lastTagInIf = lastTagInIf->next();
}
Token::eraseTokens(elseTag->previous(), lastTagInIf);
ret = true;
}
} else {
// Handle else // Handle else
if (boolValue == false) { if (boolValue == false) {
// Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" or ";{bbb;}" // Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" or ";{bbb;}"
if (tok->previous())
tok = tok->previous();
else
tok->str(";");
Token::eraseTokens(tok, elseTag->next()); //remove '(false)'
tok->deleteNext(3);
//delete dead code inside scope
eraseDeadCode(tok, elseTag);
//remove 'else'
elseTag->deleteThis();
//remove 'if'
tok->deleteThis();
} else { } else {
if (elseTag->next()->str() == "{") {
// Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}" // Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}"
const Token *end = elseTag->next()->link(); const Token *end = elseTag->next()->link()->next();
// Remove the "else { aaa; }" // Remove "else { bbb; }"
Token::eraseTokens(elseTag->previous(), end->next()); elseTag = elseTag->previous();
} eraseDeadCode(elseTag, end);
// Remove "if( true )" // Remove "if( true )"
if (tok->previous()) tok->deleteNext(3);
tok = tok->previous(); tok->deleteThis();
else
tok->str(";");
tok->deleteNext(4);
} }
ret = true; ret = true;
} }
}
// Handle if without else // Handle if without else
else { else {
if (boolValue == false) { if (boolValue == false) {
// Remove if and its content //remove '(false)'
if (tok->previous()) tok->deleteNext(3);
tok = tok->previous(); //delete dead code inside scope
else eraseDeadCode(tok, elseTag);
tok->str(";"); //remove 'if'
tok->deleteThis();
Token::eraseTokens(tok, elseTag);
} else { } else {
// convert "if( true ) {aaa;}" => "{aaa;}" // convert "if( true ) {aaa;}" => "{aaa;}"
if (tok->previous()) tok->deleteNext(3);
tok = tok->previous(); tok->deleteThis();
else
tok->str(";");
tok->deleteNext(4);
} }
ret = true; ret = true;