2009-01-26 17:38:08 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
|
2009-01-26 17:38:08 +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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-26 17:38:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef tokenizeH
|
|
|
|
#define tokenizeH
|
|
|
|
//---------------------------------------------------------------------------
|
2010-08-06 17:44:26 +02:00
|
|
|
|
2012-01-06 17:31:10 +01:00
|
|
|
#include "path.h"
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2011-02-12 20:12:07 +01:00
|
|
|
#include <list>
|
2009-01-26 17:38:08 +01:00
|
|
|
#include <vector>
|
2011-02-12 19:43:33 +01:00
|
|
|
#include <set>
|
2009-07-13 19:11:31 +02:00
|
|
|
|
|
|
|
class Token;
|
|
|
|
class ErrorLogger;
|
|
|
|
class Settings;
|
2010-11-23 18:41:07 +01:00
|
|
|
class SymbolDatabase;
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/** @brief The main purpose is to tokenize the source code. It also has functions that simplify the token list */
|
2011-10-13 20:53:06 +02:00
|
|
|
class Tokenizer {
|
2009-01-26 17:38:08 +01:00
|
|
|
private:
|
2010-03-17 22:16:18 +01:00
|
|
|
/** Deallocate lists */
|
2009-07-05 22:16:43 +02:00
|
|
|
void deallocateTokens();
|
2009-01-26 17:38:08 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
Tokenizer();
|
2009-07-13 19:11:31 +02:00
|
|
|
Tokenizer(const Settings * settings, ErrorLogger *errorLogger);
|
2009-11-22 15:06:33 +01:00
|
|
|
virtual ~Tokenizer();
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2012-01-06 20:56:28 +01:00
|
|
|
/** Returns the source file path. e.g. "file.cpp" */
|
|
|
|
std::string getSourceFilePath() const;
|
2011-12-27 18:01:39 +01:00
|
|
|
|
2011-12-27 18:01:39 +01:00
|
|
|
/** Is the code JAVA. Used for bailouts */
|
2012-01-06 20:56:28 +01:00
|
|
|
bool isJava() const;
|
2011-12-27 18:01:39 +01:00
|
|
|
|
|
|
|
/** Is the code C#. Used for bailouts */
|
2012-01-06 20:56:28 +01:00
|
|
|
bool isCSharp() const;
|
2011-12-27 18:01:39 +01:00
|
|
|
|
2011-12-27 18:01:39 +01:00
|
|
|
/** Is the code JAVA/C#. Used for bailouts */
|
2012-01-06 20:56:28 +01:00
|
|
|
bool isJavaOrCSharp() const;
|
2010-10-28 18:51:55 +02:00
|
|
|
|
2011-10-26 21:17:27 +02:00
|
|
|
/** Is the code C. Used for bailouts */
|
2012-01-06 20:56:28 +01:00
|
|
|
bool isC() const;
|
2011-10-26 21:17:27 +02:00
|
|
|
|
|
|
|
/** Is the code CPP. Used for bailouts */
|
2012-01-06 20:56:28 +01:00
|
|
|
bool isCPP() const;
|
2011-10-26 21:17:27 +02:00
|
|
|
|
2011-12-27 18:00:12 +01:00
|
|
|
/**
|
|
|
|
* Check if inner scope ends with a call to a noreturn function
|
|
|
|
* \param endScopeToken The '}' token
|
|
|
|
* \param unknown set to true if it's unknown if the scope is noreturn
|
|
|
|
* \return true if scope ends with a function call that might be 'noreturn'
|
|
|
|
*/
|
|
|
|
bool IsScopeNoReturn(const Token *endScopeToken, bool *unknown = 0) const;
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
/**
|
|
|
|
* Tokenize code
|
2009-06-20 22:13:19 +02:00
|
|
|
* @param code input stream for code, e.g.
|
2009-07-18 10:25:22 +02:00
|
|
|
* \code
|
2009-06-20 22:13:19 +02:00
|
|
|
* #file "p.h"
|
|
|
|
* class Foo
|
|
|
|
* {
|
|
|
|
* private:
|
|
|
|
* void Bar();
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* #endfile
|
|
|
|
* void Foo::Bar()
|
|
|
|
* {
|
|
|
|
* }
|
2009-07-18 10:25:22 +02:00
|
|
|
* \endcode
|
2009-06-20 22:13:19 +02:00
|
|
|
*
|
2009-01-26 17:38:08 +01:00
|
|
|
* @param FileName The filename
|
2010-01-23 22:18:11 +01:00
|
|
|
* @param configuration E.g. "A" for code where "#ifdef A" is true
|
2010-12-30 22:13:31 +01:00
|
|
|
* @param preprocessorCondition Set this flag to true if the code is a preprocessor condition. It disables some simplifications
|
|
|
|
* @return false if source code contains syntax errors
|
2009-01-26 17:38:08 +01:00
|
|
|
*/
|
2010-09-02 23:01:12 +02:00
|
|
|
bool tokenize(std::istream &code,
|
|
|
|
const char FileName[],
|
|
|
|
const std::string &configuration = "",
|
|
|
|
const bool preprocessorCondition = false);
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2009-05-09 21:32:29 +02:00
|
|
|
/**
|
|
|
|
* Create tokens from code.
|
2010-04-02 20:48:32 +02:00
|
|
|
* The code must be preprocessed first:
|
|
|
|
* - multiline strings are not handled.
|
|
|
|
* - UTF in the code are not handled.
|
|
|
|
* - comments are not handled.
|
|
|
|
* @param code input stream for code
|
2009-05-09 21:32:29 +02:00
|
|
|
*/
|
|
|
|
void createTokens(std::istream &code);
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
/** Set variable id */
|
|
|
|
void setVarId();
|
|
|
|
|
2009-11-10 22:10:56 +01:00
|
|
|
/**
|
|
|
|
* Simplify tokenlist
|
|
|
|
*
|
|
|
|
* @return false if there is an error that requires aborting
|
|
|
|
* the checking of this file.
|
|
|
|
*/
|
|
|
|
bool simplifyTokenList();
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2011-01-01 11:26:48 +01:00
|
|
|
/**
|
|
|
|
* Delete all tokens in given token list
|
|
|
|
* @param tok token list to delete
|
|
|
|
*/
|
2009-01-26 17:38:08 +01:00
|
|
|
static void deleteTokens(Token *tok);
|
2011-01-01 11:26:48 +01:00
|
|
|
|
2011-11-20 18:08:07 +01:00
|
|
|
/**
|
|
|
|
* Deletes dead code between 'begin' and 'end'.
|
|
|
|
* In general not everything can be erased, such as:
|
|
|
|
* - code after labels;
|
|
|
|
* - code outside the scope where the function is called;
|
|
|
|
* - code after a change of scope caused by 'switch(...);'
|
|
|
|
* instructions, like 'case %any%;' or 'default;'
|
2011-11-20 19:01:31 +01:00
|
|
|
* Also, if the dead code contains a 'switch' block
|
|
|
|
* and inside it there's a label, the function removes all
|
|
|
|
* the 'switch(..)' tokens and every occurrence of 'case %any%; | default;'
|
|
|
|
* expression, such as the 'switch' block is reduced to a simple block.
|
2011-11-20 18:08:07 +01:00
|
|
|
*
|
|
|
|
* @param begin Tokens after this have a possibility to be erased.
|
|
|
|
* @param end Tokens before this have a possibility to be erased.
|
|
|
|
*/
|
|
|
|
static void eraseDeadCode(Token *begin, const Token *end);
|
|
|
|
|
2011-05-18 07:25:30 +02:00
|
|
|
/**
|
2011-12-09 20:47:51 +01:00
|
|
|
* Simplify '* & ( %var% ) =' or any combination of '* &' and '()'
|
|
|
|
* parentheses around '%var%' to '%var% ='
|
2011-05-18 07:25:30 +02:00
|
|
|
*/
|
2011-12-09 20:47:51 +01:00
|
|
|
void simplifyMulAndParens(void);
|
2011-05-18 07:25:30 +02:00
|
|
|
|
2011-01-01 11:26:48 +01:00
|
|
|
/**
|
|
|
|
* Get parameter name of function
|
|
|
|
* @param ftok The token for the function name in a function
|
|
|
|
* implementation/declaration
|
|
|
|
* @param par parameter number (1,2,3,..)
|
|
|
|
* @return if the parameter was found then the parameter name is
|
|
|
|
* returned. Otherwise NULL is returned.
|
|
|
|
*/
|
2010-08-06 18:32:31 +02:00
|
|
|
static const char *getParameterName(const Token *ftok, unsigned int par);
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2010-12-04 15:49:25 +01:00
|
|
|
/**
|
|
|
|
* Get file:line for a given token
|
|
|
|
* @param tok given token
|
|
|
|
* @return location for given token
|
|
|
|
*/
|
2009-01-26 17:38:08 +01:00
|
|
|
std::string fileLine(const Token *tok) const;
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
/**
|
|
|
|
* Calculates sizeof value for given type.
|
|
|
|
* @param type Token which will contain e.g. "int", "*", or string.
|
|
|
|
* @return sizeof for given type, or 0 if it can't be calculated.
|
|
|
|
*/
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int sizeOfType(const Token *type) const;
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2011-01-01 11:26:48 +01:00
|
|
|
/**
|
|
|
|
* Get filenames (the sourcefile + the files it include).
|
|
|
|
* The first filename is the filename for the sourcefile
|
|
|
|
* @return vector with filenames
|
|
|
|
*/
|
2009-01-26 17:38:08 +01:00
|
|
|
const std::vector<std::string> *getFiles() const;
|
|
|
|
|
2011-01-01 09:26:24 +01:00
|
|
|
/**
|
|
|
|
* Get function token by function name
|
|
|
|
* @todo better handling of overloaded functions
|
|
|
|
* @todo only scan parent scopes
|
|
|
|
* @param funcname function name
|
|
|
|
*/
|
2009-07-05 22:16:43 +02:00
|
|
|
const Token *getFunctionTokenByName(const char funcname[]) const;
|
2011-01-01 09:26:24 +01:00
|
|
|
|
|
|
|
/** get tokens */
|
2009-01-26 17:38:08 +01:00
|
|
|
const Token *tokens() const;
|
|
|
|
|
2011-01-01 11:26:48 +01:00
|
|
|
/**
|
|
|
|
* get filename for given token
|
|
|
|
* @param tok The given token
|
|
|
|
* @return filename for the given token
|
|
|
|
*/
|
2009-02-09 21:51:04 +01:00
|
|
|
std::string file(const Token *tok) const;
|
|
|
|
|
2009-10-17 23:11:48 +02:00
|
|
|
/**
|
2011-01-01 09:26:24 +01:00
|
|
|
* get error messages that the tokenizer generate
|
2009-10-17 23:11:48 +02:00
|
|
|
*/
|
2010-12-29 12:43:29 +01:00
|
|
|
virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings);
|
2009-10-17 23:11:48 +02:00
|
|
|
|
2010-09-09 19:40:36 +02:00
|
|
|
/** Simplify assignment in function call "f(x=g());" => "x=g();f(x);"
|
|
|
|
*/
|
|
|
|
void simplifyAssignmentInFunctionCall();
|
|
|
|
|
2009-11-21 19:53:07 +01:00
|
|
|
/**
|
|
|
|
* Simplify constant calculations such as "1+2" => "3"
|
|
|
|
* @return true if modifications to token-list are done.
|
|
|
|
* false if no modifications are done.
|
|
|
|
*/
|
|
|
|
bool simplifyCalculations();
|
|
|
|
|
2011-12-28 22:44:53 +01:00
|
|
|
/** Simplify Java and C# syntax */
|
|
|
|
void simplifyJavaAndCSharp();
|
|
|
|
|
2010-02-20 18:13:09 +01:00
|
|
|
/** Insert array size where it isn't given */
|
|
|
|
void arraySize();
|
|
|
|
|
2011-12-10 14:13:48 +01:00
|
|
|
/** Simplify labels and 'case|default' syntaxes */
|
|
|
|
void simplifyLabelsCaseDefault();
|
2010-02-21 09:47:41 +01:00
|
|
|
|
2011-01-30 08:34:58 +01:00
|
|
|
/** Remove macros in global scope */
|
|
|
|
void removeMacrosInGlobalScope();
|
|
|
|
|
2010-01-31 09:33:57 +01:00
|
|
|
/** Remove redundant assignment */
|
|
|
|
void removeRedundantAssignment();
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
/**
|
|
|
|
* Replace sizeof() to appropriate size.
|
|
|
|
*/
|
|
|
|
void simplifySizeof();
|
|
|
|
|
2009-03-18 20:32:05 +01:00
|
|
|
/**
|
2010-01-30 09:33:16 +01:00
|
|
|
* Simplify variable declarations (split up)
|
2011-11-05 12:23:05 +01:00
|
|
|
* \param only_k_r_fpar Only simplify K&R function parameters
|
2009-03-18 20:32:05 +01:00
|
|
|
*/
|
2011-11-05 12:23:05 +01:00
|
|
|
void simplifyVarDecl(bool only_k_r_fpar);
|
2009-03-18 20:32:05 +01:00
|
|
|
|
2010-01-30 09:33:16 +01:00
|
|
|
/**
|
|
|
|
* Simplify variable initialization
|
|
|
|
* ; int *p(0);
|
|
|
|
* =>
|
|
|
|
* ; int *p = 0;
|
|
|
|
*/
|
|
|
|
void simplifyInitVar();
|
2010-04-14 19:04:16 +02:00
|
|
|
Token * initVar(Token * tok);
|
2010-01-30 09:33:16 +01:00
|
|
|
|
2011-09-18 16:31:31 +02:00
|
|
|
/**
|
|
|
|
* Convert platform dependent types to standard types.
|
|
|
|
* 32 bits: size_t -> unsigned long
|
|
|
|
* 64 bits: size_t -> unsigned long long
|
|
|
|
*/
|
|
|
|
void simplifyPlatformTypes();
|
|
|
|
|
2010-03-28 15:56:13 +02:00
|
|
|
/**
|
2010-12-15 18:45:53 +01:00
|
|
|
* Collapse compound standard types into a single token.
|
2010-03-28 15:56:13 +02:00
|
|
|
* unsigned long long int => long _isUnsigned=true,_isLong=true
|
|
|
|
*/
|
|
|
|
void simplifyStdType();
|
|
|
|
|
2009-06-05 23:33:13 +02:00
|
|
|
/**
|
|
|
|
* Simplify question mark - colon operator
|
|
|
|
* Example: 0 ? (2/0) : 0 => 0
|
2009-06-10 23:12:26 +02:00
|
|
|
* @return true if something is modified
|
|
|
|
* false if nothing is done.
|
2009-06-05 23:33:13 +02:00
|
|
|
*/
|
|
|
|
bool simplifyQuestionMark();
|
2009-03-18 20:32:05 +01:00
|
|
|
|
2010-10-31 08:47:13 +01:00
|
|
|
/**
|
|
|
|
* Simplify compound assignments
|
|
|
|
* Example: ";a+=b;" => ";a=a+b;"
|
|
|
|
*/
|
|
|
|
void simplifyCompoundAssignment();
|
|
|
|
|
2009-03-23 18:20:56 +01:00
|
|
|
/**
|
2010-03-17 22:16:18 +01:00
|
|
|
* simplify if-assignments
|
2009-03-23 18:20:56 +01:00
|
|
|
* Example: "if(a=b);" => "a=b;if(a);"
|
|
|
|
*/
|
2009-08-29 21:17:17 +02:00
|
|
|
void simplifyIfAssign();
|
2009-03-23 18:20:56 +01:00
|
|
|
|
2010-05-01 10:26:15 +02:00
|
|
|
/**
|
2010-12-15 18:45:53 +01:00
|
|
|
* Simplify multiple assignments.
|
2010-05-01 10:26:15 +02:00
|
|
|
* Example: "a = b = c = 0;" => "a = 0; b = 0; c = 0;"
|
|
|
|
*/
|
|
|
|
void simplifyVariableMultipleAssign();
|
|
|
|
|
2009-03-24 18:23:21 +01:00
|
|
|
/**
|
2010-03-17 22:16:18 +01:00
|
|
|
* simplify if-not
|
2009-03-24 18:23:21 +01:00
|
|
|
* Example: "if(0==x);" => "if(!x);"
|
|
|
|
*/
|
2009-08-29 21:19:45 +02:00
|
|
|
void simplifyIfNot();
|
2009-03-24 18:23:21 +01:00
|
|
|
|
2009-07-25 13:11:29 +02:00
|
|
|
/**
|
2010-03-17 22:16:18 +01:00
|
|
|
* simplify if-not NULL
|
2011-04-16 12:07:56 +02:00
|
|
|
* Example: "if(0!=x);" => "if(x);"
|
|
|
|
* Special case: 'x = (0 != x);' is removed.
|
2009-07-25 13:11:29 +02:00
|
|
|
*/
|
2009-08-29 21:21:06 +02:00
|
|
|
void simplifyIfNotNull();
|
2009-07-25 13:11:29 +02:00
|
|
|
|
2010-03-17 22:16:18 +01:00
|
|
|
/** @brief simplify if (a) { if (a) */
|
2010-01-22 20:20:43 +01:00
|
|
|
void simplifyIfSameInnerCondition();
|
|
|
|
|
2009-05-31 10:42:27 +02:00
|
|
|
/**
|
2009-08-29 15:29:19 +02:00
|
|
|
* Simplify the "not" and "and" keywords to "!" and "&&"
|
|
|
|
* accordingly.
|
|
|
|
* Examples:
|
2010-03-17 22:16:18 +01:00
|
|
|
* - "if (not p)" => "if (!p)"
|
|
|
|
* - "if (p and q)" => "if (p && q)"
|
2009-05-31 10:42:27 +02:00
|
|
|
*/
|
2010-05-13 13:59:41 +02:00
|
|
|
bool simplifyLogicalOperators();
|
2009-05-31 10:42:27 +02:00
|
|
|
|
2009-06-10 23:12:26 +02:00
|
|
|
/**
|
2010-03-17 22:16:18 +01:00
|
|
|
* Simplify comma into a semicolon when possible:
|
|
|
|
* - "delete a, delete b" => "delete a; delete b;"
|
|
|
|
* - "a = 0, b = 0;" => "a = 0; b = 0;"
|
|
|
|
* - "return a(), b;" => "a(); return b;"
|
2009-06-10 23:12:26 +02:00
|
|
|
*/
|
2009-08-29 21:23:39 +02:00
|
|
|
void simplifyComma();
|
2009-06-10 23:12:26 +02:00
|
|
|
|
2009-02-07 21:06:00 +01:00
|
|
|
/** Add braces to an if-block
|
2011-06-11 21:51:12 +02:00
|
|
|
* @return true if no syntax errors
|
2009-02-07 21:06:00 +01:00
|
|
|
*/
|
2011-06-11 21:51:12 +02:00
|
|
|
bool simplifyIfAddBraces();
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2009-08-29 21:26:56 +02:00
|
|
|
/**
|
|
|
|
* Add braces to an do-while block
|
2009-08-22 12:42:19 +02:00
|
|
|
*/
|
2009-08-29 21:26:56 +02:00
|
|
|
void simplifyDoWhileAddBraces();
|
2009-08-22 12:42:19 +02:00
|
|
|
|
2009-09-30 13:35:00 +02:00
|
|
|
/**
|
|
|
|
* typedef A mytype;
|
|
|
|
* mytype c;
|
|
|
|
*
|
|
|
|
* Becomes:
|
|
|
|
* typedef A mytype;
|
|
|
|
* A c;
|
|
|
|
*/
|
|
|
|
void simplifyTypedef();
|
|
|
|
|
2009-08-29 21:06:14 +02:00
|
|
|
/**
|
|
|
|
* Simplify casts
|
2009-02-07 21:06:00 +01:00
|
|
|
*/
|
2009-08-29 21:06:14 +02:00
|
|
|
void simplifyCasts();
|
2009-02-07 21:06:00 +01:00
|
|
|
|
2012-01-26 17:07:10 +01:00
|
|
|
/**
|
|
|
|
* Change (multiple) arrays to (multiple) pointers.
|
|
|
|
*/
|
|
|
|
void simplifyUndefinedSizeArray();
|
|
|
|
|
2009-02-07 21:06:00 +01:00
|
|
|
/**
|
|
|
|
* A simplify function that replaces a variable with its value in cases
|
|
|
|
* when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)"
|
|
|
|
*
|
|
|
|
* @return true if modifications to token-list are done.
|
|
|
|
* false if no modifications are done.
|
|
|
|
*/
|
|
|
|
bool simplifyKnownVariables();
|
|
|
|
|
2011-02-12 09:24:20 +01:00
|
|
|
/**
|
|
|
|
* Utility function for simplifyKnownVariables. Get data about an
|
|
|
|
* assigned variable.
|
|
|
|
*/
|
|
|
|
bool simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2, Token **_tok3, std::string &value, unsigned int &valueVarId, bool &valueIsPointer, bool floatvar);
|
|
|
|
|
2011-02-11 20:12:51 +01:00
|
|
|
/**
|
|
|
|
* utility function for simplifyKnownVariables. Perform simplification
|
|
|
|
* of a given variable
|
|
|
|
*/
|
2011-02-20 18:18:27 +01:00
|
|
|
bool simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel);
|
2011-02-11 20:12:51 +01:00
|
|
|
|
2009-08-24 23:10:12 +02:00
|
|
|
/** Replace a "goto" with the statements */
|
|
|
|
void simplifyGoto();
|
|
|
|
|
2011-10-15 01:34:07 +02:00
|
|
|
/** Simplify redundant code placed after control flow statements :
|
2011-12-03 01:58:26 +01:00
|
|
|
* 'return', 'throw', 'goto', 'break' and 'continue'
|
2011-10-15 01:34:07 +02:00
|
|
|
*/
|
2011-10-15 12:45:48 +02:00
|
|
|
void simplifyFlowControl();
|
2011-10-15 01:34:07 +02:00
|
|
|
|
2009-09-05 18:46:27 +02:00
|
|
|
/** Expand nested strcat() calls. */
|
|
|
|
void simplifyNestedStrcat();
|
|
|
|
|
2009-02-17 20:18:26 +01:00
|
|
|
/** Simplify "if else" */
|
2009-08-29 21:28:39 +02:00
|
|
|
void elseif();
|
2009-02-17 20:18:26 +01:00
|
|
|
|
2010-04-03 14:28:32 +02:00
|
|
|
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno, bool split = false);
|
2010-03-31 17:14:49 +02:00
|
|
|
void addtoken(const Token *tok, const unsigned int lineno, const unsigned int fileno);
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2009-07-14 12:06:38 +02:00
|
|
|
/**
|
|
|
|
* Simplify the operator "?:"
|
|
|
|
*/
|
2009-08-29 21:30:13 +02:00
|
|
|
void simplifyConditionOperator();
|
2009-07-14 12:06:38 +02:00
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
/** Simplify conditions
|
|
|
|
* @return true if something is modified
|
|
|
|
* false if nothing is done.
|
|
|
|
*/
|
|
|
|
bool simplifyConditions();
|
|
|
|
|
2010-12-15 18:45:53 +01:00
|
|
|
/** Remove redundant code, e.g. if( false ) { int a; } should be
|
2009-01-26 17:38:08 +01:00
|
|
|
* removed, because it is never executed.
|
|
|
|
* @return true if something is modified
|
|
|
|
* false if nothing is done.
|
|
|
|
*/
|
2011-10-17 02:16:49 +02:00
|
|
|
bool removeRedundantConditions();
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2011-09-03 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Remove redundant for:
|
|
|
|
* "for (x=0;x<1;x++) { }" => "{ x = 1; }"
|
|
|
|
*/
|
|
|
|
void removeRedundantFor();
|
|
|
|
|
|
|
|
|
2010-10-27 10:34:06 +02:00
|
|
|
/**
|
|
|
|
* Reduces "; ;" to ";", except in "( ; ; )"
|
|
|
|
*/
|
|
|
|
void removeRedundantSemicolons();
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
/** Simplify function calls - constant return value
|
|
|
|
* @return true if something is modified
|
|
|
|
* false if nothing is done.
|
|
|
|
*/
|
|
|
|
bool simplifyFunctionReturn();
|
|
|
|
|
2010-03-13 11:52:48 +01:00
|
|
|
/** Struct initialization */
|
|
|
|
void simplifyStructInit();
|
|
|
|
|
2010-03-18 18:14:52 +01:00
|
|
|
/** Struct simplification
|
|
|
|
* "struct S { } s;" => "struct S { }; S s;"
|
|
|
|
*/
|
|
|
|
|
|
|
|
void simplifyStructDecl();
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
/**
|
2010-12-15 18:45:53 +01:00
|
|
|
* Remove redundant parenthesis:
|
2009-06-12 15:04:58 +02:00
|
|
|
* - "((x))" => "(x)"
|
|
|
|
* - "(function())" => "function()"
|
2009-06-12 16:14:01 +02:00
|
|
|
* - "(delete x)" => "delete x"
|
|
|
|
* - "(delete [] x)" => "delete [] x"
|
2009-01-26 17:38:08 +01:00
|
|
|
* @return true if modifications to token-list are done.
|
|
|
|
* false if no modifications are done.
|
|
|
|
*/
|
2011-03-30 16:45:31 +02:00
|
|
|
bool simplifyRedundantParenthesis();
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2010-01-31 16:29:12 +01:00
|
|
|
/** Simplify references */
|
|
|
|
void simplifyReference();
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
/**
|
|
|
|
* Simplify functions like "void f(x) int x; {"
|
|
|
|
* into "void f(int x) {"
|
2012-01-23 16:10:15 +01:00
|
|
|
* @return false only if there's a syntax error
|
2009-01-26 23:26:50 +01:00
|
|
|
*/
|
2012-01-23 16:10:15 +01:00
|
|
|
bool simplifyFunctionParameters();
|
2009-01-26 23:26:50 +01:00
|
|
|
|
2009-05-03 21:23:47 +02:00
|
|
|
/**
|
|
|
|
* Simplify templates
|
|
|
|
*/
|
|
|
|
void simplifyTemplates();
|
|
|
|
|
2011-12-24 21:51:55 +01:00
|
|
|
void simplifyDoublePlusAndDoubleMinus();
|
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
void simplifyRedundantConsecutiveBraces();
|
|
|
|
|
|
|
|
void simplifyArrayAccessSyntax();
|
|
|
|
|
|
|
|
void simplifyParameterVoid();
|
|
|
|
|
2012-01-04 12:55:51 +01:00
|
|
|
void concatenateDoubleSharp();
|
|
|
|
|
|
|
|
void simplifyLineMacro();
|
|
|
|
|
|
|
|
void simplifyNull();
|
|
|
|
|
|
|
|
void concatenateNegativeNumber();
|
|
|
|
|
|
|
|
void simplifyRoundCurlyParenthesis();
|
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
void simplifyDebugNew();
|
|
|
|
|
2012-01-04 12:55:51 +01:00
|
|
|
void simplifySQL();
|
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
bool hasEnumsWithTypedef();
|
|
|
|
|
|
|
|
void simplifyDefaultAndDeleteInsideClass();
|
|
|
|
|
|
|
|
bool hasComplicatedSyntaxErrorsInTemplates();
|
|
|
|
|
|
|
|
void simplifyReservedWordNullptr();
|
|
|
|
|
2009-09-12 22:54:47 +02:00
|
|
|
/**
|
|
|
|
* Simplify e.g. 'atol("0")' into '0'
|
|
|
|
*/
|
2009-09-13 09:35:02 +02:00
|
|
|
void simplifyMathFunctions();
|
2009-09-12 22:54:47 +02:00
|
|
|
|
2009-09-27 16:04:10 +02:00
|
|
|
/**
|
|
|
|
* Modify strings in the token list by replacing hex and oct
|
|
|
|
* values. E.g. "\x61" -> "a" and "\000" -> "\0"
|
|
|
|
* @param source The string to be modified, e.g. "\x61"
|
|
|
|
* @return Modified string, e.g. "a"
|
|
|
|
*/
|
|
|
|
std::string simplifyString(const std::string &source);
|
|
|
|
|
2009-11-04 23:58:15 +01:00
|
|
|
/**
|
|
|
|
* Use "<" comparison instead of ">"
|
|
|
|
* Use "<=" comparison instead of ">="
|
|
|
|
*/
|
|
|
|
void simplifyComparisonOrder();
|
|
|
|
|
2009-11-12 22:49:39 +01:00
|
|
|
/**
|
|
|
|
* Change "int const x;" into "const int x;"
|
|
|
|
*/
|
|
|
|
void simplifyConst();
|
|
|
|
|
2009-12-28 08:37:34 +01:00
|
|
|
/**
|
|
|
|
* simplify "while (0)"
|
|
|
|
*/
|
|
|
|
void simplifyWhile0();
|
|
|
|
|
2010-05-30 12:49:39 +02:00
|
|
|
/**
|
|
|
|
* Simplify while(func() && errno==EINTR)
|
|
|
|
*/
|
|
|
|
void simplifyErrNoInWhile();
|
|
|
|
|
2010-03-06 15:28:52 +01:00
|
|
|
/**
|
|
|
|
* Simplify while(func(f))
|
|
|
|
*/
|
|
|
|
void simplifyFuncInWhile();
|
|
|
|
|
2010-01-04 17:22:06 +01:00
|
|
|
/**
|
|
|
|
* Replace enum with constant value
|
|
|
|
*/
|
|
|
|
void simplifyEnum();
|
|
|
|
|
2010-01-12 21:25:31 +01:00
|
|
|
/**
|
|
|
|
* Remove "std::" before some function names
|
|
|
|
*/
|
|
|
|
void simplifyStd();
|
|
|
|
|
2011-10-16 08:09:57 +02:00
|
|
|
/** Simplify pointer to standard type (C only) */
|
|
|
|
void simplifyPointerToStandardType();
|
|
|
|
|
2010-01-20 21:19:06 +01:00
|
|
|
/** Simplify function pointers */
|
|
|
|
void simplifyFunctionPointers();
|
|
|
|
|
2009-10-01 19:45:48 +02:00
|
|
|
/**
|
|
|
|
* Remove exception specifications. This function calls itself recursively.
|
|
|
|
* @param tok First token in scope to cleanup
|
|
|
|
*/
|
|
|
|
void removeExceptionSpecifications(Token *tok) const;
|
|
|
|
|
2009-09-09 23:25:58 +02:00
|
|
|
void insertTokens(Token *dest, const Token *src, unsigned int n);
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2011-02-10 04:02:17 +01:00
|
|
|
/**
|
|
|
|
* Copy tokens.
|
|
|
|
* @param dest destination token where copied tokens will be inserted after
|
|
|
|
* @param first first token to copy
|
|
|
|
* @param last last token to copy
|
|
|
|
* @return new location of last token copied
|
|
|
|
*/
|
2012-01-21 17:15:00 +01:00
|
|
|
Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);
|
2011-02-10 04:02:17 +01:00
|
|
|
|
2009-11-28 22:08:43 +01:00
|
|
|
/**
|
|
|
|
* Send error message to error logger about internal bug.
|
2010-01-17 16:38:28 +01:00
|
|
|
* @param tok the token that this bug concerns.
|
2009-11-28 22:08:43 +01:00
|
|
|
*/
|
|
|
|
void cppcheckError(const Token *tok) const;
|
|
|
|
|
2009-03-15 00:39:45 +01:00
|
|
|
/**
|
|
|
|
* Setup links for tokens so that one can call Token::link().
|
|
|
|
*
|
|
|
|
* @return false if there was a mismatch with tokens, this
|
|
|
|
* should mean that source code was not valid.
|
|
|
|
*/
|
|
|
|
bool createLinks();
|
|
|
|
|
2010-07-22 19:57:48 +02:00
|
|
|
/** Syntax error */
|
|
|
|
void syntaxError(const Token *tok);
|
|
|
|
|
|
|
|
/** Syntax error. Example: invalid number of ')' */
|
2009-05-07 22:17:29 +02:00
|
|
|
void syntaxError(const Token *tok, char c);
|
|
|
|
|
2009-09-13 15:35:37 +02:00
|
|
|
/**
|
|
|
|
* assert that tokens are ok - used during debugging for example
|
|
|
|
* to catch problems in simplifyTokenList.
|
|
|
|
* @return always true.
|
|
|
|
*/
|
|
|
|
bool validate() const;
|
|
|
|
|
2010-04-24 09:40:05 +02:00
|
|
|
/**
|
|
|
|
* Remove __declspec()
|
|
|
|
*/
|
|
|
|
void simplifyDeclspec();
|
|
|
|
|
2010-04-21 21:08:47 +02:00
|
|
|
/**
|
|
|
|
* Remove calling convention
|
|
|
|
*/
|
|
|
|
void simplifyCallingConvention();
|
|
|
|
|
2010-05-27 18:15:42 +02:00
|
|
|
/**
|
|
|
|
* Remove __attribute__ ((?))
|
|
|
|
*/
|
|
|
|
void simplifyAttribute();
|
|
|
|
|
2010-06-30 08:10:39 +02:00
|
|
|
/**
|
|
|
|
* Remove keywords "volatile", "inline", "register", and "restrict"
|
|
|
|
*/
|
|
|
|
void simplifyKeyword();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove __asm
|
|
|
|
*/
|
|
|
|
void simplifyAsm();
|
|
|
|
|
2010-08-15 11:54:28 +02:00
|
|
|
/**
|
|
|
|
* Simplify bitfields - the field width is removed as we don't use it.
|
|
|
|
*/
|
|
|
|
void simplifyBitfields();
|
|
|
|
|
2010-06-30 08:10:39 +02:00
|
|
|
/**
|
|
|
|
* Remove __builtin_expect(...), likely(...), and unlikely(...)
|
|
|
|
*/
|
|
|
|
void simplifyBuiltinExpect();
|
|
|
|
|
2011-02-02 07:40:08 +01:00
|
|
|
/**
|
|
|
|
* Remove unnecessary member qualification
|
|
|
|
*/
|
|
|
|
void removeUnnecessaryQualification();
|
|
|
|
|
2011-06-29 03:46:54 +02:00
|
|
|
/**
|
|
|
|
* unnecessary member qualification error
|
|
|
|
*/
|
|
|
|
void unnecessaryQualificationError(const Token *tok, const std::string &qualification);
|
|
|
|
|
2010-08-18 22:42:04 +02:00
|
|
|
/**
|
|
|
|
* Remove Microsoft MFC 'DECLARE_MESSAGE_MAP()'
|
|
|
|
*/
|
|
|
|
void simplifyMicrosoftMFC();
|
|
|
|
|
2010-09-01 18:10:12 +02:00
|
|
|
/**
|
2011-09-23 01:59:56 +02:00
|
|
|
* Convert Microsoft memory functions
|
|
|
|
* CopyMemory(dst, src, len) -> memcpy(dst, src, len)
|
|
|
|
* FillMemory(dst, len, val) -> memset(dst, val, len)
|
|
|
|
* MoveMemory(dst, src, len) -> memmove(dst, src, len)
|
|
|
|
* ZeroMemory(dst, len) -> memset(dst, 0, len)
|
|
|
|
*/
|
|
|
|
void simplifyMicrosoftMemoryFunctions();
|
|
|
|
|
2011-09-24 20:51:03 +02:00
|
|
|
/**
|
|
|
|
* Convert Microsoft string functions
|
|
|
|
* _tcscpy -> strcpy
|
|
|
|
*/
|
|
|
|
void simplifyMicrosoftStringFunctions();
|
|
|
|
|
2011-09-23 01:59:56 +02:00
|
|
|
/**
|
|
|
|
* Remove Borland code
|
|
|
|
*/
|
2010-09-01 18:10:12 +02:00
|
|
|
void simplifyBorland();
|
|
|
|
|
2010-12-02 17:41:49 +01:00
|
|
|
/**
|
|
|
|
* Remove Qt signals and slots
|
|
|
|
*/
|
|
|
|
void simplifyQtSignalsSlots();
|
|
|
|
|
2011-01-27 18:44:20 +01:00
|
|
|
/**
|
|
|
|
* Collapse operator name tokens into single token
|
|
|
|
* operator = => operator=
|
|
|
|
*/
|
|
|
|
void simplifyOperatorName();
|
|
|
|
|
2010-01-29 22:22:18 +01:00
|
|
|
/**
|
|
|
|
* This will return a short name describing function parameters
|
|
|
|
* e.g. parameters: (int a, char b) should get name "int,char,".
|
|
|
|
* This should help to identify functions with the same name,
|
|
|
|
* but with different parameters.
|
|
|
|
* @param start The "(" token
|
|
|
|
* @return, e.g. "int,char,"
|
|
|
|
*/
|
|
|
|
static std::string getNameForFunctionParams(const Token *start);
|
|
|
|
|
2010-02-16 07:33:23 +01:00
|
|
|
/**
|
|
|
|
* check for duplicate enum definition
|
|
|
|
*/
|
|
|
|
bool duplicateDefinition(Token **tokPtr, const Token *name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* duplicate enum definition error
|
|
|
|
*/
|
|
|
|
void duplicateEnumError(const Token *tok1, const Token *tok2, const std::string & type);
|
|
|
|
|
2011-08-15 00:06:05 +02:00
|
|
|
bool duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, bool undefinedStruct);
|
2010-02-20 09:07:29 +01:00
|
|
|
void duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string & type);
|
2010-12-04 15:49:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report error - duplicate declarations
|
|
|
|
*/
|
2010-04-12 19:05:31 +02:00
|
|
|
void duplicateDeclarationError(const Token *tok1, const Token *tok2, const std::string &type);
|
2010-03-17 22:08:02 +01:00
|
|
|
|
2010-08-26 20:44:13 +02:00
|
|
|
void unsupportedTypedef(const Token *tok) const;
|
|
|
|
|
2010-09-30 21:22:49 +02:00
|
|
|
/** Was there templates in the code? */
|
2011-10-13 20:53:06 +02:00
|
|
|
bool codeWithTemplates() const {
|
2010-09-30 21:22:49 +02:00
|
|
|
return _codeWithTemplates;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void setSettings(const Settings *settings) {
|
2010-12-01 18:00:55 +01:00
|
|
|
_settings = settings;
|
|
|
|
}
|
|
|
|
|
2011-01-16 18:13:54 +01:00
|
|
|
const SymbolDatabase *getSymbolDatabase() const;
|
2010-12-07 07:08:49 +01:00
|
|
|
|
2011-01-16 18:13:54 +01:00
|
|
|
Token *deleteInvalidTypedef(Token *typeDef);
|
2011-01-04 07:43:40 +01:00
|
|
|
|
2011-02-26 14:42:19 +01:00
|
|
|
/**
|
|
|
|
* Get variable count.
|
|
|
|
* @return number of variables
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
unsigned int varIdCount() const {
|
2011-02-26 14:42:19 +01:00
|
|
|
return _varId;
|
|
|
|
}
|
|
|
|
|
2011-06-26 22:53:16 +02:00
|
|
|
/**
|
|
|
|
* Simplify e.g. 'return(strncat(temp,"a",1));' into
|
|
|
|
* strncat(temp,"a",1); return temp;
|
|
|
|
*/
|
2012-01-26 11:49:08 +01:00
|
|
|
void simplifyReturnStrncat();
|
2011-06-26 22:53:16 +02:00
|
|
|
|
2011-09-28 13:36:48 +02:00
|
|
|
/**
|
|
|
|
* Output list of unknown types.
|
|
|
|
*/
|
|
|
|
void printUnknownTypes();
|
|
|
|
|
2010-03-17 22:08:02 +01:00
|
|
|
private:
|
|
|
|
/** Disable copy constructor, no implementation */
|
|
|
|
Tokenizer(const Tokenizer &);
|
|
|
|
|
|
|
|
/** Disable assignment operator, no implementation */
|
2010-02-12 21:38:17 +01:00
|
|
|
Tokenizer &operator=(const Tokenizer &);
|
2009-09-13 09:03:48 +02:00
|
|
|
|
2011-01-01 11:26:48 +01:00
|
|
|
/** Token list */
|
2009-07-13 19:11:31 +02:00
|
|
|
Token *_tokens, *_tokensBack;
|
2011-01-01 11:26:48 +01:00
|
|
|
|
|
|
|
/** settings */
|
2010-12-01 18:00:55 +01:00
|
|
|
const Settings * _settings;
|
2011-01-01 11:26:48 +01:00
|
|
|
|
|
|
|
/** errorlogger */
|
2009-07-13 19:11:31 +02:00
|
|
|
ErrorLogger * const _errorLogger;
|
2010-01-23 22:18:11 +01:00
|
|
|
|
2011-10-24 02:52:55 +02:00
|
|
|
/** Symbol database that all checks etc can use */
|
|
|
|
mutable SymbolDatabase *_symbolDatabase;
|
|
|
|
|
2010-01-23 22:18:11 +01:00
|
|
|
/** E.g. "A" for code where "#ifdef A" is true. This is used to
|
|
|
|
print additional information in error situations. */
|
|
|
|
std::string _configuration;
|
2010-09-30 21:22:49 +02:00
|
|
|
|
2011-10-24 02:52:55 +02:00
|
|
|
/** sizeof information for known types */
|
|
|
|
std::map<std::string, unsigned int> _typeSize;
|
|
|
|
|
|
|
|
/** filenames for the tokenized source code (source + included) */
|
|
|
|
std::vector<std::string> _files;
|
|
|
|
|
|
|
|
/** variable count */
|
|
|
|
unsigned int _varId;
|
|
|
|
|
2010-09-30 21:22:49 +02:00
|
|
|
/**
|
|
|
|
* was there any templates? templates that are "unused" are
|
|
|
|
* removed from the token list
|
|
|
|
*/
|
|
|
|
bool _codeWithTemplates;
|
2009-01-26 17:38:08 +01:00
|
|
|
};
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|