Skip to content

Enforce the clang option working-directory during parsing #866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/clang_tu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ Range fromTokenRangeDefaulted(const SourceManager &sm, const LangOptions &lang,

std::unique_ptr<CompilerInvocation>
buildCompilerInvocation(const std::string &main, std::vector<const char *> args,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs) {
IntrusiveRefCntPtr<llvm::vfs::FileSystem> 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
Expand Down Expand Up @@ -146,12 +147,14 @@ buildCompilerInvocation(const std::string &main, std::vector<const char *> args,
if (StringRef(cmd.getCreator().getName()) != "clang")
return nullptr;
const llvm::opt::ArgStringList &cc_args = cmd.getArguments();
auto c_args = const_cast<llvm::opt::ArgStringList *>(&cc_args);
c_args->append({intern("-working-directory=" + working_dir)});
auto ci = std::make_unique<CompilerInvocation>();
#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;

Expand Down
3 changes: 2 additions & 1 deletion src/clang_tu.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Range fromTokenRangeDefaulted(const clang::SourceManager &sm,

std::unique_ptr<clang::CompilerInvocation>
buildCompilerInvocation(const std::string &main, std::vector<const char *> args,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS);
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const std::string &working_dir);

const char *clangBuiltinTypeName(int);
} // namespace ccls
2 changes: 1 addition & 1 deletion src/indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs =
llvm::vfs::getRealFileSystem();
std::shared_ptr<CompilerInvocation> ci =
buildCompilerInvocation(main, args, fs);
buildCompilerInvocation(main, args, fs, opt_wdir);
// e.g. .s
if (!ci)
return {};
Expand Down
1 change: 1 addition & 0 deletions src/query.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <llvm/ADT/DenseMap.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringMap.h>
#include <llvm/ADT/STLExtras.h>

namespace llvm {
template <> struct DenseMapInfo<ccls::ExtentRef> {
Expand Down
12 changes: 6 additions & 6 deletions src/sema_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ void *preambleMain(void *manager_) {
auto stat_cache = std::make_unique<PreambleStatCache>();
IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs =
stat_cache->producer(session->fs);
if (std::unique_ptr<CompilerInvocation> ci =
buildCompilerInvocation(task.path, session->file.args, fs))
if (std::unique_ptr<CompilerInvocation> ci = buildCompilerInvocation(
task.path, session->file.args, fs, session->file.directory))
buildPreamble(*session, *ci, fs, task, std::move(stat_cache));

if (task.comp_task) {
Expand Down Expand Up @@ -475,8 +475,8 @@ void *completionMain(void *manager_) {
std::shared_ptr<PreambleData> preamble = session->getPreamble();
IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs =
preamble ? preamble->stat_cache->consumer(session->fs) : session->fs;
std::unique_ptr<CompilerInvocation> ci =
buildCompilerInvocation(task->path, session->file.args, fs);
std::unique_ptr<CompilerInvocation> ci = buildCompilerInvocation(
task->path, session->file.args, fs, session->file.directory);
if (!ci)
continue;
auto &fOpts = ci->getFrontendOpts();
Expand Down Expand Up @@ -569,8 +569,8 @@ void *diagnosticMain(void *manager_) {
std::shared_ptr<PreambleData> preamble = session->getPreamble();
IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs =
preamble ? preamble->stat_cache->consumer(session->fs) : session->fs;
std::unique_ptr<CompilerInvocation> ci =
buildCompilerInvocation(task.path, session->file.args, fs);
std::unique_ptr<CompilerInvocation> ci = buildCompilerInvocation(
task.path, session->file.args, fs, session->file.directory);
if (!ci)
continue;
if (preamble) {
Expand Down