Refactoring: Move template code into templatesimplifier: simplifyTemplatesGetTemplateNamePosition()

This commit is contained in:
Reijo Tomperi 2012-01-03 23:49:50 +02:00
parent ac290b1a8a
commit 0369681a2c
4 changed files with 47 additions and 48 deletions

View File

@ -551,4 +551,22 @@ bool TemplateSimplifier::simplifyTemplatesInstantiateMatch(const Token *instance
return true;
}
int TemplateSimplifier::simplifyTemplatesGetTemplateNamePosition(const Token *tok)
{
// get the position of the template name
int namepos = 0;
if (Token::Match(tok, "> class|struct %type% {|:"))
namepos = 2;
else if (Token::Match(tok, "> %type% *|&| %type% ("))
namepos = 2;
else if (Token::Match(tok, "> %type% %type% *|&| %type% ("))
namepos = 3;
else {
// Name not found
return -1;
}
if ((tok->strAt(namepos) == "*" || tok->strAt(namepos) == "&"))
++namepos;
return namepos;
}

View File

@ -101,6 +101,13 @@ public:
*/
static bool simplifyTemplatesInstantiateMatch(const Token *instance, const std::string &name, size_t numberOfArguments, const char patternAfter[]);
/**
* 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.
*/
static int simplifyTemplatesGetTemplateNamePosition(const Token *tok);
};
/// @}

View File

@ -2677,44 +2677,6 @@ void Tokenizer::simplifyLabelsCaseDefault()
}
}
int Tokenizer::simplifyTemplatesGetTemplateNamePosition(const Token *tok)
{
// get the position of the template name
int namepos = 0;
if (Token::Match(tok, "> class|struct %type% {|:"))
namepos = 2;
else if (Token::Match(tok, "> %type% *|&| %type% ("))
namepos = 2;
else if (Token::Match(tok, "> %type% %type% *|&| %type% ("))
namepos = 3;
else {
// debug message that we bail out..
if (_settings->debugwarnings) {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.setfile(file(tok));
locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::debug,
"simplifyTemplates: bailing out",
"debug",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
}
return -1;
}
if ((tok->strAt(namepos) == "*" || tok->strAt(namepos) == "&"))
++namepos;
return namepos;
}
void Tokenizer::simplifyTemplatesExpandTemplate(const Token *tok,
const std::string &name,
std::vector<const Token *> &typeParametersInDeclaration,
@ -2845,9 +2807,29 @@ void Tokenizer::simplifyTemplateInstantions(const Token *tok,
return;
// get the position of the template name
int namepos = simplifyTemplatesGetTemplateNamePosition(tok);
if (namepos == -1)
int namepos = TemplateSimplifier::simplifyTemplatesGetTemplateNamePosition(tok);
if (namepos == -1) {
// debug message that we bail out..
if (_settings->debugwarnings) {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.setfile(file(tok));
locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::debug,
"simplifyTemplates: bailing out",
"debug",
false);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
}
return;
}
// name of template function/class..
const std::string name(tok->strAt(namepos));

View File

@ -509,14 +509,6 @@ public:
std::vector<const Token *> &typesUsedInTemplateInstantion,
std::list<Token *> &templateInstantiations);
/**
* 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);
/**
* Simplify e.g. 'atol("0")' into '0'
*/