Cppcheck
valueflow.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 valueflowH
21 #define valueflowH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 #include "mathlib.h"
26 #include "vfvalue.h"
27 
28 #include <cstdlib>
29 #include <functional>
30 #include <list>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 class ErrorLogger;
36 struct InferModel;
37 class Settings;
38 class SymbolDatabase;
39 class TimerResultsIntf;
40 class Token;
41 class TokenList;
42 class ValueType;
43 class Variable;
44 class Scope;
45 
46 template<class T>
47 class ValuePtr;
48 
49 namespace ValueFlow {
50  /// Constant folding of expression. This can be used before the full ValueFlow has been executed (ValueFlow::setValues).
51  const Value * valueFlowConstantFoldAST(Token *expr, const Settings &settings);
52 
53  /// Perform valueflow analysis.
54  void setValues(TokenList& tokenlist,
55  SymbolDatabase& symboldatabase,
56  ErrorLogger& errorLogger,
57  const Settings& settings,
58  TimerResultsIntf* timerResults);
59 
60  std::string eitherTheConditionIsRedundant(const Token *condition);
61 
62  size_t getSizeOf(const ValueType &vt, const Settings &settings, int maxRecursion = 0);
63 
64  const Value* findValue(const std::list<Value>& values,
65  const Settings& settings,
66  const std::function<bool(const Value&)> &pred);
67 
68  std::vector<Value> isOutOfBounds(const Value& size, const Token* indexTok, bool possible = true);
69 
71 
72  bool isContainerSizeChanged(const Token* tok, int indirect, const Settings& settings, int depth = 20);
73 
74  struct LifetimeToken {
75  const Token* token{};
77  bool addressOf{};
78  bool inconclusive{};
79 
80  LifetimeToken() = default;
81 
83  : token(token), errorPath(std::move(errorPath))
84  {}
85 
88  {}
89 
90  static std::vector<LifetimeToken> setAddressOf(std::vector<LifetimeToken> v, bool b) {
91  for (LifetimeToken& x : v)
92  x.addressOf = b;
93  return v;
94  }
95 
96  static std::vector<LifetimeToken> setInconclusive(std::vector<LifetimeToken> v, bool b) {
97  for (LifetimeToken& x : v)
98  x.inconclusive = b;
99  return v;
100  }
101  };
102 
103  const Token *parseCompareInt(const Token *tok, Value &true_value, Value &false_value, const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate);
104  const Token *parseCompareInt(const Token *tok, Value &true_value, Value &false_value);
105 
107 
108  const Token* solveExprValue(const Token* expr,
109  const std::function<std::vector<MathLib::bigint>(const Token*)>& eval,
110  Value& value);
111 
112  std::vector<LifetimeToken> getLifetimeTokens(const Token* tok,
113  const Settings& settings,
114  bool escape = false,
115  Value::ErrorPath errorPath = Value::ErrorPath{});
116 
117  bool hasLifetimeToken(const Token* tok, const Token* lifetime, const Settings& settings);
118 
119  const Variable* getLifetimeVariable(const Token* tok, Value::ErrorPath& errorPath, const Settings& settings, bool* addressOf = nullptr);
120 
121  const Variable* getLifetimeVariable(const Token* tok, const Settings& settings);
122 
123  bool isLifetimeBorrowed(const Token *tok, const Settings &settings);
124 
125  std::string lifetimeMessage(const Token *tok, const Value *val, Value::ErrorPath &errorPath);
126 
127  CPPCHECKLIB Value getLifetimeObjValue(const Token *tok, bool inconclusive = false);
128 
129  CPPCHECKLIB std::vector<Value> getLifetimeObjValues(const Token* tok,
130  bool inconclusive = false,
131  MathLib::bigint path = 0);
132 
133  const Token* getEndOfExprScope(const Token* tok, const Scope* defaultScope = nullptr, bool smallest = true);
134 
135  void combineValueProperties(const Value& value1, const Value& value2, Value& result);
136 }
137 
138 #endif // valueflowH
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
long long bigint
Definition: mathlib.h:68
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
std::list< ErrorPathItem > ErrorPath
Definition: vfvalue.h:43
Value type.
Information about a member variable.
#define CPPCHECKLIB
Definition: config.h:35
const Token * parseCompareInt(const Token *tok, Value &true_value, Value &false_value, const std::function< std::vector< MathLib::bigint >(const Token *)> &evaluate)
Definition: valueflow.cpp:350
bool isContainerSizeChanged(const Token *tok, int indirect, const Settings &settings, int depth=20)
Definition: valueflow.cpp:8461
std::string eitherTheConditionIsRedundant(const Token *condition)
Definition: valueflow.cpp:9680
CPPCHECKLIB Value getLifetimeObjValue(const Token *tok, bool inconclusive=false)
Definition: valueflow.cpp:3575
const Value * valueFlowConstantFoldAST(Token *expr, const Settings &settings)
Constant folding of expression. This can be used before the full ValueFlow has been executed (ValueFl...
Definition: valueflow.cpp:9408
CPPCHECKLIB ValuePtr< InferModel > makeIntegralInferModel()
Definition: valueflow.cpp:6977
const Value * findValue(const std::list< Value > &values, const Settings &settings, const std::function< bool(const Value &)> &pred)
std::vector< LifetimeToken > getLifetimeTokens(const Token *tok, const Settings &settings, bool escape=false, Value::ErrorPath errorPath=Value::ErrorPath{})
Definition: valueflow.cpp:3735
std::string lifetimeMessage(const Token *tok, const Value *val, Value::ErrorPath &errorPath)
Definition: valueflow.cpp:3490
void combineValueProperties(const Value &value1, const Value &value2, Value &result)
Definition: valueflow.cpp:424
bool isLifetimeBorrowed(const Token *tok, const Settings &settings)
Definition: valueflow.cpp:3907
const Token * getEndOfExprScope(const Token *tok, const Scope *defaultScope=nullptr, bool smallest=true)
Definition: valueflow.cpp:3974
const Variable * getLifetimeVariable(const Token *tok, Value::ErrorPath &errorPath, const Settings &settings, bool *addressOf=nullptr)
Definition: valueflow.cpp:3765
bool hasLifetimeToken(const Token *tok, const Token *lifetime, const Settings &settings)
Definition: valueflow.cpp:3742
const Token * solveExprValue(const Token *expr, const std::function< std::vector< MathLib::bigint >(const Token *)> &eval, Value &value)
Definition: valueflow.cpp:8392
std::vector< Value > isOutOfBounds(const Value &size, const Token *indexTok, bool possible=true)
Definition: valueflow.cpp:9757
size_t getSizeOf(const ValueType &vt, const Settings &settings, int maxRecursion=0)
Definition: valueflow.cpp:1197
Value asImpossible(Value v)
Definition: valueflow.cpp:6269
CPPCHECKLIB std::vector< Value > getLifetimeObjValues(const Token *tok, bool inconclusive=false, MathLib::bigint path=0)
Definition: valueflow.cpp:3528
void setValues(TokenList &tokenlist, SymbolDatabase &symboldatabase, ErrorLogger &errorLogger, const Settings &settings, TimerResultsIntf *timerResults)
Perform valueflow analysis.
Definition: valueflow.cpp:9604
static ValueFlow::Value evaluate(const std::string &op, const ValueFlow::Value &lhs, const ValueFlow::Value &rhs)
LifetimeToken(const Token *token, bool addressOf, Value::ErrorPath errorPath)
Definition: valueflow.h:86
const Token * token
Definition: valueflow.h:75
Value::ErrorPath errorPath
Definition: valueflow.h:76
static std::vector< LifetimeToken > setInconclusive(std::vector< LifetimeToken > v, bool b)
Definition: valueflow.h:96
LifetimeToken(const Token *token, Value::ErrorPath errorPath)
Definition: valueflow.h:82
static std::vector< LifetimeToken > setAddressOf(std::vector< LifetimeToken > v, bool b)
Definition: valueflow.h:90