diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bde928f..26a8a164 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,21 @@ else() ) endif() +set(FBS_SCHEMA_FILE "${CMAKE_CURRENT_SOURCE_DIR}/include/Index/schema.fbs") +set(GENERATED_HEADER "${CMAKE_CURRENT_BINARY_DIR}/generated/schema_generated.h") + +add_custom_command( + OUTPUT ${GENERATED_HEADER} + COMMAND $ --cpp -o ${CMAKE_CURRENT_BINARY_DIR}/generated ${FBS_SCHEMA_FILE} + DEPENDS ${FBS_SCHEMA_FILE} + COMMENT "Generating C++ header from ${FBS_SCHEMA_FILE}" +) + +add_custom_target( + generate_flatbuffers_schema + DEPENDS ${GENERATED_HEADER} +) + file(GLOB_RECURSE CLICE_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/AST/*.cpp" "${PROJECT_SOURCE_DIR}/src/Async/*.cpp" @@ -65,14 +80,19 @@ file(GLOB_RECURSE CLICE_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/Support/*.cpp" ) add_library(clice-core STATIC "${CLICE_SOURCES}") +add_dependencies(clice-core generate_flatbuffers_schema) -target_include_directories(clice-core PUBLIC ${PROJECT_SOURCE_DIR}/include) +target_include_directories(clice-core PUBLIC + "${PROJECT_SOURCE_DIR}/include" + "${CMAKE_CURRENT_BINARY_DIR}/generated" +) target_link_libraries(clice-core PUBLIC clice_options uv_a spdlog::spdlog tomlplusplus::tomlplusplus roaring::roaring + flatbuffers llvm-libs ) diff --git a/cmake/package.cmake b/cmake/package.cmake index 74d18a8b..10fed4e6 100644 --- a/cmake/package.cmake +++ b/cmake/package.cmake @@ -122,8 +122,16 @@ FetchContent_Declare( ) set(ENABLE_ROARING_TESTS OFF CACHE INTERNAL "") -set(CMAKE_MODULE_PATH "") -FetchContent_MakeAvailable(libuv spdlog tomlplusplus croaring) +FetchContent_Declare( + flatbuffers + GIT_REPOSITORY https://github.com/google/flatbuffers.git + GIT_TAG v25.9.23 +) +set(FLATBUFFERS_BUILD_GRPC OFF CACHE BOOL "" FORCE) +set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "" FORCE) +set(FLATBUFFERS_BUILD_FLATHASH OFF CACHE BOOL "" FORCE) + +FetchContent_MakeAvailable(libuv spdlog tomlplusplus croaring flatbuffers) if(WIN32) target_compile_definitions(uv_a PRIVATE _CRT_SECURE_NO_WARNINGS) diff --git a/include/Index/MergedIndex.h b/include/Index/MergedIndex.h index b2cec52d..7ff8278e 100644 --- a/include/Index/MergedIndex.h +++ b/include/Index/MergedIndex.h @@ -59,12 +59,12 @@ struct MergedIndex { /// A map between source file path and its header contexts. llvm::StringMap contexts; - /// All merged symbol relations. - llvm::DenseMap> relations; - /// All merged symbol occurrences. llvm::DenseMap occurrences; + /// All merged symbol relations. + llvm::DenseMap> relations; + void remove(llvm::StringRef path); void merge(llvm::StringRef path, std::uint32_t include, FileIndex& index); diff --git a/include/Index/schema.fbs b/include/Index/schema.fbs new file mode 100644 index 00000000..c64d178e --- /dev/null +++ b/include/Index/schema.fbs @@ -0,0 +1,65 @@ +namespace clice.index.binary; + +struct Range { + begin: uint; + end: uint; +} + +struct Occurrence { + range: Range; + target: ulong; +} + +struct Relation { + kind: int; + padding: int; + range: Range; + target_symbol: ulong; +} + +table CacheEntry { + sha256: string; + canonical_id: uint; +} + +struct Context { + include_: uint; + canonical_id: uint; +} + +table HeaderContexts { + version: uint; + includes: [Context]; +} + +table HeaderContextsEntry { + path: string; + contexts: HeaderContexts; +} + +table OccurrenceEntry { + occurrence: Occurrence; + context: [ubyte]; +} + +table RelationEntry { + relation: Relation; + context: [ubyte]; +} + +table SymbolRelationsEntry { + symbol: ulong; + relations: [RelationEntry]; +} + +table MergedIndex { + max_canonical_id: uint; + + canonical_cache: [CacheEntry]; + + contexts: [HeaderContextsEntry]; + + occurrences: [OccurrenceEntry]; + + relations: [SymbolRelationsEntry]; +} diff --git a/src/Index/MergedIndex.cpp b/src/Index/MergedIndex.cpp index 7d74bf5e..6dded7b1 100644 --- a/src/Index/MergedIndex.cpp +++ b/src/Index/MergedIndex.cpp @@ -1,5 +1,6 @@ #include "Index/MergedIndex.h" #include "llvm/Support/SHA256.h" +#include "schema_generated.h" namespace clice::index { diff --git a/xmake.lua b/xmake.lua index 73150128..b3addb4e 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,4 +1,4 @@ -set_xmakever("2.9.7") +set_xmakever("3.0.3") set_project("clice") set_allowedplats("windows", "linux", "macosx") @@ -12,7 +12,6 @@ option("ci", {default = false}) if has_config("dev") then set_policy("build.ccache", true) - set_policy("compatibility.version", "3.0") if is_plat("windows") then set_runtimes("MD") if is_mode("debug") then @@ -41,7 +40,7 @@ if has_config("release") then end add_defines("TOML_EXCEPTIONS=0") -add_requires(libuv_require, "spdlog[header_only=n,std_format,noexcept]" ,"toml++", "croaring") +add_requires(libuv_require, "spdlog[header_only=n,std_format,noexcept]" ,"toml++", "croaring", "flatbuffers") add_requires("clice-llvm", {alias = "llvm"}) add_rules("mode.release", "mode.debug", "mode.releasedbg") @@ -50,9 +49,11 @@ add_rules("clice_build_config") target("clice-core") set_kind("$(kind)") - add_files("src/**.cpp|Driver/*.cpp") + add_files("src/**.cpp|Driver/*.cpp", "include/Index/schema.fbs") add_includedirs("include", {public = true}) + add_rules("flatbuffers.schema.gen") + add_packages("flatbuffers") add_packages("libuv", "spdlog", "toml++", "croaring", {public = true}) if is_mode("debug") then @@ -204,6 +205,46 @@ rule("clice_build_config") end end) +rule("flatbuffers.schema.gen") + set_extensions(".fbs") + + on_prepare_files(function (target, jobgraph, sourcebatch, opt) + import("lib.detect.find_tool") + import("core.project.depend") + import("utils.progress") + + assert(target:pkg("flatbuffers"), "Please configure add_packages(\"flatbuffers\") for target(" .. target:name() .. ")") + local envs = target:pkgenvs() + local flatc = assert(find_tool("flatc", {envs = envs}), "flatc not found!") + + local group_name = path.join(target:fullname(), "generate/fbs") + local autogendir = path.join(target:autogendir(), "rules/flatbuffers") + jobgraph:group(group_name, function() + for _, sourcefile in ipairs(sourcebatch.sourcefiles) do + local job = path.join(group_name, sourcefile) + local generate_dir = path.normalize(path.join(autogendir, path.directory(sourcefile))) + target:add("includedirs", generate_dir, {public = true}) + os.mkdir(generate_dir) + jobgraph:add(job, function (index, total, opt) + local argv = { + "--cpp", + "-o", generate_dir, + sourcefile + } + + depend.on_changed(function() + progress.show(flatc.progress or 0, "${color.build.object}generating.fbs %s", sourcefile) + os.vrunv(flatc.program, argv) + end, { + files = sourcefile, + dependfile = target:dependfile(sourcefile), + changed = target:is_rebuilt() + }) + end) + end + end) + end, {jobgraph = true}) + package("clice-llvm") if has_config("llvm") then set_sourcedir(get_config("llvm"))