2022-01-26 19:02:20 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 Cppcheck team.
|
2022-01-26 19:02:20 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-10-30 17:57:46 +01:00
|
|
|
#ifndef GUARD_PROGRAMMEMORY_H
|
|
|
|
#define GUARD_PROGRAMMEMORY_H
|
|
|
|
|
2022-02-11 19:44:08 +01:00
|
|
|
#include "config.h"
|
2021-07-24 22:44:18 +02:00
|
|
|
#include "mathlib.h"
|
2023-01-26 22:23:22 +01:00
|
|
|
#include "vfvalue.h" // needed for alias
|
2022-02-11 19:44:08 +01:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <functional>
|
2019-10-30 17:57:46 +01:00
|
|
|
#include <map>
|
2022-04-13 12:24:00 +02:00
|
|
|
#include <string>
|
2020-02-16 16:02:22 +01:00
|
|
|
#include <unordered_map>
|
2022-09-16 18:59:15 +02:00
|
|
|
#include <utility>
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2020-04-13 13:44:48 +02:00
|
|
|
class Token;
|
2021-12-15 19:47:27 +01:00
|
|
|
class Settings;
|
2020-04-13 13:44:48 +02:00
|
|
|
|
2022-02-06 20:15:45 +01:00
|
|
|
// 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
|
2022-10-16 13:51:17 +02:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-06 20:15:45 +01:00
|
|
|
ExprIdToken(const Token* tok) : tok(tok) {}
|
|
|
|
// TODO: Make this constructor only available from ProgramMemory
|
|
|
|
// cppcheck-suppress noExplicitConstructor
|
2022-10-16 13:51:17 +02:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-06 20:15:45 +01:00
|
|
|
ExprIdToken(nonneg int exprid) : exprid(exprid) {}
|
|
|
|
|
|
|
|
nonneg int getExpressionId() const;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Hash {
|
|
|
|
std::size_t operator()(ExprIdToken etok) const;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-10-30 17:57:46 +01:00
|
|
|
struct ProgramMemory {
|
2022-02-06 20:15:45 +01:00
|
|
|
using Map = std::unordered_map<ExprIdToken, ValueFlow::Value, ExprIdToken::Hash>;
|
|
|
|
|
|
|
|
ProgramMemory() = default;
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2022-09-16 18:59:15 +02:00
|
|
|
explicit ProgramMemory(Map values) : mValues(std::move(values)) {}
|
2022-02-06 20:15:45 +01:00
|
|
|
|
|
|
|
void setValue(const Token* expr, 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;
|
2022-02-06 20:15:45 +01:00
|
|
|
void setIntValue(const Token* expr, MathLib::bigint value, bool impossible = false);
|
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;
|
2022-02-06 20:15:45 +01:00
|
|
|
void setContainerSizeValue(const Token* expr, MathLib::bigint value, bool isEqual = true);
|
2020-08-11 03:08:49 +02:00
|
|
|
|
2022-02-06 20:15:45 +01:00
|
|
|
void setUnknown(const Token* expr);
|
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
|
|
|
|
2022-02-06 20:15:45 +01:00
|
|
|
const ValueFlow::Value& at(nonneg int exprid) const;
|
|
|
|
ValueFlow::Value& at(nonneg int exprid);
|
|
|
|
|
|
|
|
void erase_if(const std::function<bool(const ExprIdToken&)>& pred);
|
|
|
|
|
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);
|
2022-02-06 20:15:45 +01:00
|
|
|
|
|
|
|
Map::iterator begin() {
|
|
|
|
return mValues.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Map::iterator end() {
|
|
|
|
return mValues.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
Map::const_iterator begin() const {
|
|
|
|
return mValues.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Map::const_iterator end() const {
|
|
|
|
return mValues.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Map mValues;
|
2019-10-30 17:57:46 +01:00
|
|
|
};
|
|
|
|
|
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-12-20 07:28:40 +01:00
|
|
|
void execute(const Token* expr,
|
|
|
|
ProgramMemory* const programMemory,
|
|
|
|
MathLib::bigint* result,
|
|
|
|
bool* error,
|
|
|
|
const Settings* settings = 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
|
2022-03-13 20:07:58 +01:00
|
|
|
* \param pm program memory
|
2019-10-30 17:57:46 +01:00
|
|
|
*/
|
2021-12-20 07:28:40 +01:00
|
|
|
bool conditionIsFalse(const Token* condition, ProgramMemory pm, const Settings* settings = nullptr);
|
2019-10-30 17:57:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is condition always true when variable has given value?
|
|
|
|
* \param condition top ast token in condition
|
2022-03-13 20:07:58 +01:00
|
|
|
* \param pm program memory
|
2019-10-30 17:57:46 +01:00
|
|
|
*/
|
2021-12-20 07:28:40 +01:00
|
|
|
bool conditionIsTrue(const Token* condition, ProgramMemory pm, const Settings* settings = nullptr);
|
2019-10-30 17:57:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get program memory by looking backwards from given token.
|
|
|
|
*/
|
2022-02-06 20:15:45 +01:00
|
|
|
ProgramMemory getProgramMemory(const Token* tok, const Token* expr, 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
|
|
|
|
2022-04-06 06:35:38 +02:00
|
|
|
ValueFlow::Value evaluateLibraryFunction(const std::unordered_map<nonneg int, ValueFlow::Value>& args,
|
|
|
|
const std::string& returnValue,
|
|
|
|
const Settings* settings);
|
|
|
|
|
2019-10-30 17:57:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|