Skip to content

Commit

Permalink
#12: convert sanitizer executable into plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cz4rs committed Feb 18, 2021
1 parent 1eb8afa commit dfbc341
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 44 deletions.
11 changes: 4 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ file(
SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/src/sanitizer/*.cc
)
add_executable(

add_library(
sanitizer
SHARED
${HEADER_FILES} ${SOURCE_FILES}
)

Expand All @@ -57,7 +59,6 @@ target_include_directories(
$<INSTALL_INTERFACE:include/fmt>
)


# target_include_directories(
# ${SERIALIZATION_SANITIZER_LIBRARY} PUBLIC
# $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
Expand All @@ -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(
Expand Down
77 changes: 49 additions & 28 deletions src/sanitizer/sanitizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"

#include <memory>

Expand All @@ -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<std::string> Filename("o", cl::desc("Filename to output generated code"));
static cl::list<std::string> Includes("I", cl::desc("Include directories"), cl::ZeroOrMore);
static cl::opt<bool> GenerateInline("inline", cl::desc("Generate code inline and modify files"));
static cl::opt<bool> OutputMainFile("include-input", cl::desc("Output input file with generated code"));
static cl::opt<bool> IncludeVTHeader("Ivt", cl::desc("Include VT headers in generated code"));
Expand Down Expand Up @@ -159,34 +159,55 @@ 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");
// 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 <vt/transport.h>\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<MyFrontendAction>().get());

// if (Filename == "" and out != nullptr) {
// fclose(out);
// }
// return 0;
// }

struct SanitizerPluginAction : public PluginASTAction {
public:
bool ParseArgs(CompilerInstance const&,
std::vector<std::string> const&) override {
return true;
}

if (IncludeVTHeader) {
fmt::print(out, "#include <vt/transport.h>\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);
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance&,
StringRef) override {
return std::make_unique<MyASTConsumer>(rw_);
}

Tool.run(newFrontendActionFactory<MyFrontendAction>().get());
private:
Rewriter rw_;
};

if (Filename == "" and out != nullptr) {
fclose(out);
}
return 0;
}
// register the plugin
static FrontendPluginRegistry::Add<SanitizerPluginAction>
X(/*Name=*/"sanitizer",
/*Desc=*/"Serialization Sanitizer");
18 changes: 9 additions & 9 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down

0 comments on commit dfbc341

Please sign in to comment.