Unit test framework integration
Along with conan
This commit is contained in:
parent
53c24c6d5e
commit
7b294045bf
@ -1,9 +1,16 @@
|
|||||||
cmake_minimum_required(VERSION 3.17)
|
cmake_minimum_required(VERSION 3.17)
|
||||||
project("C Analyzer")
|
project("C Analyzer")
|
||||||
|
|
||||||
|
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter")
|
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-lib)
|
||||||
add_subdirectory(op-finder)
|
add_subdirectory(op-finder)
|
||||||
|
|
||||||
|
if(WITH_TESTS)
|
||||||
|
enable_testing()
|
||||||
|
add_subdirectory(op-finder-tests)
|
||||||
|
endif()
|
||||||
|
|||||||
11
README.md
11
README.md
@ -1,2 +1,13 @@
|
|||||||
# masters-thesis
|
# 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
6
conanfile.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[requires]
|
||||||
|
catch2/2.13.4
|
||||||
|
nlohmann_json/3.9.1
|
||||||
|
|
||||||
|
[generators]
|
||||||
|
cmake_find_package
|
||||||
25447
dependencies/nlohmann/json.hpp
vendored
25447
dependencies/nlohmann/json.hpp
vendored
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.17)
|
cmake_minimum_required(VERSION 3.17)
|
||||||
|
|
||||||
|
find_package(nlohmann_json REQUIRED)
|
||||||
find_package(Clang REQUIRED)
|
find_package(Clang REQUIRED)
|
||||||
find_package(LLVM REQUIRED COMPONENTS Support Option Core)
|
find_package(LLVM REQUIRED COMPONENTS Support Option Core)
|
||||||
|
|
||||||
@ -29,7 +30,6 @@ add_library(op-finder-lib STATIC
|
|||||||
target_include_directories(op-finder-lib
|
target_include_directories(op-finder-lib
|
||||||
PUBLIC
|
PUBLIC
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
${PROJECT_SOURCE_DIR}/dependencies
|
|
||||||
${LLVM_INCLUDE_DIRS}
|
${LLVM_INCLUDE_DIRS}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,6 +39,8 @@ target_compile_definitions(op-finder-lib
|
|||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(op-finder-lib
|
target_link_libraries(op-finder-lib
|
||||||
|
PUBLIC
|
||||||
|
nlohmann_json::nlohmann_json
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${llvm_libs}
|
${llvm_libs}
|
||||||
clangTooling
|
clangTooling
|
||||||
|
|||||||
@ -14,22 +14,21 @@
|
|||||||
class OperationStorage : public IOperationOutput
|
class OperationStorage : public IOperationOutput
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OperationStorage() = delete;
|
OperationStorage() = default;
|
||||||
OperationStorage(const OperationStorage&) = delete;
|
OperationStorage(const OperationStorage&) = delete;
|
||||||
|
|
||||||
explicit OperationStorage(const std::string& output_filename);
|
|
||||||
~OperationStorage() override;
|
~OperationStorage() override;
|
||||||
|
|
||||||
void enablePrettyPrint();
|
void enablePrettyPrint();
|
||||||
|
void toStream(std::ostream& stream);
|
||||||
|
void toFile(const std::string& output_filename);
|
||||||
|
|
||||||
void pushOperation(const std::string& filename, OperationLog&& op) override;
|
void pushOperation(const std::string& filename, OperationLog&& op) override;
|
||||||
[[nodiscard]] const std::unordered_map<std::string, std::vector<OperationLog>>& getOperations() const;
|
[[nodiscard]] const std::unordered_map<std::string, std::vector<OperationLog>>& getOperations() const;
|
||||||
private:
|
private:
|
||||||
std::unordered_map<std::string, std::vector<OperationLog>> _operations;
|
std::unordered_map<std::string, std::vector<OperationLog>> _operations;
|
||||||
std::string _output_filename;
|
|
||||||
|
|
||||||
bool _pretty_print = false;
|
bool _pretty_print = false;
|
||||||
|
|
||||||
void _dumpToFile();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //C_ANALYZER_OPERATIONSTORAGE_HPP
|
#endif //C_ANALYZER_OPERATIONSTORAGE_HPP
|
||||||
|
|||||||
@ -9,20 +9,32 @@
|
|||||||
|
|
||||||
#include <llvm/Support/CommandLine.h>
|
#include <llvm/Support/CommandLine.h>
|
||||||
|
|
||||||
OperationStorage::OperationStorage(const std::string& output_filename)
|
|
||||||
: _output_filename(output_filename)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
OperationStorage::~OperationStorage()
|
OperationStorage::~OperationStorage()
|
||||||
{
|
{ }
|
||||||
_dumpToFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OperationStorage::enablePrettyPrint()
|
void OperationStorage::enablePrettyPrint()
|
||||||
{
|
{
|
||||||
_pretty_print = true;
|
_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)
|
void OperationStorage::pushOperation(const std::string& filename, OperationLog&& op)
|
||||||
{
|
{
|
||||||
auto it = _operations.find(filename);
|
auto it = _operations.find(filename);
|
||||||
@ -37,15 +49,3 @@ const std::unordered_map<std::string, std::vector<OperationLog>>& OperationStora
|
|||||||
{
|
{
|
||||||
return _operations;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
27
op-finder-tests/CMakeLists.txt
Normal file
27
op-finder-tests/CMakeLists.txt
Normal 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)
|
||||||
7
op-finder-tests/basic_operations.cpp
Normal file
7
op-finder-tests/basic_operations.cpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 07.03.21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
|
||||||
6
op-finder-tests/main.cpp
Normal file
6
op-finder-tests/main.cpp
Normal 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>
|
||||||
@ -5,6 +5,8 @@
|
|||||||
// Declares llvm::cl::extrahelp.
|
// Declares llvm::cl::extrahelp.
|
||||||
#include "llvm/Support/CommandLine.h"
|
#include "llvm/Support/CommandLine.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "OperationAstMatcher.hpp"
|
#include "OperationAstMatcher.hpp"
|
||||||
#include "OperationFinder.hpp"
|
#include "OperationFinder.hpp"
|
||||||
#include "OperationStorage.hpp"
|
#include "OperationStorage.hpp"
|
||||||
@ -50,7 +52,7 @@ int main(int argc, const char** argv)
|
|||||||
ClangTool Tool(OptionsParser.getCompilations(),
|
ClangTool Tool(OptionsParser.getCompilations(),
|
||||||
OptionsParser.getSourcePathList());
|
OptionsParser.getSourcePathList());
|
||||||
|
|
||||||
OperationStorage storage(OutputFile.getValue());
|
OperationStorage storage;
|
||||||
|
|
||||||
if (PrettyPrint.getValue())
|
if (PrettyPrint.getValue())
|
||||||
storage.enablePrettyPrint();
|
storage.enablePrettyPrint();
|
||||||
@ -70,5 +72,10 @@ int main(int argc, const char** argv)
|
|||||||
|
|
||||||
OperationFinderAstAction action(&op_finder);
|
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);
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user