diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a7e7b7473..7b2dcbe8b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index d4239e88e..189a88113 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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);")); + }