Cppcheck
Static Public Member Functions | Private Member Functions | Static Private Member Functions
TemplateSimplifier Class Reference

Simplify templates from the preprocessed and partially simplified code. More...

#include <templatesimplifier.h>

List of all members.

Static Public Member Functions

static void cleanupAfterSimplify (Token *tokens)
 Used after simplifyTemplates to perform a little cleanup.
static const TokenhasComplicatedSyntaxErrorsInTemplates (Token *tokens)
static unsigned int templateParameters (const Token *tok)
 is the token pointing at a template parameters block < int , 3 > => yes
static std::set< std::string > expandSpecialized (Token *tokens)
 Expand specialized templates : "template<>..".
static std::list< Token * > getTemplateDeclarations (Token *tokens, bool &codeWithTemplates)
 Get template declarations.
static std::list< Token * > getTemplateInstantiations (Token *tokens)
 Get template instantiations.
static void useDefaultArgumentValues (const std::list< Token * > &templates, const std::list< Token * > &templateInstantiations)
 simplify template instantiations (use default argument values)
static bool instantiateMatch (const Token *instance, const std::string &name, std::size_t numberOfArguments, const char patternAfter[])
 Match template declaration/instantiation.
static int getTemplateNamePosition (const Token *tok)
 Match template declaration/instantiation.
static void expandTemplate (TokenList &tokenlist, const Token *tok, const std::string &name, std::vector< const Token * > &typeParametersInDeclaration, const std::string &newName, std::vector< const Token * > &typesUsedInTemplateInstantiation, std::list< Token * > &templateInstantiations)
static bool simplifyTemplateInstantiations (TokenList &tokenlist, ErrorLogger &errorlogger, const Settings *_settings, const Token *tok, std::list< Token * > &templateInstantiations, std::set< std::string > &expandedtemplates)
 Simplify templates : expand all instantiations for a template.
static void simplifyTemplates (TokenList &tokenlist, ErrorLogger &errorlogger, const Settings *_settings, bool &_codeWithTemplates)
 Simplify templates.
static bool simplifyNumericCalculations (Token *tok)
 Simplify constant calculations such as "1+2" => "3".
static bool simplifyCalculations (Token *_tokens)
 Simplify constant calculations such as "1+2" => "3".

Private Member Functions

 TemplateSimplifier ()
 ~TemplateSimplifier ()

Static Private Member Functions

static bool removeTemplate (Token *tok)
 Remove a specific "template < ..." template class/function.

Detailed Description

Simplify templates from the preprocessed and partially simplified code.

Definition at line 41 of file templatesimplifier.h.


Constructor & Destructor Documentation


Member Function Documentation

void TemplateSimplifier::cleanupAfterSimplify ( Token tokens) [static]

Used after simplifyTemplates to perform a little cleanup.

Sometimes the simplifyTemplates isn't fully successful and then there are function calls etc with "wrong" syntax.

Definition at line 36 of file templatesimplifier.cpp.

References Token::eraseTokens(), Token::Match(), Token::next(), Token::str(), Token::tokAt(), and Scope::type.

Referenced by Tokenizer::tokenize().

std::set< std::string > TemplateSimplifier::expandSpecialized ( Token tokens) [static]

Expand specialized templates : "template<>..".

Returns:
names of expanded templates

Definition at line 322 of file templatesimplifier.cpp.

References Token::eraseTokens(), Token::findmatch(), Token::findsimplematch(), Token::isName(), Token::next(), Token::previous(), Token::simpleMatch(), Token::str(), templateParameters(), and Token::tokAt().

Referenced by simplifyTemplates().

void TemplateSimplifier::expandTemplate ( TokenList tokenlist,
const Token tok,
const std::string &  name,
std::vector< const Token * > &  typeParametersInDeclaration,
const std::string &  newName,
std::vector< const Token * > &  typesUsedInTemplateInstantiation,
std::list< Token * > &  templateInstantiations 
) [static]
std::list< Token * > TemplateSimplifier::getTemplateDeclarations ( Token tokens,
bool &  codeWithTemplates 
) [static]

Get template declarations.

Returns:
list of template declarations

Definition at line 384 of file templatesimplifier.cpp.

References Token::Match(), Token::next(), and Token::simpleMatch().

Referenced by simplifyTemplates().

std::list< Token * > TemplateSimplifier::getTemplateInstantiations ( Token tokens) [static]

Get template instantiations.

Returns:
list of template instantiations

Definition at line 412 of file templatesimplifier.cpp.

References Token::findClosingBracket(), Token::Match(), Token::next(), Token::previous(), Token::simpleMatch(), templateParameters(), and Token::tokAt().

Referenced by simplifyTemplates().

int TemplateSimplifier::getTemplateNamePosition ( const Token tok) [static]

Match template declaration/instantiation.

Parameters:
tokThe ">" token e.g. before "class"
Returns:
-1 to bail out or positive integer to identity the position of the template name.

Definition at line 561 of file templatesimplifier.cpp.

References Token::Match(), and Token::strAt().

Referenced by simplifyTemplateInstantiations().

Returns:
0 if there are no syntax errors or return token which identifies the location of syntax error.

Definition at line 67 of file templatesimplifier.cpp.

References Token::link(), Token::Match(), Token::next(), Token::simpleMatch(), and Token::tokAt().

bool TemplateSimplifier::instantiateMatch ( const Token instance,
const std::string &  name,
std::size_t  numberOfArguments,
const char  patternAfter[] 
) [static]

Match template declaration/instantiation.

Parameters:
instancetemplate instantiation
namename of template
numberOfArgumentsnumber of template arguments
patternAfterpattern that must match the tokens after the ">"
Returns:
match => true

Definition at line 543 of file templatesimplifier.cpp.

References Token::findsimplematch(), Token::Match(), Token::next(), Token::simpleMatch(), and templateParameters().

Referenced by expandTemplate(), and simplifyTemplateInstantiations().

bool TemplateSimplifier::removeTemplate ( Token tok) [static, private]

Remove a specific "template < ..." template class/function.

Definition at line 256 of file templatesimplifier.cpp.

References Token::deleteNext(), Token::deleteThis(), Token::eraseTokens(), Token::Match(), Token::next(), and Token::simpleMatch().

Referenced by simplifyTemplates().

bool TemplateSimplifier::simplifyCalculations ( Token _tokens) [static]

Simplify constant calculations such as "1+2" => "3".

This also performs simple cleanup of parentheses etc.

Parameters:
_tokensstart token
Returns:
true if modifications to token-list are done. false if no modifications are done.

Definition at line 780 of file templatesimplifier.cpp.

References MathLib::divide(), Token::eraseTokens(), MathLib::isInt(), MathLib::longToString(), Token::Match(), MathLib::multiply(), Token::next(), Token::simpleMatch(), simplifyNumericCalculations(), Token::str(), and MathLib::toLongNumber().

Referenced by simplifyTemplateInstantiations().

bool TemplateSimplifier::simplifyTemplateInstantiations ( TokenList tokenlist,
ErrorLogger errorlogger,
const Settings _settings,
const Token tok,
std::list< Token * > &  templateInstantiations,
std::set< std::string > &  expandedtemplates 
) [static]

Simplify templates : expand all instantiations for a template.

Todo:
It seems that inner templates should be instantiated recursively
Parameters:
tokenlisttoken list
errorloggererror logger
_settingssettings
toktoken where the template declaration begins
templateInstantiationsa list of template usages (not necessarily just for this template)
expandedtemplatesall templates that has been expanded so far. The full names are stored.
Returns:
true if the template was instantiated

Definition at line 965 of file templatesimplifier.cpp.

References Severity::debug, Settings::debugwarnings, Token::eraseTokens(), expandTemplate(), TokenList::front(), getTemplateNamePosition(), instantiateMatch(), Token::isLong(), Token::isSigned(), Token::isUnsigned(), Token::Match(), Token::next(), Token::previous(), ErrorLogger::reportErr(), Token::simpleMatch(), simplifyCalculations(), Token::str(), Token::strAt(), templateParameters(), and Token::tokAt().

Referenced by simplifyTemplates().

void TemplateSimplifier::simplifyTemplates ( TokenList tokenlist,
ErrorLogger errorlogger,
const Settings _settings,
bool &  _codeWithTemplates 
) [static]

Simplify templates.

Parameters:
tokenlisttoken list
errorloggererror logger
_settingssettings
_codeWithTemplatesoutput parameter that is set if code contains templates

Definition at line 1143 of file templatesimplifier.cpp.

References expandSpecialized(), TokenList::front(), getTemplateDeclarations(), getTemplateInstantiations(), Token::next(), removeTemplate(), Token::simpleMatch(), simplifyTemplateInstantiations(), and useDefaultArgumentValues().

unsigned int TemplateSimplifier::templateParameters ( const Token tok) [static]

is the token pointing at a template parameters block < int , 3 > => yes

Parameters:
tokstart token that must point at "<"
Returns:
number of parameters (invalid parameters => 0)

Definition at line 165 of file templatesimplifier.cpp.

References Token::eChar, Token::isName(), Token::isNumber(), Token::link(), Token::Match(), Token::next(), Token::str(), Token::tokAt(), and Token::type().

Referenced by expandSpecialized(), getTemplateInstantiations(), instantiateMatch(), setVarIdParseDeclaration(), setVarIdStructMembers(), Tokenizer::simplifyConstTernaryOp(), simplifyTemplateInstantiations(), and Tokenizer::simplifyVarDecl().

void TemplateSimplifier::useDefaultArgumentValues ( const std::list< Token * > &  templates,
const std::list< Token * > &  templateInstantiations 
) [static]

simplify template instantiations (use default argument values)

Parameters:
templateslist of template declarations
templateInstantiationslist of template instantiations

Definition at line 448 of file templatesimplifier.cpp.

References Token::createMutualLinks(), Token::deleteThis(), Token::eraseTokens(), Token::insertToken(), Token::link(), Token::Match(), Token::next(), Token::previous(), Token::str(), and Token::tokAt().

Referenced by simplifyTemplates().


The documentation for this class was generated from the following files: