1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/* -*- C++ -*-
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 Cppcheck team.
*
* 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 GUARD_PROGRAMMEMORY_H
#define GUARD_PROGRAMMEMORY_H
#include "config.h"
#include "mathlib.h"
#include "vfvalue.h" // needed for alias
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
class Scope;
class Token;
class Settings;
// Class used to handle heterogeneous lookup in unordered_map(since we can't use C++20 yet)
struct ExprIdToken {
const Token* tok = nullptr;
nonneg int exprid = 0;
ExprIdToken() = default;
// cppcheck-suppress noExplicitConstructor
// NOLINTNEXTLINE(google-explicit-constructor)
ExprIdToken(const Token* tok);
// TODO: Make this constructor only available from ProgramMemory
// cppcheck-suppress noExplicitConstructor
// NOLINTNEXTLINE(google-explicit-constructor)
ExprIdToken(nonneg int exprid) : exprid(exprid) {}
nonneg int getExpressionId() const;
bool operator==(const ExprIdToken& rhs) const {
return getExpressionId() == rhs.getExpressionId();
}
bool operator<(const ExprIdToken& rhs) const {
return getExpressionId() < rhs.getExpressionId();
}
template<class T, class U>
friend bool operator!=(const T& lhs, const U& rhs)
{
return !(lhs == rhs);
}
template<class T, class U>
friend bool operator<=(const T& lhs, const U& rhs)
{
return !(lhs > rhs);
}
template<class T, class U>
friend bool operator>(const T& lhs, const U& rhs)
{
return rhs < lhs;
}
template<class T, class U>
friend bool operator>=(const T& lhs, const U& rhs)
{
return !(lhs < rhs);
}
const Token& operator*() const NOEXCEPT {
return *tok;
}
const Token* operator->() const NOEXCEPT {
return tok;
}
struct Hash {
std::size_t operator()(ExprIdToken etok) const;
};
};
struct CPPCHECKLIB ProgramMemory {
using Map = std::unordered_map<ExprIdToken, ValueFlow::Value, ExprIdToken::Hash>;
ProgramMemory() : mValues(new Map()) {}
explicit ProgramMemory(Map values) : mValues(new Map(std::move(values))) {}
void setValue(const Token* expr, const ValueFlow::Value& value);
const ValueFlow::Value* getValue(nonneg int exprid, bool impossible = false) const;
bool getIntValue(nonneg int exprid, MathLib::bigint& result) const;
void setIntValue(const Token* expr, MathLib::bigint value, bool impossible = false);
bool getContainerSizeValue(nonneg int exprid, MathLib::bigint& result) const;
bool getContainerEmptyValue(nonneg int exprid, MathLib::bigint& result) const;
void setContainerSizeValue(const Token* expr, MathLib::bigint value, bool isEqual = true);
void setUnknown(const Token* expr);
bool getTokValue(nonneg int exprid, const Token*& result) const;
bool hasValue(nonneg int exprid);
const ValueFlow::Value& at(nonneg int exprid) const;
ValueFlow::Value& at(nonneg int exprid);
void erase_if(const std::function<bool(const ExprIdToken&)>& pred);
void swap(ProgramMemory &pm) NOEXCEPT;
void clear();
bool empty() const;
void replace(ProgramMemory pm);
Map::const_iterator begin() const {
return mValues->cbegin();
}
Map::const_iterator end() const {
return mValues->cend();
}
friend bool operator==(const ProgramMemory& x, const ProgramMemory& y) {
return x.mValues == y.mValues;
}
friend bool operator!=(const ProgramMemory& x, const ProgramMemory& y) {
return x.mValues != y.mValues;
}
private:
void copyOnWrite();
std::shared_ptr<Map> mValues;
};
struct ProgramMemoryState {
ProgramMemory state;
std::map<nonneg int, const Token*> origins;
const Settings& settings;
explicit ProgramMemoryState(const Settings& s);
void replace(ProgramMemory pm, const Token* origin = nullptr);
void addState(const Token* tok, const ProgramMemory::Map& vars);
void assume(const Token* tok, bool b, bool isEmpty = false);
void removeModifiedVars(const Token* tok);
ProgramMemory get(const Token* tok, const Token* ctx, const ProgramMemory::Map& vars) const;
};
std::vector<ValueFlow::Value> execute(const Scope* scope, ProgramMemory& pm, const Settings& settings);
void execute(const Token* expr,
ProgramMemory& programMemory,
MathLib::bigint* result,
bool* error,
const Settings& settings);
/**
* Is condition always false when variable has given value?
* \param condition top ast token in condition
* \param pm program memory
*/
bool conditionIsFalse(const Token* condition, ProgramMemory pm, const Settings& settings);
/**
* Is condition always true when variable has given value?
* \param condition top ast token in condition
* \param pm program memory
*/
bool conditionIsTrue(const Token* condition, ProgramMemory pm, const Settings& settings);
/**
* Get program memory by looking backwards from given token.
*/
ProgramMemory getProgramMemory(const Token* tok, const Token* expr, const ValueFlow::Value& value, const Settings& settings);
ValueFlow::Value evaluateLibraryFunction(const std::unordered_map<nonneg int, ValueFlow::Value>& args,
const std::string& returnValue,
const Settings& settings,
bool cpp);
#endif
|