12#include "clang/DependencyScanning/DependencyScanningService.h"
13#include "clang/Tooling/DependencyScanningTool.h"
14#include "clang/Tooling/Tooling.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/StringSet.h"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/Path.h"
20#include "llvm/TargetParser/Host.h"
26 llvm::SmallString<128> Result(
Path);
27 llvm::sys::path::remove_dots(Result,
true);
28 llvm::sys::path::native(Result, llvm::sys::path::Style::posix);
36 llvm::SmallString<128> Result;
37 if (llvm::sys::path::is_absolute(
Path) || WorkingDir.empty())
41 llvm::sys::path::append(Result,
Path);
44 return normalizePath(Result).str().str();
51struct ParsedCompileCommandInfo {
52 std::string SourceFile;
53 std::optional<std::string> OutputModuleFile;
55 llvm::StringMap<std::string> RequiredModuleFiles;
60std::optional<ParsedCompileCommandInfo>
61parseCompileCommandInfo(tooling::CompileCommand Cmd,
const ThreadsafeFS &TFS) {
62 auto FS = TFS.view(std::nullopt);
63 auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
64 ? llvm::cl::TokenizeWindowsCommandLine
65 : llvm::cl::TokenizeGNUCommandLine;
66 tooling::addExpandedResponseFiles(Cmd.CommandLine, Cmd.Directory, Tokenizer,
69 ParsedCompileCommandInfo Result;
70 Result.SourceFile = normalizePath(Cmd.Filename, Cmd.Directory);
72 bool SawPrecompile =
false;
73 for (
size_t I = 1; I < Cmd.CommandLine.size(); ++I) {
74 llvm::StringRef Arg = Cmd.CommandLine[I];
75 if (Arg ==
"--precompile") {
80 if (Arg.consume_front(
"-fmodule-output=")) {
81 Result.OutputModuleFile = normalizePath(Arg, Cmd.Directory);
84 if (Arg ==
"-fmodule-output" && I + 1 < Cmd.CommandLine.size()) {
85 Result.OutputModuleFile =
86 normalizePath(Cmd.CommandLine[++I], Cmd.Directory);
89 if (SawPrecompile && Arg ==
"-o" && I + 1 < Cmd.CommandLine.size()) {
90 Result.OutputModuleFile =
91 normalizePath(Cmd.CommandLine[++I], Cmd.Directory);
94 if (SawPrecompile && Arg.starts_with(
"-o") && Arg.size() > 2) {
95 Result.OutputModuleFile = normalizePath(Arg.drop_front(2), Cmd.Directory);
99 if (!Arg.consume_front(
"-fmodule-file="))
102 auto Sep = Arg.find(
'=');
103 if (Sep == llvm::StringRef::npos || Sep == 0 || Sep + 1 == Arg.size())
106 Result.RequiredModuleFiles[Arg.take_front(Sep)] =
107 normalizePath(Arg.drop_front(Sep + 1), Cmd.Directory);
113std::optional<tooling::CompileCommand>
114getCompileCommandForFile(
const clang::tooling::CompilationDatabase &CDB,
117 auto Candidates = CDB.getCompileCommands(FilePath);
118 if (Candidates.empty())
124 tooling::CompileCommand Cmd = std::move(Candidates.front());
127 Mangler(Cmd, FilePath);
149class ModuleDependencyScanner {
151 ModuleDependencyScanner(
152 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
153 const ThreadsafeFS &TFS)
154 : CDB(CDB), Service([&TFS] {
155 dependencies::DependencyScanningServiceOptions Opts;
156 Opts.MakeVFS = [&] {
return TFS.view(std::nullopt); };
157 Opts.Mode = dependencies::ScanningMode::CanonicalPreprocessing;
158 Opts.EmitWarnings =
false;
159 Opts.ReportAbsolutePaths =
false;
164 struct ModuleDependencyInfo {
166 std::optional<std::string> ModuleName;
168 std::vector<std::string> RequiredModules;
172 std::optional<ModuleDependencyInfo>
190 PathRef getSourceForModuleName(llvm::StringRef ModuleName)
const;
194 std::vector<std::string>
199 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
202 bool GlobalScanned =
false;
204 clang::dependencies::DependencyScanningService Service;
209 llvm::StringMap<std::string> ModuleNameToSource;
212std::optional<ModuleDependencyScanner::ModuleDependencyInfo>
213ModuleDependencyScanner::scan(
PathRef FilePath,
215 auto Cmd = getCompileCommandForFile(*CDB, FilePath, Mangler);
219 using namespace clang::tooling;
221 DependencyScanningTool ScanningTool(Service);
224 llvm::raw_string_ostream OS(S);
225 DiagnosticOptions DiagOpts;
226 DiagOpts.ShowCarets =
false;
227 TextDiagnosticPrinter DiagConsumer(OS, DiagOpts);
229 std::optional<P1689Rule> ScanningResult =
230 ScanningTool.getP1689ModuleDependencyFile(*Cmd, Cmd->Directory,
233 if (!ScanningResult) {
234 elog(
"Scanning modules dependencies for {0} failed: {1}", FilePath, S);
236 for (
auto &Arg : Cmd->CommandLine)
237 Cmdline += Arg +
" ";
238 elog(
"The command line the scanning tool use is: {0}", Cmdline);
242 ModuleDependencyInfo Result;
244 if (ScanningResult->Provides) {
245 Result.ModuleName = ScanningResult->Provides->ModuleName;
247 auto [Iter, Inserted] = ModuleNameToSource.try_emplace(
248 ScanningResult->Provides->ModuleName, FilePath);
250 if (!Inserted && Iter->second != FilePath) {
251 elog(
"Detected multiple source files ({0}, {1}) declaring the same "
253 "Now clangd may find the wrong source in such case.",
254 Iter->second, FilePath, ScanningResult->Provides->ModuleName);
258 for (
auto &Required : ScanningResult->Requires)
259 Result.RequiredModules.push_back(Required.ModuleName);
264void ModuleDependencyScanner::globalScan(
269 for (
auto &File : CDB->getAllFiles())
272 GlobalScanned =
true;
275PathRef ModuleDependencyScanner::getSourceForModuleName(
276 llvm::StringRef ModuleName)
const {
279 "We should only call getSourceForModuleName after calling globalScan()");
281 if (
auto It = ModuleNameToSource.find(ModuleName);
282 It != ModuleNameToSource.end())
288std::vector<std::string> ModuleDependencyScanner::getRequiredModules(
290 auto ScanningResult = scan(File, Mangler);
294 return ScanningResult->RequiredModules;
308 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
310 : Scanner(CDB, TFS) {}
315 return Scanner.getRequiredModules(
File, Mangler);
319 this->Mangler = std::move(Mangler);
325 PathRef RequiredSourceFile)
override {
326 Scanner.globalScan(Mangler);
327 return Scanner.getSourceForModuleName(ModuleName).str();
331 auto ScanningResult = Scanner.scan(
File, Mangler);
332 if (!ScanningResult || !ScanningResult->ModuleName)
335 return *ScanningResult->ModuleName;
346 ModuleDependencyScanner Scanner;
378 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
380 : CDB(std::
move(CDB)), TFS(TFS) {}
383 auto Parsed = parseFileCommand(
File);
387 std::vector<std::string> Result;
388 Result.reserve(Parsed->RequiredModuleFiles.size());
389 for (
const auto &Required : Parsed->RequiredModuleFiles)
390 Result.push_back(Required.getKey().str());
395 indexProducerCommands();
396 auto It = SourceToModuleName.find(
398 if (It == SourceToModuleName.end() || It->second.Ambiguous)
400 return It->second.Name;
404 indexProducerCommands();
405 auto It = ModuleNameToDistinctSources.find(ModuleName);
406 if (It == ModuleNameToDistinctSources.end())
413 PathRef RequiredSourceFile)
override {
414 auto Parsed = parseFileCommand(RequiredSourceFile);
418 auto It = Parsed->RequiredModuleFiles.find(ModuleName);
419 if (It == Parsed->RequiredModuleFiles.end())
422 indexProducerCommands();
424 if (SourceIt == PCMToSource.end())
427 return SourceIt->second;
431 this->Mangler = std::move(Mangler);
432 ProducerCommandsIndexed =
false;
434 ModuleNameToDistinctSources.clear();
435 SourceToModuleName.clear();
441 std::optional<ParsedCompileCommandInfo> parseFileCommand(
PathRef File)
const {
442 auto Cmd = getCompileCommandForFile(*CDB,
File, Mangler);
445 return parseCompileCommandInfo(std::move(*Cmd), TFS);
455 void indexProducerCommands() {
456 if (ProducerCommandsIndexed)
459 std::vector<ParsedCompileCommandInfo> ParsedCommands;
460 auto AllFiles = CDB->getAllFiles();
461 ParsedCommands.reserve(AllFiles.size());
462 for (
const auto &File : AllFiles) {
463 auto Parsed = parseFileCommand(File);
467 if (Parsed->OutputModuleFile)
468 PCMToSource[maybeCaseFoldPath(*Parsed->OutputModuleFile)] =
471 ParsedCommands.push_back(std::move(*Parsed));
474 for (
const auto &Parsed : ParsedCommands) {
475 for (
const auto &Required : Parsed.RequiredModuleFiles) {
477 PCMToSource.find(maybeCaseFoldPath(Required.getValue()));
478 if (SourceIt == PCMToSource.end())
480 ModuleNameToDistinctSources[Required.getKey()].insert(
481 maybeCaseFoldPath(SourceIt->second));
484 SourceToModuleName[maybeCaseFoldPath(SourceIt->second)];
485 if (Recovered.Name.empty())
486 Recovered.Name = Required.getKey().str();
487 else if (Recovered.Name != Required.getKey()) {
488 if (!Recovered.Ambiguous) {
489 elog(
"Detected conflicting module names ('{0}' and '{1}') for "
490 "the same module file {2} produced by source {3}",
491 Recovered.Name, Required.getKey(), Required.getValue(),
494 Recovered.Ambiguous =
true;
499 ProducerCommandsIndexed =
true;
502 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
503 const ThreadsafeFS &TFS;
504 CommandMangler Mangler;
505 bool ProducerCommandsIndexed =
false;
507 llvm::StringMap<std::string> PCMToSource;
509 using DistinctSourceSet = llvm::StringSet<>;
510 llvm::StringMap<DistinctSourceSet> ModuleNameToDistinctSources;
512 struct RecoveredModuleName {
514 bool Ambiguous =
false;
516 llvm::StringMap<RecoveredModuleName> SourceToModuleName;
529 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
538 return Scanning->getRequiredModules(
File);
543 return Scanning->getModuleNameForSource(
File);
547 PathRef RequiredSourceFile)
override {
548 auto FromCompileCommands =
549 CompileCommands->getSourceForModuleName(ModuleName, RequiredSourceFile);
554 if (!FromCompileCommands.empty() &&
555 Scanning->getModuleNameForSource(FromCompileCommands) == ModuleName)
556 return FromCompileCommands;
558 return Scanning->getSourceForModuleName(ModuleName, RequiredSourceFile);
562 auto FromCompileCommands = CompileCommands->getModuleNameState(ModuleName);
564 return FromCompileCommands;
565 return Scanning->getModuleNameState(ModuleName);
569 this->Mangler = std::move(Mangler);
570 auto ForwardMangler = [
this](tooling::CompileCommand &Command,
573 this->Mangler(Command, CommandPath);
575 CompileCommands->setCommandMangler(ForwardMangler);
576 Scanning->setCommandMangler(std::move(ForwardMangler));
580 std::unique_ptr<CompileCommandsProjectModules> CompileCommands;
581 std::unique_ptr<ScanningAllProjectModules> Scanning;
624 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
626 return std::make_unique<CompoundProjectModules>(std::move(CDB), TFS);
void elog(const char *Fmt, Ts &&... Vals)
Reads project module information directly from compile commands.
CompileCommandsProjectModules(std::shared_ptr< const clang::tooling::CompilationDatabase > CDB, const ThreadsafeFS &TFS)
std::string getModuleNameForSource(PathRef File) override
void setCommandMangler(CommandMangler Mangler) override
std::string getSourceForModuleName(llvm::StringRef ModuleName, PathRef RequiredSourceFile) override
ModuleNameState getModuleNameState(llvm::StringRef ModuleName) override
std::vector< std::string > getRequiredModules(PathRef File) override
std::vector< std::string > getRequiredModules(PathRef File) override
ModuleNameState getModuleNameState(llvm::StringRef ModuleName) override
std::string getModuleNameForSource(PathRef File) override
void setCommandMangler(CommandMangler Mangler) override
std::string getSourceForModuleName(llvm::StringRef ModuleName, PathRef RequiredSourceFile) override
CompoundProjectModules(std::shared_ptr< const clang::tooling::CompilationDatabase > CDB, const ThreadsafeFS &TFS)
An interface to query the modules information in the project.
llvm::unique_function< void(tooling::CompileCommand &, PathRef) const > CommandMangler
TODO: The existing ScanningAllProjectModules is not efficient.
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
ModuleNameState getModuleNameState(llvm::StringRef) override
std::string getSourceForModuleName(llvm::StringRef ModuleName, PathRef RequiredSourceFile) override
RequiredSourceFile is not used intentionally.
~ScanningAllProjectModules() override=default
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
std::string maybeCaseFoldPath(PathRef Path)
llvm::StringRef PathRef
A typedef to represent a ref to file path.
std::string Path
A typedef to represent a file path.
std::unique_ptr< ProjectModules > getProjectModules(std::shared_ptr< const clang::tooling::CompilationDatabase > CDB, const ThreadsafeFS &TFS)
Creates the project-modules facade used by clangd.