Changed relationship between templatesimplifier and tokenizer:

- Pass a tokenizer to templatesimplifier to reduce code duplication (Make use of Tokenizer::reportError; remove redundant TemplateSimplifier::addtoken2) and amount of arguments passed to functions

Removed ctor and dtor implementation from TemplateSimplifier: This class shouldn't be instanciated.
This commit is contained in:
PKEuS 2012-04-23 20:45:36 +02:00
parent 6ae135124e
commit 8cbed66089
4 changed files with 38 additions and 113 deletions

View File

@ -18,10 +18,9 @@
#include "templatesimplifier.h" #include "templatesimplifier.h"
#include "mathlib.h" #include "mathlib.h"
#include "errorlogger.h"
#include "token.h" #include "token.h"
#include "tokenize.h"
#include "settings.h" #include "settings.h"
#include "check.h"
#include <sstream> #include <sstream>
#include <list> #include <list>
#include <set> #include <set>
@ -32,14 +31,6 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
TemplateSimplifier::TemplateSimplifier()
{
}
TemplateSimplifier::~TemplateSimplifier()
{
}
void TemplateSimplifier::cleanupAfterSimplify(Token *tokens) void TemplateSimplifier::cleanupAfterSimplify(Token *tokens)
{ {
bool goback = false; bool goback = false;
@ -568,27 +559,8 @@ int TemplateSimplifier::simplifyTemplatesGetTemplateNamePosition(const Token *to
} }
void TemplateSimplifier::addtoken2(Token ** token, const char str[], const unsigned int lineno, const unsigned int fileno)
{
(*token)->insertToken(str);
(*token)->linenr(lineno);
(*token)->fileIndex(fileno);
}
void TemplateSimplifier::addtoken2(Token ** token, const Token * tok, const unsigned int lineno, const unsigned int fileno)
{
(*token)->insertToken(tok->str());
(*token)->linenr(lineno);
(*token)->fileIndex(fileno);
(*token)->isUnsigned(tok->isUnsigned());
(*token)->isSigned(tok->isSigned());
(*token)->isLong(tok->isLong());
(*token)->isUnused(tok->isUnused());
}
void TemplateSimplifier::simplifyTemplatesExpandTemplate( void TemplateSimplifier::simplifyTemplatesExpandTemplate(
Token *_tokens, Tokenizer& tokenizer,
Token **_tokensBack,
const Token *tok, const Token *tok,
const std::string &name, const std::string &name,
std::vector<const Token *> &typeParametersInDeclaration, std::vector<const Token *> &typeParametersInDeclaration,
@ -596,7 +568,7 @@ void TemplateSimplifier::simplifyTemplatesExpandTemplate(
std::vector<const Token *> &typesUsedInTemplateInstantion, std::vector<const Token *> &typesUsedInTemplateInstantion,
std::list<Token *> &templateInstantiations) std::list<Token *> &templateInstantiations)
{ {
for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next()) { for (const Token *tok3 = tokenizer.tokens(); tok3; tok3 = tok3->next()) {
if (tok3->str() == "{" || tok3->str() == "(") if (tok3->str() == "{" || tok3->str() == "(")
tok3 = tok3->link(); tok3 = tok3->link();
@ -607,7 +579,7 @@ void TemplateSimplifier::simplifyTemplatesExpandTemplate(
// member function implemented outside class definition // member function implemented outside class definition
else if (TemplateSimplifier::simplifyTemplatesInstantiateMatch(tok3, name, typeParametersInDeclaration.size(), ":: ~| %var% (")) { else if (TemplateSimplifier::simplifyTemplatesInstantiateMatch(tok3, name, typeParametersInDeclaration.size(), ":: ~| %var% (")) {
addtoken2(_tokensBack, newName.c_str(), tok3->linenr(), tok3->fileIndex()); tokenizer.addtoken(newName.c_str(), tok3->linenr(), tok3->fileIndex());
while (tok3->str() != "::") while (tok3->str() != "::")
tok3 = tok3->next(); tok3 = tok3->next();
} }
@ -631,8 +603,8 @@ void TemplateSimplifier::simplifyTemplatesExpandTemplate(
// the "}" token should only be added if indentlevel is 1 but I add it always intentionally // the "}" token should only be added if indentlevel is 1 but I add it always intentionally
// if indentlevel ever becomes 0, cppcheck will write: // if indentlevel ever becomes 0, cppcheck will write:
// ### Error: Invalid number of character { // ### Error: Invalid number of character {
addtoken2(_tokensBack, "}", tok3->linenr(), tok3->fileIndex()); tokenizer.addtoken("}", tok3->linenr(), tok3->fileIndex());
Token::createMutualLinks(braces.top(), *_tokensBack); Token::createMutualLinks(braces.top(), tokenizer._tokensBack);
braces.pop(); braces.pop();
break; break;
} }
@ -651,7 +623,7 @@ void TemplateSimplifier::simplifyTemplatesExpandTemplate(
for (const Token *typetok = typesUsedInTemplateInstantion[itype]; for (const Token *typetok = typesUsedInTemplateInstantion[itype];
typetok && !Token::Match(typetok, "[,>]"); typetok && !Token::Match(typetok, "[,>]");
typetok = typetok->next()) { typetok = typetok->next()) {
addtoken2(_tokensBack, typetok, tok3->linenr(), tok3->fileIndex()); tokenizer.addtoken(typetok, tok3->linenr(), tok3->fileIndex());
} }
continue; continue;
} }
@ -659,36 +631,36 @@ void TemplateSimplifier::simplifyTemplatesExpandTemplate(
// replace name.. // replace name..
if (Token::Match(tok3, (name + " !!<").c_str())) { if (Token::Match(tok3, (name + " !!<").c_str())) {
addtoken2(_tokensBack, newName.c_str(), tok3->linenr(), tok3->fileIndex()); tokenizer.addtoken(newName.c_str(), tok3->linenr(), tok3->fileIndex());
continue; continue;
} }
// copy // copy
addtoken2(_tokensBack, tok3, tok3->linenr(), tok3->fileIndex()); tokenizer.addtoken(tok3, tok3->linenr(), tok3->fileIndex());
if (Token::Match(tok3, "%type% <")) { if (Token::Match(tok3, "%type% <")) {
//if (!Token::simpleMatch(tok3, (name + " <").c_str())) //if (!Token::simpleMatch(tok3, (name + " <").c_str()))
//done = false; //done = false;
templateInstantiations.push_back(*_tokensBack); templateInstantiations.push_back(tokenizer._tokensBack);
} }
// link() newly tokens manually // link() newly tokens manually
if (tok3->str() == "{") { if (tok3->str() == "{") {
braces.push(*_tokensBack); braces.push(tokenizer._tokensBack);
} else if (tok3->str() == "}") { } else if (tok3->str() == "}") {
assert(braces.empty() == false); assert(braces.empty() == false);
Token::createMutualLinks(braces.top(), *_tokensBack); Token::createMutualLinks(braces.top(), tokenizer._tokensBack);
braces.pop(); braces.pop();
} else if (tok3->str() == "(") { } else if (tok3->str() == "(") {
brackets.push(*_tokensBack); brackets.push(tokenizer._tokensBack);
} else if (tok3->str() == "[") { } else if (tok3->str() == "[") {
brackets2.push(*_tokensBack); brackets2.push(tokenizer._tokensBack);
} else if (tok3->str() == ")") { } else if (tok3->str() == ")") {
assert(brackets.empty() == false); assert(brackets.empty() == false);
Token::createMutualLinks(brackets.top(), *_tokensBack); Token::createMutualLinks(brackets.top(), tokenizer._tokensBack);
brackets.pop(); brackets.pop();
} else if (tok3->str() == "]") { } else if (tok3->str() == "]") {
assert(brackets2.empty() == false); assert(brackets2.empty() == false);
Token::createMutualLinks(brackets2.top(), *_tokensBack); Token::createMutualLinks(brackets2.top(), tokenizer._tokensBack);
brackets2.pop(); brackets2.pop();
} }
@ -958,11 +930,8 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
void TemplateSimplifier::simplifyTemplateInstantions( void TemplateSimplifier::simplifyTemplateInstantions(
Token *_tokens, Tokenizer& tokenizer,
Token **_tokensBack,
ErrorLogger *_errorLogger,
const Settings *_settings, const Settings *_settings,
const std::vector<std::string> &files,
const Token *tok, const Token *tok,
std::list<Token *> &templateInstantiations, std::list<Token *> &templateInstantiations,
std::set<std::string> &expandedtemplates) std::set<std::string> &expandedtemplates)
@ -987,22 +956,7 @@ void TemplateSimplifier::simplifyTemplateInstantions(
if (namepos == -1) { if (namepos == -1) {
// debug message that we bail out.. // debug message that we bail out..
if (_settings->debugwarnings) { if (_settings->debugwarnings) {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; tokenizer.reportError(tok, Severity::debug, "debug", "simplifyTemplates: bailing out");
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.setfile(files[tok->fileIndex()]);
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; return;
} }
@ -1019,7 +973,7 @@ void TemplateSimplifier::simplifyTemplateInstantions(
for (std::list<Token *>::const_iterator iter2 = templateInstantiations.begin(); iter2 != templateInstantiations.end(); ++iter2) { for (std::list<Token *>::const_iterator iter2 = templateInstantiations.begin(); iter2 != templateInstantiations.end(); ++iter2) {
if (amountOftemplateInstantiations != templateInstantiations.size()) { if (amountOftemplateInstantiations != templateInstantiations.size()) {
amountOftemplateInstantiations = templateInstantiations.size(); amountOftemplateInstantiations = templateInstantiations.size();
simplifyCalculations(_tokens); simplifyCalculations(tokenizer._tokens);
++recursiveCount; ++recursiveCount;
if (recursiveCount > 100) { if (recursiveCount > 100) {
// bail out.. // bail out..
@ -1068,19 +1022,8 @@ void TemplateSimplifier::simplifyTemplateInstantions(
if (typeForNewName.empty() || typeParametersInDeclaration.size() != typesUsedInTemplateInstantion.size()) { if (typeForNewName.empty() || typeParametersInDeclaration.size() != typesUsedInTemplateInstantion.size()) {
if (_settings->debugwarnings) { if (_settings->debugwarnings) {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; tokenizer.reportError(tok, Severity::debug, "debug",
ErrorLogger::ErrorMessage::FileLocation loc; "Failed to instantiate template. The checking continues anyway.");
loc.line = tok2->linenr();
loc.setfile(files[tok2->fileIndex()]);
locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::debug,
"Failed to instantiate template. The checking continues anyway.",
"debug",
false);
_errorLogger->reportErr(errmsg);
} }
if (typeForNewName.empty()) if (typeForNewName.empty())
continue; continue;
@ -1092,7 +1035,7 @@ void TemplateSimplifier::simplifyTemplateInstantions(
if (expandedtemplates.find(newName) == expandedtemplates.end()) { if (expandedtemplates.find(newName) == expandedtemplates.end()) {
expandedtemplates.insert(newName); expandedtemplates.insert(newName);
TemplateSimplifier::simplifyTemplatesExpandTemplate(_tokens,_tokensBack, tok,name,typeParametersInDeclaration,newName,typesUsedInTemplateInstantion,templateInstantiations); TemplateSimplifier::simplifyTemplatesExpandTemplate(tokenizer, tok,name,typeParametersInDeclaration,newName,typesUsedInTemplateInstantion,templateInstantiations);
} }
// Replace all these template usages.. // Replace all these template usages..
@ -1144,29 +1087,26 @@ void TemplateSimplifier::simplifyTemplateInstantions(
void TemplateSimplifier::simplifyTemplates( void TemplateSimplifier::simplifyTemplates(
Token *_tokens, Tokenizer& tokenizer,
Token **_tokensBack,
ErrorLogger *_errorLogger,
const Settings *_settings, const Settings *_settings,
const std::vector<std::string> &_files,
bool &_codeWithTemplates bool &_codeWithTemplates
) )
{ {
std::set<std::string> expandedtemplates(TemplateSimplifier::simplifyTemplatesExpandSpecialized(_tokens)); std::set<std::string> expandedtemplates(TemplateSimplifier::simplifyTemplatesExpandSpecialized(tokenizer._tokens));
// Locate templates and set member variable _codeWithTemplates if the code has templates. // Locate templates and set member variable _codeWithTemplates if the code has templates.
// this info is used by checks // this info is used by checks
std::list<Token *> templates(TemplateSimplifier::simplifyTemplatesGetTemplateDeclarations(_tokens,_codeWithTemplates)); std::list<Token *> templates(TemplateSimplifier::simplifyTemplatesGetTemplateDeclarations(tokenizer._tokens, _codeWithTemplates));
if (templates.empty()) { if (templates.empty()) {
TemplateSimplifier::removeTemplates(_tokens); TemplateSimplifier::removeTemplates(tokenizer._tokens);
return; return;
} }
// There are templates.. // There are templates..
// Remove "typename" unless used in template arguments.. // Remove "typename" unless used in template arguments..
for (Token *tok = _tokens; tok; tok = tok->next()) { for (Token *tok = tokenizer._tokens; tok; tok = tok->next()) {
if (tok->str() == "typename") if (tok->str() == "typename")
tok->deleteThis(); tok->deleteThis();
@ -1179,11 +1119,11 @@ void TemplateSimplifier::simplifyTemplates(
} }
// Locate possible instantiations of templates.. // Locate possible instantiations of templates..
std::list<Token *> templateInstantiations(TemplateSimplifier::simplifyTemplatesGetTemplateInstantiations(_tokens)); std::list<Token *> templateInstantiations(TemplateSimplifier::simplifyTemplatesGetTemplateInstantiations(tokenizer._tokens));
// No template instantiations? Then remove all templates. // No template instantiations? Then remove all templates.
if (templateInstantiations.empty()) { if (templateInstantiations.empty()) {
TemplateSimplifier::removeTemplates(_tokens); TemplateSimplifier::removeTemplates(tokenizer._tokens);
return; return;
} }
@ -1197,15 +1137,11 @@ void TemplateSimplifier::simplifyTemplates(
//done = true; //done = true;
for (std::list<Token *>::reverse_iterator iter1 = templates.rbegin(); iter1 != templates.rend(); ++iter1) { for (std::list<Token *>::reverse_iterator iter1 = templates.rbegin(); iter1 != templates.rend(); ++iter1) {
TemplateSimplifier::simplifyTemplateInstantions( TemplateSimplifier::simplifyTemplateInstantions(
_tokens, tokenizer,
_tokensBack,
_errorLogger,
_settings, _settings,
_files,
*iter1, templateInstantiations, expandedtemplates); *iter1, templateInstantiations, expandedtemplates);
} }
} }
TemplateSimplifier::removeTemplates(_tokens); TemplateSimplifier::removeTemplates(tokenizer._tokens);
} }

View File

@ -28,7 +28,7 @@
#include <vector> #include <vector>
class Token; class Token;
class ErrorLogger; class Tokenizer;
class Settings; class Settings;
@ -37,9 +37,9 @@ class Settings;
/** @brief Simplify templates from the preprocessed and partially simplified code. */ /** @brief Simplify templates from the preprocessed and partially simplified code. */
class TemplateSimplifier { class TemplateSimplifier {
public:
TemplateSimplifier(); TemplateSimplifier();
~TemplateSimplifier(); ~TemplateSimplifier();
public:
/** /**
* Used after simplifyTemplates to perform a little cleanup. * Used after simplifyTemplates to perform a little cleanup.
@ -111,11 +111,8 @@ public:
*/ */
static int simplifyTemplatesGetTemplateNamePosition(const Token *tok); static int simplifyTemplatesGetTemplateNamePosition(const Token *tok);
static void addtoken2(Token ** token, const char str[], const unsigned int lineno, const unsigned int fileno);
static void addtoken2(Token ** token, const Token * tok, const unsigned int lineno, const unsigned int fileno);
static void simplifyTemplatesExpandTemplate( static void simplifyTemplatesExpandTemplate(
Token *_tokens, Tokenizer& tokenizer,
Token **_tokensBack,
const Token *tok, const Token *tok,
const std::string &name, const std::string &name,
std::vector<const Token *> &typeParametersInDeclaration, std::vector<const Token *> &typeParametersInDeclaration,
@ -131,11 +128,8 @@ public:
* @param expandedtemplates all templates that has been expanded so far. The full names are stored. * @param expandedtemplates all templates that has been expanded so far. The full names are stored.
*/ */
static void simplifyTemplateInstantions( static void simplifyTemplateInstantions(
Token *_tokens, Tokenizer& tokenizer,
Token **_tokensBack,
ErrorLogger *_errorLogger,
const Settings *_settings, const Settings *_settings,
const std::vector<std::string> &files,
const Token *tok, const Token *tok,
std::list<Token *> &templateInstantiations, std::list<Token *> &templateInstantiations,
std::set<std::string> &expandedtemplates); std::set<std::string> &expandedtemplates);
@ -144,11 +138,8 @@ public:
* Simplify templates * Simplify templates
*/ */
static void simplifyTemplates( static void simplifyTemplates(
Token *_tokens, Tokenizer& tokenizer,
Token **_tokensBack,
ErrorLogger *_errorLogger,
const Settings *_settings, const Settings *_settings,
const std::vector<std::string> &_files,
bool &_codeWithTemplates); bool &_codeWithTemplates);
/** /**

View File

@ -2711,11 +2711,8 @@ void Tokenizer::simplifyTemplates()
} }
TemplateSimplifier::simplifyTemplates( TemplateSimplifier::simplifyTemplates(
_tokens, *this,
&_tokensBack,
_errorLogger,
_settings, _settings,
_files,
_codeWithTemplates); _codeWithTemplates);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -39,6 +39,7 @@ class TimerResults;
/** @brief The main purpose is to tokenize the source code. It also has functions that simplify the token list */ /** @brief The main purpose is to tokenize the source code. It also has functions that simplify the token list */
class Tokenizer { class Tokenizer {
friend class TemplateSimplifier; // TODO: Remove this. Cleanup interface between Tokenizer and TemplateSimplifier.
private: private:
/** Deallocate lists */ /** Deallocate lists */
void deallocateTokens(); void deallocateTokens();