From d5f514c8209a848ea59d34dc6c19b426e53a5d9d Mon Sep 17 00:00:00 2001 From: Martin Ettl Date: Sat, 21 Sep 2013 16:59:06 +0200 Subject: [PATCH] Fixed #5031: simplify labs|fabs|abs(-1) to 1. --- lib/tokenize.cpp | 26 +++++++++++++++++++++++++- test/testtokenize.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9d46147d0..cef06fbcb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8247,7 +8247,11 @@ void Tokenizer::cppcheckError(const Token *tok) const "Analysis failed. If the code is valid then please report this failure."); } - +// ------------------------------------------------------ +// Simplify math functions. +// It simplifies following functions: atol(),abs(),fabs() +// labs(),llabs() in the tokenlist. +// ------------------------------------------------------ void Tokenizer::simplifyMathFunctions() { for (Token *tok = list.front(); tok; tok = tok->next()) { @@ -8272,6 +8276,26 @@ void Tokenizer::simplifyMathFunctions() // Convert string into a number tok->str(MathLib::longToString(MathLib::toLongNumber(tok->strValue()))); + // Delete remaining ) + tok->deleteNext(); + } else if (Token::Match(tok, "abs|fabs|labs|llabs ( %num% )")) { + + if (tok->previous() && + Token::simpleMatch(tok->tokAt(-2), "std ::")) { + // Delete "std ::" + tok = tok->tokAt(-2); + tok->deleteNext(); + tok->deleteThis(); + } + + // Delete abs ( + tok->deleteNext(); + tok->deleteThis(); + std::string strNumber(tok->str()); + if (!strNumber.empty() && strNumber[0] == '-') + strNumber = strNumber.substr(1); + // insert result into token list + tok->str(strNumber); // Delete remaining ) tok->deleteNext(); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 22ec7f348..72cb3a813 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -496,6 +496,8 @@ private: TEST_CASE(platformWin32AStringCat); // ticket #5015 TEST_CASE(platformWin32WStringCat); // ticket #5015 + TEST_CASE(simplifyMathFunctions); // ticket #5031 + TEST_CASE(simplifyMathExpressions); //ticket #1620 // AST data @@ -8276,6 +8278,46 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Win32W)); } + void simplifyMathFunctions() { //#5031 + const char code1[] = "void foo() {\n" + " std::cout<