clang-tools 22.0.0git
ScanningProjectModules.cpp
Go to the documentation of this file.
1//===------------------ ProjectModules.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#include "ProjectModules.h"
10#include "support/Logger.h"
11#include "clang/DependencyScanning/DependencyScanningService.h"
12#include "clang/Tooling/DependencyScanningTool.h"
13
14namespace clang::clangd {
15namespace {
16/// A scanner to query the dependency information for C++20 Modules.
17///
18/// The scanner can scan a single file with `scan(PathRef)` member function
19/// or scan the whole project with `globalScan(vector<PathRef>)` member
20/// function. See the comments of `globalScan` to see the details.
21///
22/// The ModuleDependencyScanner can get the directly required module names for a
23/// specific source file. Also the ModuleDependencyScanner can get the source
24/// file declaring the primary module interface for a specific module name.
25///
26/// IMPORTANT NOTE: we assume that every module unit is only declared once in a
27/// source file in the project. But the assumption is not strictly true even
28/// besides the invalid projects. The language specification requires that every
29/// module unit should be unique in a valid program. But a project can contain
30/// multiple programs. Then it is valid that we can have multiple source files
31/// declaring the same module in a project as long as these source files don't
32/// interfere with each other.
33class ModuleDependencyScanner {
34public:
35 ModuleDependencyScanner(
36 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
37 const ThreadsafeFS &TFS)
38 : CDB(CDB), TFS(TFS),
39 Service(dependencies::ScanningMode::CanonicalPreprocessing,
40 dependencies::ScanningOutputFormat::P1689) {}
41
42 /// The scanned modules dependency information for a specific source file.
43 struct ModuleDependencyInfo {
44 /// The name of the module if the file is a module unit.
45 std::optional<std::string> ModuleName;
46 /// A list of names for the modules that the file directly depends.
47 std::vector<std::string> RequiredModules;
48 };
49
50 /// Scanning the single file specified by \param FilePath.
51 std::optional<ModuleDependencyInfo>
52 scan(PathRef FilePath, const ProjectModules::CommandMangler &Mangler);
53
54 /// Scanning every source file in the current project to get the
55 /// <module-name> to <module-unit-source> map.
56 /// TODO: We should find an efficient method to get the <module-name>
57 /// to <module-unit-source> map. We can make it either by providing
58 /// a global module dependency scanner to monitor every file. Or we
59 /// can simply require the build systems (or even the end users)
60 /// to provide the map.
61 void globalScan(const ProjectModules::CommandMangler &Mangler);
62
63 /// Get the source file from the module name. Note that the language
64 /// guarantees all the module names are unique in a valid program.
65 /// This function should only be called after globalScan.
66 ///
67 /// TODO: We should handle the case that there are multiple source files
68 /// declaring the same module.
69 PathRef getSourceForModuleName(llvm::StringRef ModuleName) const;
70
71 /// Return the direct required modules. Indirect required modules are not
72 /// included.
73 std::vector<std::string>
74 getRequiredModules(PathRef File,
75 const ProjectModules::CommandMangler &Mangler);
76
77private:
78 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
79 const ThreadsafeFS &TFS;
80
81 // Whether the scanner has scanned the project globally.
82 bool GlobalScanned = false;
83
84 clang::dependencies::DependencyScanningService Service;
85
86 // TODO: Add a scanning cache.
87
88 // Map module name to source file path.
89 llvm::StringMap<std::string> ModuleNameToSource;
90};
91
92std::optional<ModuleDependencyScanner::ModuleDependencyInfo>
93ModuleDependencyScanner::scan(PathRef FilePath,
94 const ProjectModules::CommandMangler &Mangler) {
95 auto Candidates = CDB->getCompileCommands(FilePath);
96 if (Candidates.empty())
97 return std::nullopt;
98
99 // Choose the first candidates as the compile commands as the file.
100 // Following the same logic with
101 // DirectoryBasedGlobalCompilationDatabase::getCompileCommand.
102 tooling::CompileCommand Cmd = std::move(Candidates.front());
103
104 if (Mangler)
105 Mangler(Cmd, FilePath);
106
107 using namespace clang::tooling;
108
109 llvm::SmallString<128> FilePathDir(FilePath);
110 llvm::sys::path::remove_filename(FilePathDir);
111 DependencyScanningTool ScanningTool(Service, TFS.view(FilePathDir));
112
113 std::string S;
114 llvm::raw_string_ostream OS(S);
115 DiagnosticOptions DiagOpts;
116 DiagOpts.ShowCarets = false;
117 TextDiagnosticPrinter DiagConsumer(OS, DiagOpts);
118
119 std::optional<P1689Rule> ScanningResult =
120 ScanningTool.getP1689ModuleDependencyFile(Cmd, Cmd.Directory,
121 DiagConsumer);
122
123 if (!ScanningResult) {
124 elog("Scanning modules dependencies for {0} failed: {1}", FilePath, S);
125 return std::nullopt;
126 }
127
128 ModuleDependencyInfo Result;
129
130 if (ScanningResult->Provides) {
131 Result.ModuleName = ScanningResult->Provides->ModuleName;
132
133 auto [Iter, Inserted] = ModuleNameToSource.try_emplace(
134 ScanningResult->Provides->ModuleName, FilePath);
135
136 if (!Inserted && Iter->second != FilePath) {
137 elog("Detected multiple source files ({0}, {1}) declaring the same "
138 "module: '{2}'. "
139 "Now clangd may find the wrong source in such case.",
140 Iter->second, FilePath, ScanningResult->Provides->ModuleName);
141 }
142 }
143
144 for (auto &Required : ScanningResult->Requires)
145 Result.RequiredModules.push_back(Required.ModuleName);
146
147 return Result;
148}
149
150void ModuleDependencyScanner::globalScan(
151 const ProjectModules::CommandMangler &Mangler) {
152 if (GlobalScanned)
153 return;
154
155 for (auto &File : CDB->getAllFiles())
156 scan(File, Mangler);
157
158 GlobalScanned = true;
159}
160
161PathRef ModuleDependencyScanner::getSourceForModuleName(
162 llvm::StringRef ModuleName) const {
163 assert(
164 GlobalScanned &&
165 "We should only call getSourceForModuleName after calling globalScan()");
166
167 if (auto It = ModuleNameToSource.find(ModuleName);
168 It != ModuleNameToSource.end())
169 return It->second;
170
171 return {};
172}
173
174std::vector<std::string> ModuleDependencyScanner::getRequiredModules(
176 auto ScanningResult = scan(File, Mangler);
177 if (!ScanningResult)
178 return {};
179
180 return ScanningResult->RequiredModules;
181}
182} // namespace
183
184/// TODO: The existing `ScanningAllProjectModules` is not efficient. See the
185/// comments in ModuleDependencyScanner for detail.
186///
187/// In the future, we wish the build system can provide a well design
188/// compilation database for modules then we can query that new compilation
189/// database directly. Or we need to have a global long-live scanner to detect
190/// the state of each file.
192public:
194 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
195 const ThreadsafeFS &TFS)
196 : Scanner(CDB, TFS) {}
197
198 ~ScanningAllProjectModules() override = default;
199
200 std::vector<std::string> getRequiredModules(PathRef File) override {
201 return Scanner.getRequiredModules(File, Mangler);
202 }
203
204 void setCommandMangler(CommandMangler Mangler) override {
205 this->Mangler = std::move(Mangler);
206 }
207
208 /// RequiredSourceFile is not used intentionally. See the comments of
209 /// ModuleDependencyScanner for detail.
210 std::string getSourceForModuleName(llvm::StringRef ModuleName,
211 PathRef RequiredSourceFile) override {
212 Scanner.globalScan(Mangler);
213 return Scanner.getSourceForModuleName(ModuleName).str();
214 }
215
216 std::string getModuleNameForSource(PathRef File) override {
217 auto ScanningResult = Scanner.scan(File, Mangler);
218 if (!ScanningResult || !ScanningResult->ModuleName)
219 return {};
220
221 return *ScanningResult->ModuleName;
222 }
223
224private:
225 ModuleDependencyScanner Scanner;
226 CommandMangler Mangler;
227};
228
229std::unique_ptr<ProjectModules> scanningProjectModules(
230 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
231 const ThreadsafeFS &TFS) {
232 return std::make_unique<ScanningAllProjectModules>(CDB, TFS);
233}
234
235} // namespace clang::clangd
void elog(const char *Fmt, Ts &&... Vals)
Definition Logger.h:61
An interface to query the modules information in the project.
llvm::unique_function< void(tooling::CompileCommand &, PathRef) const > CommandMangler
void setCommandMangler(CommandMangler Mangler) override
ScanningAllProjectModules(std::shared_ptr< const clang::tooling::CompilationDatabase > CDB, const ThreadsafeFS &TFS)
std::string getModuleNameForSource(PathRef File) override
std::vector< std::string > getRequiredModules(PathRef File) override
std::string getSourceForModuleName(llvm::StringRef ModuleName, PathRef RequiredSourceFile) override
RequiredSourceFile is not used intentionally.
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > view(std::nullopt_t CWD) const
Obtain a vfs::FileSystem with an arbitrary initial working directory.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:45
std::unique_ptr< ProjectModules > scanningProjectModules(std::shared_ptr< const clang::tooling::CompilationDatabase > CDB, const ThreadsafeFS &TFS)
Providing modules information for the project by scanning every file.
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition Path.h:29