Refactor for later library use

This commit is contained in:
Erki 2021-02-28 22:09:23 +02:00
parent b609c48bfe
commit 0e7cf93c9b
6 changed files with 83 additions and 56 deletions

View File

@ -63,9 +63,11 @@ std::tuple<std::string, unsigned int, unsigned int> resolveLocations(const TOp*
}
OperationFinder::OperationFinder(const std::string& output_dir)
: _storage(output_dir)
{ }
OperationFinder::OperationFinder(IOperationOutput* storage)
: _storage(storage)
{
assert(storage);
}
void OperationFinder::addMatcher(MatchFinder &finder)
{
@ -103,13 +105,13 @@ void OperationFinder::_processAssignment(const clang::ast_matchers::MatchFinder:
<< column_number << ":"
<< "Assignment: Assigned to: ";
OperationStorage::Log log;
OperationLog log;
log.line = line_number;
log.operation = "=";
_processExpressionTypes(log, op, op->getLHS(), op->getRHS());
_storage.pushOperation(file_name, log);
_storage->pushOperation(file_name, log);
llvm::outs() << "\n";
}
@ -127,13 +129,13 @@ void OperationFinder::_processArithmetic(const MatchFinder::MatchResult& result)
<< column_number << ":"
<< "Binary arithmetic: Type: " << getOpcode(op) << " LHS: ";
OperationStorage::Log log;
OperationLog log;
log.line = line_number;
log.operation = getOpcode(op).str();
_processExpressionTypes(log, op, op->getLHS(), op->getRHS());
_storage.pushOperation(file_name, log);
_storage->pushOperation(file_name, log);
llvm::outs() << "\n";
}
@ -153,18 +155,18 @@ void OperationFinder::_processUnaryArithmetic(const MatchFinder::MatchResult &re
<< column_number << ":"
<< "Unary arithmetic: Type: " << getOpcode(op) << " LHS: ";
OperationStorage::Log log;
OperationLog log;
log.line = line_number;
log.operation = getOpcode(op).str();
_processExpressionTypes(log, op, lhs, nullptr);
_storage.pushOperation(file_name, log);
_storage->pushOperation(file_name, log);
llvm::outs() << "\n";
}
void OperationFinder::_processExpressionTypes(OperationStorage::Log& log, const Expr* source, const Expr* op1, const Expr* op2)
void OperationFinder::_processExpressionTypes(OperationLog& log, const Expr* source, const Expr* op1, const Expr* op2)
{
auto printTypeName = [](const QualType& t) -> std::string

View File

@ -8,12 +8,12 @@
#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include "OperationStorage.hpp"
#include "OperationLog.hpp"
class OperationFinder : public clang::ast_matchers::MatchFinder::MatchCallback
{
public:
OperationFinder(const std::string& output_dir);
explicit OperationFinder(IOperationOutput* storage);
void addMatcher(clang::ast_matchers::MatchFinder& finder);
@ -23,9 +23,9 @@ private:
void _processArithmetic(const clang::ast_matchers::MatchFinder::MatchResult& result);
void _processUnaryArithmetic(const clang::ast_matchers::MatchFinder::MatchResult& result);
void _processExpressionTypes(OperationStorage::Log& log, const clang::Expr* source, const clang::Expr* op1, const clang::Expr* op2);
void _processExpressionTypes(OperationLog& log, const clang::Expr* source, const clang::Expr* op1, const clang::Expr* op2);
OperationStorage _storage;
IOperationOutput* _storage;
};
#endif //LLVM_PROTO_OPERATIONFINDER_HPP

View File

@ -0,0 +1,47 @@
//
// Created by erki on 28.02.21.
//
#ifndef C_ANALYZER_OPERATIONLOG_HPP
#define C_ANALYZER_OPERATIONLOG_HPP
struct OperationLog
{
std::string operation;
unsigned int line;
std::string operand_lhs;
std::string operand_rhs;
std::string operand_result;
};
class IOperationOutput
{
public:
virtual ~IOperationOutput() = default;
virtual void pushOperation(const std::string& filename, const OperationLog& op) = 0;
};
#include <nlohmann/json.hpp>
inline void to_json(nlohmann::json& j, const OperationLog& l)
{
j = nlohmann::json{
{"operation", l.operation},
{"line", l.line},
{"operand_lhs", l.operand_lhs},
{"operand_rhs", l.operand_rhs},
{"operand_result", l.operand_result}
};
}
inline void from_json(const nlohmann::json& j, OperationLog& l)
{
j.at("operation").get_to(l.operation);
j.at("line").get_to(l.line);
j.at("operand_lhs").get_to(l.operand_lhs);
j.at("operand_rhs").get_to(l.operand_rhs);
j.at("operand_result").get_to(l.operand_result);
}
#endif //C_ANALYZER_OPERATIONLOG_HPP

View File

@ -8,27 +8,6 @@
#include <filesystem>
#include <llvm/Support/CommandLine.h>
#include <nlohmann/json.hpp>
void to_json(nlohmann::json& j, const OperationStorage::Log& l)
{
j = nlohmann::json{
{"operation", l.operation},
{"line", l.line},
{"operand_lhs", l.operand_lhs},
{"operand_rhs", l.operand_rhs},
{"operand_result", l.operand_result}
};
}
void from_json(const nlohmann::json& j, OperationStorage::Log& l)
{
j.at("operation").get_to(l.operation);
j.at("line").get_to(l.line);
j.at("operand_lhs").get_to(l.operand_lhs);
j.at("operand_rhs").get_to(l.operand_rhs);
j.at("operand_result").get_to(l.operand_result);
}
OperationStorage::OperationStorage(const std::string& output_filename)
: _output_filename(output_filename)
@ -39,7 +18,7 @@ OperationStorage::~OperationStorage()
_dumpToFile();
}
void OperationStorage::pushOperation(const std::string& filename, const Log& op)
void OperationStorage::pushOperation(const std::string& filename, const OperationLog& op)
{
auto it = _operations.find(filename);
@ -49,7 +28,7 @@ void OperationStorage::pushOperation(const std::string& filename, const Log& op)
it->second.push_back(op);
}
const std::unordered_map<std::string, std::vector<OperationStorage::Log>>& OperationStorage::getOperations() const
const std::unordered_map<std::string, std::vector<OperationLog>>& OperationStorage::getOperations() const
{
return _operations;
}

View File

@ -9,28 +9,21 @@
#include <unordered_map>
#include <vector>
class OperationStorage
#include "OperationLog.hpp"
class OperationStorage : public IOperationOutput
{
public:
struct Log
{
std::string operation;
unsigned int line;
std::string operand_lhs;
std::string operand_rhs;
std::string operand_result;
};
OperationStorage() = delete;
OperationStorage(const OperationStorage&) = delete;
explicit OperationStorage(const std::string& output_filename);
~OperationStorage();
~OperationStorage() override;
void pushOperation(const std::string& filename, const Log& op);
[[nodiscard]] const std::unordered_map<std::string, std::vector<Log>>& getOperations() const;
void pushOperation(const std::string& filename, const OperationLog& op) override;
[[nodiscard]] const std::unordered_map<std::string, std::vector<OperationLog>>& getOperations() const;
private:
std::unordered_map<std::string, std::vector<Log>> _operations;
std::unordered_map<std::string, std::vector<OperationLog>> _operations;
std::string _output_filename;
void _dumpToFile();

View File

@ -6,6 +6,7 @@
#include "llvm/Support/CommandLine.h"
#include "OperationFinder.hpp"
#include "OperationStorage.hpp"
using namespace clang::tooling;
using namespace clang::ast_matchers;
@ -13,18 +14,21 @@ using namespace llvm;
// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static llvm::cl::OptionCategory MyToolCategory("op-finder options");
static cl::OptionCategory MyToolCategory("op-finder options");
static llvm::cl::opt<std::string> OutputFile("o", cl::desc("File to output the JSON to."),
static cl::opt<std::string> OutputFile("o", cl::desc("File to output the JSON to."),
cl::cat(MyToolCategory));
static cl::opt<std::string> RootDirectory("r", cl::desc("The root directory of the source files."),
cl::cat(MyToolCategory));
// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
// A help message for this specific tool can be added afterwards.
static llvm::cl::extrahelp MoreHelp("\nThe program takes the input <source0> ... files, parses their\n"
static cl::extrahelp MoreHelp("\nThe program takes the input <source0> ... files, parses their\n"
"AST and outputs a singular file containing a list of all noteworthy operations\n"
"for later analysis.\n");
@ -42,7 +46,9 @@ int main(int argc, const char** argv)
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
OperationFinder op_finder(OutputFile.getValue());
OperationStorage storage(OutputFile.getValue());
OperationFinder op_finder(&storage);
MatchFinder finder;
op_finder.addMatcher(finder);