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.Format = dependencies::ScanningOutputFormat::P1689;
163 struct ModuleDependencyInfo {
165 std::optional<std::string> ModuleName;
167 std::vector<std::string> RequiredModules;
171 std::optional<ModuleDependencyInfo>
189 PathRef getSourceForModuleName(llvm::StringRef ModuleName)
const;
193 std::vector<std::string>
198 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
201 bool GlobalScanned =
false;
203 clang::dependencies::DependencyScanningService Service;
208 llvm::StringMap<std::string> ModuleNameToSource;
211std::optional<ModuleDependencyScanner::ModuleDependencyInfo>
212ModuleDependencyScanner::scan(
PathRef FilePath,
214 auto Cmd = getCompileCommandForFile(*CDB, FilePath, Mangler);
218 using namespace clang::tooling;
220 DependencyScanningTool ScanningTool(Service);
223 llvm::raw_string_ostream OS(S);
224 DiagnosticOptions DiagOpts;
225 DiagOpts.ShowCarets =
false;
226 TextDiagnosticPrinter DiagConsumer(OS, DiagOpts);
228 std::optional<P1689Rule> ScanningResult =
229 ScanningTool.getP1689ModuleDependencyFile(*Cmd, Cmd->Directory,
232 if (!ScanningResult) {
233 elog(
"Scanning modules dependencies for {0} failed: {1}", FilePath, S);
235 for (
auto &Arg : Cmd->CommandLine)
236 Cmdline += Arg +
" ";
237 elog(
"The command line the scanning tool use is: {0}", Cmdline);
241 ModuleDependencyInfo Result;
243 if (ScanningResult->Provides) {
244 Result.ModuleName = ScanningResult->Provides->ModuleName;
246 auto [Iter, Inserted] = ModuleNameToSource.try_emplace(
247 ScanningResult->Provides->ModuleName, FilePath);
249 if (!Inserted && Iter->second != FilePath) {
250 elog(
"Detected multiple source files ({0}, {1}) declaring the same "
252 "Now clangd may find the wrong source in such case.",
253 Iter->second, FilePath, ScanningResult->Provides->ModuleName);
257 for (
auto &Required : ScanningResult->Requires)
258 Result.RequiredModules.push_back(Required.ModuleName);
263void ModuleDependencyScanner::globalScan(
268 for (
auto &File : CDB->getAllFiles())
271 GlobalScanned =
true;
274PathRef ModuleDependencyScanner::getSourceForModuleName(
275 llvm::StringRef ModuleName)
const {
278 "We should only call getSourceForModuleName after calling globalScan()");
280 if (
auto It = ModuleNameToSource.find(ModuleName);
281 It != ModuleNameToSource.end())
287std::vector<std::string> ModuleDependencyScanner::getRequiredModules(
289 auto ScanningResult = scan(File, Mangler);
293 return ScanningResult->RequiredModules;
307 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
309 : Scanner(CDB, TFS) {}
314 return Scanner.getRequiredModules(
File, Mangler);
318 this->Mangler = std::move(Mangler);
324 PathRef RequiredSourceFile)
override {
325 Scanner.globalScan(Mangler);
326 return Scanner.getSourceForModuleName(ModuleName).str();
330 auto ScanningResult = Scanner.scan(
File, Mangler);
331 if (!ScanningResult || !ScanningResult->ModuleName)
334 return *ScanningResult->ModuleName;
345 ModuleDependencyScanner Scanner;
377 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
379 : CDB(std::
move(CDB)), TFS(TFS) {}
382 auto Parsed = parseFileCommand(
File);
386 std::vector<std::string> Result;
387 Result.reserve(Parsed->RequiredModuleFiles.size());
388 for (
const auto &Required : Parsed->RequiredModuleFiles)
389 Result.push_back(Required.getKey().str());
394 indexProducerCommands();
395 auto It = SourceToModuleName.find(
397 if (It == SourceToModuleName.end() || It->second.Ambiguous)
399 return It->second.Name;
403 indexProducerCommands();
404 auto It = ModuleNameToDistinctSources.find(ModuleName);
405 if (It == ModuleNameToDistinctSources.end())
412 PathRef RequiredSourceFile)
override {
413 auto Parsed = parseFileCommand(RequiredSourceFile);
417 auto It = Parsed->RequiredModuleFiles.find(ModuleName);
418 if (It == Parsed->RequiredModuleFiles.end())
421 indexProducerCommands();
423 if (SourceIt == PCMToSource.end())
426 return SourceIt->second;
430 this->Mangler = std::move(Mangler);
431 ProducerCommandsIndexed =
false;
433 ModuleNameToDistinctSources.clear();
434 SourceToModuleName.clear();
440 std::optional<ParsedCompileCommandInfo> parseFileCommand(
PathRef File)
const {
441 auto Cmd = getCompileCommandForFile(*CDB,
File, Mangler);
444 return parseCompileCommandInfo(std::move(*Cmd), TFS);
454 void indexProducerCommands() {
455 if (ProducerCommandsIndexed)
458 std::vector<ParsedCompileCommandInfo> ParsedCommands;
459 auto AllFiles = CDB->getAllFiles();
460 ParsedCommands.reserve(AllFiles.size());
461 for (
const auto &File : AllFiles) {
462 auto Parsed = parseFileCommand(File);
466 if (Parsed->OutputModuleFile)
467 PCMToSource[maybeCaseFoldPath(*Parsed->OutputModuleFile)] =
470 ParsedCommands.push_back(std::move(*Parsed));
473 for (
const auto &Parsed : ParsedCommands) {
474 for (
const auto &Required : Parsed.RequiredModuleFiles) {
476 PCMToSource.find(maybeCaseFoldPath(Required.getValue()));
477 if (SourceIt == PCMToSource.end())
479 ModuleNameToDistinctSources[Required.getKey()].insert(
480 maybeCaseFoldPath(SourceIt->second));
483 SourceToModuleName[maybeCaseFoldPath(SourceIt->second)];
484 if (Recovered.Name.empty())
485 Recovered.Name = Required.getKey().str();
486 else if (Recovered.Name != Required.getKey()) {
487 if (!Recovered.Ambiguous) {
488 elog(
"Detected conflicting module names ('{0}' and '{1}') for "
489 "the same module file {2} produced by source {3}",
490 Recovered.Name, Required.getKey(), Required.getValue(),
493 Recovered.Ambiguous =
true;
498 ProducerCommandsIndexed =
true;
501 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
502 const ThreadsafeFS &TFS;
503 CommandMangler Mangler;
504 bool ProducerCommandsIndexed =
false;
506 llvm::StringMap<std::string> PCMToSource;
508 using DistinctSourceSet = llvm::StringSet<>;
509 llvm::StringMap<DistinctSourceSet> ModuleNameToDistinctSources;
511 struct RecoveredModuleName {
513 bool Ambiguous =
false;
515 llvm::StringMap<RecoveredModuleName> SourceToModuleName;
528 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
537 return Scanning->getRequiredModules(
File);
542 return Scanning->getModuleNameForSource(
File);
546 PathRef RequiredSourceFile)
override {
547 auto FromCompileCommands =
548 CompileCommands->getSourceForModuleName(ModuleName, RequiredSourceFile);
553 if (!FromCompileCommands.empty() &&
554 Scanning->getModuleNameForSource(FromCompileCommands) == ModuleName)
555 return FromCompileCommands;
557 return Scanning->getSourceForModuleName(ModuleName, RequiredSourceFile);
561 auto FromCompileCommands = CompileCommands->getModuleNameState(ModuleName);
563 return FromCompileCommands;
564 return Scanning->getModuleNameState(ModuleName);
568 this->Mangler = std::move(Mangler);
569 auto ForwardMangler = [
this](tooling::CompileCommand &Command,
572 this->Mangler(Command, CommandPath);
574 CompileCommands->setCommandMangler(ForwardMangler);
575 Scanning->setCommandMangler(std::move(ForwardMangler));
579 std::unique_ptr<CompileCommandsProjectModules> CompileCommands;
580 std::unique_ptr<ScanningAllProjectModules> Scanning;
623 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
625 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.