2022-12-13 22:29:23 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2022-12-13 22:29:23 +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 fwdanalysisH
|
|
|
|
#define fwdanalysisH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class Token;
|
|
|
|
class Library;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forward data flow analysis for checks
|
|
|
|
* - unused value
|
|
|
|
* - redundant assignment
|
|
|
|
* - valueflow analysis
|
|
|
|
*/
|
|
|
|
class FwdAnalysis {
|
|
|
|
public:
|
2023-08-08 11:05:02 +02:00
|
|
|
FwdAnalysis(bool cpp, const Library &library) : mCpp(cpp), mLibrary(library) {}
|
2022-12-13 22:29:23 +01:00
|
|
|
|
|
|
|
bool hasOperand(const Token *tok, const Token *lhs) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if "expr" is reassigned. 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 Token where expr is reassigned. If it's not reassigned then nullptr is returned.
|
|
|
|
*/
|
|
|
|
const Token *reassign(const Token *expr, const Token *startToken, const Token *endToken);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
struct KnownAndToken {
|
2023-08-08 11:05:02 +02:00
|
|
|
bool known{};
|
|
|
|
const Token* token{};
|
2022-12-13 22:29:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Is there some possible alias for given expression */
|
|
|
|
bool possiblyAliased(const Token *expr, const Token *startToken) const;
|
|
|
|
|
|
|
|
std::set<nonneg int> getExprVarIds(const Token* expr, bool* localOut = nullptr, bool* unknownVarIdOut = nullptr) const;
|
|
|
|
private:
|
|
|
|
static bool isEscapedAlias(const Token* expr);
|
|
|
|
|
|
|
|
/** Result of forward analysis */
|
|
|
|
struct Result {
|
|
|
|
enum class Type { NONE, READ, WRITE, BREAK, RETURN, BAILOUT } type;
|
2023-08-08 11:05:02 +02:00
|
|
|
explicit Result(Type type) : type(type) {}
|
2022-12-13 22:29:23 +01:00
|
|
|
Result(Type type, const Token *token) : type(type), token(token) {}
|
2023-08-08 11:05:02 +02:00
|
|
|
const Token* token{};
|
2022-12-13 22:29:23 +01:00
|
|
|
};
|
|
|
|
|
2023-10-08 09:10:17 +02:00
|
|
|
Result check(const Token *expr, const Token *startToken, const Token *endToken);
|
|
|
|
Result checkRecursive(const Token *expr, const Token *startToken, const Token *endToken, const std::set<nonneg int> &exprVarIds, bool local, bool inInnerClass, int depth=0);
|
2022-12-13 22:29:23 +01:00
|
|
|
|
|
|
|
// Is expression a l-value global data?
|
|
|
|
bool isGlobalData(const Token *expr) const;
|
|
|
|
|
|
|
|
const bool mCpp;
|
|
|
|
const Library &mLibrary;
|
2023-08-08 11:05:02 +02:00
|
|
|
enum class What { Reassign, UnusedValue, ValueFlow } mWhat = What::Reassign;
|
2022-12-13 22:29:23 +01:00
|
|
|
std::vector<KnownAndToken> mValueFlow;
|
2023-08-08 11:05:02 +02:00
|
|
|
bool mValueFlowKnown = true;
|
2022-12-13 22:29:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // fwdanalysisH
|