diff --git a/src/tokenize.cpp b/src/tokenize.cpp index cb590d71a..8a6ceb7fb 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1938,13 +1938,11 @@ bool Tokenizer::simplifyQuestionMark() !Token::Match(tok->previous(), "%num%")) continue; - if (tok->previous()->str() == "false" || - tok->previous()->str() == "0") + // Find the ":" token.. + Token *semicolon = 0; { - // Use code after semicolon, remove code before it. - const Token *end = 0; unsigned int parlevel = 0; - for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) + for (Token *tok2 = tok; tok2; tok2 = tok2->next()) { if (tok2->str() == "(") ++parlevel; @@ -1956,17 +1954,21 @@ bool Tokenizer::simplifyQuestionMark() } else if (parlevel == 0 && tok2->str() == ":") { - end = tok2; + semicolon = tok2; break; } } + } + if (!semicolon || !semicolon->next()) + continue; - if (!end || !end->next()) - continue; - - end = end->next(); + if (tok->previous()->str() == "false" || + tok->previous()->str() == "0") + { + // Use code after semicolon, remove code before it. + semicolon = semicolon->next(); tok = tok->tokAt(-2); - while (tok->next() != end) + while (tok->next() != semicolon) { tok->deleteNext(); } @@ -1974,9 +1976,18 @@ bool Tokenizer::simplifyQuestionMark() tok = tok->next(); ret = true; } - else + + // The condition is true. Delete the operator after the ":".. + else if (Token::Match(semicolon, ": %num%") || Token::Match(semicolon, ": %var% [);]")) { - /** @todo simplify "(true ? x : y)" to "(x)" */ + // delete the condition token and the "?" + tok = tok->tokAt(-2); + tok->deleteNext(); + tok->deleteNext(); + + // delete the ":" token and the token after it.. + semicolon->deleteThis(); + semicolon->deleteThis(); } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 36058deeb..d0a499f75 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -1090,7 +1090,19 @@ private: { const char code[] = "(1?(false?1:2):3)"; - TODO_ASSERT_EQUALS("( 2 )", tok(code)); + ASSERT_EQUALS("( 2 )", tok(code)); + } + + { + const char code[] = "( true ? a ( ) : b ( ) )"; + ASSERT_EQUALS(code, tok(code)); + TODO_ASSERT_EQUALS("( a ( ) )", tok(code)); + } + + { + const char code[] = "( true ? abc . a : abc . b )"; + ASSERT_EQUALS(code, tok(code)); + TODO_ASSERT_EQUALS("( abc . a )", tok(code)); } } };