clang-tools  11.0.0
GlobalCompilationDatabase.h
Go to the documentation of this file.
1 //===--- GlobalCompilationDatabase.h -----------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H
11 
12 #include "CompileCommands.h"
13 #include "support/Function.h"
14 #include "support/Path.h"
15 #include "clang/Tooling/ArgumentsAdjusters.h"
16 #include "clang/Tooling/CompilationDatabase.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringMap.h"
19 #include <memory>
20 #include <mutex>
21 #include <vector>
22 
23 namespace clang {
24 namespace clangd {
25 
26 class Logger;
27 
28 struct ProjectInfo {
29  // The directory in which the compilation database was discovered.
30  // Empty if directory-based compilation database discovery was not used.
31  std::string SourceRoot;
32 };
33 
34 /// Provides compilation arguments used for parsing C and C++ files.
36 public:
37  virtual ~GlobalCompilationDatabase() = default;
38 
39  /// If there are any known-good commands for building this file, returns one.
40  virtual llvm::Optional<tooling::CompileCommand>
41  getCompileCommand(PathRef File) const = 0;
42 
43  /// Finds the closest project to \p File.
44  virtual llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const {
45  return llvm::None;
46  }
47 
48  /// Makes a guess at how to build a file.
49  /// The default implementation just runs clang on the file.
50  /// Clangd should treat the results as unreliable.
51  virtual tooling::CompileCommand getFallbackCommand(PathRef File) const;
52 
54  /// The callback is notified when files may have new compile commands.
55  /// The argument is a list of full file paths.
56  CommandChanged::Subscription watch(CommandChanged::Listener L) const {
57  return OnCommandChanged.observe(std::move(L));
58  }
59 
60 protected:
62 };
63 
64 /// Gets compile args from tooling::CompilationDatabases built for parent
65 /// directories.
67  : public GlobalCompilationDatabase {
68 public:
70  llvm::Optional<Path> CompileCommandsDir);
72 
73  /// Scans File's parents looking for compilation databases.
74  /// Any extra flags will be added.
75  /// Might trigger OnCommandChanged, if CDB wasn't broadcasted yet.
76  llvm::Optional<tooling::CompileCommand>
77  getCompileCommand(PathRef File) const override;
78 
79  /// Returns the path to first directory containing a compilation database in
80  /// \p File's parents.
81  llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override;
82 
83 private:
84  /// Caches compilation databases loaded from directories.
85  struct CachedCDB {
86  std::string Path; // Not case-folded.
87  std::unique_ptr<clang::tooling::CompilationDatabase> CDB = nullptr;
88  bool SentBroadcast = false;
89  };
90  CachedCDB &getCDBInDirLocked(PathRef File) const;
91 
92  struct CDBLookupRequest {
94  // Whether this lookup should trigger discovery of the CDB found.
95  bool ShouldBroadcast = false;
96  };
97  struct CDBLookupResult {
98  tooling::CompilationDatabase *CDB = nullptr;
99  ProjectInfo PI;
100  };
101  llvm::Optional<CDBLookupResult> lookupCDB(CDBLookupRequest Request) const;
102 
103  // Performs broadcast on governed files.
104  void broadcastCDB(CDBLookupResult Res) const;
105 
106  mutable std::mutex Mutex;
107  // Keyed by possibly-case-folded directory path.
108  mutable llvm::StringMap<CachedCDB> CompilationDatabases;
109 
110  /// Used for command argument pointing to folder where compile_commands.json
111  /// is located.
112  llvm::Optional<Path> CompileCommandsDir;
113 };
114 
115 /// Extracts system include search path from drivers matching QueryDriverGlobs
116 /// and adds them to the compile flags. Base may not be nullptr.
117 /// Returns Base when \p QueryDriverGlobs is empty.
118 std::unique_ptr<GlobalCompilationDatabase>
119 getQueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
120  std::unique_ptr<GlobalCompilationDatabase> Base);
121 
122 /// Wraps another compilation database, and supports overriding the commands
123 /// using an in-memory mapping.
125 public:
126  // Base may be null, in which case no entries are inherited.
127  // FallbackFlags are added to the fallback compile command.
128  // Adjuster is applied to all commands, fallback or not.
130  std::vector<std::string> FallbackFlags = {},
131  tooling::ArgumentsAdjuster Adjuster = nullptr);
132 
133  llvm::Optional<tooling::CompileCommand>
134  getCompileCommand(PathRef File) const override;
135  tooling::CompileCommand getFallbackCommand(PathRef File) const override;
136  /// Project info is gathered purely from the inner compilation database to
137  /// ensure consistency.
138  llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override;
139 
140  /// Sets or clears the compilation command for a particular file.
141  void
143  llvm::Optional<tooling::CompileCommand> CompilationCommand);
144 
145 private:
146  mutable std::mutex Mutex;
147  llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */
148  const GlobalCompilationDatabase *Base;
149  tooling::ArgumentsAdjuster ArgsAdjuster;
150  std::vector<std::string> FallbackFlags;
151  CommandChanged::Subscription BaseChanged;
152 };
153 
154 } // namespace clangd
155 } // namespace clang
156 
157 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H
clang::clangd::ProjectInfo::SourceRoot
std::string SourceRoot
Definition: GlobalCompilationDatabase.h:31
clang::clangd::DirectoryBasedGlobalCompilationDatabase::~DirectoryBasedGlobalCompilationDatabase
~DirectoryBasedGlobalCompilationDatabase() override
Base
std::unique_ptr< GlobalCompilationDatabase > Base
Definition: GlobalCompilationDatabaseTests.cpp:85
clang::clangd::OverlayCDB::OverlayCDB
OverlayCDB(const GlobalCompilationDatabase *Base, std::vector< std::string > FallbackFlags={}, tooling::ArgumentsAdjuster Adjuster=nullptr)
Definition: GlobalCompilationDatabase.cpp:244
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:20
Path.h
clang::clangd::DirectoryBasedGlobalCompilationDatabase::getCompileCommand
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File) const override
Scans File's parents looking for compilation databases.
Definition: GlobalCompilationDatabase.cpp:70
clang::clangd::OverlayCDB::getCompileCommand
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File) const override
If there are any known-good commands for building this file, returns one.
Definition: GlobalCompilationDatabase.cpp:256
clang::clangd::DirectoryBasedGlobalCompilationDatabase::getProjectInfo
llvm::Optional< ProjectInfo > getProjectInfo(PathRef File) const override
Returns the path to first directory containing a compilation database in File's parents.
Definition: GlobalCompilationDatabase.cpp:234
clang::clangd::OverlayCDB::getProjectInfo
llvm::Optional< ProjectInfo > getProjectInfo(PathRef File) const override
Project info is gathered purely from the inner compilation database to ensure consistency.
Definition: GlobalCompilationDatabase.cpp:300
clang::clangd::TextDocumentSyncKind::None
Documents should not be synced at all.
clang::clangd::GlobalCompilationDatabase::getCompileCommand
virtual llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File) const =0
If there are any known-good commands for building this file, returns one.
clang::clangd::GlobalCompilationDatabase
Provides compilation arguments used for parsing C and C++ files.
Definition: GlobalCompilationDatabase.h:35
clang::clangd::ProjectInfo
Definition: GlobalCompilationDatabase.h:28
clang::clangd::Event::observe
Subscription observe(Listener L)
Definition: Function.h:75
clang::clangd::DirectoryBasedGlobalCompilationDatabase::DirectoryBasedGlobalCompilationDatabase
DirectoryBasedGlobalCompilationDatabase(llvm::Optional< Path > CompileCommandsDir)
Definition: GlobalCompilationDatabase.cpp:62
CompileCommands.h
clang::clangd::OverlayCDB::getFallbackCommand
tooling::CompileCommand getFallbackCommand(PathRef File) const override
Makes a guess at how to build a file.
Definition: GlobalCompilationDatabase.cpp:273
clang::clangd::GlobalCompilationDatabase::getFallbackCommand
virtual tooling::CompileCommand getFallbackCommand(PathRef File) const
Makes a guess at how to build a file.
Definition: GlobalCompilationDatabase.cpp:45
clang::clangd::GlobalCompilationDatabase::~GlobalCompilationDatabase
virtual ~GlobalCompilationDatabase()=default
clang::clangd::Event< std::vector< std::string > >::Listener
std::function< void(const std::vector< std::string > &)> Listener
Definition: Function.h:34
clang::clangd::CompletionItemKind::File
clang::clangd::getQueryDriverDatabase
std::unique_ptr< GlobalCompilationDatabase > getQueryDriverDatabase(llvm::ArrayRef< std::string > QueryDriverGlobs, std::unique_ptr< GlobalCompilationDatabase > Base)
Extracts system include search path from drivers matching QueryDriverGlobs and adds them to the compi...
Definition: QueryDriverDatabase.cpp:295
clang::clangd::OverlayCDB::setCompileCommand
void setCompileCommand(PathRef File, llvm::Optional< tooling::CompileCommand > CompilationCommand)
Sets or clears the compilation command for a particular file.
Definition: GlobalCompilationDatabase.cpp:284
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Function.h
clang::clangd::GlobalCompilationDatabase::getProjectInfo
virtual llvm::Optional< ProjectInfo > getProjectInfo(PathRef File) const
Finds the closest project to File.
Definition: GlobalCompilationDatabase.h:44
clang::clangd::DirectoryBasedGlobalCompilationDatabase
Gets compile args from tooling::CompilationDatabases built for parent directories.
Definition: GlobalCompilationDatabase.h:66
clang::clangd::GlobalCompilationDatabase::watch
CommandChanged::Subscription watch(CommandChanged::Listener L) const
The callback is notified when files may have new compile commands.
Definition: GlobalCompilationDatabase.h:56
FileName
PathRef FileName
Definition: CodeComplete.cpp:1043
clang::clangd::GlobalCompilationDatabase::OnCommandChanged
CommandChanged OnCommandChanged
Definition: GlobalCompilationDatabase.h:61
clang::clangd::Event< std::vector< std::string > >
clang::clangd::OverlayCDB
Wraps another compilation database, and supports overriding the commands using an in-memory mapping.
Definition: GlobalCompilationDatabase.h:124