varid: speedup of the algorithm for setting variable ids

This commit is contained in:
Daniel Marjamäki 2009-05-03 14:31:54 +02:00
parent f6d6a3855e
commit c9eab77683
1 changed files with 69 additions and 39 deletions

View File

@ -738,9 +738,11 @@ void Tokenizer::setVarId()
{ {
const std::string &classname(tok->next()->str()); const std::string &classname(tok->next()->str());
unsigned int indentlevel = 0;
// What member variables are there in this class? // What member variables are there in this class?
std::list<const Token *> varlist;
{
unsigned int indentlevel = 0;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{ {
// Indentation.. // Indentation..
@ -753,38 +755,66 @@ void Tokenizer::setVarId()
--indentlevel; --indentlevel;
} }
// If a variable id is found in the class declaration, start // Found a member variable..
// updating the variable ids in member functions..
else if (indentlevel == 1 && tok2->varId() > 0) else if (indentlevel == 1 && tok2->varId() > 0)
{ varlist.push_back(tok2);
const unsigned int varid(tok2->varId()); }
const std::string &varname(tok2->str()); }
for (Token *tok3 = tok; tok3; tok3 = tok3->next()) // Are there any member variables in this class?
if (varlist.empty())
continue;
// Member functions for this class..
std::list<Token *> funclist;
{
const std::string funcpattern(classname + " :: %var% (");
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{ {
// Found a class function.. // Found a class function..
if (Token::Match(tok3, (classname + " :: %var% (").c_str())) if (Token::Match(tok2, funcpattern.c_str()))
{ {
unsigned int indentlevel2 = 0; // Goto the end paranthesis..
while (tok3) while (tok2 && tok2->str() != ")")
tok2 = tok2->next();
if (!tok2)
break;
// If this is a function implementation.. add it to funclist
if (Token::Match(tok2, ") const|volatile| {"))
funclist.push_back(tok2);
}
}
}
// Are there any member functions for this class?
if (funclist.empty())
continue;
// Update the variable ids..
for (std::list<const Token *>::const_iterator var = varlist.begin(); var != varlist.end(); ++var)
{ {
if (tok3->str() == "{") const unsigned int varid((*var)->varId());
++indentlevel2; const std::string &varname((*var)->str());
else if (tok3->str() == "}")
// Parse each function..
for (std::list<Token *>::iterator func = funclist.begin(); func != funclist.end(); ++func)
{ {
if (indentlevel2 <= 1) unsigned int indentlevel = 0;
break; for (Token *tok2 = *func; tok2; tok2 = tok2->next())
--indentlevel2; {
} if (tok2->str() == "{")
else if (indentlevel2 == 0 && tok3->str() == ";") ++indentlevel;
break; else if (tok2->str() == "}")
else if (indentlevel2 > 0 && tok3->varId() == 0 && tok3->str() == varname) {
tok3->varId(varid); if (indentlevel <= 1)
tok3 = tok3->next();
}
if (!tok3)
break; break;
--indentlevel;
} }
else if (indentlevel > 0 && tok2->str() == varname && tok2->varId() == 0)
tok2->varId(varid);
} }
} }
} }