2015-08-02 21:57:32 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2018-03-31 20:59:09 +02:00
|
|
|
* Copyright (C) 2007-2018 Cppcheck team.
|
2015-08-02 21:57:32 +02: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 astutilsH
|
|
|
|
#define astutilsH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2018-11-23 06:53:43 +01:00
|
|
|
#include <functional>
|
2015-08-03 09:20:50 +02:00
|
|
|
#include <string>
|
2017-04-20 19:57:39 +02:00
|
|
|
#include <vector>
|
2015-08-02 21:57:32 +02:00
|
|
|
|
2018-08-07 09:32:16 +02:00
|
|
|
#include "errorlogger.h"
|
|
|
|
|
2016-12-06 12:31:16 +01:00
|
|
|
class Library;
|
2017-05-27 04:33:47 +02:00
|
|
|
class Settings;
|
2015-08-03 09:20:50 +02:00
|
|
|
class Token;
|
2018-10-18 21:01:47 +02:00
|
|
|
class Variable;
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2018-11-23 06:53:43 +01:00
|
|
|
enum class ChildrenToVisit {
|
|
|
|
none,
|
|
|
|
op1,
|
|
|
|
op2,
|
|
|
|
op1_and_op2,
|
|
|
|
done // found what we looked for, don't visit any more children
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visit AST nodes recursively. The order is not "well defined"
|
|
|
|
*/
|
|
|
|
void visitAstNodes(const Token *ast, std::function<ChildrenToVisit(const Token *)> visitor);
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
/** Is expression a 'signed char' if no promotion is used */
|
|
|
|
bool astIsSignedChar(const Token *tok);
|
2016-02-08 08:08:35 +01:00
|
|
|
/** Is expression a 'char' if no promotion is used? */
|
|
|
|
bool astIsUnknownSignChar(const Token *tok);
|
2015-08-02 21:57:32 +02:00
|
|
|
/** Is expression of integral type? */
|
|
|
|
bool astIsIntegral(const Token *tok, bool unknown);
|
|
|
|
/** Is expression of floating point type? */
|
|
|
|
bool astIsFloat(const Token *tok, bool unknown);
|
2016-02-05 15:48:51 +01:00
|
|
|
/** Is expression of boolean type? */
|
|
|
|
bool astIsBool(const Token *tok);
|
2015-08-02 21:57:32 +02:00
|
|
|
|
2018-11-12 10:08:17 +01:00
|
|
|
bool astIsPointer(const Token *tok);
|
|
|
|
|
2018-11-17 09:41:59 +01:00
|
|
|
bool astIsIterator(const Token *tok);
|
|
|
|
|
|
|
|
bool astIsContainer(const Token *tok);
|
|
|
|
|
2015-08-10 09:41:06 +02:00
|
|
|
/**
|
|
|
|
* Get canonical type of expression. const/static/etc are not included and neither *&.
|
|
|
|
* For example:
|
|
|
|
* Expression type Return
|
|
|
|
* std::string std::string
|
|
|
|
* int * int
|
|
|
|
* static const int int
|
|
|
|
* std::vector<T> std::vector
|
|
|
|
*/
|
|
|
|
std::string astCanonicalType(const Token *expr);
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
/** Is given syntax tree a variable comparison against value */
|
|
|
|
const Token * astIsVariableComparison(const Token *tok, const std::string &comp, const std::string &rhs, const Token **vartok=nullptr);
|
|
|
|
|
2018-10-18 11:56:23 +02:00
|
|
|
const Token * nextAfterAstRightmostLeaf(const Token * tok);
|
|
|
|
|
2018-11-11 16:43:54 +01:00
|
|
|
bool precedes(const Token * tok1, const Token * tok2);
|
|
|
|
|
2018-09-28 08:38:24 +02:00
|
|
|
bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2018-03-24 07:58:37 +01:00
|
|
|
bool isEqualKnownValue(const Token * const tok1, const Token * const tok2);
|
|
|
|
|
|
|
|
bool isDifferentKnownValues(const Token * const tok1, const Token * const tok2);
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
/**
|
|
|
|
* Are two conditions opposite
|
|
|
|
* @param isNot do you want to know if cond1 is !cond2 or if cond1 and cond2 are non-overlapping. true: cond1==!cond2 false: cond1==true => cond2==false
|
|
|
|
* @param cpp c++ file
|
|
|
|
* @param cond1 condition1
|
|
|
|
* @param cond2 condition2
|
2017-05-06 11:57:02 +02:00
|
|
|
* @param library files data
|
|
|
|
* @param pure
|
2015-08-03 09:20:50 +02:00
|
|
|
*/
|
2018-09-28 08:38:24 +02:00
|
|
|
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2018-09-28 08:38:24 +02:00
|
|
|
bool isOppositeExpression(bool cpp, const Token * const tok1, const Token * const tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
|
2018-05-02 06:32:33 +02:00
|
|
|
|
2018-10-21 07:09:20 +02:00
|
|
|
bool isConstExpression(const Token *tok, const Library& library, bool pure, bool cpp);
|
2015-08-03 09:20:50 +02:00
|
|
|
|
|
|
|
bool isWithoutSideEffects(bool cpp, const Token* tok);
|
|
|
|
|
2018-05-13 20:20:55 +02:00
|
|
|
bool isUniqueExpression(const Token* tok);
|
|
|
|
|
2016-01-16 18:52:34 +01:00
|
|
|
/** Is scope a return scope (scope will unconditionally return) */
|
|
|
|
bool isReturnScope(const Token *endToken);
|
|
|
|
|
2017-11-18 11:02:52 +01:00
|
|
|
/** Is variable changed by function call?
|
|
|
|
* In case the answer of the question is inconclusive, e.g. because the function declaration is not known
|
|
|
|
* the return value is false and the output parameter inconclusive is set to true
|
|
|
|
*
|
|
|
|
* @param tok ast tree
|
|
|
|
* @param varid Variable Id
|
|
|
|
* @param settings program settings
|
|
|
|
* @param inconclusive pointer to output variable which indicates that the answer of the question is inconclusive
|
|
|
|
*/
|
|
|
|
bool isVariableChangedByFunctionCall(const Token *tok, unsigned int varid, const Settings *settings, bool *inconclusive);
|
|
|
|
|
2016-10-23 13:54:44 +02:00
|
|
|
/** Is variable changed by function call?
|
2016-11-04 15:01:05 +01:00
|
|
|
* In case the answer of the question is inconclusive, e.g. because the function declaration is not known
|
2016-10-23 13:54:44 +02:00
|
|
|
* the return value is false and the output parameter inconclusive is set to true
|
|
|
|
*
|
2016-11-04 15:01:05 +01:00
|
|
|
* @param tok token of variable in function call
|
2016-10-23 13:54:44 +02:00
|
|
|
* @param settings program settings
|
|
|
|
* @param inconclusive pointer to output variable which indicates that the answer of the question is inconclusive
|
|
|
|
*/
|
|
|
|
bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings, bool *inconclusive);
|
|
|
|
|
2015-11-11 13:45:28 +01:00
|
|
|
/** Is variable changed in block of code? */
|
2018-04-18 17:45:43 +02:00
|
|
|
bool isVariableChanged(const Token *start, const Token *end, const unsigned int varid, bool globalvar, const Settings *settings, bool cpp);
|
2015-11-11 13:45:28 +01:00
|
|
|
|
2018-10-18 21:01:47 +02:00
|
|
|
bool isVariableChanged(const Variable * var, const Settings *settings, bool cpp);
|
|
|
|
|
2015-12-06 12:50:05 +01:00
|
|
|
/** Determines the number of arguments - if token is a function call or macro
|
|
|
|
* @param start token which is supposed to be the function/macro name.
|
|
|
|
* \return Number of arguments
|
|
|
|
*/
|
|
|
|
int numberOfArguments(const Token *start);
|
|
|
|
|
2017-04-20 19:57:39 +02:00
|
|
|
/**
|
|
|
|
* Get arguments (AST)
|
|
|
|
*/
|
|
|
|
std::vector<const Token *> getArguments(const Token *ftok);
|
|
|
|
|
2017-08-29 22:35:55 +02:00
|
|
|
/**
|
|
|
|
* find lambda function end token
|
|
|
|
* \param first The [ token
|
|
|
|
* \return nullptr or the }
|
|
|
|
*/
|
|
|
|
const Token *findLambdaEndToken(const Token *first);
|
|
|
|
|
2018-04-17 14:23:04 +02:00
|
|
|
/**
|
|
|
|
* do we see a likely write of rhs through overloaded operator
|
|
|
|
* s >> x;
|
|
|
|
* a & x;
|
|
|
|
*/
|
|
|
|
bool isLikelyStreamRead(bool cpp, const Token *op);
|
|
|
|
|
2018-12-17 06:04:24 +01:00
|
|
|
bool isConstVarExpression(const Token *tok);
|
|
|
|
|
2018-12-25 11:56:06 +01:00
|
|
|
/**
|
2018-12-31 18:00:47 +01:00
|
|
|
* Forward data flow analysis for checks
|
2018-12-25 11:56:06 +01:00
|
|
|
* - unused value
|
|
|
|
* - redundant assignment
|
2018-12-31 18:00:47 +01:00
|
|
|
* - valueflow analysis
|
2018-12-25 11:56:06 +01:00
|
|
|
*/
|
2018-12-31 18:00:47 +01:00
|
|
|
class FwdAnalysis {
|
2018-12-02 17:01:52 +01:00
|
|
|
public:
|
2018-12-31 18:00:47 +01:00
|
|
|
FwdAnalysis(bool cpp, const Library &library) : mCpp(cpp), mLibrary(library), mWhat(What::Reassign) {}
|
2018-12-02 17:01:52 +01:00
|
|
|
|
2018-12-02 18:29:16 +01:00
|
|
|
bool hasOperand(const Token *tok, const Token *lhs) const;
|
2018-12-02 17:01:52 +01:00
|
|
|
|
2018-12-02 17:42:18 +01:00
|
|
|
/**
|
2018-12-02 18:29:16 +01:00
|
|
|
* Check if "expr" is reassigned. The "expr" can be a tree (x.y[12]).
|
2018-12-02 17:42:18 +01:00
|
|
|
* @param expr Symbolic expression to perform forward analysis for
|
|
|
|
* @param startToken First token in forward analysis
|
|
|
|
* @param endToken Last token in forward analysis
|
2018-12-02 18:29:16 +01:00
|
|
|
* @return Token where expr is reassigned. If it's not reassigned then nullptr is returned.
|
2018-12-02 17:42:18 +01:00
|
|
|
*/
|
2018-12-02 18:29:16 +01:00
|
|
|
const Token *reassign(const Token *expr, const Token *startToken, const Token *endToken);
|
2018-12-02 17:01:52 +01:00
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
/**
|
|
|
|
* Check if "expr" is used. The "expr" can be a tree (x.y[12]).
|
|
|
|
* @param expr Symbolic expression to perform forward analysis for
|
|
|
|
* @param startToken First token in forward analysis
|
|
|
|
* @param endToken Last token in forward analysis
|
|
|
|
* @return true if expr is used.
|
|
|
|
*/
|
|
|
|
bool unusedValue(const Token *expr, const Token *startToken, const Token *endToken);
|
|
|
|
|
2018-12-31 17:05:46 +01:00
|
|
|
std::vector<const Token *> valueFlow(const Token *expr, const Token *startToken, const Token *endToken);
|
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
/** Is there some possible alias for given expression */
|
|
|
|
bool possiblyAliased(const Token *expr, const Token *startToken) const;
|
|
|
|
|
2018-12-13 21:01:33 +01:00
|
|
|
static bool isNullOperand(const Token *expr);
|
|
|
|
|
2018-12-02 17:01:52 +01:00
|
|
|
private:
|
2018-12-02 18:29:16 +01:00
|
|
|
/** Result of forward analysis */
|
|
|
|
struct Result {
|
|
|
|
enum class Type { NONE, READ, WRITE, BREAK, RETURN, BAILOUT } type;
|
2018-12-04 18:46:23 +01:00
|
|
|
explicit Result(Type type) : type(type), token(nullptr) {}
|
2018-12-02 18:29:16 +01:00
|
|
|
Result(Type type, const Token *token) : type(type), token(token) {}
|
|
|
|
const Token *token;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Result check(const Token *expr, const Token *startToken, const Token *endToken);
|
2018-12-02 17:42:18 +01:00
|
|
|
struct Result checkRecursive(const Token *expr, const Token *startToken, const Token *endToken, const std::set<unsigned int> &exprVarIds, bool local);
|
2018-12-02 17:01:52 +01:00
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
// Is expression a l-value global data?
|
|
|
|
bool isGlobalData(const Token *expr) const;
|
|
|
|
|
2018-12-02 17:01:52 +01:00
|
|
|
const bool mCpp;
|
|
|
|
const Library &mLibrary;
|
2018-12-31 17:05:46 +01:00
|
|
|
enum class What { Reassign, UnusedValue, ValueFlow } mWhat;
|
|
|
|
std::vector<const Token *> mValueFlow;
|
2018-12-02 17:01:52 +01:00
|
|
|
};
|
|
|
|
|
2015-08-02 21:57:32 +02:00
|
|
|
#endif // astutilsH
|