Optimize usage of TemplateSimplifier::simplifyCalculations

This commit is contained in:
Daniel Marjamäki 2018-05-11 14:48:59 +02:00
parent 307f992c2c
commit 3044612fe9
3 changed files with 8 additions and 6 deletions

View File

@ -1241,10 +1241,10 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
// TODO: This is not the correct class for simplifyCalculations(), so it // TODO: This is not the correct class for simplifyCalculations(), so it
// should be moved away. // should be moved away.
bool TemplateSimplifier::simplifyCalculations(Token *_tokens) bool TemplateSimplifier::simplifyCalculations(Token *start, const Token * const end)
{ {
bool ret = false, goback = false; bool ret = false, goback = false;
for (Token *tok = _tokens; tok; tok = tok->next()) { for (Token *tok = start; tok != end; tok = tok->next()) {
if (goback) { if (goback) {
tok = tok->previous(); tok = tok->previous();
goback = false; goback = false;
@ -1527,7 +1527,6 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
for (std::list<TokenAndName>::const_iterator iter2 = templateInstantiations.begin(); iter2 != templateInstantiations.end(); ++iter2) { for (std::list<TokenAndName>::const_iterator iter2 = templateInstantiations.begin(); iter2 != templateInstantiations.end(); ++iter2) {
if (numberOfTemplateInstantiations != templateInstantiations.size()) { if (numberOfTemplateInstantiations != templateInstantiations.size()) {
numberOfTemplateInstantiations = templateInstantiations.size(); numberOfTemplateInstantiations = templateInstantiations.size();
simplifyCalculations(tokenlist.front());
++recursiveCount; ++recursiveCount;
if (recursiveCount > 100) { if (recursiveCount > 100) {
// bail out.. // bail out..
@ -1542,6 +1541,8 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
if (iter2->name != templateDeclaration.name) if (iter2->name != templateDeclaration.name)
continue; continue;
simplifyCalculations(iter2->token->next(), iter2->token->linkAt(1));
if (!matchSpecialization(tok->tokAt(namepos), iter2->token, specializations)) if (!matchSpecialization(tok->tokAt(namepos), iter2->token, specializations))
continue; continue;

View File

@ -224,11 +224,12 @@ public:
/** /**
* Simplify constant calculations such as "1+2" => "3". * Simplify constant calculations such as "1+2" => "3".
* This also performs simple cleanup of parentheses etc. * This also performs simple cleanup of parentheses etc.
* @param _tokens start token * @param start start token
* @param end end token
* @return true if modifications to token-list are done. * @return true if modifications to token-list are done.
* false if no modifications are done. * false if no modifications are done.
*/ */
static bool simplifyCalculations(Token *_tokens); static bool simplifyCalculations(Token *start, const Token *end = nullptr);
private: private:
/** /**

View File

@ -1097,7 +1097,7 @@ private:
"struct Factorial<4> { enum FacHelper { value = 4 * Factorial<3> :: value } ; } ; " "struct Factorial<4> { enum FacHelper { value = 4 * Factorial<3> :: value } ; } ; "
"struct Factorial<3> { enum FacHelper { value = 3 * Factorial<2> :: value } ; } ; " "struct Factorial<3> { enum FacHelper { value = 3 * Factorial<2> :: value } ; } ; "
"struct Factorial<2> { enum FacHelper { value = 2 * Factorial<1> :: value } ; } ; " "struct Factorial<2> { enum FacHelper { value = 2 * Factorial<1> :: value } ; } ; "
"struct Factorial<1> { enum FacHelper { value = Factorial < 0 > :: value } ; } ;"; "struct Factorial<1> { enum FacHelper { value = Factorial<0> :: value } ; } ;";
ASSERT_EQUALS(exp, tok(code)); ASSERT_EQUALS(exp, tok(code));
} }