/src/cppcheck/lib/valueflow.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- C++ -*- |
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 "errortypes.h" |
26 | | #include "mathlib.h" |
27 | | #include "vfvalue.h" |
28 | | |
29 | | #include <cstdlib> |
30 | | #include <functional> |
31 | | #include <list> |
32 | | #include <string> |
33 | | #include <utility> |
34 | | #include <vector> |
35 | | |
36 | | class ErrorLogger; |
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 | | namespace ValueFlow { |
47 | | /// Constant folding of expression. This can be used before the full ValueFlow has been executed (ValueFlow::setValues). |
48 | | const Value * valueFlowConstantFoldAST(Token *expr, const Settings &settings); |
49 | | |
50 | | /// Perform valueflow analysis. |
51 | | void setValues(TokenList& tokenlist, |
52 | | SymbolDatabase& symboldatabase, |
53 | | ErrorLogger& errorLogger, |
54 | | const Settings& settings, |
55 | | TimerResultsIntf* timerResults); |
56 | | |
57 | | std::string eitherTheConditionIsRedundant(const Token *condition); |
58 | | |
59 | | size_t getSizeOf(const ValueType &vt, const Settings &settings, int maxRecursion = 0); |
60 | | |
61 | | const Value* findValue(const std::list<Value>& values, |
62 | | const Settings& settings, |
63 | | const std::function<bool(const Value&)> &pred); |
64 | | |
65 | | std::vector<Value> isOutOfBounds(const Value& size, const Token* indexTok, bool possible = true); |
66 | | |
67 | | Value asImpossible(Value v); |
68 | | |
69 | | bool isContainerSizeChanged(const Token* tok, int indirect, const Settings& settings, int depth = 20); |
70 | | |
71 | | struct LifetimeToken { |
72 | | const Token* token{}; |
73 | | ErrorPath errorPath; |
74 | | bool addressOf{}; |
75 | | bool inconclusive{}; |
76 | | |
77 | | LifetimeToken() = default; |
78 | | |
79 | | LifetimeToken(const Token* token, ErrorPath errorPath) |
80 | 11.2k | : token(token), errorPath(std::move(errorPath)) |
81 | 11.2k | {} |
82 | | |
83 | | LifetimeToken(const Token* token, bool addressOf, ErrorPath errorPath) |
84 | 0 | : token(token), errorPath(std::move(errorPath)), addressOf(addressOf) |
85 | 0 | {} |
86 | | |
87 | 0 | static std::vector<LifetimeToken> setAddressOf(std::vector<LifetimeToken> v, bool b) { |
88 | 0 | for (LifetimeToken& x : v) |
89 | 0 | x.addressOf = b; |
90 | 0 | return v; |
91 | 0 | } |
92 | | |
93 | 0 | static std::vector<LifetimeToken> setInconclusive(std::vector<LifetimeToken> v, bool b) { |
94 | 0 | for (LifetimeToken& x : v) |
95 | 0 | x.inconclusive = b; |
96 | 0 | return v; |
97 | 0 | } |
98 | | }; |
99 | | |
100 | | const Token *parseCompareInt(const Token *tok, Value &true_value, Value &false_value, const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate); |
101 | | const Token *parseCompareInt(const Token *tok, Value &true_value, Value &false_value); |
102 | | |
103 | | const Token* solveExprValue(const Token* expr, |
104 | | const std::function<std::vector<MathLib::bigint>(const Token*)>& eval, |
105 | | Value& value); |
106 | | |
107 | | std::vector<LifetimeToken> getLifetimeTokens(const Token* tok, |
108 | | const Settings& settings, |
109 | | bool escape = false, |
110 | | ErrorPath errorPath = ErrorPath{}); |
111 | | |
112 | | bool hasLifetimeToken(const Token* tok, const Token* lifetime, const Settings& settings); |
113 | | |
114 | | const Variable* getLifetimeVariable(const Token* tok, ErrorPath& errorPath, const Settings& settings, bool* addressOf = nullptr); |
115 | | |
116 | | const Variable* getLifetimeVariable(const Token* tok, const Settings& settings); |
117 | | |
118 | | bool isLifetimeBorrowed(const Token *tok, const Settings &settings); |
119 | | |
120 | | std::string lifetimeMessage(const Token *tok, const Value *val, ErrorPath &errorPath); |
121 | | |
122 | | CPPCHECKLIB Value getLifetimeObjValue(const Token *tok, bool inconclusive = false); |
123 | | |
124 | | CPPCHECKLIB std::vector<Value> getLifetimeObjValues(const Token* tok, |
125 | | bool inconclusive = false, |
126 | | MathLib::bigint path = 0); |
127 | | |
128 | | const Token* getEndOfExprScope(const Token* tok, const Scope* defaultScope = nullptr, bool smallest = true); |
129 | | |
130 | | void combineValueProperties(const Value& value1, const Value& value2, Value& result); |
131 | | } |
132 | | |
133 | | #endif // valueflowH |