diff --git a/src/clang_tu.cc b/src/clang_tu.cc index 664f6fbb..9c1b581e 100644 --- a/src/clang_tu.cc +++ b/src/clang_tu.cc @@ -108,9 +108,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 @@ -162,12 +163,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 b3288eb7..7e2abf6c 100644 --- a/src/clang_tu.hh +++ b/src/clang_tu.hh @@ -51,7 +51,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 8f84bc02..d894d773 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1315,7 +1315,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 0a7f6eb8..c861fb7c 100644 --- a/src/sema_manager.cc +++ b/src/sema_manager.cc @@ -467,8 +467,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) { @@ -509,8 +509,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(); @@ -610,8 +610,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) {