Refactorization: Changed simplifyMathFunctions() to a single pass simplifier

This commit is contained in:
PKEuS 2014-10-31 17:52:29 +01:00
parent f8371f65f0
commit c7d315fba3
3 changed files with 14 additions and 9 deletions

View File

@ -1672,7 +1672,7 @@ bool Tokenizer::tokenizeCondition(const std::string &code)
simplifyCAlternativeTokens();
// Convert e.g. atol("0") into 0
while (simplifyMathFunctions()) {};
simplifyMathFunctions();
simplifyDoublePlusAndDoubleMinus();
@ -3563,7 +3563,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
simplifyInitVar();
// Convert e.g. atol("0") into 0
while (simplifyMathFunctions()) {};
simplifyMathFunctions();
simplifyDoublePlusAndDoubleMinus();
@ -3786,7 +3786,7 @@ bool Tokenizer::simplifyTokenList2()
simplifyStaticConst();
while (simplifyMathFunctions()) {};
simplifyMathFunctions();
validate();
@ -8384,10 +8384,10 @@ bool Tokenizer::isTwoNumber(const std::string &s)
// Reference:
// - http://www.cplusplus.com/reference/cmath/
// ------------------------------------------------------
bool Tokenizer::simplifyMathFunctions()
void Tokenizer::simplifyMathFunctions()
{
bool simplifcationMade = false;
for (Token *tok = list.front(); tok; tok = tok->next()) {
bool simplifcationMade = false;
if (tok->isName() && tok->strAt(1) == "(") { // precondition for function
if (Token::Match(tok, "atol ( %str% )")) { //@todo Add support for atoll()
if (tok->previous() &&
@ -8619,9 +8619,13 @@ bool Tokenizer::simplifyMathFunctions()
}
}
}
// Jump back to begin of statement if a simplification was performed
if (simplifcationMade) {
while (tok->previous() && tok->str() != ";") {
tok = tok->previous();
}
}
}
// returns true if a simplifcation was performed and false otherwise.
return simplifcationMade;
}
void Tokenizer::simplifyComma()

View File

@ -499,9 +499,8 @@ public:
/**
* Simplify e.g. 'atol("0")' into '0'
* @return true if simplifcations performed and false otherwise.
*/
bool simplifyMathFunctions();
void simplifyMathFunctions();
/**
* Simplify e.g. 'sin(0)' into '0'

View File

@ -8088,6 +8088,7 @@ private:
" std::cout<<llabs(-1);\n"
" std::cout<<atol(\"1\");\n"
" std::cout<<atol(\"x\");\n"
" std::cout<<abs(atol(\"1\"));\n" // nested calls
"}";
const char expected1[] = "void foo ( ) {\n"
"std :: cout << 0 ;\n"
@ -8104,6 +8105,7 @@ private:
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << atol ( \"x\" ) ;\n"
"std :: cout << 1 ;\n"
"}";
ASSERT_EQUALS(expected1, tokenizeAndStringify(code1));