From 943693acfb03043a9b864fbfac5e176ab6016bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 6 Jan 2018 08:40:06 +0100 Subject: [PATCH] TemplateSimplifier: Improved code for template aliases --- lib/templatesimplifier.cpp | 16 ++++++++++++++-- test/testsimplifytemplate.cpp | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 8fa5690f1..62410c6b3 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -803,9 +803,20 @@ void TemplateSimplifier::simplifyTemplateAliases(std::listnext(); // the '<' const Token * const endToken1 = templateAlias.token->next()->findClosingBracket(); Token * const endToken2 = Tokenizer::copyTokens(tok2, templateAlias.token->tokAt(2), endToken1->previous(), false); - for (; tok2 != endToken2; tok2 = tok2->next()) { - if (!tok2->isName() || aliasParameterNames.find(tok2->str()) == aliasParameterNames.end()) + for (const Token *tok1 = templateAlias.token->next(); tok2 != endToken2; tok1 = tok1->next(), tok2 = tok2->next()) { + if (!tok2->isName()) continue; + if (aliasParameterNames.find(tok2->str()) == aliasParameterNames.end()) { + // Create template instance.. + if (Token::Match(tok1, "%name% <")) { + const std::list::iterator it = std::find_if(templateInstantiations->begin(), + templateInstantiations->end(), + FindToken(tok1)); + if (it != templateInstantiations->end()) + templateInstantiations->push_back(TokenAndName(tok2, it->scope, it->name)); + } + continue; + } const unsigned int argnr = aliasParameterNames[tok2->str()]; const Token * const fromStart = args[argnr].first; const Token * const fromEnd = args[argnr].second->previous(); @@ -818,6 +829,7 @@ void TemplateSimplifier::simplifyTemplateAliases(std::listnext(); + // Remove alias usage code (parameters) Token::eraseTokens(tok2, args.back().second); } if (endToken) { diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 1a3b47d3b..708df4962 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -1612,7 +1612,7 @@ private: const char code[] = "template struct Tag {};\n" "template using SPtr = std::shared_ptr)>;\n" "SPtr<0> s;"; - const char expected[] = "template < int > struct Tag { } ; std :: shared_ptr < void ( Tag < 0 > ) > s ;"; + const char expected[] = "; std :: shared_ptr < void ( Tag<0> ) > s ; struct Tag<0> { } ;"; ASSERT_EQUALS(expected, tok(code)); }