Cppcheck
fwdanalysis.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 //---------------------------------------------------------------------------
20 #ifndef fwdanalysisH
21 #define fwdanalysisH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 
26 #include <set>
27 #include <vector>
28 
29 class Token;
30 class Settings;
31 
32 /**
33  * Forward data flow analysis for checks
34  * - unused value
35  * - redundant assignment
36  * - valueflow analysis
37  */
38 class FwdAnalysis {
39 public:
40  explicit FwdAnalysis(const Settings &settings) : mSettings(settings) {}
41 
42  bool hasOperand(const Token *tok, const Token *lhs) const;
43 
44  /**
45  * Check if "expr" is reassigned. The "expr" can be a tree (x.y[12]).
46  * @param expr Symbolic expression to perform forward analysis for
47  * @param startToken First token in forward analysis
48  * @param endToken Last token in forward analysis
49  * @return Token where expr is reassigned. If it's not reassigned then nullptr is returned.
50  */
51  const Token *reassign(const Token *expr, const Token *startToken, const Token *endToken);
52 
53  /**
54  * Check if "expr" is used. The "expr" can be a tree (x.y[12]).
55  * @param expr Symbolic expression to perform forward analysis for
56  * @param startToken First token in forward analysis
57  * @param endToken Last token in forward analysis
58  * @return true if expr is used.
59  */
60  bool unusedValue(const Token *expr, const Token *startToken, const Token *endToken);
61 
62  struct KnownAndToken {
63  bool known{};
64  const Token* token{};
65  };
66 
67  /** Is there some possible alias for given expression */
68  bool possiblyAliased(const Token *expr, const Token *startToken) const;
69 
70  std::set<nonneg int> getExprVarIds(const Token* expr, bool* localOut = nullptr, bool* unknownVarIdOut = nullptr) const;
71 private:
72  static bool isEscapedAlias(const Token* expr);
73 
74  /** Result of forward analysis */
75  struct Result {
76  enum class Type { NONE, READ, WRITE, BREAK, RETURN, BAILOUT } type;
77  explicit Result(Type type) : type(type) {}
79  const Token* token{};
80  };
81 
82  Result check(const Token *expr, const Token *startToken, const Token *endToken);
83  Result checkRecursive(const Token *expr, const Token *startToken, const Token *endToken, const std::set<nonneg int> &exprVarIds, bool local, bool inInnerClass, int depth=0);
84 
87  std::vector<KnownAndToken> mValueFlow;
88  bool mValueFlowKnown = true;
89 };
90 
91 #endif // fwdanalysisH
Forward data flow analysis for checks.
Definition: fwdanalysis.h:38
const Settings & mSettings
Definition: fwdanalysis.h:85
bool possiblyAliased(const Token *expr, const Token *startToken) const
Is there some possible alias for given expression.
std::vector< KnownAndToken > mValueFlow
Definition: fwdanalysis.h:87
bool hasOperand(const Token *tok, const Token *lhs) const
enum FwdAnalysis::What mWhat
bool mValueFlowKnown
Definition: fwdanalysis.h:88
static bool isEscapedAlias(const Token *expr)
Result check(const Token *expr, const Token *startToken, const Token *endToken)
FwdAnalysis(const Settings &settings)
Definition: fwdanalysis.h:40
Result checkRecursive(const Token *expr, const Token *startToken, const Token *endToken, const std::set< nonneg int > &exprVarIds, bool local, bool inInnerClass, int depth=0)
Definition: fwdanalysis.cpp:96
std::set< nonneg int > getExprVarIds(const Token *expr, bool *localOut=nullptr, bool *unknownVarIdOut=nullptr) const
const Token * reassign(const Token *expr, const Token *startToken, const Token *endToken)
Check if "expr" is reassigned.
bool unusedValue(const Token *expr, const Token *startToken, const Token *endToken)
Check if "expr" is used.
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
Information about a class type.
Result of forward analysis.
Definition: fwdanalysis.h:75
Result(Type type, const Token *token)
Definition: fwdanalysis.h:78
Result(Type type)
Definition: fwdanalysis.h:77
enum FwdAnalysis::Result::Type type
const Token * token
Definition: fwdanalysis.h:79