Resolve "directory" in compile_commands.json in case CWD is not the project root

Also delete an unneeded chdir hack.

Close #703
This commit is contained in:
Fangrui Song 2020-12-19 21:42:21 -08:00
parent feb153a843
commit 4c66fdf09d

View File

@ -425,14 +425,13 @@ void Project::loadDirectory(const std::string &root, Project::Folder &folder) {
} else { } else {
LOG_S(INFO) << "loaded " << path.c_str(); LOG_S(INFO) << "loaded " << path.c_str();
for (tooling::CompileCommand &cmd : cdb->getAllCompileCommands()) { for (tooling::CompileCommand &cmd : cdb->getAllCompileCommands()) {
static bool once;
Project::Entry entry; Project::Entry entry;
entry.root = root; entry.root = root;
doPathMapping(entry.root); doPathMapping(entry.root);
// If workspace folder is real/ but entries use symlink/, convert to // If workspace folder is real/ but entries use symlink/, convert to
// real/. // real/.
entry.directory = realPath(cmd.Directory); entry.directory = realPath(resolveIfRelative(root, cmd.Directory));
entry.directory.push_back('/'); entry.directory.push_back('/');
normalizeFolder(entry.directory); normalizeFolder(entry.directory);
entry.directory.pop_back(); entry.directory.pop_back();
@ -450,18 +449,7 @@ void Project::loadDirectory(const std::string &root, Project::Folder &folder) {
entry.args.push_back(intern(args[i])); entry.args.push_back(intern(args[i]));
} }
entry.compdb_size = entry.args.size(); entry.compdb_size = entry.args.size();
// Work around relative --sysroot= as it isn't affected by
// -working-directory=. chdir is thread hostile but this function runs
// before indexers do actual work and it works when there is only one
// workspace folder.
if (!once) {
once = true;
llvm::vfs::getRealFileSystem()->setCurrentWorkingDirectory(
entry.directory);
}
proc.getSearchDirs(entry); proc.getSearchDirs(entry);
if (seen.insert(entry.filename).second) if (seen.insert(entry.filename).second)
folder.entries.push_back(entry); folder.entries.push_back(entry);
} }