2018-08-15 00:03:32 +08:00
|
|
|
//===--- GlobalCompilationDatabase.cpp ---------------------------*- C++-*-===//
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
#include "GlobalCompilationDatabase.h"
|
2019-07-19 00:13:23 +08:00
|
|
|
#include "FS.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Logger.h"
|
|
|
|
#include "support/Path.h"
|
2019-01-22 17:10:20 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Tooling/ArgumentsAdjusters.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2019-07-11 17:54:31 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
2019-01-22 17:10:20 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2019-07-11 17:54:31 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2019-07-19 00:13:23 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
[clangd] (take 2) Try harder to find a plausible `clang` as argv0, particularly on Mac.
Summary:
This was originally committed in 88bccded8fa169481fa367debf5ec615640635a1,
and reverted in 93f77617abba512d2861e2fc50ce385883f587b6.
This version is now much more testable: the "detect toolchain properties" part
is still not tested but also not active in tests.
All the command manipulation based on the detected properties is
directly tested, and also not active in other tests.
Fixes https://github.com/clangd/clangd/issues/211
Fixes https://github.com/clangd/clangd/issues/178
Reviewers: kbobyrev, ilya-biryukov
Subscribers: mgorny, ormris, cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71029
2019-11-30 02:37:48 +08:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
[clangd] (take 2) Try harder to find a plausible `clang` as argv0, particularly on Mac.
Summary:
This was originally committed in 88bccded8fa169481fa367debf5ec615640635a1,
and reverted in 93f77617abba512d2861e2fc50ce385883f587b6.
This version is now much more testable: the "detect toolchain properties" part
is still not tested but also not active in tests.
All the command manipulation based on the detected properties is
directly tested, and also not active in other tests.
Fixes https://github.com/clangd/clangd/issues/211
Fixes https://github.com/clangd/clangd/issues/178
Reviewers: kbobyrev, ilya-biryukov
Subscribers: mgorny, ormris, cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71029
2019-11-30 02:37:48 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2019-07-11 17:54:31 +08:00
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-07-06 16:44:54 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2019-01-22 17:10:20 +08:00
|
|
|
namespace {
|
|
|
|
|
2019-07-11 17:54:31 +08:00
|
|
|
// Runs the given action on all parent directories of filename, starting from
|
|
|
|
// deepest directory and going up to root. Stops whenever action succeeds.
|
|
|
|
void actOnAllParentDirectories(PathRef FileName,
|
|
|
|
llvm::function_ref<bool(PathRef)> Action) {
|
|
|
|
for (auto Path = llvm::sys::path::parent_path(FileName);
|
|
|
|
!Path.empty() && !Action(Path);
|
|
|
|
Path = llvm::sys::path::parent_path(Path))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:10:20 +08:00
|
|
|
} // namespace
|
2017-07-06 16:44:54 +08:00
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
tooling::CompileCommand
|
|
|
|
GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
|
[clangd] (take 2) Try harder to find a plausible `clang` as argv0, particularly on Mac.
Summary:
This was originally committed in 88bccded8fa169481fa367debf5ec615640635a1,
and reverted in 93f77617abba512d2861e2fc50ce385883f587b6.
This version is now much more testable: the "detect toolchain properties" part
is still not tested but also not active in tests.
All the command manipulation based on the detected properties is
directly tested, and also not active in other tests.
Fixes https://github.com/clangd/clangd/issues/211
Fixes https://github.com/clangd/clangd/issues/178
Reviewers: kbobyrev, ilya-biryukov
Subscribers: mgorny, ormris, cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71029
2019-11-30 02:37:48 +08:00
|
|
|
std::vector<std::string> Argv = {"clang"};
|
2019-06-18 19:54:17 +08:00
|
|
|
// Clang treats .h files as C by default and files without extension as linker
|
|
|
|
// input, resulting in unhelpful diagnostics.
|
2018-04-20 19:35:17 +08:00
|
|
|
// Parsing as Objective C++ is friendly to more cases.
|
2019-06-18 19:54:17 +08:00
|
|
|
auto FileExtension = llvm::sys::path::extension(File);
|
|
|
|
if (FileExtension.empty() || FileExtension == ".h")
|
2018-04-20 19:35:17 +08:00
|
|
|
Argv.push_back("-xobjective-c++-header");
|
2020-01-29 03:23:46 +08:00
|
|
|
Argv.push_back(std::string(File));
|
2019-04-15 20:32:28 +08:00
|
|
|
tooling::CompileCommand Cmd(llvm::sys::path::parent_path(File),
|
|
|
|
llvm::sys::path::filename(File), std::move(Argv),
|
|
|
|
/*Output=*/"");
|
|
|
|
Cmd.Heuristic = "clangd fallback";
|
|
|
|
return Cmd;
|
2017-07-06 16:44:54 +08:00
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::
|
2019-01-07 23:45:19 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase(
|
|
|
|
llvm::Optional<Path> CompileCommandsDir)
|
2017-12-13 20:51:22 +08:00
|
|
|
: CompileCommandsDir(std::move(CompileCommandsDir)) {}
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2018-04-20 19:35:17 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::
|
|
|
|
~DirectoryBasedGlobalCompilationDatabase() = default;
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<tooling::CompileCommand>
|
2019-07-11 17:54:31 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
|
|
|
|
CDBLookupRequest Req;
|
|
|
|
Req.FileName = File;
|
|
|
|
Req.ShouldBroadcast = true;
|
|
|
|
|
|
|
|
auto Res = lookupCDB(Req);
|
|
|
|
if (!Res) {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
log("Failed to find compilation database for {0}", File);
|
2019-07-11 17:54:31 +08:00
|
|
|
return llvm::None;
|
2017-07-06 16:44:54 +08:00
|
|
|
}
|
2019-07-11 17:54:31 +08:00
|
|
|
|
|
|
|
auto Candidates = Res->CDB->getCompileCommands(File);
|
|
|
|
if (!Candidates.empty())
|
|
|
|
return std::move(Candidates.front());
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2017-12-04 18:08:45 +08:00
|
|
|
}
|
2017-07-06 16:44:54 +08:00
|
|
|
|
2019-07-26 22:07:11 +08:00
|
|
|
// For platforms where paths are case-insensitive (but case-preserving),
|
|
|
|
// we need to do case-insensitive comparisons and use lowercase keys.
|
|
|
|
// FIXME: Make Path a real class with desired semantics instead.
|
|
|
|
// This class is not the only place this problem exists.
|
|
|
|
// FIXME: Mac filesystems default to case-insensitive, but may be sensitive.
|
|
|
|
|
|
|
|
static std::string maybeCaseFoldPath(PathRef Path) {
|
|
|
|
#if defined(_WIN32) || defined(__APPLE__)
|
|
|
|
return Path.lower();
|
|
|
|
#else
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(Path);
|
2019-07-26 22:07:11 +08:00
|
|
|
#endif
|
|
|
|
}
|
2019-07-11 17:54:31 +08:00
|
|
|
|
2019-07-26 22:07:11 +08:00
|
|
|
static bool pathEqual(PathRef A, PathRef B) {
|
|
|
|
#if defined(_WIN32) || defined(__APPLE__)
|
|
|
|
return A.equals_lower(B);
|
|
|
|
#else
|
|
|
|
return A == B;
|
|
|
|
#endif
|
|
|
|
}
|
2019-07-11 17:54:31 +08:00
|
|
|
|
2019-07-26 22:07:11 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::CachedCDB &
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
|
|
|
|
// FIXME(ibiryukov): Invalidate cached compilation databases on changes
|
|
|
|
auto Key = maybeCaseFoldPath(Dir);
|
|
|
|
auto R = CompilationDatabases.try_emplace(Key);
|
|
|
|
if (R.second) { // Cache miss, try to load CDB.
|
|
|
|
CachedCDB &Entry = R.first->second;
|
2020-01-29 18:54:22 +08:00
|
|
|
std::string Error;
|
2020-01-29 03:23:46 +08:00
|
|
|
Entry.Path = std::string(Dir);
|
2020-04-22 20:24:12 +08:00
|
|
|
Entry.CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
|
|
|
|
// Check for $src/build, the conventional CMake build root.
|
|
|
|
// Probe existence first to avoid each plugin doing IO if it doesn't exist.
|
|
|
|
if (!CompileCommandsDir && !Entry.CDB) {
|
|
|
|
llvm::SmallString<256> BuildDir = Dir;
|
|
|
|
llvm::sys::path::append(BuildDir, "build");
|
|
|
|
if (llvm::sys::fs::is_directory(BuildDir)) {
|
|
|
|
vlog("Found candidate build directory {0}", BuildDir);
|
|
|
|
Entry.CDB =
|
|
|
|
tooling::CompilationDatabase::loadFromDirectory(BuildDir, Error);
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 18:54:22 +08:00
|
|
|
if (Entry.CDB)
|
|
|
|
log("Loaded compilation database from {0}", Dir);
|
2019-07-26 22:07:11 +08:00
|
|
|
}
|
|
|
|
return R.first->second;
|
2017-10-02 23:13:20 +08:00
|
|
|
}
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2019-07-11 17:54:31 +08:00
|
|
|
llvm::Optional<DirectoryBasedGlobalCompilationDatabase::CDBLookupResult>
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::lookupCDB(
|
|
|
|
CDBLookupRequest Request) const {
|
|
|
|
assert(llvm::sys::path::is_absolute(Request.FileName) &&
|
2017-12-22 17:47:34 +08:00
|
|
|
"path must be absolute");
|
2017-10-02 23:13:20 +08:00
|
|
|
|
2019-07-26 22:07:11 +08:00
|
|
|
bool ShouldBroadcast = false;
|
2019-07-11 17:54:31 +08:00
|
|
|
CDBLookupResult Result;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2019-07-26 22:07:11 +08:00
|
|
|
CachedCDB *Entry = nullptr;
|
2019-07-11 17:54:31 +08:00
|
|
|
if (CompileCommandsDir) {
|
2019-07-26 22:07:11 +08:00
|
|
|
Entry = &getCDBInDirLocked(*CompileCommandsDir);
|
2019-07-11 17:54:31 +08:00
|
|
|
} else {
|
2019-07-19 00:13:23 +08:00
|
|
|
// Traverse the canonical version to prevent false positives. i.e.:
|
|
|
|
// src/build/../a.cc can detect a CDB in /src/build if not canonicalized.
|
2019-07-26 22:07:11 +08:00
|
|
|
// FIXME(sammccall): this loop is hot, use a union-find-like structure.
|
2019-07-19 00:13:23 +08:00
|
|
|
actOnAllParentDirectories(removeDots(Request.FileName),
|
2019-07-26 22:07:11 +08:00
|
|
|
[&](PathRef Path) {
|
|
|
|
Entry = &getCDBInDirLocked(Path);
|
|
|
|
return Entry->CDB != nullptr;
|
2019-07-19 00:13:23 +08:00
|
|
|
});
|
2019-07-11 17:54:31 +08:00
|
|
|
}
|
|
|
|
|
2019-07-26 22:07:11 +08:00
|
|
|
if (!Entry || !Entry->CDB)
|
2019-07-11 17:54:31 +08:00
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
// Mark CDB as broadcasted to make sure discovery is performed once.
|
2019-07-26 22:07:11 +08:00
|
|
|
if (Request.ShouldBroadcast && !Entry->SentBroadcast) {
|
|
|
|
Entry->SentBroadcast = true;
|
|
|
|
ShouldBroadcast = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result.CDB = Entry->CDB.get();
|
|
|
|
Result.PI.SourceRoot = Entry->Path;
|
2019-07-11 17:54:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Maybe make the following part async, since this can block retrieval
|
|
|
|
// of compile commands.
|
2019-07-26 22:07:11 +08:00
|
|
|
if (ShouldBroadcast)
|
2019-07-11 17:54:31 +08:00
|
|
|
broadcastCDB(Result);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryBasedGlobalCompilationDatabase::broadcastCDB(
|
|
|
|
CDBLookupResult Result) const {
|
|
|
|
assert(Result.CDB && "Trying to broadcast an invalid CDB!");
|
|
|
|
|
|
|
|
std::vector<std::string> AllFiles = Result.CDB->getAllFiles();
|
|
|
|
// We assume CDB in CompileCommandsDir owns all of its entries, since we don't
|
|
|
|
// perform any search in parent paths whenever it is set.
|
2018-11-20 18:56:03 +08:00
|
|
|
if (CompileCommandsDir) {
|
2019-07-11 17:54:31 +08:00
|
|
|
assert(*CompileCommandsDir == Result.PI.SourceRoot &&
|
|
|
|
"Trying to broadcast a CDB outside of CompileCommandsDir!");
|
|
|
|
OnCommandChanged.broadcast(std::move(AllFiles));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StringMap<bool> DirectoryHasCDB;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
// Uniquify all parent directories of all files.
|
|
|
|
for (llvm::StringRef File : AllFiles) {
|
|
|
|
actOnAllParentDirectories(File, [&](PathRef Path) {
|
|
|
|
auto It = DirectoryHasCDB.try_emplace(Path);
|
|
|
|
// Already seen this path, and all of its parents.
|
|
|
|
if (!It.second)
|
|
|
|
return true;
|
|
|
|
|
2019-07-26 22:07:11 +08:00
|
|
|
CachedCDB &Entry = getCDBInDirLocked(Path);
|
|
|
|
It.first->second = Entry.CDB != nullptr;
|
|
|
|
return pathEqual(Path, Result.PI.SourceRoot);
|
2019-07-11 17:54:31 +08:00
|
|
|
});
|
2018-11-20 18:56:03 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 17:54:31 +08:00
|
|
|
|
|
|
|
std::vector<std::string> GovernedFiles;
|
|
|
|
for (llvm::StringRef File : AllFiles) {
|
|
|
|
// A file is governed by this CDB if lookup for the file would find it.
|
|
|
|
// Independent of whether it has an entry for that file or not.
|
|
|
|
actOnAllParentDirectories(File, [&](PathRef Path) {
|
|
|
|
if (DirectoryHasCDB.lookup(Path)) {
|
2019-07-26 22:07:11 +08:00
|
|
|
if (pathEqual(Path, Result.PI.SourceRoot))
|
2019-07-19 00:13:23 +08:00
|
|
|
// Make sure listeners always get a canonical path for the file.
|
|
|
|
GovernedFiles.push_back(removeDots(File));
|
2019-07-11 17:54:31 +08:00
|
|
|
// Stop as soon as we hit a CDB.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
OnCommandChanged.broadcast(std::move(GovernedFiles));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<ProjectInfo>
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::getProjectInfo(PathRef File) const {
|
|
|
|
CDBLookupRequest Req;
|
|
|
|
Req.FileName = File;
|
|
|
|
Req.ShouldBroadcast = false;
|
|
|
|
auto Res = lookupCDB(Req);
|
|
|
|
if (!Res)
|
|
|
|
return llvm::None;
|
|
|
|
return Res->PI;
|
2018-11-20 18:56:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
|
2019-01-22 17:10:20 +08:00
|
|
|
std::vector<std::string> FallbackFlags,
|
[clangd] (take 2) Try harder to find a plausible `clang` as argv0, particularly on Mac.
Summary:
This was originally committed in 88bccded8fa169481fa367debf5ec615640635a1,
and reverted in 93f77617abba512d2861e2fc50ce385883f587b6.
This version is now much more testable: the "detect toolchain properties" part
is still not tested but also not active in tests.
All the command manipulation based on the detected properties is
directly tested, and also not active in other tests.
Fixes https://github.com/clangd/clangd/issues/211
Fixes https://github.com/clangd/clangd/issues/178
Reviewers: kbobyrev, ilya-biryukov
Subscribers: mgorny, ormris, cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71029
2019-11-30 02:37:48 +08:00
|
|
|
tooling::ArgumentsAdjuster Adjuster)
|
|
|
|
: Base(Base), ArgsAdjuster(std::move(Adjuster)),
|
2019-01-22 17:10:20 +08:00
|
|
|
FallbackFlags(std::move(FallbackFlags)) {
|
2018-11-20 18:56:03 +08:00
|
|
|
if (Base)
|
|
|
|
BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
|
|
|
|
OnCommandChanged.broadcast(Changes);
|
|
|
|
});
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-07-06 16:44:54 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<tooling::CompileCommand>
|
2019-07-11 17:54:31 +08:00
|
|
|
OverlayCDB::getCompileCommand(PathRef File) const {
|
2019-01-22 17:10:20 +08:00
|
|
|
llvm::Optional<tooling::CompileCommand> Cmd;
|
2018-11-02 21:09:36 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2019-07-19 00:13:23 +08:00
|
|
|
auto It = Commands.find(removeDots(File));
|
2019-07-11 17:54:31 +08:00
|
|
|
if (It != Commands.end())
|
2019-01-22 17:10:20 +08:00
|
|
|
Cmd = It->second;
|
2018-11-02 21:09:36 +08:00
|
|
|
}
|
2019-01-22 17:10:20 +08:00
|
|
|
if (!Cmd && Base)
|
2019-07-11 17:54:31 +08:00
|
|
|
Cmd = Base->getCompileCommand(File);
|
2019-01-22 17:10:20 +08:00
|
|
|
if (!Cmd)
|
|
|
|
return llvm::None;
|
[clangd] (take 2) Try harder to find a plausible `clang` as argv0, particularly on Mac.
Summary:
This was originally committed in 88bccded8fa169481fa367debf5ec615640635a1,
and reverted in 93f77617abba512d2861e2fc50ce385883f587b6.
This version is now much more testable: the "detect toolchain properties" part
is still not tested but also not active in tests.
All the command manipulation based on the detected properties is
directly tested, and also not active in other tests.
Fixes https://github.com/clangd/clangd/issues/211
Fixes https://github.com/clangd/clangd/issues/178
Reviewers: kbobyrev, ilya-biryukov
Subscribers: mgorny, ormris, cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71029
2019-11-30 02:37:48 +08:00
|
|
|
if (ArgsAdjuster)
|
|
|
|
Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, Cmd->Filename);
|
2019-01-22 17:10:20 +08:00
|
|
|
return Cmd;
|
2018-11-02 21:09:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
|
|
|
|
auto Cmd = Base ? Base->getFallbackCommand(File)
|
|
|
|
: GlobalCompilationDatabase::getFallbackCommand(File);
|
2018-08-02 01:39:29 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2018-11-02 21:09:36 +08:00
|
|
|
Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
|
|
|
|
FallbackFlags.end());
|
[clangd] (take 2) Try harder to find a plausible `clang` as argv0, particularly on Mac.
Summary:
This was originally committed in 88bccded8fa169481fa367debf5ec615640635a1,
and reverted in 93f77617abba512d2861e2fc50ce385883f587b6.
This version is now much more testable: the "detect toolchain properties" part
is still not tested but also not active in tests.
All the command manipulation based on the detected properties is
directly tested, and also not active in other tests.
Fixes https://github.com/clangd/clangd/issues/211
Fixes https://github.com/clangd/clangd/issues/178
Reviewers: kbobyrev, ilya-biryukov
Subscribers: mgorny, ormris, cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71029
2019-11-30 02:37:48 +08:00
|
|
|
if (ArgsAdjuster)
|
|
|
|
Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename);
|
2018-11-02 21:09:36 +08:00
|
|
|
return Cmd;
|
2018-08-02 01:39:29 +08:00
|
|
|
}
|
|
|
|
|
2018-11-02 21:09:36 +08:00
|
|
|
void OverlayCDB::setCompileCommand(
|
|
|
|
PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
|
2019-07-19 00:13:23 +08:00
|
|
|
// We store a canonical version internally to prevent mismatches between set
|
|
|
|
// and get compile commands. Also it assures clients listening to broadcasts
|
|
|
|
// doesn't receive different names for the same file.
|
|
|
|
std::string CanonPath = removeDots(File);
|
2018-11-20 18:56:03 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
if (Cmd)
|
2019-07-19 00:13:23 +08:00
|
|
|
Commands[CanonPath] = std::move(*Cmd);
|
2018-11-20 18:56:03 +08:00
|
|
|
else
|
2019-07-19 00:13:23 +08:00
|
|
|
Commands.erase(CanonPath);
|
2018-11-20 18:56:03 +08:00
|
|
|
}
|
2019-07-19 00:13:23 +08:00
|
|
|
OnCommandChanged.broadcast({CanonPath});
|
2018-08-02 01:39:29 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:54:31 +08:00
|
|
|
llvm::Optional<ProjectInfo> OverlayCDB::getProjectInfo(PathRef File) const {
|
[clangd] Always retrieve ProjectInfo from Base in OverlayCDB
Summary:
Clangd is returning current working directory for overriden commands.
This can cause inconsistencies between:
- header and the main files, as OverlayCDB only contains entries for the main
files it direct any queries for the headers to the base, creating a
discrepancy between the two.
- different clangd instances, as the results will be different depending on the
timing of execution of the query and override of the command. hence clangd
might see two different project infos for the same file between different
invocations.
- editors and the way user has invoked it, as current working directory of
clangd will depend on those, hence even when there's no underlying base CWD
might change depending on the editor, or the directory user has started the
editor in.
This patch gets rid of that discrepency by always directing queries to base or
returning llvm::None in absence of it.
For a sample bug see https://reviews.llvm.org/D83099#2154185.
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D83934
2020-07-16 17:27:31 +08:00
|
|
|
// It wouldn't make much sense to treat files with overridden commands
|
|
|
|
// specially when we can't do the same for the (unknown) local headers they
|
|
|
|
// include or changing behavior mid-air after receiving an override.
|
2019-07-11 17:54:31 +08:00
|
|
|
if (Base)
|
|
|
|
return Base->getProjectInfo(File);
|
|
|
|
return llvm::None;
|
|
|
|
}
|
2017-07-06 16:44:54 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|