From 1b5a50e8592473a2df68a47d24d8f21841d4da75 Mon Sep 17 00:00:00 2001 From: ykiko Date: Sat, 21 Sep 2024 21:54:07 +0800 Subject: [PATCH] update command. --- include/Server/Command.h | 26 ++++++++++++++++++++++---- include/Server/Config.h | 6 +++--- include/Server/Server.h | 1 - src/Server/Command.cpp | 29 +++++++++++------------------ src/Server/Config.cpp | 31 +++++++++++++++++-------------- tests/AST/Resolver.cpp | 3 ++- tests/Feature/SemanticTokens.cpp | 10 ++++------ 7 files changed, 59 insertions(+), 47 deletions(-) diff --git a/include/Server/Command.h b/include/Server/Command.h index 0d9d9028..01e6badb 100644 --- a/include/Server/Command.h +++ b/include/Server/Command.h @@ -4,15 +4,33 @@ namespace clice { -class CompilationDatabase { +namespace command { +std::vector decorate(const clang::tooling::CompileCommand& command); + +class Command { public: - void load(clang::StringRef path); + /// according to config, decorate the input command. + /// e.g. add `resource-dir`, remove or transform unsupported flags like `/std:lastest`. + Command(const clang::tooling::CompileCommand& command); - std::vector lookup(clang::StringRef path); + Command& append(llvm::StringRef arg) { + auto data = allocator.Allocate(arg.size() + 1); + if(!arg.empty()) { + std::memcpy(data, arg.data(), arg.size()); + } + data[arg.size()] = '\0'; + args.push_back(data); + return *this; + } private: - std::unique_ptr CDB; + std::vector args; + llvm::BumpPtrAllocator allocator; }; +// TODO: compile database logic + +} // namespace command + } // namespace clice diff --git a/include/Server/Config.h b/include/Server/Config.h index d6779ce7..a71dfbc0 100644 --- a/include/Server/Config.h +++ b/include/Server/Config.h @@ -20,13 +20,13 @@ struct ServerOption { struct FrontendOption { std::vector append; std::vector remove; - std::string resource_dictionary = "${executable}/../lib/clang/${llvm_version}"; + std::string resource_dictionary = "${binary}/../lib/clang/${llvm_version}"; std::string compile_commands_directory = "${workplace}/build"; }; -ServerOption& server(); +const ServerOption& server(); -FrontendOption& frontend(); +const FrontendOption& frontend(); }; // namespace clice::config diff --git a/include/Server/Server.h b/include/Server/Server.h index 9fce09dc..01131daa 100644 --- a/include/Server/Server.h +++ b/include/Server/Server.h @@ -9,7 +9,6 @@ namespace clice { struct Server { using Handler = llvm::unique_function; Scheduler scheduler; - CompilationDatabase CDB; llvm::StringMap handlers; static Server instance; diff --git a/src/Server/Command.cpp b/src/Server/Command.cpp index b336bc91..dbe04c1f 100644 --- a/src/Server/Command.cpp +++ b/src/Server/Command.cpp @@ -1,28 +1,21 @@ +#include +#include #include -#include -#include namespace clice { -std::vector CompilationDatabase::lookup(clang::StringRef path) { - auto& command = CDB->getCompileCommands(path).front(); - std::vector args; +namespace command { + +Command::Command(const clang::tooling::CompileCommand& command) { + // FIXME: for(auto& arg: command.CommandLine) { - // TODO: - // some modification - args.push_back(arg.c_str()); + append(arg); } - return args; + + append("-resource-dir"); + append(config::frontend().resource_dictionary); } -void CompilationDatabase::load(clang::StringRef path) { - std::string error; - CDB = clang::tooling::CompilationDatabase::loadFromDirectory(path, error); - if(!CDB) { - spdlog::error("Failed to load compilation database: {}", error); - spdlog::default_logger()->flush(); - std::terminate(); - } -} +} // namespace command } // namespace clice diff --git a/src/Server/Config.cpp b/src/Server/Config.cpp index 52b90780..8bba1441 100644 --- a/src/Server/Config.cpp +++ b/src/Server/Config.cpp @@ -7,20 +7,25 @@ namespace clice::config { +namespace { + +/// predefined variables. +llvm::StringMap predefined = { + /// the directory of the workplace. + {"workplace", "" }, + /// the directory of the executable. + {"binary", "" }, + /// the version of the clice. + {"version", "0.0.1"}, + /// the version of dependent llvm. + {"llvm_version", "20" }, +}; + struct Config { ServerOption server; FrontendOption frontend; }; -namespace { - -llvm::StringMap predefined = { - {"workplace", "" }, /// the directory of the workplace. - {"executable", "" }, /// the directory of the executable. - {"version", "0.0.1"}, /// the version of the clice. - {"llvm_version", "20" }, /// the version of dependent llvm. -}; - /// global config instance. Config config = {}; @@ -56,7 +61,7 @@ std::string replace(std::string_view text) { } // namespace int parse(int argc, const char** argv) { - predefined["executable"] = argv[0]; + predefined["binary"] = path::parent_path(argv[0]); // FIXME: // if(version) { @@ -80,16 +85,14 @@ void initialize(std::string_view workplace) { refl::walk(config, [&](std::string_view name, Field& field) { if constexpr(std::is_same_v) { - llvm::outs() << "replace: " << field << '\n'; field = replace(field); - llvm::outs() << "with: " << field << '\n'; } }); return; } -ServerOption& server() { return config.server; } +const ServerOption& server() { return config.server; } -FrontendOption& frontend() { return config.frontend; } +const FrontendOption& frontend() { return config.frontend; } } // namespace clice::config diff --git a/tests/AST/Resolver.cpp b/tests/AST/Resolver.cpp index b203510f..a02ea1cd 100644 --- a/tests/AST/Resolver.cpp +++ b/tests/AST/Resolver.cpp @@ -9,7 +9,8 @@ std::vector compileArgs = { "clang++", "-std=c++20", "main.cpp", - "-resource-dir=/home/ykiko/C++/clice2/build/lib/clang/20", + "-resource-dir", + "/home/ykiko/C++/clice2/build/lib/clang/20", }; struct Visitor : public clang::RecursiveASTVisitor { diff --git a/tests/Feature/SemanticTokens.cpp b/tests/Feature/SemanticTokens.cpp index 1c9600dd..f295592d 100644 --- a/tests/Feature/SemanticTokens.cpp +++ b/tests/Feature/SemanticTokens.cpp @@ -17,19 +17,17 @@ TEST(test, test) { "clang++", "-std=c++20", "main.cpp", - "-resource-dir=/home/ykiko/C++/clice2/build/lib/clang/20", + "-resource-dir", + "/home/ykiko/C++/clice2/build/lib/clang/20", }; #include const char* code = R"( -void f(); - -void f() {} +#include )"; - auto preamble = clice::Preamble::build("main.cpp", code, compileArgs); - auto AST = clice::ParsedAST::build("main.cpp", code, compileArgs, preamble.get()); + auto AST = clice::ParsedAST::build("main.cpp", code, compileArgs); auto fileID = AST->getFileID("main.cpp"); auto tokens = AST->tokenBuffer.spelledTokens(fileID); AST->context.getTranslationUnitDecl()->dump();