2018-08-15 00:03:32 +08:00
|
|
|
//===--- GlobalCompilationDatabase.h -----------------------------*- 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
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_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 "CompileCommands.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/Function.h"
|
|
|
|
#include "support/Path.h"
|
2020-12-04 16:09:03 +08:00
|
|
|
#include "support/ThreadsafeFS.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 "clang/Tooling/ArgumentsAdjusters.h"
|
2019-07-11 07:38:00 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2019-01-22 17:10:20 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2017-05-16 18:06:20 +08:00
|
|
|
#include <vector>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
class Logger;
|
|
|
|
|
[clangd] Auto-index watches global CDB for changes.
Summary:
Instead of receiving compilation commands, auto-index is triggered by just
filenames to reindex, and gets commands from the global comp DB internally.
This has advantages:
- more of the work can be done asynchronously (fetching compilation commands
upfront can be slow for large CDBs)
- we get access to the CDB which can be used to retrieve interpolated commands
for headers (useful in some cases where the original TU goes away)
- fits nicely with the filename-only change observation from r347297
The interface to GlobalCompilationDatabase gets extended: when retrieving a
compile command, the GCDB can optionally report the project the file belongs to.
This naturally fits together with getCompileCommand: it's hard to implement one
without the other. But because most callers don't care, I've ended up with an
awkward optional-out-param-in-virtual method pattern - maybe there's a better
one.
This is the main missing integration point between ClangdServer and
BackgroundIndex, after this we should be able to add an auto-index flag.
Reviewers: ioeric, kadircet
Subscribers: MaskRay, jkorous, arphaman, cfe-commits, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D54865
llvm-svn: 347538
2018-11-26 17:51:50 +08:00
|
|
|
struct ProjectInfo {
|
|
|
|
// The directory in which the compilation database was discovered.
|
|
|
|
// Empty if directory-based compilation database discovery was not used.
|
|
|
|
std::string SourceRoot;
|
|
|
|
};
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
/// Provides compilation arguments used for parsing C and C++ files.
|
2017-05-16 17:38:59 +08:00
|
|
|
class GlobalCompilationDatabase {
|
|
|
|
public:
|
|
|
|
virtual ~GlobalCompilationDatabase() = default;
|
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
/// If there are any known-good commands for building this file, returns one.
|
|
|
|
virtual llvm::Optional<tooling::CompileCommand>
|
2019-07-11 17:54:31 +08:00
|
|
|
getCompileCommand(PathRef File) const = 0;
|
|
|
|
|
|
|
|
/// Finds the closest project to \p File.
|
|
|
|
virtual llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const {
|
|
|
|
return llvm::None;
|
|
|
|
}
|
2017-12-04 18:08:45 +08:00
|
|
|
|
|
|
|
/// Makes a guess at how to build a file.
|
|
|
|
/// The default implementation just runs clang on the file.
|
|
|
|
/// Clangd should treat the results as unreliable.
|
|
|
|
virtual tooling::CompileCommand getFallbackCommand(PathRef File) const;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-11-20 18:56:03 +08:00
|
|
|
using CommandChanged = Event<std::vector<std::string>>;
|
|
|
|
/// The callback is notified when files may have new compile commands.
|
|
|
|
/// The argument is a list of full file paths.
|
|
|
|
CommandChanged::Subscription watch(CommandChanged::Listener L) const {
|
|
|
|
return OnCommandChanged.observe(std::move(L));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
mutable CommandChanged OnCommandChanged;
|
2017-05-16 17:38:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Gets compile args from tooling::CompilationDatabases built for parent
|
|
|
|
/// directories.
|
|
|
|
class DirectoryBasedGlobalCompilationDatabase
|
|
|
|
: public GlobalCompilationDatabase {
|
|
|
|
public:
|
2020-12-04 16:09:03 +08:00
|
|
|
struct Options {
|
|
|
|
Options(const ThreadsafeFS &TFS) : TFS(TFS) {}
|
|
|
|
|
|
|
|
const ThreadsafeFS &TFS;
|
|
|
|
// Frequency to check whether e.g. compile_commands.json has changed.
|
|
|
|
std::chrono::steady_clock::duration RevalidateAfter =
|
|
|
|
std::chrono::seconds(5);
|
|
|
|
// Frequency to check whether e.g. compile_commands.json has been created.
|
|
|
|
// (This is more expensive to check frequently, as we check many locations).
|
|
|
|
std::chrono::steady_clock::duration RevalidateMissingAfter =
|
|
|
|
std::chrono::seconds(30);
|
|
|
|
// Only look for a compilation database in this one fixed directory.
|
|
|
|
llvm::Optional<Path> CompileCommandsDir;
|
|
|
|
};
|
|
|
|
|
|
|
|
DirectoryBasedGlobalCompilationDatabase(const Options &Opts);
|
2018-04-20 19:35:17 +08:00
|
|
|
~DirectoryBasedGlobalCompilationDatabase() override;
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
/// Scans File's parents looking for compilation databases.
|
|
|
|
/// Any extra flags will be added.
|
2019-07-11 17:54:31 +08:00
|
|
|
/// Might trigger OnCommandChanged, if CDB wasn't broadcasted yet.
|
2017-12-04 18:08:45 +08:00
|
|
|
llvm::Optional<tooling::CompileCommand>
|
2019-07-11 17:54:31 +08:00
|
|
|
getCompileCommand(PathRef File) const override;
|
|
|
|
|
|
|
|
/// Returns the path to first directory containing a compilation database in
|
|
|
|
/// \p File's parents.
|
|
|
|
llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override;
|
2017-12-04 18:08:45 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
private:
|
2020-12-04 16:09:03 +08:00
|
|
|
Options Opts;
|
|
|
|
|
2020-12-15 21:00:03 +08:00
|
|
|
class DirectoryCache;
|
|
|
|
// If there's an explicit CompileCommandsDir, cache of the CDB found there.
|
|
|
|
mutable std::unique_ptr<DirectoryCache> OnlyDirCache;
|
|
|
|
|
|
|
|
// Keyed by possibly-case-folded directory path.
|
|
|
|
// We can hand out pointers as they're stable and entries are never removed.
|
|
|
|
// Empty if CompileCommandsDir is given (OnlyDirCache is used instead).
|
|
|
|
mutable llvm::StringMap<DirectoryCache> DirCaches;
|
|
|
|
// DirCaches access must be locked (unlike OnlyDirCache, which is threadsafe).
|
|
|
|
mutable std::mutex DirCachesMutex;
|
|
|
|
|
|
|
|
std::vector<DirectoryCache *>
|
|
|
|
getDirectoryCaches(llvm::ArrayRef<llvm::StringRef> Dirs) const;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2019-07-11 17:54:31 +08:00
|
|
|
struct CDBLookupRequest {
|
|
|
|
PathRef FileName;
|
|
|
|
// Whether this lookup should trigger discovery of the CDB found.
|
|
|
|
bool ShouldBroadcast = false;
|
2020-12-04 16:09:03 +08:00
|
|
|
// Cached results newer than this are considered fresh and not checked
|
|
|
|
// against disk.
|
|
|
|
std::chrono::steady_clock::time_point FreshTime;
|
|
|
|
std::chrono::steady_clock::time_point FreshTimeMissing;
|
2019-07-11 17:54:31 +08:00
|
|
|
};
|
|
|
|
struct CDBLookupResult {
|
2020-12-15 21:00:03 +08:00
|
|
|
std::shared_ptr<const tooling::CompilationDatabase> CDB;
|
2019-07-11 17:54:31 +08:00
|
|
|
ProjectInfo PI;
|
|
|
|
};
|
|
|
|
llvm::Optional<CDBLookupResult> lookupCDB(CDBLookupRequest Request) const;
|
|
|
|
|
|
|
|
// Performs broadcast on governed files.
|
|
|
|
void broadcastCDB(CDBLookupResult Res) const;
|
2020-12-04 16:09:03 +08:00
|
|
|
|
|
|
|
// cache test calls lookupCDB directly to ensure valid/invalid times.
|
|
|
|
friend class DirectoryBasedGlobalCompilationDatabaseCacheTest;
|
2017-05-16 17:38:59 +08:00
|
|
|
};
|
2018-06-13 17:20:41 +08:00
|
|
|
|
2019-06-26 15:45:27 +08:00
|
|
|
/// Extracts system include search path from drivers matching QueryDriverGlobs
|
|
|
|
/// and adds them to the compile flags. Base may not be nullptr.
|
|
|
|
/// Returns Base when \p QueryDriverGlobs is empty.
|
|
|
|
std::unique_ptr<GlobalCompilationDatabase>
|
|
|
|
getQueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
|
|
|
|
std::unique_ptr<GlobalCompilationDatabase> Base);
|
|
|
|
|
2018-11-02 21:09:36 +08:00
|
|
|
/// Wraps another compilation database, and supports overriding the commands
|
|
|
|
/// using an in-memory mapping.
|
|
|
|
class OverlayCDB : public GlobalCompilationDatabase {
|
2018-08-02 01:39:29 +08:00
|
|
|
public:
|
2018-11-02 21:09:36 +08:00
|
|
|
// Base may be null, in which case no entries are inherited.
|
|
|
|
// FallbackFlags are added to the fallback compile command.
|
[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
|
|
|
// Adjuster is applied to all commands, fallback or not.
|
2018-11-02 21:09:36 +08:00
|
|
|
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 = nullptr);
|
2018-11-02 21:09:36 +08:00
|
|
|
|
2018-08-02 01:39:29 +08:00
|
|
|
llvm::Optional<tooling::CompileCommand>
|
2019-07-11 17:54:31 +08:00
|
|
|
getCompileCommand(PathRef File) const override;
|
2018-11-02 21:09:36 +08:00
|
|
|
tooling::CompileCommand getFallbackCommand(PathRef File) const override;
|
[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
|
|
|
/// Project info is gathered purely from the inner compilation database to
|
|
|
|
/// ensure consistency.
|
2019-07-11 17:54:31 +08:00
|
|
|
llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override;
|
2018-08-02 01:39:29 +08:00
|
|
|
|
2018-11-02 21:09:36 +08:00
|
|
|
/// Sets or clears the compilation command for a particular file.
|
|
|
|
void
|
|
|
|
setCompileCommand(PathRef File,
|
|
|
|
llvm::Optional<tooling::CompileCommand> CompilationCommand);
|
2018-08-02 01:39:29 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::mutex Mutex;
|
|
|
|
llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */
|
2018-11-02 21:09:36 +08:00
|
|
|
const GlobalCompilationDatabase *Base;
|
[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 ArgsAdjuster;
|
2018-11-02 21:09:36 +08:00
|
|
|
std::vector<std::string> FallbackFlags;
|
2018-11-20 18:56:03 +08:00
|
|
|
CommandChanged::Subscription BaseChanged;
|
2018-08-02 01:39:29 +08:00
|
|
|
};
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
2018-08-15 00:03:32 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H
|