Revert "Optimization: Made Tokenizer::setVarId a bit faster by temporarily assigning 'string id'. This means less string comparisons are needed."

This reverts commit 4b8080e425.
This commit is contained in:
Daniel Marjamäki 2010-04-12 21:17:31 +02:00
parent 05eb894b82
commit 38a3fe37f9
1 changed files with 24 additions and 65 deletions

View File

@ -2187,57 +2187,27 @@ void Tokenizer::updateClassList()
void Tokenizer::setVarId() void Tokenizer::setVarId()
{ {
unsigned int _varId = 0; // Clear all variable ids
// Assign string ids so that string comparisons will not be needed
{
std::map<std::string, unsigned int> id1;
unsigned int strid = 0;
// Initialize id according to strings..
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{
if (!tok->isName())
tok->varId(0); tok->varId(0);
else
{
std::map<std::string, unsigned int>::const_iterator it;
it = id1.find(tok->str());
if (it != id1.end())
tok->varId(it->second);
else
{
++strid;
id1[tok->str()] = strid;
tok->varId(strid);
}
}
}
_varId = strid + 1;
}
const unsigned int varId1(_varId);
// Set variable ids.. // Set variable ids..
unsigned int _varId = 0;
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
if (tok != _tokens && !Token::Match(tok, "[,;{}(] %var%")) if (tok != _tokens && !Token::Match(tok, "[,;{}(] %type%"))
continue; continue;
if (Token::Match(tok, "[,;{}(] %var%")) if (Token::Match(tok, "[,;{}(] %type%"))
tok = tok->next(); tok = tok->next();
if (tok->varId() >= varId1)
continue;
if (tok->str() == "new") if (tok->str() == "new")
continue; continue;
if (tok->str() == "unsigned") if (tok->str() == "unsigned")
tok = tok->next(); tok = tok->next();
if (Token::Match(tok, "class|struct %var% :|{|;")) if (Token::Match(tok, "class|struct %type% :|{|;"))
continue; continue;
if (Token::Match(tok, "else|return|typedef|delete|sizeof")) if (Token::Match(tok, "else|return|typedef|delete|sizeof"))
@ -2250,7 +2220,7 @@ void Tokenizer::setVarId()
tok = tok->tokAt(2); tok = tok->tokAt(2);
// Skip template arguments.. // Skip template arguments..
if (Token::Match(tok, "%var% <")) if (Token::Match(tok, "%type% <"))
{ {
int level = 1; int level = 1;
bool again; bool again;
@ -2263,13 +2233,13 @@ void Tokenizer::setVarId()
while (Token::Match(tok2, "%var% ::")) while (Token::Match(tok2, "%var% ::"))
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
if (Token::Match(tok2, "%var% <")) if (Token::Match(tok2, "%type% <"))
{ {
level++; level++;
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
again = true; again = true;
} }
else if (Token::Match(tok2, "%var% ,")) else if (Token::Match(tok2, "%type% ,"))
{ {
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
again = true; again = true;
@ -2307,13 +2277,13 @@ void Tokenizer::setVarId()
while (again); while (again);
} }
// Determine "string id" for variable.. // Determine name of declared variable..
unsigned int strid = 0; std::string varname;
Token *tok2 = tok->tokAt(1); Token *tok2 = tok->tokAt(1);
while (tok2) while (tok2)
{ {
if (tok2->isName()) if (tok2->isName())
strid = (tok2->str() == "operator") ? 0 : tok2->varId(); varname = tok2->str();
else if (tok2->str() != "*" && tok2->str() != "&") else if (tok2->str() != "*" && tok2->str() != "&")
break; break;
tok2 = tok2->next(); tok2 = tok2->next();
@ -2323,7 +2293,7 @@ void Tokenizer::setVarId()
if (!tok2) if (!tok2)
break; break;
if (strid == 0) if (varname == "operator" && Token::Match(tok2, "=|+|-|*|/|[| ]| ("))
continue; continue;
// Is it a function? // Is it a function?
@ -2337,7 +2307,7 @@ void Tokenizer::setVarId()
if (Token::Match(tok2->next(), "%num%") || if (Token::Match(tok2->next(), "%num%") ||
Token::Match(tok2->next(), "%bool%") || Token::Match(tok2->next(), "%bool%") ||
tok2->next()->str()[0] == '"' || tok2->next()->str()[0] == '"' ||
tok2->next()->varId() >= varId1) tok2->next()->varId() != 0)
{ {
// This is not a function // This is not a function
} }
@ -2348,7 +2318,7 @@ void Tokenizer::setVarId()
} }
// Variable declaration found => Set variable ids // Variable declaration found => Set variable ids
if (Token::Match(tok2, "[,();[=]")) if (Token::Match(tok2, "[,();[=]") && !varname.empty())
{ {
++_varId; ++_varId;
int indentlevel = 0; int indentlevel = 0;
@ -2357,7 +2327,7 @@ void Tokenizer::setVarId()
bool funcDeclaration = false; bool funcDeclaration = false;
for (tok2 = tok->next(); tok2; tok2 = tok2->next()) for (tok2 = tok->next(); tok2; tok2 = tok2->next())
{ {
if (!dot && tok2->varId() == strid && !Token::Match(tok2->previous(), "struct|union")) if (!dot && tok2->str() == varname && !Token::Match(tok2->previous(), "struct|union"))
tok2->varId(_varId); tok2->varId(_varId);
else if (tok2->str() == "{") else if (tok2->str() == "{")
++indentlevel; ++indentlevel;
@ -2393,13 +2363,13 @@ void Tokenizer::setVarId()
{ {
// str.clear is a variable // str.clear is a variable
// str.clear() is a member function // str.clear() is a member function
if (tok->varId() > varId1 && if (tok->varId() != 0 &&
Token::Match(tok->next(), ". %var% !!(") && Token::Match(tok->next(), ". %var% !!(") &&
tok->tokAt(2)->varId() <= varId1) tok->tokAt(2)->varId() == 0)
{ {
++_varId; ++_varId;
const std::string pattern("%varid% . " + tok->strAt(2)); const std::string pattern(std::string("%varid% . ") + tok->strAt(2));
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{ {
if (Token::Match(tok2, pattern.c_str(), tok->varId())) if (Token::Match(tok2, pattern.c_str(), tok->varId()))
@ -2418,7 +2388,7 @@ void Tokenizer::setVarId()
{ {
if (Token::simpleMatch(tok2->tokAt(3), "(")) if (Token::simpleMatch(tok2->tokAt(3), "("))
allMemberFunctions.push_back(tok2); allMemberFunctions.push_back(tok2);
else if (tok2->tokAt(2)->varId() >= _varId) else if (tok2->tokAt(2)->varId() != 0)
allMemberVars.push_back(tok2); allMemberVars.push_back(tok2);
} }
} }
@ -2431,6 +2401,7 @@ void Tokenizer::setVarId()
{ {
const std::string &classname(tok->next()->str()); const std::string &classname(tok->next()->str());
// What member variables are there in this class? // What member variables are there in this class?
std::map<std::string, unsigned int> varlist; std::map<std::string, unsigned int> varlist;
{ {
@ -2448,7 +2419,7 @@ void Tokenizer::setVarId()
} }
// Found a member variable.. // Found a member variable..
else if (indentlevel == 1 && tok2->varId() >= varId1) else if (indentlevel == 1 && tok2->varId() > 0)
varlist[tok2->str()] = tok2->varId(); varlist[tok2->str()] = tok2->varId();
} }
} }
@ -2507,7 +2478,7 @@ void Tokenizer::setVarId()
--indentlevel; --indentlevel;
} }
else if (indentlevel > 0 && else if (indentlevel > 0 &&
tok2->varId() > 0 && tok2->varId() == 0 &&
varlist.find(tok2->str()) != varlist.end()) varlist.find(tok2->str()) != varlist.end())
{ {
tok2->varId(varlist[tok2->str()]); tok2->varId(varlist[tok2->str()]);
@ -2517,18 +2488,6 @@ void Tokenizer::setVarId()
} }
} }
// Cleanup string ids..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->varId())
{
if (tok->varId() >= varId1)
tok->varId(tok->varId() - varId1);
else
tok->varId(0);
}
}
} }