templates: speedup by breaking out inner loops (#257)

This commit is contained in:
Daniel Marjamäki 2009-05-05 20:16:57 +02:00
parent fd0e592767
commit feba87187a
2 changed files with 42 additions and 3 deletions

View File

@ -469,10 +469,30 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
void Tokenizer::simplifyTemplates()
{
// Locate templates..
std::list<Token *> templates;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (!Token::simpleMatch(tok, "template <"))
continue;
if (Token::simpleMatch(tok, "template <"))
templates.push_back(tok);
}
if (templates.empty())
return;
// Locate possible instantiations of templates..
std::list<Token *> used;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "%var% <") && tok->str() != "template")
used.push_back(tok);
}
if (used.empty())
return;
// expand templates
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
{
Token *tok = *iter1;
std::vector<std::string> type;
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
@ -490,6 +510,7 @@ void Tokenizer::simplifyTemplates()
const bool isfunc(tok->strAt(3)[0] == '(');
// locate template usage..
std::string s(name + " <");
for (unsigned int i = 0; i < type.size(); ++i)
{
@ -498,8 +519,11 @@ void Tokenizer::simplifyTemplates()
s += " %any% ";
}
const std::string pattern(s + "> ");
for (Token *tok2 = _tokens; tok2; tok2 = tok2->next())
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
{
Token *tok2 = *iter2;
if (tok2->str() != name)
continue;

View File

@ -83,6 +83,7 @@ private:
TEST_CASE(template3);
TEST_CASE(template4);
TEST_CASE(template5);
TEST_CASE(template6);
TEST_CASE(namespaces);
@ -561,6 +562,20 @@ private:
ASSERT_EQUALS(expected, sizeof_(code));
}
void template6()
{
const char code[] = "template <classname T> class Fred { };\n"
"Fred<float> fred1;\n"
"Fred<float> fred2;";
const std::string expected(" template < classname T > class Fred { } ;"
" Fred<float> fred1 ;"
" Fred<float> fred2 ;"
" class Fred<float> { }");
ASSERT_EQUALS(expected, sizeof_(code));
}
void namespaces()
{