diff --git a/CMakeLists.txt b/CMakeLists.txt index 08a3a48..f2090b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,8 +41,10 @@ file( SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/sanitizer/*.cc ) -add_executable( + +add_library( sanitizer + SHARED ${HEADER_FILES} ${SOURCE_FILES} ) @@ -57,7 +59,6 @@ target_include_directories( $ ) - # target_include_directories( # ${SERIALIZATION_SANITIZER_LIBRARY} PUBLIC # $ @@ -84,13 +85,9 @@ target_link_libraries( sanitizer PUBLIC ${CLANG_LIBS} ) -target_link_libraries( - sanitizer PUBLIC clangASTMatchers clangToolingCore clangLex clangParse clangSema -) - install( TARGETS sanitizer - RUNTIME DESTINATION bin + LIBRARY DESTINATION bin ) install( diff --git a/src/sanitizer/sanitizer.cc b/src/sanitizer/sanitizer.cc index 890c0b6..6384491 100644 --- a/src/sanitizer/sanitizer.cc +++ b/src/sanitizer/sanitizer.cc @@ -54,6 +54,7 @@ #include "clang/Rewrite/Core/Rewriter.h" #include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendPluginRegistry.h" #include @@ -67,10 +68,9 @@ using namespace llvm; using namespace clang::tooling; using namespace clang::ast_matchers; -static FILE* out = nullptr; +static FILE* out = stdout; static cl::opt Filename("o", cl::desc("Filename to output generated code")); -static cl::list Includes("I", cl::desc("Include directories"), cl::ZeroOrMore); static cl::opt GenerateInline("inline", cl::desc("Generate code inline and modify files")); static cl::opt OutputMainFile("include-input", cl::desc("Output input file with generated code")); static cl::opt IncludeVTHeader("Ivt", cl::desc("Include VT headers in generated code")); @@ -147,46 +147,55 @@ struct MyFrontendAction : ASTFrontendAction { Rewriter rw_; }; -// Apply a custom category to all command-line options so that they are the -// only ones displayed. -static cl::OptionCategory SerializeCheckerCategory("Serialize sanitizer"); - -// 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 cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); - // A help message for this specific tool can be added afterwards. static cl::extrahelp MoreHelp("\nGenerates sanitizer code for serializers\n"); -int main(int argc, const char **argv) { - CommonOptionsParser OptionsParser(argc, argv, SerializeCheckerCategory); - - ClangTool Tool( - OptionsParser.getCompilations(), OptionsParser.getSourcePathList() - ); - - if (Filename == "") { - out = stdout; - } else { - out = fopen(Filename.c_str(), "w"); - } - - if (IncludeVTHeader) { - fmt::print(out, "#include \n"); +// int main(int argc, const char **argv) { +// if (Filename == "") { +// out = stdout; +// } else { +// out = fopen(Filename.c_str(), "w"); +// } + +// if (IncludeVTHeader) { +// fmt::print(out, "#include \n"); +// } + +// for (auto&& e : Includes) { +// auto str = std::string("-I") + e; +// ArgumentsAdjuster ad1 = getInsertArgumentAdjuster(str.c_str()); +// Tool.appendArgumentsAdjuster(ad1); +// fmt::print(stderr, "Including {}\n", e); +// } + +// Tool.run(newFrontendActionFactory().get()); + +// if (Filename == "" and out != nullptr) { +// fclose(out); +// } +// return 0; +// } + +struct SanitizerPluginAction : public PluginASTAction { +public: + bool ParseArgs(CompilerInstance const&, + std::vector const& args) override { + for (auto&& arg : args) { + llvm::errs() << arg; + } + return true; } - for (auto&& e : Includes) { - auto str = std::string("-I") + e; - ArgumentsAdjuster ad1 = getInsertArgumentAdjuster(str.c_str()); - Tool.appendArgumentsAdjuster(ad1); - fmt::print(stderr, "Including {}\n", e); + std::unique_ptr CreateASTConsumer(CompilerInstance&, + StringRef) override { + return std::make_unique(rw_); } - Tool.run(newFrontendActionFactory().get()); +private: + Rewriter rw_; +}; - if (Filename == "" and out != nullptr) { - fclose(out); - } - return 0; -} +// register the plugin +static FrontendPluginRegistry::Add + X(/*Name=*/"sanitizer", + /*Desc=*/"Serialization Sanitizer"); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c50a2a6..a56d087 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,22 +24,22 @@ foreach(test_file ${TEST_SOURCE_FILES}) get_filename_component(test_name ${test_file} NAME_WE) + # FIXME: this is obviously not the right way (just checking CI) add_custom_command( OUTPUT "${test_name}.generated.cc" - COMMAND ${PROJECT_BINARY_DIR}/sanitizer - ARGS "-p" "${PROJECT_BINARY_DIR}/compile_commands.json" - "-include-input" - ">" "${test_name}.generated.cc" - ${test_file} + COMMAND cp + ARGS ${test_file} + "${test_name}.generated.cc" + COMMAND clang++ + ARGS "-std=c++14" + "-Xclang" "-load" "-Xclang" "${PROJECT_BINARY_DIR}/libsanitizer.so" + "-Xclang" "-plugin" "-Xclang" "sanitizer" "-c" ${test_file} + ">>" "${test_name}.generated.cc" DEPENDS sanitizer ) add_executable(${test_name} ${TEST_HEADER_FILES} "${test_name}.generated.cc") - # add executable not intended to run but to generate the compile command - # needed for the utility to build - add_executable("${test_name}-skip" "${test_name}.cc") - add_test(${test_name} ${test_name}) list(APPEND test_name_list ${test_name})