Tokenizer::setVarId: Refactoring, use continue in loops

This commit is contained in:
Daniel Marjamäki 2016-05-12 18:58:24 +02:00
parent 372763c85e
commit 1d21cf5755
1 changed files with 98 additions and 96 deletions

View File

@ -2888,121 +2888,123 @@ void Tokenizer::setVarIdPass2()
// class members.. // class members..
std::map<std::string, std::map<std::string, unsigned int> > varsByClass; std::map<std::string, std::map<std::string, unsigned int> > varsByClass;
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "namespace|class|struct %name% {|:|::")) { if (!Token::Match(tok, "namespace|class|struct %name% {|:|::"))
std::string classname(tok->next()->str()); continue;
const Token* tokStart = tok->tokAt(2);
unsigned int nestedCount = 1;
while (Token::Match(tokStart, ":: %name%")) {
classname += " :: " + tokStart->strAt(1);
tokStart = tokStart->tokAt(2);
nestedCount++;
}
std::map<std::string, unsigned int>& thisClassVars = varsByClass[classname]; std::string classname(tok->next()->str());
while (tokStart && tokStart->str() != "{") { const Token* tokStart = tok->tokAt(2);
if (Token::Match(tokStart, "public|private|protected %name%")) unsigned int nestedCount = 1;
tokStart = tokStart->next(); while (Token::Match(tokStart, ":: %name%")) {
if (tokStart->strAt(1) == "," || tokStart->strAt(1) == "{") { classname += " :: " + tokStart->strAt(1);
const std::map<std::string, unsigned int>& baseClassVars = varsByClass[tokStart->str()]; tokStart = tokStart->tokAt(2);
thisClassVars.insert(baseClassVars.begin(), baseClassVars.end()); nestedCount++;
} }
std::map<std::string, unsigned int>& thisClassVars = varsByClass[classname];
while (tokStart && tokStart->str() != "{") {
if (Token::Match(tokStart, "public|private|protected %name%"))
tokStart = tokStart->next(); tokStart = tokStart->next();
if (tokStart->strAt(1) == "," || tokStart->strAt(1) == "{") {
const std::map<std::string, unsigned int>& baseClassVars = varsByClass[tokStart->str()];
thisClassVars.insert(baseClassVars.begin(), baseClassVars.end());
} }
// What member variables are there in this class? tokStart = tokStart->next();
if (tokStart) { }
for (Token *tok2 = tokStart->next(); tok2 && tok2 != tokStart->link(); tok2 = tok2->next()) { // What member variables are there in this class?
// skip parentheses.. if (tokStart) {
if (tok2->link()) { for (Token *tok2 = tokStart->next(); tok2 && tok2 != tokStart->link(); tok2 = tok2->next()) {
if (tok2->str() == "{") { // skip parentheses..
if (tok2->strAt(-1) == ")" || tok2->strAt(-2) == ")") if (tok2->link()) {
setVarIdClassFunction(classname, tok2, tok2->link(), thisClassVars, structMembers, &_varId); if (tok2->str() == "{") {
tok2 = tok2->link(); if (tok2->strAt(-1) == ")" || tok2->strAt(-2) == ")")
} else if (tok2->str() == "(" && tok2->link()->strAt(1) != "(") setVarIdClassFunction(classname, tok2, tok2->link(), thisClassVars, structMembers, &_varId);
tok2 = tok2->link(); tok2 = tok2->link();
} } else if (tok2->str() == "(" && tok2->link()->strAt(1) != "(")
tok2 = tok2->link();
// Found a member variable..
else if (tok2->varId() > 0)
thisClassVars[tok2->str()] = tok2->varId();
} }
}
// Are there any member variables in this class? // Found a member variable..
if (thisClassVars.empty()) else if (tok2->varId() > 0)
thisClassVars[tok2->str()] = tok2->varId();
}
}
// Are there any member variables in this class?
if (thisClassVars.empty())
continue;
// Member variables
for (std::list<Token *>::iterator func = allMemberVars.begin(); func != allMemberVars.end(); ++func) {
if (!Token::simpleMatch(*func, classname.c_str()))
continue; continue;
// Member variables Token *tok2 = *func;
for (std::list<Token *>::iterator func = allMemberVars.begin(); func != allMemberVars.end(); ++func) { tok2 = tok2->tokAt(2);
if (!Token::simpleMatch(*func, classname.c_str())) tok2->varId(thisClassVars[tok2->str()]);
continue; }
Token *tok2 = *func; if (isC() || tok->str() == "namespace")
tok2 = tok2->tokAt(2); continue;
tok2->varId(thisClassVars[tok2->str()]);
}
if (isC() || tok->str() == "namespace") // Set variable ids in member functions for this class..
for (std::list<Token *>::iterator func = allMemberFunctions.begin(); func != allMemberFunctions.end(); ++func) {
Token *tok2 = *func;
if (!Token::Match(tok2, classname.c_str()))
continue; continue;
// Set variable ids in member functions for this class.. if (Token::Match(tok2, "%name% <"))
for (std::list<Token *>::iterator func = allMemberFunctions.begin(); func != allMemberFunctions.end(); ++func) { tok2 = tok2->next()->findClosingBracket();
Token *tok2 = *func;
if (!Token::Match(tok2, classname.c_str())) // Found a class function..
continue; if (!Token::Match(tok2, "%any% :: ~| %name%"))
continue;
if (Token::Match(tok2, "%name% <")) // Goto the end parentheses..
tok2 = tok2->next()->findClosingBracket(); tok2 = tok2->tokAt(nestedCount*2);
if (tok2->str() == "~")
tok2 = tok2->linkAt(2);
else
tok2 = tok2->linkAt(1);
// Found a class function.. // If this is a function implementation.. add it to funclist
if (!Token::Match(tok2, "%any% :: ~| %name%")) Token * start = const_cast<Token *>(isFunctionHead(tok2, "{"));
continue; if (start) {
setVarIdClassFunction(classname, start, start->link(), thisClassVars, structMembers, &_varId);
}
// Goto the end parentheses.. if (Token::Match(tok2, ") %name% ("))
tok2 = tok2->tokAt(nestedCount*2); tok2 = tok2->linkAt(2);
if (tok2->str() == "~")
tok2 = tok2->linkAt(2);
else
tok2 = tok2->linkAt(1);
// If this is a function implementation.. add it to funclist // constructor with initializer list
Token * start = const_cast<Token *>(isFunctionHead(tok2, "{")); if (!Token::Match(tok2, ") : ::| %name%"))
if (start) { continue;
setVarIdClassFunction(classname, start, start->link(), thisClassVars, structMembers, &_varId);
}
if (Token::Match(tok2, ") %name% (")) Token *tok3 = tok2;
tok2 = tok2->linkAt(2); while (Token::Match(tok3, "[)}] [,:]")) {
tok3 = tok3->tokAt(2);
if (Token::Match(tok3, ":: %name%"))
tok3 = tok3->next();
while (Token::Match(tok3, "%name% :: %name%"))
tok3 = tok3->tokAt(2);
if (!Token::Match(tok3, "%name% (|{|<"))
break;
// constructor with initializer list // set varid
if (Token::Match(tok2, ") : ::| %name%")) { std::map<std::string, unsigned int>::const_iterator varpos = thisClassVars.find(tok3->str());
Token *tok3 = tok2; if (varpos != thisClassVars.end())
while (Token::Match(tok3, "[)}] [,:]")) { tok3->varId(varpos->second);
tok3 = tok3->tokAt(2);
if (Token::Match(tok3, ":: %name%"))
tok3 = tok3->next();
while (Token::Match(tok3, "%name% :: %name%"))
tok3 = tok3->tokAt(2);
if (!Token::Match(tok3, "%name% (|{|<"))
break;
// set varid // goto end of var
std::map<std::string, unsigned int>::const_iterator varpos = thisClassVars.find(tok3->str()); if (tok3->strAt(1) == "<") {
if (varpos != thisClassVars.end()) tok3 = tok3->next()->findClosingBracket();
tok3->varId(varpos->second); if (tok3 && tok3->next() && tok3->next()->link())
tok3 = tok3->next()->link();
// goto end of var } else
if (tok3->strAt(1) == "<") { tok3 = tok3->linkAt(1);
tok3 = tok3->next()->findClosingBracket(); }
if (tok3 && tok3->next() && tok3->next()->link()) if (Token::Match(tok3, ")|} {")) {
tok3 = tok3->next()->link(); setVarIdClassFunction(classname, tok2, tok3->next()->link(), thisClassVars, structMembers, &_varId);
} else
tok3 = tok3->linkAt(1);
}
if (Token::Match(tok3, ")|} {")) {
setVarIdClassFunction(classname, tok2, tok3->next()->link(), thisClassVars, structMembers, &_varId);
}
}
} }
} }
} }