Tokenizer '?:' handling code refactorization:
move '=|,|(|[|{|}|;|case|return (%bool%) ?' code handling from simplifyConditionOperator in simplifyConstTernaryOp in order to use a more efficient code, thus to remove code duplication.
This commit is contained in:
parent
18e1eb7c70
commit
93f1fed205
|
@ -4148,30 +4148,6 @@ void Tokenizer::simplifyCompoundAssignment()
|
||||||
void Tokenizer::simplifyConditionOperator()
|
void Tokenizer::simplifyConditionOperator()
|
||||||
{
|
{
|
||||||
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
||||||
if (Token::Match(tok,"return|= ( %bool% ) ?")) {
|
|
||||||
Token *tok2 = tok->tokAt(5);
|
|
||||||
while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->isArithmeticalOp()))
|
|
||||||
tok2 = tok2->next();
|
|
||||||
if (tok2 && tok2->str() == ":") {
|
|
||||||
if (tok->strAt(2) == "false") {
|
|
||||||
Token::eraseTokens(tok,tok2->next());
|
|
||||||
} else {
|
|
||||||
Token *tok3 = tok2->next();
|
|
||||||
while (tok3 && (tok3->isName() || tok3->isNumber() || tok3->isArithmeticalOp()))
|
|
||||||
tok3 = tok3->next();
|
|
||||||
|
|
||||||
if (tok3 && tok3->str() == ";") {
|
|
||||||
tok->deleteNext(4);
|
|
||||||
tok = tok2;
|
|
||||||
while (tok && tok->str() != ";")
|
|
||||||
tok->deleteThis();
|
|
||||||
if (!tok)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tok->str() == "(" || tok->str() == "[" ||
|
if (tok->str() == "(" || tok->str() == "[" ||
|
||||||
(tok->str() == "{" && tok->previous() && tok->previous()->str() == "="))
|
(tok->str() == "{" && tok->previous() && tok->previous()->str() == "="))
|
||||||
tok = tok->link();
|
tok = tok->link();
|
||||||
|
@ -4423,23 +4399,32 @@ bool Tokenizer::simplifyConstTernaryOp()
|
||||||
if (tok->str() != "?")
|
if (tok->str() != "?")
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (!Token::Match(tok->tokAt(-2), "=|,|(|[|{|}|;|case|return"))
|
if (!Token::Match(tok->tokAt(-2), "=|,|(|[|{|}|;|case|return %any%") &&
|
||||||
|
!Token::Match(tok->tokAt(-4), "=|,|(|[|{|}|;|case|return ( %any% )"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (!tok->previous()->isBoolean() && !tok->previous()->isNumber())
|
const unsigned int offset = (tok->previous()->str() == ")") ? 2 : 1;
|
||||||
|
|
||||||
|
if (!tok->tokAt(-offset)->isBoolean() && !tok->tokAt(-offset)->isNumber())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Find the ":" token..
|
// Find the token ":" then go to the next token
|
||||||
Token *semicolon = skipTernaryOp(tok);
|
Token *semicolon = skipTernaryOp(tok);
|
||||||
if (!semicolon || semicolon->previous()->str() != ":" || !semicolon->next())
|
if (!semicolon || semicolon->previous()->str() != ":" || !semicolon->next())
|
||||||
continue;
|
continue;
|
||||||
semicolon = semicolon->previous();
|
|
||||||
|
|
||||||
if (tok->previous()->str() == "false" ||
|
// go back before the condition, if possible
|
||||||
tok->previous()->str() == "0") {
|
|
||||||
// Use code after semicolon, remove code before it.
|
|
||||||
semicolon = semicolon->next();
|
|
||||||
tok = tok->tokAt(-2);
|
tok = tok->tokAt(-2);
|
||||||
|
if (offset == 2) {
|
||||||
|
// go further back before the "("
|
||||||
|
tok = tok->tokAt(-2);
|
||||||
|
//simplify the parenthesis
|
||||||
|
tok->deleteNext();
|
||||||
|
tok->next()->deleteNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tok->next()->str() == "false" || tok->next()->str() == "0") {
|
||||||
|
// Use code after semicolon, remove code before it.
|
||||||
Token::eraseTokens(tok, semicolon);
|
Token::eraseTokens(tok, semicolon);
|
||||||
|
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
@ -4448,38 +4433,22 @@ bool Tokenizer::simplifyConstTernaryOp()
|
||||||
|
|
||||||
// The condition is true. Delete the operator after the ":"..
|
// The condition is true. Delete the operator after the ":"..
|
||||||
else {
|
else {
|
||||||
const Token *end = 0;
|
|
||||||
|
|
||||||
// check the operator after the :
|
|
||||||
if (Token::simpleMatch(semicolon, ": (")) {
|
|
||||||
end = semicolon->next()->link();
|
|
||||||
if (!Token::Match(end, ") !!."))
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete the condition token and the "?"
|
// delete the condition token and the "?"
|
||||||
tok = tok->tokAt(-2);
|
|
||||||
tok->deleteNext(2);
|
tok->deleteNext(2);
|
||||||
|
|
||||||
// delete operator after the :
|
unsigned int ternaryOplevel = 0;
|
||||||
if (end) {
|
for (const Token *endTok = semicolon; endTok; endTok = endTok->next()) {
|
||||||
Token::eraseTokens(semicolon->previous(), end->next());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int colonlevel = 0;
|
|
||||||
for (const Token *endTok = semicolon->next(); endTok; endTok = endTok->next()) {
|
|
||||||
if (endTok->str() == "(" || endTok->str() == "[" || endTok->str() == "{") {
|
if (endTok->str() == "(" || endTok->str() == "[" || endTok->str() == "{") {
|
||||||
endTok = endTok->link();
|
endTok = endTok->link();
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (endTok->str() == "?")
|
else if (endTok->str() == "?")
|
||||||
++colonlevel;
|
++ternaryOplevel;
|
||||||
else if (Token::Match(endTok, ")|}|]|;|:")) {
|
else if (Token::Match(endTok, ")|}|]|;|,|:")) {
|
||||||
if (endTok->str() == ":" && colonlevel)
|
if (endTok->str() == ":" && ternaryOplevel)
|
||||||
--colonlevel;
|
--ternaryOplevel;
|
||||||
else {
|
else {
|
||||||
Token::eraseTokens(semicolon->previous(), endTok);
|
Token::eraseTokens(semicolon->tokAt(-2), endTok);
|
||||||
ret = true;
|
ret = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue