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
// Handle "else if" if (boolValue == false) {
if (boolValue == false) { // Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" or ";{bbb;}"
// 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(); //remove '(false)'
if (!Token::simpleMatch(lastTagInIf, "else")) tok->deleteNext(3);
break; //delete dead code inside scope
eraseDeadCode(tok, elseTag);
lastTagInIf = lastTagInIf->next(); //remove 'else'
if (lastTagInIf->str() == "if") elseTag->deleteThis();
lastTagInIf = lastTagInIf->next(); //remove 'if'
} tok->deleteThis();
Token::eraseTokens(elseTag->previous(), lastTagInIf);
ret = true;
}
} else { } else {
// Handle else // Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}"
if (boolValue == false) { const Token *end = elseTag->next()->link()->next();
// Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" or ";{bbb;}"
if (tok->previous())
tok = tok->previous();
else
tok->str(";");
Token::eraseTokens(tok, elseTag->next()); // Remove "else { bbb; }"
} else { elseTag = elseTag->previous();
if (elseTag->next()->str() == "{") { eraseDeadCode(elseTag, end);
// Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}"
const Token *end = elseTag->next()->link();
// Remove the "else { aaa; }" // Remove "if( true )"
Token::eraseTokens(elseTag->previous(), end->next()); tok->deleteNext(3);
} tok->deleteThis();
// Remove "if( true )"
if (tok->previous())
tok = tok->previous();
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;