From 7beed982f23e37ac096d567c08e45926704b79a6 Mon Sep 17 00:00:00 2001 From: Shiyu Date: Wed, 3 Sep 2025 20:34:02 +0800 Subject: [PATCH] Fix: store absolute path of source file in `CompilationDatabase` (#222) --- include/Compiler/Command.h | 2 +- src/Compiler/Command.cpp | 14 ++++---------- src/Server/Lifecycle.cpp | 2 +- tests/unit/Compiler/Command.cpp | 9 +++++---- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/include/Compiler/Command.h b/include/Compiler/Command.h index 6f0391e5..76e6748e 100644 --- a/include/Compiler/Command.h +++ b/include/Compiler/Command.h @@ -115,7 +115,7 @@ public: /// Load compile commands from given directories. If no valid commands are found, /// search recursively from the workspace directory. - auto load_compile_commands(this Self& self, + auto load_compile_database(this Self& self, llvm::ArrayRef compile_commands_dirs, llvm::StringRef workspace) -> void; diff --git a/src/Compiler/Command.cpp b/src/Compiler/Command.cpp index 1c3d2b73..3a5535a6 100644 --- a/src/Compiler/Command.cpp +++ b/src/Compiler/Command.cpp @@ -436,16 +436,10 @@ auto CompilationDatabase::load_commands(this Self& self, continue; } - /// Always store relative path of source file. + /// Always store absolute path of source file. std::string source; if(auto file = object.getString("file")) { - if(path::is_absolute(*file)) { - llvm::SmallString<256> buffer = *file; - path::replace_path_prefix(buffer, workspace, ""); - source = path::relative_path(buffer).str(); - } else { - source = file->str(); - } + source = path::is_absolute(*file) ? file->str() : path::join(*directory, *file); } else { continue; } @@ -551,7 +545,7 @@ auto CompilationDatabase::guess_or_fallback(this Self& self, llvm::StringRef fil return info; } -auto CompilationDatabase::load_compile_commands(this Self& self, +auto CompilationDatabase::load_compile_database(this Self& self, llvm::ArrayRef compile_commands_dirs, llvm::StringRef workspace) -> void { auto try_load = [&self, workspace](llvm::StringRef dir) { @@ -576,7 +570,7 @@ auto CompilationDatabase::load_compile_commands(this Self& self, return; } - log::info( + log::warn( "Can not found any valid CDB file from given directories, search recursively from workspace: {} ...", workspace); diff --git a/src/Server/Lifecycle.cpp b/src/Server/Lifecycle.cpp index bd5ac680..3a4aaf0a 100644 --- a/src/Server/Lifecycle.cpp +++ b/src/Server/Lifecycle.cpp @@ -27,7 +27,7 @@ async::Task Server::on_initialize(proto::InitializeParams params) { opening_files.set_capability(config::server.max_active_file); /// Load compile commands.json - database.load_compile_commands(config::server.compile_commands_dirs, workspace); + database.load_compile_database(config::server.compile_commands_dirs, workspace); /// Load cache info. load_cache_info(); diff --git a/tests/unit/Compiler/Command.cpp b/tests/unit/Compiler/Command.cpp index 952ee5b5..cd95dc58 100644 --- a/tests/unit/Compiler/Command.cpp +++ b/tests/unit/Compiler/Command.cpp @@ -235,6 +235,7 @@ suite<"Command"> command = [] { for(size_t i = 0; i < arguments.size(); i++) { llvm::StringRef arg = info.arguments[i]; llvm::StringRef expect_arg = arguments[i]; + // llvm::outs() << "arg: " << arg << ", expect: " << expect_arg << "\n"; expect(that % arg == expect_arg); } }; @@ -253,7 +254,7 @@ suite<"Command"> command = [] { expect_load(cmake, "/home/developer/clice", - "src/Driver/clice.cpp", + "/home/developer/clice/src/Driver/clice.cpp", "/home/developer/clice/build", { "/usr/bin/c++", @@ -269,7 +270,7 @@ suite<"Command"> command = [] { "-Wno-deprecated-declarations", "-Wno-undefined-inline", "-O3", - "src/Driver/clice.cpp", + "/home/developer/clice/src/Driver/clice.cpp", }); }; @@ -285,7 +286,7 @@ suite<"Command"> command = [] { expect_load( xmake, "/home/developer/clice", - "src/Driver/clice.cc", + "/home/developer/clice/src/Driver/clice.cc", "/home/developer/clice", { "/usr/bin/clang", @@ -307,7 +308,7 @@ suite<"Command"> command = [] { "/home/developer/.xmake/packages/t/toml++/v3.4.0/bde7344d843e41928b1d325fe55450e0/include", "-fsanitize=address", "-fno-rtti", - "src/Driver/clice.cc", + "/home/developer/clice/src/Driver/clice.cc", }); }; #endif