From 7dd07472c1acf0c369ce88efc5660f2a92934e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 25 Jun 2013 20:10:40 +0200 Subject: [PATCH] Fixed #4530 (Tokenizer: improved simplification of strlen in calculation) --- lib/tokenize.cpp | 20 ++++++++++---------- test/testsimplifytokens.cpp | 7 ++++++- 2 files changed, 16 insertions(+), 11 deletions(-) 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);")); + }