blob: 0a97a30e0bd5ace2059adaa0bd4ea9e93445bf27 [file] [log] [blame]
//===--- GlobalCompilationDatabase.h -----------------------------*- C++-*-===//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "Function.h"
#include "Path.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringMap.h"
#include <memory>
#include <mutex>
#include <vector>
namespace clang {
namespace tooling {
class CompilationDatabase;
struct CompileCommand;
} // namespace tooling
namespace clangd {
class Logger;
struct ProjectInfo {
// The directory in which the compilation database was discovered.
// Empty if directory-based compilation database discovery was not used.
std::string SourceRoot;
/// Provides compilation arguments used for parsing C and C++ files.
class GlobalCompilationDatabase {
virtual ~GlobalCompilationDatabase() = default;
/// If there are any known-good commands for building this file, returns one.
/// If the ProjectInfo pointer is set, it will also be populated.
virtual llvm::Optional<tooling::CompileCommand>
getCompileCommand(PathRef File, ProjectInfo * = nullptr) const = 0;
/// 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;
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));
mutable CommandChanged OnCommandChanged;
/// Gets compile args from tooling::CompilationDatabases built for parent
/// directories.
class DirectoryBasedGlobalCompilationDatabase
: public GlobalCompilationDatabase {
llvm::Optional<Path> CompileCommandsDir);
~DirectoryBasedGlobalCompilationDatabase() override;
/// Scans File's parents looking for compilation databases.
/// Any extra flags will be added.
getCompileCommand(PathRef File, ProjectInfo * = nullptr) const override;
tooling::CompilationDatabase *getCDBForFile(PathRef File,
ProjectInfo *) const;
std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
getCDBInDirLocked(PathRef File) const;
mutable std::mutex Mutex;
/// Caches compilation databases loaded from directories(keys are
/// directories).
mutable llvm::StringMap<std::unique_ptr<clang::tooling::CompilationDatabase>>
/// Used for command argument pointing to folder where compile_commands.json
/// is located.
llvm::Optional<Path> CompileCommandsDir;
/// Wraps another compilation database, and supports overriding the commands
/// using an in-memory mapping.
class OverlayCDB : public GlobalCompilationDatabase {
// Base may be null, in which case no entries are inherited.
// FallbackFlags are added to the fallback compile command.
OverlayCDB(const GlobalCompilationDatabase *Base,
std::vector<std::string> FallbackFlags = {},
llvm::Optional<std::string> ResourceDir = llvm::None);
getCompileCommand(PathRef File, ProjectInfo * = nullptr) const override;
tooling::CompileCommand getFallbackCommand(PathRef File) const override;
/// Sets or clears the compilation command for a particular file.
setCompileCommand(PathRef File,
llvm::Optional<tooling::CompileCommand> CompilationCommand);
mutable std::mutex Mutex;
llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */
const GlobalCompilationDatabase *Base;
std::string ResourceDir;
std::vector<std::string> FallbackFlags;
CommandChanged::Subscription BaseChanged;
} // namespace clangd
} // namespace clang