Tokenizer: Improved simplification of 'a?(1):b'

This commit is contained in:
Daniel Marjamäki 2012-09-08 10:45:00 +02:00
parent 2722f53edd
commit a39b58046f
2 changed files with 13 additions and 0 deletions

View File

@ -6586,6 +6586,18 @@ bool Tokenizer::simplifyRedundantParenthesis()
continue;
}
if (Token::simpleMatch(tok->previous(), "? (") && Token::simpleMatch(tok->link(), ") :")) {
const Token *tok2 = tok->next();
while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->isBoolean() || tok2->isArithmeticalOp()))
tok2 = tok2->next();
if (tok2 && tok2->str() == ")") {
tok->link()->deleteThis();
tok->deleteThis();
ret = true;
continue;
}
}
while (Token::Match(tok->previous(), "[{([,:] ( !!{") && Token::Match(tok->link(), ") [;,])]")) {
// We have "( ... )", remove the parenthesis
tok->link()->deleteThis();

View File

@ -4574,6 +4574,7 @@ private:
void removeParentheses15() {
ASSERT_EQUALS("a = b ? c : 123 ;", tokenizeAndStringify("a = b ? c : (123);", false));
ASSERT_EQUALS("a = b ? c : 579 ;", tokenizeAndStringify("a = b ? c : ((123)+(456));", false));
ASSERT_EQUALS("a = b ? 123 : c ;", tokenizeAndStringify("a = b ? (123) : c;", false));
}
void tokenize_double() {