2019-10-30 17:57:46 +01:00
|
|
|
#ifndef GUARD_PROGRAMMEMORY_H
|
|
|
|
#define GUARD_PROGRAMMEMORY_H
|
|
|
|
|
2021-07-24 22:44:18 +02:00
|
|
|
#include "mathlib.h"
|
2021-08-23 09:03:48 +02:00
|
|
|
#include "settings.h"
|
2019-10-30 17:57:46 +01:00
|
|
|
#include "utils.h"
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "valueflow.h" // needed for alias
|
2021-07-24 22:44:18 +02:00
|
|
|
#include <functional>
|
2019-10-30 17:57:46 +01:00
|
|
|
#include <map>
|
2020-02-16 16:02:22 +01:00
|
|
|
#include <unordered_map>
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2020-04-13 13:44:48 +02:00
|
|
|
class Token;
|
|
|
|
|
2019-10-30 17:57:46 +01:00
|
|
|
struct ProgramMemory {
|
2021-01-27 19:49:13 +01:00
|
|
|
using Map = std::unordered_map<nonneg int, ValueFlow::Value>;
|
2020-02-19 07:55:04 +01:00
|
|
|
Map values;
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
void setValue(nonneg int exprid, const ValueFlow::Value& value);
|
2021-01-28 12:37:56 +01:00
|
|
|
const ValueFlow::Value* getValue(nonneg int exprid, bool impossible = false) const;
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
bool getIntValue(nonneg int exprid, MathLib::bigint* result) const;
|
|
|
|
void setIntValue(nonneg int exprid, MathLib::bigint value);
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
bool getContainerSizeValue(nonneg int exprid, MathLib::bigint* result) const;
|
2021-01-28 12:37:56 +01:00
|
|
|
bool getContainerEmptyValue(nonneg int exprid, MathLib::bigint* result) const;
|
2021-07-16 18:49:07 +02:00
|
|
|
void setContainerSizeValue(nonneg int exprid, MathLib::bigint value, bool isEqual = true);
|
2020-08-11 03:08:49 +02:00
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
void setUnknown(nonneg int exprid);
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
bool getTokValue(nonneg int exprid, const Token** result) const;
|
|
|
|
bool hasValue(nonneg int exprid);
|
2019-10-30 17:57:46 +01:00
|
|
|
|
|
|
|
void swap(ProgramMemory &pm);
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
bool empty() const;
|
|
|
|
|
|
|
|
void replace(const ProgramMemory &pm);
|
|
|
|
|
|
|
|
void insert(const ProgramMemory &pm);
|
|
|
|
};
|
|
|
|
|
2021-01-11 08:00:13 +01:00
|
|
|
void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, const Token* endTok, const Settings* settings, bool then);
|
|
|
|
|
2020-02-19 07:55:04 +01:00
|
|
|
struct ProgramMemoryState {
|
|
|
|
ProgramMemory state;
|
2021-01-27 19:49:13 +01:00
|
|
|
std::map<nonneg int, const Token*> origins;
|
2021-08-23 09:03:48 +02:00
|
|
|
const Settings* settings;
|
|
|
|
|
|
|
|
explicit ProgramMemoryState(const Settings* s);
|
2020-02-19 07:55:04 +01:00
|
|
|
|
|
|
|
void insert(const ProgramMemory &pm, const Token* origin = nullptr);
|
|
|
|
void replace(const ProgramMemory &pm, const Token* origin = nullptr);
|
|
|
|
|
|
|
|
void addState(const Token* tok, const ProgramMemory::Map& vars);
|
|
|
|
|
2021-07-16 18:49:07 +02:00
|
|
|
void assume(const Token* tok, bool b, bool isEmpty = false);
|
2020-02-19 07:55:04 +01:00
|
|
|
|
|
|
|
void removeModifiedVars(const Token* tok);
|
|
|
|
|
2021-06-09 09:20:43 +02:00
|
|
|
ProgramMemory get(const Token* tok, const Token* ctx, const ProgramMemory::Map& vars) const;
|
2020-02-19 07:55:04 +01:00
|
|
|
};
|
|
|
|
|
2021-07-24 22:44:18 +02:00
|
|
|
using PMEvaluateFunction =
|
2021-08-07 20:51:18 +02:00
|
|
|
std::function<bool (const Token* expr, ProgramMemory* const programMemory, MathLib::bigint* result)>;
|
2021-07-24 22:44:18 +02:00
|
|
|
|
|
|
|
void execute(const Token* expr,
|
|
|
|
ProgramMemory* const programMemory,
|
|
|
|
MathLib::bigint* result,
|
|
|
|
bool* error,
|
|
|
|
const PMEvaluateFunction& f = nullptr);
|
2019-10-30 17:57:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is condition always false when variable has given value?
|
|
|
|
* \param condition top ast token in condition
|
|
|
|
* \param programMemory program memory
|
|
|
|
*/
|
|
|
|
bool conditionIsFalse(const Token *condition, const ProgramMemory &programMemory);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is condition always true when variable has given value?
|
|
|
|
* \param condition top ast token in condition
|
|
|
|
* \param programMemory program memory
|
|
|
|
*/
|
|
|
|
bool conditionIsTrue(const Token *condition, const ProgramMemory &programMemory);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get program memory by looking backwards from given token.
|
|
|
|
*/
|
2021-08-25 06:56:19 +02:00
|
|
|
ProgramMemory getProgramMemory(const Token* tok, nonneg int exprid, const ValueFlow::Value& value, const Settings *settings);
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2020-02-19 07:55:04 +01:00
|
|
|
ProgramMemory getProgramMemory(const Token *tok, const ProgramMemory::Map& vars);
|
2020-02-16 16:02:22 +01:00
|
|
|
|
2019-10-30 17:57:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|