diff --git a/src/clang_tu.cc b/src/clang_tu.cc index 9e026652..23e41f16 100644 --- a/src/clang_tu.cc +++ b/src/clang_tu.cc @@ -96,9 +96,10 @@ Range fromTokenRangeDefaulted(const SourceManager &sm, const LangOptions &lang, std::unique_ptr buildCompilerInvocation(const std::string &main, std::vector args, - IntrusiveRefCntPtr vfs) { + IntrusiveRefCntPtr vfs, + const std::string &working_dir) { std::string save = "-resource-dir=" + g_config->clang.resourceDir; - args.push_back(save.c_str()); + args.push_back(intern(save)); args.push_back("-fsyntax-only"); // Similar to clang/tools/driver/driver.cpp:insertTargetAndModeArgs but don't @@ -146,12 +147,14 @@ buildCompilerInvocation(const std::string &main, std::vector args, if (StringRef(cmd.getCreator().getName()) != "clang") return nullptr; const llvm::opt::ArgStringList &cc_args = cmd.getArguments(); + auto c_args = const_cast(&cc_args); + c_args->append({intern("-working-directory=" + working_dir)}); auto ci = std::make_unique(); #if LLVM_VERSION_MAJOR >= 10 // rC370122 - if (!CompilerInvocation::CreateFromArgs(*ci, cc_args, *diags)) + if (!CompilerInvocation::CreateFromArgs(*ci, *c_args, *diags)) #else if (!CompilerInvocation::CreateFromArgs( - *ci, cc_args.data(), cc_args.data() + cc_args.size(), *diags)) + *ci, c_args->data(), c_args->data() + c_args->size(), *diags)) #endif return nullptr; diff --git a/src/clang_tu.hh b/src/clang_tu.hh index 7dbfc4a8..7b696346 100644 --- a/src/clang_tu.hh +++ b/src/clang_tu.hh @@ -42,7 +42,8 @@ Range fromTokenRangeDefaulted(const clang::SourceManager &sm, std::unique_ptr buildCompilerInvocation(const std::string &main, std::vector args, - llvm::IntrusiveRefCntPtr VFS); + llvm::IntrusiveRefCntPtr VFS, + const std::string &working_dir); const char *clangBuiltinTypeName(int); } // namespace ccls diff --git a/src/indexer.cc b/src/indexer.cc index 644c0ccb..3ff1629a 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1277,7 +1277,7 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs, llvm::IntrusiveRefCntPtr fs = llvm::vfs::getRealFileSystem(); std::shared_ptr ci = - buildCompilerInvocation(main, args, fs); + buildCompilerInvocation(main, args, fs, opt_wdir); // e.g. .s if (!ci) return {}; diff --git a/src/query.hh b/src/query.hh index cd3a19c1..3b033a9e 100644 --- a/src/query.hh +++ b/src/query.hh @@ -10,6 +10,7 @@ #include #include #include +#include namespace llvm { template <> struct DenseMapInfo { diff --git a/src/sema_manager.cc b/src/sema_manager.cc index dbe57672..39e21843 100644 --- a/src/sema_manager.cc +++ b/src/sema_manager.cc @@ -433,8 +433,8 @@ void *preambleMain(void *manager_) { auto stat_cache = std::make_unique(); IntrusiveRefCntPtr fs = stat_cache->producer(session->fs); - if (std::unique_ptr ci = - buildCompilerInvocation(task.path, session->file.args, fs)) + if (std::unique_ptr ci = buildCompilerInvocation( + task.path, session->file.args, fs, session->file.directory)) buildPreamble(*session, *ci, fs, task, std::move(stat_cache)); if (task.comp_task) { @@ -475,8 +475,8 @@ void *completionMain(void *manager_) { std::shared_ptr preamble = session->getPreamble(); IntrusiveRefCntPtr fs = preamble ? preamble->stat_cache->consumer(session->fs) : session->fs; - std::unique_ptr ci = - buildCompilerInvocation(task->path, session->file.args, fs); + std::unique_ptr ci = buildCompilerInvocation( + task->path, session->file.args, fs, session->file.directory); if (!ci) continue; auto &fOpts = ci->getFrontendOpts(); @@ -569,8 +569,8 @@ void *diagnosticMain(void *manager_) { std::shared_ptr preamble = session->getPreamble(); IntrusiveRefCntPtr fs = preamble ? preamble->stat_cache->consumer(session->fs) : session->fs; - std::unique_ptr ci = - buildCompilerInvocation(task.path, session->file.args, fs); + std::unique_ptr ci = buildCompilerInvocation( + task.path, session->file.args, fs, session->file.directory); if (!ci) continue; if (preamble) {