masters-thesis/op-finder/OperationLog.hpp
Erki 83ca2deb0e Better branch detection model
For loops are now broken up into 2 branches: 1 (init) and 2 (cond + inc).
2021-03-07 13:02:14 +02:00

115 lines
2.6 KiB
C++

//
// Created by erki on 28.02.21.
//
#ifndef C_ANALYZER_OPERATIONLOG_HPP
#define C_ANALYZER_OPERATIONLOG_HPP
#include <vector>
#include <variant>
#include <nlohmann/json.hpp>
struct OperationLog
{
struct IEntry
{
[[nodiscard]] virtual nlohmann::json toJson() const = 0;
};
struct FunctionCall : IEntry
{
static constexpr char TYPE_NAME[] = "function_call";
std::string function_name;
std::string call_result_type;
[[nodiscard]] nlohmann::json toJson() const override;
};
struct BasicOperation : IEntry
{
static constexpr char TYPE_NAME[] = "basic_operation";
std::string operation_name;
std::string type_lhs;
std::string type_rhs;
std::string type_result;
[[nodiscard]] nlohmann::json toJson() const override;
};
unsigned int line = 0;
int branch_number = 0;
std::string entry_type;
std::unique_ptr<IEntry> entry;
OperationLog() = default;
OperationLog(const OperationLog&) = delete;
OperationLog(OperationLog&&) = default;
void DecodeEntry(const nlohmann::json& j);
};
class IOperationOutput
{
public:
virtual ~IOperationOutput() = default;
virtual void pushOperation(const std::string& filename, OperationLog&& op) = 0;
};
inline void to_json(nlohmann::json& j, const OperationLog& l)
{
j = nlohmann::json{
{"line", l.line},
{"entry_type", l.entry_type},
{"entry", l.entry->toJson()},
{"branch_number", l.branch_number}
};
}
inline void from_json(const nlohmann::json& j, OperationLog& l)
{
j.at("line").get_to(l.line);
j.at("entry_type").get_to(l.entry_type);
l.DecodeEntry(j["entry"]);
j.at("branch_number").get_to(l.branch_number);
}
inline void to_json(nlohmann::json& j, const OperationLog::BasicOperation& bo)
{
j = nlohmann::json{
{"operation_name", bo.operation_name},
{"type_lhs", bo.type_lhs},
{"type_rhs", bo.type_rhs},
{"type_result", bo.type_result}
};
}
inline void from_json(const nlohmann::json& j, OperationLog::BasicOperation& bo)
{
j.at("operation_name").get_to(bo.operation_name);
j.at("type_lhs").get_to(bo.type_lhs);
j.at("type_rhs").get_to(bo.type_rhs);
j.at("type_result").get_to(bo.type_result);
}
inline void to_json(nlohmann::json& j, const OperationLog::FunctionCall& fcall)
{
j = nlohmann::json{
{"function_name", fcall.function_name},
{"call_result_type", fcall.call_result_type}
};
}
inline void from_json(const nlohmann::json& j, OperationLog::FunctionCall& fcall)
{
j.at("function_name").get_to(fcall.function_name);
j.at("call_result_type").get_to(fcall.call_result_type);
}
#endif //C_ANALYZER_OPERATIONLOG_HPP