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);
251 !
pathEqual(normalizePath(Iter->second), normalizePath(FilePath))) {
252 elog(
"Detected multiple source files ({0}, {1}) declaring the same "
254 "Now clangd may find the wrong source in such case.",
255 Iter->second, FilePath, ScanningResult->Provides->ModuleName);
259 for (
auto &Required : ScanningResult->Requires)
260 Result.RequiredModules.push_back(Required.ModuleName);
265void ModuleDependencyScanner::globalScan(
270 for (
auto &File : CDB->getAllFiles())
273 GlobalScanned =
true;
276PathRef ModuleDependencyScanner::getSourceForModuleName(
277 llvm::StringRef ModuleName)
const {
280 "We should only call getSourceForModuleName after calling globalScan()");
282 if (
auto It = ModuleNameToSource.find(ModuleName);
283 It != ModuleNameToSource.end())
289std::vector<std::string> ModuleDependencyScanner::getRequiredModules(
291 auto ScanningResult = scan(File, Mangler);
295 return ScanningResult->RequiredModules;
309 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
311 : Scanner(CDB, TFS) {}
316 return Scanner.getRequiredModules(
File, Mangler);
320 this->Mangler = std::move(Mangler);
326 PathRef RequiredSourceFile)
override {
327 Scanner.globalScan(Mangler);
328 return Scanner.getSourceForModuleName(ModuleName).str();
332 auto ScanningResult = Scanner.scan(
File, Mangler);
333 if (!ScanningResult || !ScanningResult->ModuleName)
336 return *ScanningResult->ModuleName;
347 ModuleDependencyScanner Scanner;
379 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
381 : CDB(std::
move(CDB)), TFS(TFS) {}
384 auto Parsed = parseFileCommand(
File);
388 std::vector<std::string> Result;
389 Result.reserve(Parsed->RequiredModuleFiles.size());
390 for (
const auto &Required : Parsed->RequiredModuleFiles)
391 Result.push_back(Required.getKey().str());
396 indexProducerCommands();
397 auto It = SourceToModuleName.find(
399 if (It == SourceToModuleName.end() || It->second.Ambiguous)
401 return It->second.Name;
405 indexProducerCommands();
406 auto It = ModuleNameToDistinctSources.find(ModuleName);
407 if (It == ModuleNameToDistinctSources.end())
414 PathRef RequiredSourceFile)
override {
415 auto Parsed = parseFileCommand(RequiredSourceFile);
419 auto It = Parsed->RequiredModuleFiles.find(ModuleName);
420 if (It == Parsed->RequiredModuleFiles.end())
423 indexProducerCommands();
425 if (SourceIt == PCMToSource.end())
428 return SourceIt->second;
432 this->Mangler = std::move(Mangler);
433 ProducerCommandsIndexed =
false;
435 ModuleNameToDistinctSources.clear();
436 SourceToModuleName.clear();
442 std::optional<ParsedCompileCommandInfo> parseFileCommand(
PathRef File)
const {
443 auto Cmd = getCompileCommandForFile(*CDB,
File, Mangler);
446 return parseCompileCommandInfo(std::move(*Cmd), TFS);
456 void indexProducerCommands() {
457 if (ProducerCommandsIndexed)
460 std::vector<ParsedCompileCommandInfo> ParsedCommands;
461 auto AllFiles = CDB->getAllFiles();
462 ParsedCommands.reserve(AllFiles.size());
463 for (
const auto &File : AllFiles) {
464 auto Parsed = parseFileCommand(File);
468 if (Parsed->OutputModuleFile)
469 PCMToSource[maybeCaseFoldPath(*Parsed->OutputModuleFile)] =
472 ParsedCommands.push_back(std::move(*Parsed));
475 for (
const auto &Parsed : ParsedCommands) {
476 for (
const auto &Required : Parsed.RequiredModuleFiles) {
478 PCMToSource.find(maybeCaseFoldPath(Required.getValue()));
479 if (SourceIt == PCMToSource.end())
481 ModuleNameToDistinctSources[Required.getKey()].insert(
482 maybeCaseFoldPath(SourceIt->second));
485 SourceToModuleName[maybeCaseFoldPath(SourceIt->second)];
486 if (Recovered.Name.empty())
487 Recovered.Name = Required.getKey().str();
488 else if (Recovered.Name != Required.getKey()) {
489 if (!Recovered.Ambiguous) {
490 elog(
"Detected conflicting module names ('{0}' and '{1}') for "
491 "the same module file {2} produced by source {3}",
492 Recovered.Name, Required.getKey(), Required.getValue(),
495 Recovered.Ambiguous =
true;
500 ProducerCommandsIndexed =
true;
503 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
504 const ThreadsafeFS &TFS;
505 CommandMangler Mangler;
506 bool ProducerCommandsIndexed =
false;
508 llvm::StringMap<std::string> PCMToSource;
510 using DistinctSourceSet = llvm::StringSet<>;
511 llvm::StringMap<DistinctSourceSet> ModuleNameToDistinctSources;
513 struct RecoveredModuleName {
515 bool Ambiguous =
false;
517 llvm::StringMap<RecoveredModuleName> SourceToModuleName;
530 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
539 return Scanning->getRequiredModules(
File);
544 return Scanning->getModuleNameForSource(
File);
548 PathRef RequiredSourceFile)
override {
549 auto FromCompileCommands =
550 CompileCommands->getSourceForModuleName(ModuleName, RequiredSourceFile);
555 if (!FromCompileCommands.empty() &&
556 Scanning->getModuleNameForSource(FromCompileCommands) == ModuleName)
557 return FromCompileCommands;
559 return Scanning->getSourceForModuleName(ModuleName, RequiredSourceFile);
563 auto FromCompileCommands = CompileCommands->getModuleNameState(ModuleName);
565 return FromCompileCommands;
566 return Scanning->getModuleNameState(ModuleName);
570 this->Mangler = std::move(Mangler);
571 auto ForwardMangler = [
this](tooling::CompileCommand &Command,
574 this->Mangler(Command, CommandPath);
576 CompileCommands->setCommandMangler(ForwardMangler);
577 Scanning->setCommandMangler(std::move(ForwardMangler));
581 std::unique_ptr<CompileCommandsProjectModules> CompileCommands;
582 std::unique_ptr<ScanningAllProjectModules> Scanning;
625 std::shared_ptr<const clang::tooling::CompilationDatabase> CDB,
627 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)
bool pathEqual(PathRef A, PathRef B)
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.