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"
|
2020-12-01 19:48:41 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.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"
|
2020-12-01 19:48:41 +08:00
|
|
|
#include <chrono>
|
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
|
|
|
|
2020-12-01 19:48:41 +08:00
|
|
|
// Loads and caches the CDB from a single directory.
|
|
|
|
//
|
|
|
|
// This class is threadsafe, which is to say we have independent locks for each
|
|
|
|
// directory we're searching for a CDB.
|
|
|
|
// Loading is deferred until first access.
|
|
|
|
//
|
|
|
|
// The DirectoryBasedCDB keeps a map from path => DirectoryCache.
|
|
|
|
// Typical usage is to:
|
|
|
|
// - 1) determine all the paths that might be searched
|
|
|
|
// - 2) acquire the map lock and get-or-create all the DirectoryCache entries
|
|
|
|
// - 3) release the map lock and query the caches as desired
|
|
|
|
//
|
|
|
|
// FIXME: this should revalidate the cache sometimes
|
|
|
|
// FIXME: IO should go through a VFS
|
|
|
|
class DirectoryBasedGlobalCompilationDatabase::DirectoryCache {
|
|
|
|
// Absolute canonical path that we're the cache for. (Not case-folded).
|
|
|
|
const std::string Path;
|
|
|
|
|
|
|
|
// True if we've looked for a CDB here and found none.
|
|
|
|
// (This makes it possible for get() to return without taking a lock)
|
|
|
|
// FIXME: this should have an expiry time instead of lasting forever.
|
|
|
|
std::atomic<bool> FinalizedNoCDB = {false};
|
|
|
|
|
|
|
|
// Guards following cache state.
|
|
|
|
std::mutex Mu;
|
|
|
|
// Has cache been filled from disk? FIXME: this should be an expiry time.
|
|
|
|
bool CachePopulated = false;
|
|
|
|
// Whether a new CDB has been loaded but not broadcast yet.
|
|
|
|
bool NeedsBroadcast = false;
|
|
|
|
// Last loaded CDB, meaningful if CachePopulated is set.
|
|
|
|
// shared_ptr so we can overwrite this when callers are still using the CDB.
|
|
|
|
std::shared_ptr<tooling::CompilationDatabase> CDB;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DirectoryCache(llvm::StringRef Path) : Path(Path) {
|
|
|
|
assert(llvm::sys::path::is_absolute(Path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the CDB associated with this directory.
|
|
|
|
// ShouldBroadcast:
|
|
|
|
// - as input, signals whether the caller is willing to broadcast a
|
|
|
|
// newly-discovered CDB. (e.g. to trigger background indexing)
|
|
|
|
// - as output, signals whether the caller should do so.
|
|
|
|
// (If a new CDB is discovered and ShouldBroadcast is false, we mark the
|
|
|
|
// CDB as needing broadcast, and broadcast it next time we can).
|
|
|
|
std::shared_ptr<const tooling::CompilationDatabase>
|
|
|
|
get(bool &ShouldBroadcast) {
|
|
|
|
// Fast path for common case without taking lock.
|
|
|
|
if (FinalizedNoCDB.load()) {
|
|
|
|
ShouldBroadcast = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
|
|
|
auto RequestBroadcast = llvm::make_scope_exit([&, OldCDB(CDB.get())] {
|
|
|
|
// If we loaded a new CDB, it should be broadcast at some point.
|
|
|
|
if (CDB != nullptr && CDB.get() != OldCDB)
|
|
|
|
NeedsBroadcast = true;
|
|
|
|
else if (CDB == nullptr) // nothing to broadcast anymore!
|
|
|
|
NeedsBroadcast = false;
|
|
|
|
// If we have something to broadcast, then do so iff allowed.
|
|
|
|
if (!ShouldBroadcast)
|
|
|
|
return;
|
|
|
|
ShouldBroadcast = NeedsBroadcast;
|
|
|
|
NeedsBroadcast = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// For now, we never actually attempt to revalidate a populated cache.
|
|
|
|
if (CachePopulated)
|
|
|
|
return CDB;
|
|
|
|
assert(CDB == nullptr);
|
|
|
|
|
|
|
|
load();
|
|
|
|
CachePopulated = true;
|
|
|
|
|
|
|
|
if (!CDB)
|
|
|
|
FinalizedNoCDB.store(true);
|
|
|
|
return CDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StringRef path() const { return Path; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Updates `CDB` from disk state.
|
|
|
|
void load() {
|
|
|
|
std::string Error; // ignored, because it's often "didn't find anything".
|
|
|
|
CDB = tooling::CompilationDatabase::loadFromDirectory(Path, Error);
|
|
|
|
if (!CDB) {
|
|
|
|
// Fallback: check for $src/build, the conventional CMake build root.
|
|
|
|
// Probe existence first to avoid each plugin doing IO if it doesn't
|
|
|
|
// exist.
|
|
|
|
llvm::SmallString<256> BuildDir(Path);
|
|
|
|
llvm::sys::path::append(BuildDir, "build");
|
|
|
|
if (llvm::sys::fs::is_directory(BuildDir)) {
|
|
|
|
vlog("Found candidate build directory {0}", BuildDir);
|
|
|
|
CDB = tooling::CompilationDatabase::loadFromDirectory(BuildDir, Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (CDB) {
|
|
|
|
log("Loaded compilation database from {0}", Path);
|
|
|
|
} else {
|
|
|
|
vlog("No compilation database at {0}", Path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::
|
2019-01-07 23:45:19 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase(
|
2020-12-01 19:48:41 +08:00
|
|
|
llvm::Optional<Path> CompileCommandsDir) {
|
|
|
|
if (CompileCommandsDir)
|
|
|
|
OnlyDirCache = std::make_unique<DirectoryCache>(*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
|
|
|
|
2020-12-01 19:48:41 +08:00
|
|
|
std::vector<DirectoryBasedGlobalCompilationDatabase::DirectoryCache *>
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::getDirectoryCaches(
|
|
|
|
llvm::ArrayRef<llvm::StringRef> Dirs) const {
|
|
|
|
std::vector<std::string> FoldedDirs;
|
|
|
|
FoldedDirs.reserve(Dirs.size());
|
|
|
|
for (const auto &Dir : Dirs)
|
|
|
|
FoldedDirs.push_back(maybeCaseFoldPath(Dir));
|
|
|
|
|
|
|
|
std::vector<DirectoryCache *> Ret;
|
|
|
|
Ret.reserve(Dirs.size());
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> Lock(DirCachesMutex);
|
|
|
|
for (unsigned I = 0; I < Dirs.size(); ++I)
|
|
|
|
Ret.push_back(&DirCaches.try_emplace(FoldedDirs[I], Dirs[I]).first->second);
|
|
|
|
return Ret;
|
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;
|
2020-12-01 19:48:41 +08:00
|
|
|
DirectoryCache *DirCache = nullptr;
|
|
|
|
std::shared_ptr<const tooling::CompilationDatabase> CDB = nullptr;
|
|
|
|
if (OnlyDirCache) {
|
|
|
|
DirCache = OnlyDirCache.get();
|
|
|
|
ShouldBroadcast = Request.ShouldBroadcast;
|
|
|
|
CDB = DirCache->get(ShouldBroadcast);
|
|
|
|
} else {
|
|
|
|
// Traverse the canonical version to prevent false positives. i.e.:
|
|
|
|
// src/build/../a.cc can detect a CDB in /src/build if not canonicalized.
|
|
|
|
std::string CanonicalPath = removeDots(Request.FileName);
|
|
|
|
std::vector<llvm::StringRef> SearchDirs;
|
|
|
|
actOnAllParentDirectories(CanonicalPath, [&](PathRef Path) {
|
|
|
|
SearchDirs.push_back(Path);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
for (DirectoryCache *Candidate : getDirectoryCaches(SearchDirs)) {
|
|
|
|
bool CandidateShouldBroadcast = Request.ShouldBroadcast;
|
|
|
|
if ((CDB = Candidate->get(CandidateShouldBroadcast))) {
|
|
|
|
DirCache = Candidate;
|
|
|
|
ShouldBroadcast = CandidateShouldBroadcast;
|
|
|
|
break;
|
|
|
|
}
|
2019-07-11 17:54:31 +08:00
|
|
|
}
|
2020-12-01 19:48:41 +08:00
|
|
|
}
|
2019-07-11 17:54:31 +08:00
|
|
|
|
2020-12-01 19:48:41 +08:00
|
|
|
if (!CDB)
|
|
|
|
return llvm::None;
|
2019-07-26 22:07:11 +08:00
|
|
|
|
2020-12-01 19:48:41 +08:00
|
|
|
CDBLookupResult Result;
|
|
|
|
Result.CDB = std::move(CDB);
|
|
|
|
Result.PI.SourceRoot = DirCache->path().str();
|
2019-07-11 17:54:31 +08:00
|
|
|
|
2020-12-01 19:48:41 +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.
|
2020-12-01 19:48:41 +08:00
|
|
|
if (OnlyDirCache) {
|
|
|
|
assert(OnlyDirCache->path() == Result.PI.SourceRoot &&
|
2019-07-11 17:54:31 +08:00
|
|
|
"Trying to broadcast a CDB outside of CompileCommandsDir!");
|
|
|
|
OnCommandChanged.broadcast(std::move(AllFiles));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-01 19:48:41 +08:00
|
|
|
// Uniquify all parent directories of all files.
|
2019-07-11 17:54:31 +08:00
|
|
|
llvm::StringMap<bool> DirectoryHasCDB;
|
2020-12-01 19:48:41 +08:00
|
|
|
std::vector<llvm::StringRef> FileAncestors;
|
|
|
|
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;
|
|
|
|
|
|
|
|
FileAncestors.push_back(It.first->getKey());
|
|
|
|
return pathEqual(Path, Result.PI.SourceRoot);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Work out which ones have CDBs in them.
|
|
|
|
for (DirectoryCache *Dir : getDirectoryCaches(FileAncestors)) {
|
|
|
|
bool ShouldBroadcast = false;
|
|
|
|
if (Dir->get(ShouldBroadcast))
|
|
|
|
DirectoryHasCDB.find(Dir->path())->setValue(true);
|
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
|