From 0e7cf93c9b9e2f7240e2ae1e7eb9fe54e9c25d5e Mon Sep 17 00:00:00 2001 From: Erki Date: Sun, 28 Feb 2021 22:09:23 +0200 Subject: [PATCH] Refactor for later library use --- op-finder/OperationFinder.cpp | 22 ++++++++-------- op-finder/OperationFinder.hpp | 8 +++--- op-finder/OperationLog.hpp | 47 ++++++++++++++++++++++++++++++++++ op-finder/OperationStorage.cpp | 25 ++---------------- op-finder/OperationStorage.hpp | 21 +++++---------- op-finder/main.cpp | 16 ++++++++---- 6 files changed, 83 insertions(+), 56 deletions(-) create mode 100644 op-finder/OperationLog.hpp diff --git a/op-finder/OperationFinder.cpp b/op-finder/OperationFinder.cpp index 1150d50..8f1c739 100644 --- a/op-finder/OperationFinder.cpp +++ b/op-finder/OperationFinder.cpp @@ -63,9 +63,11 @@ std::tuple 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 diff --git a/op-finder/OperationFinder.hpp b/op-finder/OperationFinder.hpp index b3cb68b..36a9188 100644 --- a/op-finder/OperationFinder.hpp +++ b/op-finder/OperationFinder.hpp @@ -8,12 +8,12 @@ #include #include -#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 diff --git a/op-finder/OperationLog.hpp b/op-finder/OperationLog.hpp new file mode 100644 index 0000000..4642ca9 --- /dev/null +++ b/op-finder/OperationLog.hpp @@ -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 + +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 diff --git a/op-finder/OperationStorage.cpp b/op-finder/OperationStorage.cpp index ef74dfc..dd16a43 100644 --- a/op-finder/OperationStorage.cpp +++ b/op-finder/OperationStorage.cpp @@ -8,27 +8,6 @@ #include #include -#include - -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>& OperationStorage::getOperations() const +const std::unordered_map>& OperationStorage::getOperations() const { return _operations; } diff --git a/op-finder/OperationStorage.hpp b/op-finder/OperationStorage.hpp index 0b0e7ef..809bd1a 100644 --- a/op-finder/OperationStorage.hpp +++ b/op-finder/OperationStorage.hpp @@ -9,28 +9,21 @@ #include #include -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>& getOperations() const; + void pushOperation(const std::string& filename, const OperationLog& op) override; + [[nodiscard]] const std::unordered_map>& getOperations() const; private: - std::unordered_map> _operations; + std::unordered_map> _operations; std::string _output_filename; void _dumpToFile(); diff --git a/op-finder/main.cpp b/op-finder/main.cpp index c9cb36d..7bb018d 100644 --- a/op-finder/main.cpp +++ b/op-finder/main.cpp @@ -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 OutputFile("o", cl::desc("File to output the JSON to."), +static cl::opt OutputFile("o", cl::desc("File to output the JSON to."), cl::cat(MyToolCategory)); +static cl::opt 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 ... files, parses their\n" +static cl::extrahelp MoreHelp("\nThe program takes the input ... 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);