From 46b543ba272fd938442ec8b6b8e9a44bf2d7c550 Mon Sep 17 00:00:00 2001 From: IOBYTE Date: Sun, 23 Jun 2019 04:55:09 -0400 Subject: [PATCH] template simplifier: stop running passes when nothing was simplified. (#1914) --- lib/templatesimplifier.cpp | 45 ++++++++++++++++++++++++++------------ lib/templatesimplifier.h | 1 + 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 1c79eeda0..7e223feec 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -197,7 +197,8 @@ bool TemplateSimplifier::TokenAndName::isAliasToken(const Token *tok) const } TemplateSimplifier::TemplateSimplifier(Tokenizer *tokenizer) - : mTokenizer(tokenizer), mTokenList(tokenizer->list), mSettings(tokenizer->mSettings), mErrorLogger(tokenizer->mErrorLogger) + : mTokenizer(tokenizer), mTokenList(tokenizer->list), mSettings(tokenizer->mSettings), + mErrorLogger(tokenizer->mErrorLogger), mChanged(false) { } @@ -1222,6 +1223,8 @@ void TemplateSimplifier::simplifyTemplateAliases() continue; } + mChanged = true; + // copy template-id from declaration to after instantiation Token * dst = aliasUsage.token->next()->findClosingBracket(); Token * end = TokenList::copyTokens(dst, aliasDeclaration.aliasStartToken(), aliasDeclaration.aliasEndToken()->previous(), false)->next(); @@ -2770,6 +2773,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations( if (expandedtemplates.insert(newFullName).second) { expandTemplate(templateDeclaration, instantiation, typeParametersInDeclaration, newName, !specialized && !isVar); instantiated = true; + mChanged = true; } // Replace all these template usages.. @@ -2835,6 +2839,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations( if (expandedtemplates.insert(newFullName).second) { expandTemplate(templateDeclaration, templateDeclaration, typeParametersInDeclaration, newName, !specialized && !isVar); instantiated = true; + mChanged = true; } // Replace all these template usages.. @@ -3289,20 +3294,19 @@ void TemplateSimplifier::simplifyTemplates( } } - // TODO: 4 is not the ideal number of loops. - // We should loop until the number of declarations is 0 but we can't - // do that until we instantiate unintstantiated templates with their symbolic types. - // That will allow the uninstantiated template code to be removed from the symbol database. - // Unfortunately the template simplifier doesn't handle namespaces properly so - // the uninstantiated template code in the symbol database can't be removed until #8768 - // is fixed. - - for (int i = 0; i < 4; ++i) { - if (i) { + unsigned int passCount = 0; + const unsigned int passCountMax = 10; + for (; passCount < passCountMax; ++passCount) { + if (passCount) { // it may take more than one pass to simplify type aliases + bool usingChanged = false; while (mTokenizer->simplifyUsing()) - ; + usingChanged = true; + if (!usingChanged && !mChanged) + break; + + mChanged = usingChanged; mTemplateDeclarations.clear(); mTemplateForwardDeclarations.clear(); mTemplateForwardDeclarationsMap.clear(); @@ -3316,7 +3320,7 @@ void TemplateSimplifier::simplifyTemplates( bool hasTemplates = getTemplateDeclarations(); - if (i == 0) + if (passCount == 0) codeWithTemplates = hasTemplates; // Make sure there is something to simplify. @@ -3341,7 +3345,7 @@ void TemplateSimplifier::simplifyTemplates( simplifyTemplateAliases(); if (mSettings->debugtemplate) - printOut("### Template Simplifier pass " + std::to_string(i + 1) + " ###"); + printOut("### Template Simplifier pass " + std::to_string(passCount + 1) + " ###"); std::set expandedtemplates; @@ -3419,6 +3423,19 @@ void TemplateSimplifier::simplifyTemplates( } } } + + if (passCount == passCountMax) { + if (mSettings->debugwarnings) { + const std::list locationList(1, mTokenList.front()); + const ErrorLogger::ErrorMessage errmsg(locationList, &mTokenizer->list, + Severity::debug, + "debug", + "TemplateSimplifier: pass count limit hit before simplifications were finished.", + false); + if (mErrorLogger) + mErrorLogger->reportErr(errmsg); + } + } } void TemplateSimplifier::syntaxError(const Token *tok) diff --git a/lib/templatesimplifier.h b/lib/templatesimplifier.h index 67a1487a1..a90ba8ac8 100644 --- a/lib/templatesimplifier.h +++ b/lib/templatesimplifier.h @@ -460,6 +460,7 @@ private: TokenList &mTokenList; const Settings *mSettings; ErrorLogger *mErrorLogger; + bool mChanged; std::list mTemplateDeclarations; std::list mTemplateForwardDeclarations;