2012-01-01 21:55:05 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
|
2012-01-01 21:55:05 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef templatesimplifierH
|
|
|
|
#define templatesimplifierH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2012-01-02 21:53:13 +01:00
|
|
|
#include <set>
|
2012-01-02 22:05:27 +01:00
|
|
|
#include <list>
|
2012-01-02 21:53:13 +01:00
|
|
|
#include <string>
|
2012-01-03 22:35:06 +01:00
|
|
|
#include <vector>
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2012-01-01 21:55:05 +01:00
|
|
|
|
|
|
|
class Token;
|
2012-05-06 10:38:55 +02:00
|
|
|
class TokenList;
|
|
|
|
class ErrorLogger;
|
2012-01-09 20:33:11 +01:00
|
|
|
class Settings;
|
2012-01-01 21:55:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/** @brief Simplify templates from the preprocessed and partially simplified code. */
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB TemplateSimplifier {
|
2012-01-01 21:55:05 +01:00
|
|
|
TemplateSimplifier();
|
2012-02-24 20:45:56 +01:00
|
|
|
~TemplateSimplifier();
|
2012-04-23 20:45:36 +02:00
|
|
|
public:
|
2012-01-01 21:55:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static void cleanupAfterSimplify(Token *tokens);
|
2012-01-02 21:37:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return 0 if there are no syntax errors or return token which identifies
|
|
|
|
* the location of syntax error.
|
|
|
|
*/
|
|
|
|
static const Token* hasComplicatedSyntaxErrorsInTemplates(Token *tokens);
|
2012-01-02 21:43:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* is the token pointing at a template parameters block
|
|
|
|
* < int , 3 > => yes
|
|
|
|
* \param tok start token that must point at "<"
|
|
|
|
* \return number of parameters (invalid parameters => 0)
|
|
|
|
*/
|
|
|
|
static unsigned int templateParameters(const Token *tok);
|
2012-01-02 21:46:43 +01:00
|
|
|
|
2012-01-02 21:53:13 +01:00
|
|
|
/**
|
|
|
|
* Expand specialized templates : "template<>.."
|
|
|
|
* @return names of expanded templates
|
|
|
|
*/
|
2012-09-12 20:46:56 +02:00
|
|
|
static std::set<std::string> expandSpecialized(Token *tokens);
|
2012-01-02 22:05:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template declarations
|
|
|
|
* @return list of template declarations
|
|
|
|
*/
|
2012-09-12 20:46:56 +02:00
|
|
|
static std::list<Token *> getTemplateDeclarations(Token *tokens, bool &codeWithTemplates);
|
2012-01-02 22:05:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template instantiations
|
|
|
|
* @return list of template instantiations
|
|
|
|
*/
|
2012-09-12 20:46:56 +02:00
|
|
|
static std::list<Token *> getTemplateInstantiations(Token *tokens);
|
2012-01-03 22:35:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* simplify template instantiations (use default argument values)
|
|
|
|
* @param templates list of template declarations
|
|
|
|
* @param templateInstantiations list of template instantiations
|
|
|
|
*/
|
2012-09-12 20:46:56 +02:00
|
|
|
static void useDefaultArgumentValues(const std::list<Token *> &templates,
|
2012-09-13 16:43:07 +02:00
|
|
|
const std::list<Token *> &templateInstantiations);
|
2012-01-03 22:35:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Match template declaration/instantiation
|
|
|
|
* @param instance template instantiation
|
|
|
|
* @param name name of template
|
|
|
|
* @param numberOfArguments number of template arguments
|
|
|
|
* @param patternAfter pattern that must match the tokens after the ">"
|
|
|
|
* @return match => true
|
|
|
|
*/
|
2012-09-12 20:46:56 +02:00
|
|
|
static bool instantiateMatch(const Token *instance, const std::string &name, std::size_t numberOfArguments, const char patternAfter[]);
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2012-01-03 22:49:50 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-09-12 20:46:56 +02:00
|
|
|
static int getTemplateNamePosition(const Token *tok);
|
2012-01-05 21:45:19 +01:00
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
static void expandTemplate(
|
2012-05-06 10:38:55 +02:00
|
|
|
TokenList& tokenlist,
|
2012-01-05 21:45:19 +01:00
|
|
|
const Token *tok,
|
|
|
|
const std::string &name,
|
|
|
|
std::vector<const Token *> &typeParametersInDeclaration,
|
|
|
|
const std::string &newName,
|
|
|
|
std::vector<const Token *> &typesUsedInTemplateInstantion,
|
|
|
|
std::list<Token *> &templateInstantiations);
|
2012-01-09 20:33:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify templates : expand all instantiatiations for a template
|
|
|
|
* @todo It seems that inner templates should be instantiated recursively
|
2012-07-08 19:32:33 +02:00
|
|
|
* @param tokenlist token list
|
|
|
|
* @param errorlogger error logger
|
|
|
|
* @param _settings settings
|
2012-01-09 20:33:11 +01:00
|
|
|
* @param tok token where the template declaration begins
|
|
|
|
* @param templateInstantiations a list of template usages (not necessarily just for this template)
|
|
|
|
* @param expandedtemplates all templates that has been expanded so far. The full names are stored.
|
2012-07-29 16:01:05 +02:00
|
|
|
* @return true if the template was instantiated
|
2012-01-09 20:33:11 +01:00
|
|
|
*/
|
2012-07-29 16:01:05 +02:00
|
|
|
static bool simplifyTemplateInstantions(
|
2012-05-06 10:38:55 +02:00
|
|
|
TokenList& tokenlist,
|
|
|
|
ErrorLogger& errorlogger,
|
2012-01-09 20:33:11 +01:00
|
|
|
const Settings *_settings,
|
|
|
|
const Token *tok,
|
|
|
|
std::list<Token *> &templateInstantiations,
|
|
|
|
std::set<std::string> &expandedtemplates);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify templates
|
2012-07-08 19:32:33 +02:00
|
|
|
* @param tokenlist token list
|
|
|
|
* @param errorlogger error logger
|
|
|
|
* @param _settings settings
|
|
|
|
* @param _codeWithTemplates output parameter that is set if code contains templates
|
2012-01-09 20:33:11 +01:00
|
|
|
*/
|
|
|
|
static void simplifyTemplates(
|
2012-05-06 10:38:55 +02:00
|
|
|
TokenList& tokenlist,
|
|
|
|
ErrorLogger& errorlogger,
|
2012-01-09 20:33:11 +01:00
|
|
|
const Settings *_settings,
|
|
|
|
bool &_codeWithTemplates);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify constant calculations such as "1+2" => "3"
|
2012-08-20 18:10:32 +02:00
|
|
|
* @param tok start token
|
|
|
|
* @return true if modifications to token-list are done.
|
|
|
|
* false if no modifications are done.
|
|
|
|
*/
|
|
|
|
static bool simplifyNumericCalculations(Token *tok);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify constant calculations such as "1+2" => "3".
|
|
|
|
* This also perform simple cleanup of parantheses etc.
|
2012-07-08 19:32:33 +02:00
|
|
|
* @param _tokens start token
|
2012-01-09 20:33:11 +01:00
|
|
|
* @return true if modifications to token-list are done.
|
|
|
|
* false if no modifications are done.
|
|
|
|
*/
|
|
|
|
static bool simplifyCalculations(Token *_tokens);
|
2012-07-29 16:01:05 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a specific "template < ..." template class/function
|
|
|
|
*/
|
|
|
|
static bool removeTemplate(Token *tok);
|
|
|
|
|
2012-01-01 21:55:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|