Refactoring: Make a 300 line function a little smaller.

simplifyTemplatesInstantiate()
This commit is contained in:
Reijo Tomperi 2011-12-22 22:39:15 +02:00
parent 33f56ebc27
commit 1c12d04d0d
2 changed files with 37 additions and 19 deletions

View File

@ -3079,26 +3079,10 @@ static bool simplifyTemplatesInstantiateMatch(const Token *instance, const std::
return true; return true;
} }
void Tokenizer::simplifyTemplatesInstantiate(const Token *tok, int Tokenizer::simplifyTemplatesGetTemplateNamePosition(const Token *tok)
std::list<Token *> &used,
std::set<std::string> &expandedtemplates)
{ {
// this variable is not used at the moment. The intention was to
// allow continuous instantiations until all templates has been expanded
//bool done = false;
std::vector<const Token *> type;
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next()) {
if (Token::Match(tok, "%var% ,|>"))
type.push_back(tok);
}
// bail out if the end of the file was reached
if (!tok)
return;
// get the position of the template name // get the position of the template name
unsigned char namepos = 0; int namepos = 0;
if (Token::Match(tok, "> class|struct %type% {|:")) if (Token::Match(tok, "> class|struct %type% {|:"))
namepos = 2; namepos = 2;
else if (Token::Match(tok, "> %type% *|&| %type% (")) else if (Token::Match(tok, "> %type% *|&| %type% ("))
@ -3125,11 +3109,37 @@ void Tokenizer::simplifyTemplatesInstantiate(const Token *tok,
else else
Check::reportError(errmsg); Check::reportError(errmsg);
} }
return; return -1;
} }
if ((tok->strAt(namepos) == "*" || tok->strAt(namepos) == "&")) if ((tok->strAt(namepos) == "*" || tok->strAt(namepos) == "&"))
++namepos; ++namepos;
return namepos;
}
void Tokenizer::simplifyTemplatesInstantiate(const Token *tok,
std::list<Token *> &used,
std::set<std::string> &expandedtemplates)
{
// this variable is not used at the moment. The intention was to
// allow continuous instantiations until all templates has been expanded
//bool done = false;
std::vector<const Token *> type;
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next()) {
if (Token::Match(tok, "%var% ,|>"))
type.push_back(tok);
}
// bail out if the end of the file was reached
if (!tok)
return;
// get the position of the template name
int namepos = simplifyTemplatesGetTemplateNamePosition(tok);
if (namepos == -1)
return;
// name of template function/class.. // name of template function/class..
const std::string name(tok->strAt(namepos)); const std::string name(tok->strAt(namepos));

View File

@ -489,6 +489,14 @@ public:
std::list<Token *> &used, std::list<Token *> &used,
std::set<std::string> &expandedtemplates); std::set<std::string> &expandedtemplates);
/**
* Match template declaration/instantiation
* @param tok The ">" token e.g. before "class"
* @return -1 to bail out or positive integer to identity the position
* of the template name.
*/
int simplifyTemplatesGetTemplateNamePosition(const Token *tok);
/** /**
* Used after simplifyTemplates to perform a little cleanup. * Used after simplifyTemplates to perform a little cleanup.
* Sometimes the simplifyTemplates isn't fully successful and then * Sometimes the simplifyTemplates isn't fully successful and then