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"
|
[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:
|
2017-10-02 23:13:20 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase(
|
2017-12-13 20:51:22 +08:00
|
|
|
llvm::Optional<Path> CompileCommandsDir);
|
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:
|
2019-07-26 22:07:11 +08:00
|
|
|
/// Caches compilation databases loaded from directories.
|
|
|
|
struct CachedCDB {
|
|
|
|
std::string Path; // Not case-folded.
|
|
|
|
std::unique_ptr<clang::tooling::CompilationDatabase> CDB = nullptr;
|
|
|
|
bool SentBroadcast = false;
|
|
|
|
};
|
|
|
|
CachedCDB &getCDBInDirLocked(PathRef File) 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;
|
|
|
|
};
|
|
|
|
struct CDBLookupResult {
|
|
|
|
tooling::CompilationDatabase *CDB = nullptr;
|
|
|
|
ProjectInfo PI;
|
|
|
|
};
|
|
|
|
llvm::Optional<CDBLookupResult> lookupCDB(CDBLookupRequest Request) const;
|
|
|
|
|
|
|
|
// Performs broadcast on governed files.
|
|
|
|
void broadcastCDB(CDBLookupResult Res) const;
|
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
mutable std::mutex Mutex;
|
2019-07-26 22:07:11 +08:00
|
|
|
// Keyed by possibly-case-folded directory path.
|
2019-07-11 17:54:31 +08:00
|
|
|
mutable llvm::StringMap<CachedCDB> CompilationDatabases;
|
2017-07-06 16:44:54 +08:00
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
/// Used for command argument pointing to folder where compile_commands.json
|
|
|
|
/// is located.
|
|
|
|
llvm::Optional<Path> CompileCommandsDir;
|
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);
|
|
|
|
|
[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
|
|
|
|
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;
|
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
|