Unit test framework integration

Along with conan
This commit is contained in:
Erki 2021-03-07 14:31:43 +02:00
parent 53c24c6d5e
commit 7b294045bf
11 changed files with 100 additions and 25475 deletions

View File

@ -1,9 +1,16 @@
cmake_minimum_required(VERSION 3.17)
project("C Analyzer")
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter")
option(WITH_TESTS "Build unit tests as well." OFF)
add_subdirectory(op-finder-lib)
add_subdirectory(op-finder)
if(WITH_TESTS)
enable_testing()
add_subdirectory(op-finder-tests)
endif()

View File

@ -1,2 +1,13 @@
# masters-thesis
## Building
Conan is recommended. Otherwise you have to provide `Catch2_ROOT`
and `nlohmann_json_ROOT` yourself.
```shell
> mkdir build
> cd build
> conan install .. --build=missing
> cmake .. -DWITH_TESTS=ON -DClang_ROOT=${CLANG_ROOT} -DLLVM_ROOT=${LLVM_ROOT}
```

6
conanfile.txt Normal file
View File

@ -0,0 +1,6 @@
[requires]
catch2/2.13.4
nlohmann_json/3.9.1
[generators]
cmake_find_package

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.17)
find_package(nlohmann_json REQUIRED)
find_package(Clang REQUIRED)
find_package(LLVM REQUIRED COMPONENTS Support Option Core)
@ -29,7 +30,6 @@ add_library(op-finder-lib STATIC
target_include_directories(op-finder-lib
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/dependencies
${LLVM_INCLUDE_DIRS}
)
@ -39,6 +39,8 @@ target_compile_definitions(op-finder-lib
)
target_link_libraries(op-finder-lib
PUBLIC
nlohmann_json::nlohmann_json
PRIVATE
${llvm_libs}
clangTooling

View File

@ -14,22 +14,21 @@
class OperationStorage : public IOperationOutput
{
public:
OperationStorage() = delete;
OperationStorage() = default;
OperationStorage(const OperationStorage&) = delete;
explicit OperationStorage(const std::string& output_filename);
~OperationStorage() override;
void enablePrettyPrint();
void toStream(std::ostream& stream);
void toFile(const std::string& output_filename);
void pushOperation(const std::string& filename, OperationLog&& op) override;
[[nodiscard]] const std::unordered_map<std::string, std::vector<OperationLog>>& getOperations() const;
private:
std::unordered_map<std::string, std::vector<OperationLog>> _operations;
std::string _output_filename;
bool _pretty_print = false;
void _dumpToFile();
};
#endif //C_ANALYZER_OPERATIONSTORAGE_HPP

View File

@ -9,20 +9,32 @@
#include <llvm/Support/CommandLine.h>
OperationStorage::OperationStorage(const std::string& output_filename)
: _output_filename(output_filename)
{ }
OperationStorage::~OperationStorage()
{
_dumpToFile();
}
{ }
void OperationStorage::enablePrettyPrint()
{
_pretty_print = true;
}
void OperationStorage::toStream(std::ostream& stream)
{
nlohmann::json json = _operations;
if (_pretty_print)
stream << std::setw(4) << json;
else
stream << json;
}
void OperationStorage::toFile(const std::string& output_filename)
{
std::ofstream file(output_filename);
toStream(file);
}
void OperationStorage::pushOperation(const std::string& filename, OperationLog&& op)
{
auto it = _operations.find(filename);
@ -37,15 +49,3 @@ const std::unordered_map<std::string, std::vector<OperationLog>>& OperationStora
{
return _operations;
}
void OperationStorage::_dumpToFile()
{
nlohmann::json json = _operations;
std::ofstream file(_output_filename);
if (_pretty_print)
file << std::setw(4) << json;
else
file << json;
}

View File

@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.17)
find_package(Catch2 REQUIRED)
# LLVM is typically compiled without RTTI. Weird linker errors ensue if
# you keep RTTI on and try to link.
if (NOT LLVM_ENABLE_RTTI)
if (MSVC)
string(REGEX REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
else ()
string(REGEX REPLACE "-frtti" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
endif ()
endif ()
add_executable(op-finder-tests
main.cpp basic_operations.cpp)
target_link_libraries(op-finder-tests
PUBLIC
op-finder-lib
Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(op-finder-tests)

View File

@ -0,0 +1,7 @@
//
// Created by erki on 07.03.21.
//
#include <catch2/catch.hpp>

6
op-finder-tests/main.cpp Normal file
View File

@ -0,0 +1,6 @@
//
// Created by erki on 07.03.21.
//
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include <catch2/catch.hpp>

View File

@ -5,6 +5,8 @@
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include <iostream>
#include "OperationAstMatcher.hpp"
#include "OperationFinder.hpp"
#include "OperationStorage.hpp"
@ -50,7 +52,7 @@ int main(int argc, const char** argv)
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
OperationStorage storage(OutputFile.getValue());
OperationStorage storage;
if (PrettyPrint.getValue())
storage.enablePrettyPrint();
@ -70,5 +72,10 @@ int main(int argc, const char** argv)
OperationFinderAstAction action(&op_finder);
return Tool.run(newFrontendActionFactory(&action).get());
assert(!Tool.run(newFrontendActionFactory(&action).get()));
if (!OutputFile.getValue().empty())
storage.toFile(OutputFile.getValue());
else
storage.toStream(std::cout);
}