TemplateSimplifier: Improved code for template aliases

This commit is contained in:
Daniel Marjamäki 2018-01-06 08:40:06 +01:00
parent e33fe91b1a
commit 943693acfb
2 changed files with 15 additions and 3 deletions

View File

@ -803,9 +803,20 @@ void TemplateSimplifier::simplifyTemplateAliases(std::list<TemplateSimplifier::T
tok2 = aliasUsage.token->next(); // the '<' tok2 = aliasUsage.token->next(); // the '<'
const Token * const endToken1 = templateAlias.token->next()->findClosingBracket(); const Token * const endToken1 = templateAlias.token->next()->findClosingBracket();
Token * const endToken2 = Tokenizer::copyTokens(tok2, templateAlias.token->tokAt(2), endToken1->previous(), false); Token * const endToken2 = Tokenizer::copyTokens(tok2, templateAlias.token->tokAt(2), endToken1->previous(), false);
for (; tok2 != endToken2; tok2 = tok2->next()) { for (const Token *tok1 = templateAlias.token->next(); tok2 != endToken2; tok1 = tok1->next(), tok2 = tok2->next()) {
if (!tok2->isName() || aliasParameterNames.find(tok2->str()) == aliasParameterNames.end()) if (!tok2->isName())
continue; continue;
if (aliasParameterNames.find(tok2->str()) == aliasParameterNames.end()) {
// Create template instance..
if (Token::Match(tok1, "%name% <")) {
const std::list<TokenAndName>::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 unsigned int argnr = aliasParameterNames[tok2->str()];
const Token * const fromStart = args[argnr].first; const Token * const fromStart = args[argnr].first;
const Token * const fromEnd = args[argnr].second->previous(); const Token * const fromEnd = args[argnr].second->previous();
@ -818,6 +829,7 @@ void TemplateSimplifier::simplifyTemplateAliases(std::list<TemplateSimplifier::T
endToken = endToken1->next(); endToken = endToken1->next();
// Remove alias usage code (parameters)
Token::eraseTokens(tok2, args.back().second); Token::eraseTokens(tok2, args.back().second);
} }
if (endToken) { if (endToken) {

View File

@ -1612,7 +1612,7 @@ private:
const char code[] = "template <int> struct Tag {};\n" const char code[] = "template <int> struct Tag {};\n"
"template <int ID> using SPtr = std::shared_ptr<void(Tag<ID>)>;\n" "template <int ID> using SPtr = std::shared_ptr<void(Tag<ID>)>;\n"
"SPtr<0> s;"; "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)); ASSERT_EQUALS(expected, tok(code));
} }