clang-tools  10.0.0svn
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 "Function.h"
13 #include "Path.h"
14 #include "clang/Tooling/CompilationDatabase.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/StringMap.h"
17 #include <memory>
18 #include <mutex>
19 #include <vector>
20 
21 namespace clang {
22 
23 namespace clangd {
24 
25 class Logger;
26 
27 struct ProjectInfo {
28  // The directory in which the compilation database was discovered.
29  // Empty if directory-based compilation database discovery was not used.
30  std::string SourceRoot;
31 };
32 
33 /// Provides compilation arguments used for parsing C and C++ files.
35 public:
36  virtual ~GlobalCompilationDatabase() = default;
37 
38  /// If there are any known-good commands for building this file, returns one.
39  virtual llvm::Optional<tooling::CompileCommand>
40  getCompileCommand(PathRef File) const = 0;
41 
42  /// Finds the closest project to \p File.
43  virtual llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const {
44  return llvm::None;
45  }
46 
47  /// Makes a guess at how to build a file.
48  /// The default implementation just runs clang on the file.
49  /// Clangd should treat the results as unreliable.
50  virtual tooling::CompileCommand getFallbackCommand(PathRef File) const;
51 
53  /// The callback is notified when files may have new compile commands.
54  /// The argument is a list of full file paths.
55  CommandChanged::Subscription watch(CommandChanged::Listener L) const {
56  return OnCommandChanged.observe(std::move(L));
57  }
58 
59 protected:
61 };
62 
63 /// Gets compile args from tooling::CompilationDatabases built for parent
64 /// directories.
66  : public GlobalCompilationDatabase {
67 public:
69  llvm::Optional<Path> CompileCommandsDir);
71 
72  /// Scans File's parents looking for compilation databases.
73  /// Any extra flags will be added.
74  /// Might trigger OnCommandChanged, if CDB wasn't broadcasted yet.
75  llvm::Optional<tooling::CompileCommand>
76  getCompileCommand(PathRef File) const override;
77 
78  /// Returns the path to first directory containing a compilation database in
79  /// \p File's parents.
80  llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override;
81 
82 private:
83  /// Caches compilation databases loaded from directories.
84  struct CachedCDB {
85  std::string Path; // Not case-folded.
86  std::unique_ptr<clang::tooling::CompilationDatabase> CDB = nullptr;
87  bool SentBroadcast = false;
88  };
89  CachedCDB &getCDBInDirLocked(PathRef File) const;
90 
91  struct CDBLookupRequest {
93  // Whether this lookup should trigger discovery of the CDB found.
94  bool ShouldBroadcast = false;
95  };
96  struct CDBLookupResult {
97  tooling::CompilationDatabase *CDB = nullptr;
98  ProjectInfo PI;
99  };
100  llvm::Optional<CDBLookupResult> lookupCDB(CDBLookupRequest Request) const;
101 
102  // Performs broadcast on governed files.
103  void broadcastCDB(CDBLookupResult Res) const;
104 
105  mutable std::mutex Mutex;
106  // Keyed by possibly-case-folded directory path.
107  mutable llvm::StringMap<CachedCDB> CompilationDatabases;
108 
109  /// Used for command argument pointing to folder where compile_commands.json
110  /// is located.
111  llvm::Optional<Path> CompileCommandsDir;
112 };
113 
114 /// Extracts system include search path from drivers matching QueryDriverGlobs
115 /// and adds them to the compile flags. Base may not be nullptr.
116 /// Returns Base when \p QueryDriverGlobs is empty.
117 std::unique_ptr<GlobalCompilationDatabase>
118 getQueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
119  std::unique_ptr<GlobalCompilationDatabase> Base);
120 
121 /// Wraps another compilation database, and supports overriding the commands
122 /// using an in-memory mapping.
124 public:
125  // Base may be null, in which case no entries are inherited.
126  // FallbackFlags are added to the fallback compile command.
128  std::vector<std::string> FallbackFlags = {},
129  llvm::Optional<std::string> ResourceDir = llvm::None);
130 
131  llvm::Optional<tooling::CompileCommand>
132  getCompileCommand(PathRef File) const override;
133  tooling::CompileCommand getFallbackCommand(PathRef File) const override;
134  llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override;
135 
136  /// Sets or clears the compilation command for a particular file.
137  void
138  setCompileCommand(PathRef File,
139  llvm::Optional<tooling::CompileCommand> CompilationCommand);
140 
141 private:
142  mutable std::mutex Mutex;
143  llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */
145  std::string ResourceDir;
146  std::vector<std::string> FallbackFlags;
147  CommandChanged::Subscription BaseChanged;
148 };
149 
150 } // namespace clangd
151 } // namespace clang
152 
153 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H
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...
std::function< void(const std::vector< std::string > &)> Listener
Definition: Function.h:34
static cl::list< std::string > Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory))
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
Documents should not be synced at all.
Provides compilation arguments used for parsing C and C++ files.
std::string Path
A typedef to represent a file path.
Definition: Path.h:20
PathRef FileName
CommandChanged::Subscription watch(CommandChanged::Listener L) const
The callback is notified when files may have new compile commands.
Gets compile args from tooling::CompilationDatabases built for parent directories.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::unique_ptr< GlobalCompilationDatabase > Base
Wraps another compilation database, and supports overriding the commands using an in-memory mapping...
virtual llvm::Optional< ProjectInfo > getProjectInfo(PathRef File) const
Finds the closest project to File.