"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/templatesimplifier.h" between
cppcheck-1.86.tar.gz and cppcheck-1.87.tar.gz

About: Cppcheck is a static analysis tool for C/C++ code. It checks for memory leaks, mismatching allocation-deallocation, buffer overrun, and many more.

templatesimplifier.h  (cppcheck-1.86):templatesimplifier.h  (cppcheck-1.87)
/* /*
* Cppcheck - A tool for static C/C++ code analysis * Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2018 Cppcheck team. * Copyright (C) 2007-2019 Cppcheck team.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
skipping to change at line 45 skipping to change at line 45
class Token; class Token;
class Tokenizer; class Tokenizer;
class TokenList; class TokenList;
/// @addtogroup Core /// @addtogroup Core
/// @{ /// @{
/** @brief Simplify templates from the preprocessed and partially simplified cod e. */ /** @brief Simplify templates from the preprocessed and partially simplified cod e. */
class CPPCHECKLIB TemplateSimplifier { class CPPCHECKLIB TemplateSimplifier {
public: public:
TemplateSimplifier(TokenList &tokenlist, const Settings *settings, ErrorLogg er *errorLogger); explicit TemplateSimplifier(Tokenizer *tokenizer);
~TemplateSimplifier(); ~TemplateSimplifier();
/** /**
* 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
* there are function calls etc with "wrong" syntax. * there are function calls etc with "wrong" syntax.
*/ */
void cleanupAfterSimplify(); void cleanupAfterSimplify();
/** /**
skipping to change at line 71 skipping to change at line 71
* < int , 3 > => yes * < int , 3 > => yes
* \param tok start token that must point at "<" * \param tok start token that must point at "<"
* \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);
/** /**
* Token and its full scopename * Token and its full scopename
*/ */
struct TokenAndName { struct TokenAndName {
TokenAndName(Token *tok, const std::string &s, const std::string &n, con /**
st Token *nt); * Constructor used for instantiations.
* \param tok template instantiation name token "name<...>"
* \param scope full qualification of template
*/
TokenAndName(Token *tok, const std::string &s);
/**
* Constructor used for declarations.
* \param tok template declaration token "template < ... >"
* \param scope full qualification of template
* \param nt template name token "template < ... > class name"
* \param pe template parameter end token ">"
*/
TokenAndName(Token *tok, const std::string &s, const Token *nt, const To
ken *pe);
TokenAndName(const TokenAndName& otherTok);
~TokenAndName();
bool operator == (const TokenAndName & rhs) const { bool operator == (const TokenAndName & rhs) const {
return token == rhs.token && scope == rhs.scope && name == rhs.name return token == rhs.token && scope == rhs.scope && name == rhs.name
&& nameToken == rhs.nameToken; && fullName == rhs.fullName &&
nameToken == rhs.nameToken && paramEnd == rhs.paramEnd && fla
gs == rhs.flags;
} }
Token *token; Token *token;
std::string scope; std::string scope;
std::string name; std::string name;
std::string fullName;
const Token *nameToken; const Token *nameToken;
const Token *paramEnd;
unsigned int flags;
enum {
fIsClass = (1 << 0), // class template
fIsFunction = (1 << 1), // function template
fIsVariable = (1 << 2), // variable template
fIsAlias = (1 << 3), // alias template
fIsSpecialization = (1 << 4), // user specialized template
fIsPartialSpecialization = (1 << 5), // user partial specialized tem
plate
fIsForwardDeclaration = (1 << 6), // forward declaration
};
bool isClass() const {
return getFlag(fIsClass);
}
void isClass(bool state) {
setFlag(fIsClass, state);
}
bool isFunction() const {
return getFlag(fIsFunction);
}
void isFunction(bool state) {
setFlag(fIsFunction, state);
}
bool isVariable() const {
return getFlag(fIsVariable);
}
void isVariable(bool state) {
setFlag(fIsVariable, state);
}
bool isAlias() const {
return getFlag(fIsAlias);
}
void isAlias(bool state) {
setFlag(fIsAlias, state);
}
bool isSpecialization() const {
return getFlag(fIsSpecialization);
}
void isSpecialization(bool state) {
setFlag(fIsSpecialization, state);
}
bool isPartialSpecialization() const {
return getFlag(fIsPartialSpecialization);
}
void isPartialSpecialization(bool state) {
setFlag(fIsPartialSpecialization, state);
}
bool isForwardDeclaration() const {
return getFlag(fIsForwardDeclaration);
}
void isForwardDeclaration(bool state) {
setFlag(fIsForwardDeclaration, state);
}
/**
* Get specified flag state.
* @param flag flag to get state of
* @return true if flag set or false in flag not set
*/
bool getFlag(unsigned int flag) const {
return ((flags & flag) != 0);
}
/**
* Set specified flag state.
* @param flag flag to set state
* @param state new state of flag
*/
void setFlag(unsigned int flag, bool state) {
flags = state ? flags | flag : flags & ~flag;
}
}; };
/** /**
* Match template declaration/instantiation * Match template declaration/instantiation
* @param instance template instantiation * @param instance template instantiation
* @param numberOfArguments number of template arguments * @param numberOfArguments number of template arguments
* @param patternAfter pattern that must match the tokens after the ">" * @param patternAfter pattern that must match the tokens after the ">"
* @return match => true * @return match => true
*/ */
static bool instantiateMatch(const Token *instance, const std::size_t number OfArguments, const char patternAfter[]); static bool instantiateMatch(const Token *instance, const std::size_t number OfArguments, const char patternAfter[]);
/** /**
* Match template declaration/instantiation * Match template declaration/instantiation
* @param tok The ">" token e.g. before "class" * @param tok The ">" token e.g. before "class"
* @param forward declaration or forward declaration
* @return -1 to bail out or positive integer to identity the position * @return -1 to bail out or positive integer to identity the position
* of the template name. * of the template name.
*/ */
static int getTemplateNamePosition(const Token *tok, bool forward = false); static int getTemplateNamePosition(const Token *tok);
/**
* Get function template name position
* @param tok The ">" token e.g. before "class"
* @param namepos return offset to name
* @return true if name found, false if not
* */
static bool getTemplateNamePositionTemplateFunction(const Token *tok, int &n
amepos);
/**
* Get variable template name position
* @param tok The ">" token
* @param namepos return offset to name
* @return true if name found, false if not
* */
static bool getTemplateNamePositionTemplateVariable(const Token *tok, int &n
amepos);
/** /**
* Simplify templates * Simplify templates
* @param maxtime time when the simplification should be stopped * @param maxtime time when the simplification should be stopped
* @param codeWithTemplates output parameter that is set if code contains te mplates * @param codeWithTemplates output parameter that is set if code contains te mplates
*/ */
void simplifyTemplates( void simplifyTemplates(
const std::time_t maxtime, const std::time_t maxtime,
bool &codeWithTemplates); bool &codeWithTemplates);
skipping to change at line 122 skipping to change at line 234
* false if no modifications are done. * false if no modifications are done.
*/ */
static bool simplifyNumericCalculations(Token *tok); static bool simplifyNumericCalculations(Token *tok);
/** /**
* Simplify constant calculations such as "1+2" => "3". * Simplify constant calculations such as "1+2" => "3".
* This also performs simple cleanup of parentheses etc. * This also performs simple cleanup of parentheses etc.
* @return true if modifications to token-list are done. * @return true if modifications to token-list are done.
* false if no modifications are done. * false if no modifications are done.
*/ */
bool simplifyCalculations(); bool simplifyCalculations(Token* frontToken = nullptr, Token *backToken = nu llptr);
private: private:
/** /**
* Get template declarations * Get template declarations
* @return true if code has templates. * @return true if code has templates.
*/ */
bool getTemplateDeclarations(); bool getTemplateDeclarations();
/** /**
* Get template instantiations * Get template instantiations
skipping to change at line 148 skipping to change at line 260
* when they are not present in the declaration. * when they are not present in the declaration.
*/ */
void fixForwardDeclaredDefaultArgumentValues(); void fixForwardDeclaredDefaultArgumentValues();
/** /**
* simplify template instantiations (use default argument values) * simplify template instantiations (use default argument values)
*/ */
void useDefaultArgumentValues(); void useDefaultArgumentValues();
/** /**
* Try to locate a matching declaration for each user defined
* specialization.
*/
void getSpecializations();
/**
* Try to locate a matching declaration for each user defined
* partial specialization.
*/
void getPartialSpecializations();
/**
* simplify template aliases * simplify template aliases
*/ */
void simplifyTemplateAliases(); void simplifyTemplateAliases();
/** /**
* Simplify templates : expand all instantiations for a template * Simplify templates : expand all instantiations for a template
* @todo It seems that inner templates should be instantiated recursively * @todo It seems that inner templates should be instantiated recursively
* @param templateDeclaration template declaration * @param templateDeclaration template declaration
* @param specializations template specializations (list each template name token) * @param specializations template specializations (list each template name token)
* @param maxtime time when the simplification will stop * @param maxtime time when the simplification will stop
skipping to change at line 180 skipping to change at line 304
* @param tok place to insert namespace * @param tok place to insert namespace
*/ */
void addNamespace(const TokenAndName &templateDeclaration, const Token *tok) ; void addNamespace(const TokenAndName &templateDeclaration, const Token *tok) ;
/** /**
* Simplify templates : check if namespace already present * Simplify templates : check if namespace already present
* @param templateDeclaration template declaration * @param templateDeclaration template declaration
* @param tok place to start looking for namespace * @param tok place to start looking for namespace
* @return true if namespace already present * @return true if namespace already present
*/ */
bool alreadyHasNamespace(const TokenAndName &templateDeclaration, const Toke n *tok) const; static bool alreadyHasNamespace(const TokenAndName &templateDeclaration, con st Token *tok);
/** /**
* Expand a template. Create "expanded" class/function at end of tokenlist. * Expand a template. Create "expanded" class/function at end of tokenlist.
* @param templateDeclaration Template declaration information * @param templateDeclaration Template declaration information
* @param templateDeclarationToken Template declaration token
* @param templateInstantiation Full name of template * @param templateInstantiation Full name of template
* @param typeParametersInDeclaration The type parameters of the templ ate * @param typeParametersInDeclaration The type parameters of the templ ate
* @param newName New name of class/function. * @param newName New name of class/function.
* @param copy copy or expand in place * @param copy copy or expand in place
*/ */
void expandTemplate( void expandTemplate(
const TokenAndName &templateDeclaration, const TokenAndName &templateDeclaration,
const Token *templateDeclarationToken,
const TokenAndName &templateInstantiation, const TokenAndName &templateInstantiation,
const std::vector<const Token *> &typeParametersInDeclaration, const std::vector<const Token *> &typeParametersInDeclaration,
const std::string &newName, const std::string &newName,
bool copy); bool copy);
/** /**
* Replace all matching template usages 'Foo < int >' => 'Foo<int>' * Replace all matching template usages 'Foo < int >' => 'Foo<int>'
* @param instantiationToken Template instantiation token * @param instantiation Template instantiation information.
* @param templateName full template name with scope info
* @param typeStringsUsedInTemplateInstantiation template parameters. list o f token strings. * @param typeStringsUsedInTemplateInstantiation template parameters. list o f token strings.
* @param newName The new type name * @param newName The new type name
*/ */
void replaceTemplateUsage(Token *const instantiationToken, void replaceTemplateUsage(const TokenAndName &instantiation,
const std::string &templateName,
const std::list<std::string> &typeStringsUsedInTem plateInstantiation, const std::list<std::string> &typeStringsUsedInTem plateInstantiation,
const std::string &newName); const std::string &newName);
/** /**
* @brief TemplateParametersInDeclaration * @brief TemplateParametersInDeclaration
* @param tok template < typename T, typename S > * @param tok template < typename T, typename S >
* ^ tok * ^ tok
* @param typeParametersInDeclaration template < typename T, typename S > * @param typeParametersInDeclaration template < typename T, typename S >
* ^ [0] ^ [1] * ^ [0] ^ [1]
* @return template < typename T, typename S > * @return template < typename T, typename S >
* ^ return * ^ return
*/ */
static const Token * getTemplateParametersInDeclaration( static const Token * getTemplateParametersInDeclaration(
const Token * tok, const Token * tok,
std::vector<const Token *> & typeParametersInDeclaration); std::vector<const Token *> & typeParametersInDeclaration);
/** /**
* Remove a specific "template < ..." template class/function * Remove a specific "template < ..." template class/function
*/ */
bool removeTemplate(Token *tok); static bool removeTemplate(Token *tok);
/** Syntax error */ /** Syntax error */
static void syntaxError(const Token *tok); static void syntaxError(const Token *tok);
static bool matchSpecialization( static bool matchSpecialization(
const Token *templateDeclarationNameToken, const Token *templateDeclarationNameToken,
const Token *templateInstantiationNameToken, const Token *templateInstantiationNameToken,
const std::list<const Token *> & specializations); const std::list<const Token *> & specializations);
/* /*
* Same as Token::eraseTokens() but tries to fix up lists with pointers to t he deleted tokens. * Same as Token::eraseTokens() but tries to fix up lists with pointers to t he deleted tokens.
* @param begin Tokens after this will be erased. * @param begin Tokens after this will be erased.
* @param end Tokens before this will be erased. * @param end Tokens before this will be erased.
*/ */
void eraseTokens(Token *begin, const Token *end); static void eraseTokens(Token *begin, const Token *end);
/** /**
* Delete specified token without invalidating pointer to following token. * Delete specified token without invalidating pointer to following token.
* tok will be invalidated. * tok will be invalidated.
* @param tok token to delete * @param tok token to delete
*/ */
static void deleteToken(Token *tok); static void deleteToken(Token *tok);
/** /**
* Get the new token name. * Get the new token name.
* @param tok name token * @param tok2 name token
* @param &typeStringsUsedInTemplateInstantiation type strings use in templa te instantiation * @param &typeStringsUsedInTemplateInstantiation type strings use in templa te instantiation
* @return new token name * @return new token name
*/ */
std::string getNewName( std::string getNewName(
Token *tok2, Token *tok2,
std::list<std::string> &typeStringsUsedInTemplateInstantiation); std::list<std::string> &typeStringsUsedInTemplateInstantiation);
void printOut(
const TokenAndName &tokenAndName,
const std::string &indent = " ") const;
void printOut(const std::string &text = "") const;
bool simplifyUsing();
Tokenizer *mTokenizer;
TokenList &mTokenList; TokenList &mTokenList;
const Settings *mSettings; const Settings *mSettings;
ErrorLogger *mErrorLogger; ErrorLogger *mErrorLogger;
std::list<TokenAndName> mTemplateDeclarations; std::list<TokenAndName> mTemplateDeclarations;
std::list<TokenAndName> mTemplateForwardDeclarations; std::list<TokenAndName> mTemplateForwardDeclarations;
std::map<Token *, Token *> mTemplateForwardDeclarationsMap; std::map<Token *, Token *> mTemplateForwardDeclarationsMap;
std::map<Token *, Token *> mTemplateSpecializationMap;
std::map<Token *, Token *> mTemplatePartialSpecializationMap;
std::list<TokenAndName> mTemplateInstantiations; std::list<TokenAndName> mTemplateInstantiations;
std::list<TokenAndName> mInstantiatedTemplates; std::list<TokenAndName> mInstantiatedTemplates;
std::list<TokenAndName> mMemberFunctionsToDelete; std::list<TokenAndName> mMemberFunctionsToDelete;
std::vector<Token *> mTypesUsedInTemplateInstantiation; std::vector<TokenAndName> mExplicitInstantiationsToDelete;
std::vector<TokenAndName> mTypesUsedInTemplateInstantiation;
}; };
/// @} /// @}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif // templatesimplifierH #endif // templatesimplifierH
 End of changes. 21 change blocks. 
20 lines changed or deleted 155 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)