Fixed #4530 (Tokenizer: improved simplification of strlen in calculation)

This commit is contained in:
Daniel Marjamäki 2013-06-25 20:10:40 +02:00
parent cfd960d794
commit 7dd07472c1
2 changed files with 16 additions and 11 deletions

View File

@ -3503,6 +3503,16 @@ bool Tokenizer::simplifyTokenList()
modified |= simplifyConditions();
modified |= simplifyFunctionReturn();
modified |= simplifyKnownVariables();
// replace strlen(str)
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "strlen ( %str% )")) {
tok->str(MathLib::longToString(Token::getStrLength(tok->tokAt(2))));
tok->deleteNext(3);
modified = true;
}
}
modified |= removeRedundantConditions();
modified |= simplifyRedundantParentheses();
modified |= simplifyConstTernaryOp();
@ -3511,16 +3521,6 @@ bool Tokenizer::simplifyTokenList()
simplifyConditionOperator();
// replace strlen(str)
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "strlen ( %str% )")) {
std::ostringstream ostr;
ostr << Token::getStrLength(tok->tokAt(2));
tok->str(ostr.str());
tok->deleteNext(3);
}
}
// simplify redundant for
removeRedundantFor();

View File

@ -91,6 +91,7 @@ private:
TEST_CASE(casting);
TEST_CASE(strlen1);
TEST_CASE(strlen2);
TEST_CASE(template1);
TEST_CASE(template2);
@ -1651,7 +1652,11 @@ private:
}
void strlen2() {
// #4530 - make sure calculation with strlen is simplified
ASSERT_EQUALS("i = -4 ;",
tok("i = (strlen(\"abcd\") - 8);"));
}