Fixed #5031: simplify labs|fabs|abs(-1) to 1.
This commit is contained in:
parent
3de9431507
commit
d5f514c820
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<<std::abs(0);\n" // in std:: namespeace
|
||||
" std::cout<<std::fabs(0.0);\n" // in std:: namespeace
|
||||
" std::cout<<abs(0);\n"
|
||||
" std::cout<<fabs(0.0);\n"
|
||||
" std::cout<<labs(0);\n"
|
||||
" std::cout<<llabs(0);\n"
|
||||
" std::cout<<std::abs(-1);\n" // in std:: namespeace
|
||||
" std::cout<<std::fabs(-1.0);\n" // in std:: namespeace
|
||||
" std::cout<<abs(-1);\n"
|
||||
" std::cout<<fabs(-1.0);\n"
|
||||
" std::cout<<labs(-1);\n"
|
||||
" std::cout<<llabs(-1);\n"
|
||||
" std::cout<<atol(\"1\");\n"
|
||||
"}";
|
||||
const char expected1[] = "void foo ( ) {\n"
|
||||
"std :: cout << 0 ;\n"
|
||||
"std :: cout << 0.0 ;\n"
|
||||
"std :: cout << 0 ;\n"
|
||||
"std :: cout << 0.0 ;\n"
|
||||
"std :: cout << 0 ;\n"
|
||||
"std :: cout << 0 ;\n"
|
||||
"std :: cout << 1 ;\n"
|
||||
"std :: cout << 1.0 ;\n"
|
||||
"std :: cout << 1 ;\n"
|
||||
"std :: cout << 1.0 ;\n"
|
||||
"std :: cout << 1 ;\n"
|
||||
"std :: cout << 1 ;\n"
|
||||
"std :: cout << 1 ;\n"
|
||||
"}";
|
||||
ASSERT_EQUALS(expected1, tokenizeAndStringify(code1));
|
||||
|
||||
// testcase from ticket #5031
|
||||
const char code2[] = "extern int a; void f(){printf(\"%i\", abs(--a));}\n";
|
||||
const char expected2[] = "extern int a ; void f ( ) { printf ( \"%i\" , abs ( -- a ) ) ; }";
|
||||
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2));
|
||||
}
|
||||
|
||||
|
||||
void simplifyMathExpressions() {//#1620
|
||||
const char code1[] = "void foo() {\n"
|
||||
" std::cout<<sin(0);\n"
|
||||
|
|
Loading…
Reference in New Issue