commit 2a5cb3edc35e15c03442ea6a9512adbda9079cf2 Author: York Jasper Niebuhr Date: Tue Oct 7 23:04:22 2025 +0200 CMake setup diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..12f3d9a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.16) +project(SelfPatchSLR LANGUAGES C CXX) + +add_subdirectory(plugin) +add_subdirectory(subject) diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt new file mode 100644 index 0000000..e855273 --- /dev/null +++ b/plugin/CMakeLists.txt @@ -0,0 +1,9 @@ +add_library(selfpatch-slr SHARED main.cpp) + +set_target_properties(selfpatch-slr PROPERTIES PREFIX "") +target_compile_definitions(selfpatch-slr PRIVATE _GNU_SOURCE) + +message(STATUS "C compiler: ${CMAKE_C_COMPILER}") +execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=plugin OUTPUT_VARIABLE GCC_PLUGIN_PATH OUTPUT_STRIP_TRAILING_WHITESPACE) +message(STATUS "GCC plugin path: ${GCC_PLUGIN_PATH}") +target_include_directories(selfpatch-slr PRIVATE ${GCC_PLUGIN_PATH}/include) diff --git a/plugin/main.cpp b/plugin/main.cpp new file mode 100644 index 0000000..ffbbff1 --- /dev/null +++ b/plugin/main.cpp @@ -0,0 +1,31 @@ +#include + +#include "gcc-plugin.h" +#include "plugin-version.h" +#include "tree.h" + +int plugin_is_GPL_compatible; + +static tree handle_slr_attr(tree* node, tree name, tree args, int flags, bool* no_add_attrs) { + if (!node) + return NULL_TREE; + + std::cout << "SLR attribute found!" << std::endl; + return NULL_TREE; +} + +static struct attribute_spec slr_attr = { "slr", 0, 0, false, false, false, false, handle_slr_attr, NULL }; + +void register_attributes(void* event_data, void* data) { + register_attribute(&slr_attr); +} + +int plugin_init(struct plugin_name_args* plugin_info, struct plugin_gcc_version* version) { + if (!plugin_default_version_check(version, &gcc_version)) { + std::cerr << "This plugin is for GCC version " << GCCPLUGIN_VERSION_MAJOR << "." << GCCPLUGIN_VERSION_MINOR << std::endl; + return 1; + } + + register_callback(plugin_info->base_name, PLUGIN_ATTRIBUTES, register_attributes, NULL); + return 0; +} diff --git a/subject/CMakeLists.txt b/subject/CMakeLists.txt new file mode 100644 index 0000000..5819276 --- /dev/null +++ b/subject/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(subject main.c) +add_dependencies(subject selfpatch-slr) +target_compile_options(subject PRIVATE "-fplugin=$") diff --git a/subject/main.c b/subject/main.c new file mode 100644 index 0000000..52d84e3 --- /dev/null +++ b/subject/main.c @@ -0,0 +1,30 @@ +#include +#include + +#define container_of(ptr, type, member) ({ \ + const typeof(((type*)0)->member)* __mptr = (ptr); \ + (type*)((char*)__mptr - offsetof(type, member)); }) + +struct A; +struct B; + +struct A { + char m0; + int m1; + struct B* m2; +}; + +struct B { + int m0; + char m1; + short m2; + struct A* m3; + float m4; + struct A m5; + struct B* m6; +} __attribute__((slr)); + +int main(int argc, char** argv) { + // TODO + return 0; +}