Refactoring: Move template code into templatesimplifier: removeTemplates()

This commit is contained in:
Reijo Tomperi 2012-01-02 22:46:43 +02:00
parent e9071a8bb6
commit 8084bc80fc
3 changed files with 65 additions and 70 deletions

View File

@ -21,13 +21,6 @@
#include <set> #include <set>
#include <string> #include <string>
//---------------------------------------------------------------------------
#ifdef _MSC_VER
#pragma warning(disable: 4503)
#endif
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
TemplateSimplifier::TemplateSimplifier() TemplateSimplifier::TemplateSimplifier()
@ -222,3 +215,60 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
} }
return 0; return 0;
} }
void TemplateSimplifier::removeTemplates(Token *tok)
{
bool goback = false;
for (; tok; tok = tok->next()) {
if (goback) {
tok = tok->previous();
goback = false;
}
if (!Token::simpleMatch(tok, "template <"))
continue;
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == "(") {
tok2 = tok2->link();
}
else if (tok2->str() == "{") {
tok2 = tok2->link()->next();
Token::eraseTokens(tok, tok2);
if (tok2 && tok2->str() == ";" && tok2->next())
tok->deleteNext();
tok->deleteThis();
goback = true;
break;
} else if (tok2->str() == "}") { // garbage code! (#3449)
Token::eraseTokens(tok,tok2);
tok->deleteThis();
break;
}
// don't remove constructor
if (tok2->str() == "explicit") {
Token::eraseTokens(tok, tok2);
tok->deleteThis();
goback = true;
break;
}
if (tok2->str() == ";") {
tok2 = tok2->next();
Token::eraseTokens(tok, tok2);
tok->deleteThis();
goback = true;
break;
}
if (Token::Match(tok2, ">|>> class|struct %var% [,)]")) {
tok2 = tok2->next();
Token::eraseTokens(tok, tok2);
tok->deleteThis();
goback = true;
break;
}
}
}
}

View File

@ -56,6 +56,11 @@ public:
* \return number of parameters (invalid parameters => 0) * \return number of parameters (invalid parameters => 0)
*/ */
static unsigned int templateParameters(const Token *tok); static unsigned int templateParameters(const Token *tok);
/**
* Remove "template < ..." they can cause false positives because they are not expanded
*/
static void removeTemplates(Token *tok);
}; };
/// @} /// @}

View File

@ -2681,66 +2681,6 @@ void Tokenizer::simplifyLabelsCaseDefault()
} }
} }
/**
* Remove "template < ..." they can cause false positives because they are not expanded
*/
static void removeTemplates(Token *tok)
{
bool goback = false;
for (; tok; tok = tok->next()) {
if (goback) {
tok = tok->previous();
goback = false;
}
if (!Token::simpleMatch(tok, "template <"))
continue;
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == "(") {
tok2 = tok2->link();
}
else if (tok2->str() == "{") {
tok2 = tok2->link()->next();
Token::eraseTokens(tok, tok2);
if (tok2 && tok2->str() == ";" && tok2->next())
tok->deleteNext();
tok->deleteThis();
goback = true;
break;
} else if (tok2->str() == "}") { // garbage code! (#3449)
Token::eraseTokens(tok,tok2);
tok->deleteThis();
break;
}
// don't remove constructor
if (tok2->str() == "explicit") {
Token::eraseTokens(tok, tok2);
tok->deleteThis();
goback = true;
break;
}
if (tok2->str() == ";") {
tok2 = tok2->next();
Token::eraseTokens(tok, tok2);
tok->deleteThis();
goback = true;
break;
}
if (Token::Match(tok2, ">|>> class|struct %var% [,)]")) {
tok2 = tok2->next();
Token::eraseTokens(tok, tok2);
tok->deleteThis();
goback = true;
break;
}
}
}
}
std::set<std::string> Tokenizer::simplifyTemplatesExpandSpecialized() std::set<std::string> Tokenizer::simplifyTemplatesExpandSpecialized()
{ {
std::set<std::string> expandedtemplates; std::set<std::string> expandedtemplates;
@ -3346,7 +3286,7 @@ void Tokenizer::simplifyTemplates()
std::list<Token *> templates(simplifyTemplatesGetTemplateDeclarations()); std::list<Token *> templates(simplifyTemplatesGetTemplateDeclarations());
if (templates.empty()) { if (templates.empty()) {
removeTemplates(_tokens); TemplateSimplifier::removeTemplates(_tokens);
return; return;
} }
@ -3369,7 +3309,7 @@ void Tokenizer::simplifyTemplates()
// No template instantiations? Then remove all templates. // No template instantiations? Then remove all templates.
if (templateInstantiations.empty()) { if (templateInstantiations.empty()) {
removeTemplates(_tokens); TemplateSimplifier::removeTemplates(_tokens);
return; return;
} }
@ -3386,7 +3326,7 @@ void Tokenizer::simplifyTemplates()
} }
} }
removeTemplates(_tokens); TemplateSimplifier::removeTemplates(_tokens);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------