clang 22.0.0git
clang::tooling::DependencyScanningTool Class Reference

The high-level implementation of the dependency discovery tool that runs on an individual worker thread. More...

#include "clang/Tooling/DependencyScanningTool.h"

Public Member Functions

 DependencyScanningTool (dependencies::DependencyScanningService &Service, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=llvm::vfs::createPhysicalFileSystem())
 Construct a dependency scanning tool.
std::optional< std::string > getDependencyFile (ArrayRef< std::string > CommandLine, StringRef CWD, DiagnosticConsumer &DiagConsumer)
 Print out the dependency information into a string using the dependency file format that is specified in the options (-MD is the default) and return it.
std::optional< P1689RulegetP1689ModuleDependencyFile (const CompileCommand &Command, StringRef CWD, std::string &MakeformatOutput, std::string &MakeformatOutputPath, DiagnosticConsumer &DiagConsumer)
 Collect the module dependency in P1689 format for C++20 named modules.
std::optional< P1689RulegetP1689ModuleDependencyFile (const CompileCommand &Command, StringRef CWD, DiagnosticConsumer &DiagConsumer)
std::optional< dependencies::TranslationUnitDepsgetTranslationUnitDependencies (ArrayRef< std::string > CommandLine, StringRef CWD, DiagnosticConsumer &DiagConsumer, const llvm::DenseSet< dependencies::ModuleID > &AlreadySeen, dependencies::LookupModuleOutputCallback LookupModuleOutput, std::optional< llvm::MemoryBufferRef > TUBuffer=std::nullopt)
 Given a Clang driver command-line for a translation unit, gather the modular dependencies and return the information needed for explicit build.
llvm::Expected< dependencies::TranslationUnitDepsgetModuleDependencies (StringRef ModuleName, ArrayRef< std::string > CommandLine, StringRef CWD, const llvm::DenseSet< dependencies::ModuleID > &AlreadySeen, dependencies::LookupModuleOutputCallback LookupModuleOutput)
 Given a compilation context specified via the Clang driver command-line, gather modular dependencies of module with the given name, and return the information needed for explicit build.
llvm::Error initializeCompilerInstanceWithContextOrError (StringRef CWD, ArrayRef< std::string > CommandLine)
 The following three methods provide a new interface to perform by name dependency scan.
llvm::Expected< dependencies::TranslationUnitDepscomputeDependenciesByNameWithContextOrError (StringRef ModuleName, const llvm::DenseSet< dependencies::ModuleID > &AlreadySeen, dependencies::LookupModuleOutputCallback LookupModuleOutput)
 Computes the dependeny for the module named ModuleName.
llvm::Error finalizeCompilerInstanceWithContextOrError ()
 This method finializes the compiler instance.
llvm::vfs::FileSystem & getWorkerVFS () const

Static Public Member Functions

static bool initializeWorkerCIWithContextFromCommandline (clang::dependencies::DependencyScanningWorker &Worker, StringRef CWD, ArrayRef< std::string > CommandLine, DiagnosticConsumer &DC)
 Initialize the worker's compiler instance from the commandline.

Detailed Description

The high-level implementation of the dependency discovery tool that runs on an individual worker thread.

Definition at line 34 of file DependencyScanningTool.h.

Constructor & Destructor Documentation

◆ DependencyScanningTool()

DependencyScanningTool::DependencyScanningTool ( dependencies::DependencyScanningService & Service,
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS = llvm::vfs::createPhysicalFileSystem() )

Construct a dependency scanning tool.

Parameters
ServiceThe parent service. Must outlive the tool.
FSThe filesystem for the tool to use. Defaults to the physical FS.

Definition at line 20 of file DependencyScanningTool.cpp.

Member Function Documentation

◆ computeDependenciesByNameWithContextOrError()

llvm::Expected< TranslationUnitDeps > DependencyScanningTool::computeDependenciesByNameWithContextOrError ( StringRef ModuleName,
const llvm::DenseSet< dependencies::ModuleID > & AlreadySeen,
dependencies::LookupModuleOutputCallback LookupModuleOutput )

Computes the dependeny for the module named ModuleName.

Parameters
ModuleNameThe name of the module for which this method computes . dependencies.
AlreadySeenThis stores modules which have previously been reported. Use the same instance for all calls to this function for a single DependencyScanningTool in a single build. Note that this parameter is not part of the context because it can be shared across different worker threads and each worker thread may update it.
LookupModuleOutputThis function is called to fill in "-fmodule-file=", "-o" and other output arguments for dependencies.
Returns
An instance of TranslationUnitDeps if the scan is successful. Otherwise it returns an error.

Definition at line 261 of file DependencyScanningTool.cpp.

References makeErrorFromDiagnosticsOS(), and clang::dependencies::FullDependencyConsumer::takeTranslationUnitDeps().

Referenced by getModuleDependencies().

◆ finalizeCompilerInstanceWithContextOrError()

llvm::Error DependencyScanningTool::finalizeCompilerInstanceWithContextOrError ( )

This method finializes the compiler instance.

It finalizes the diagnostics and deletes the compiler instance. Call this method once all names for a same commandline are scanned.

Returns
Error if an error occured during finalization.

Definition at line 273 of file DependencyScanningTool.cpp.

References makeErrorFromDiagnosticsOS().

Referenced by getModuleDependencies().

◆ getDependencyFile()

std::optional< std::string > DependencyScanningTool::getDependencyFile ( ArrayRef< std::string > CommandLine,
StringRef CWD,
DiagnosticConsumer & DiagConsumer )

Print out the dependency information into a string using the dependency file format that is specified in the options (-MD is the default) and return it.

Returns
std::nullopt if errors occurred (reported to the DiagConsumer), dependency file contents otherwise.

Definition at line 78 of file DependencyScanningTool.cpp.

◆ getModuleDependencies()

llvm::Expected< TranslationUnitDeps > DependencyScanningTool::getModuleDependencies ( StringRef ModuleName,
ArrayRef< std::string > CommandLine,
StringRef CWD,
const llvm::DenseSet< dependencies::ModuleID > & AlreadySeen,
dependencies::LookupModuleOutputCallback LookupModuleOutput )

Given a compilation context specified via the Clang driver command-line, gather modular dependencies of module with the given name, and return the information needed for explicit build.

TODO: this method should be removed as soon as Swift and our C-APIs adopt CompilerInstanceWithContext. We are keeping it here so that it is easier to coordinate with Swift and C-API changes.

Definition at line 162 of file DependencyScanningTool.cpp.

References computeDependenciesByNameWithContextOrError(), clang::Error, finalizeCompilerInstanceWithContextOrError(), initializeCompilerInstanceWithContextOrError(), and clang::Result.

◆ getP1689ModuleDependencyFile() [1/2]

std::optional< P1689Rule > clang::tooling::DependencyScanningTool::getP1689ModuleDependencyFile ( const CompileCommand & Command,
StringRef CWD,
DiagnosticConsumer & DiagConsumer )
inline

Definition at line 71 of file DependencyScanningTool.h.

References getP1689ModuleDependencyFile().

◆ getP1689ModuleDependencyFile() [2/2]

std::optional< P1689Rule > DependencyScanningTool::getP1689ModuleDependencyFile ( const CompileCommand & Command,
StringRef CWD,
std::string & MakeformatOutput,
std::string & MakeformatOutputPath,
DiagnosticConsumer & DiagConsumer )

Collect the module dependency in P1689 format for C++20 named modules.

Parameters
MakeformatOutputThe output parameter for dependency information in make format if the command line requires to generate make-format dependency information by -MD -MF <dep_file>.
MakeformatOutputPathThe output parameter for the path to
MakeformatOutput.
Returns
std::nullopt if errors occurred (reported to the DiagConsumer), P1689 dependency format rules otherwise.

Definition at line 91 of file DependencyScanningTool.cpp.

References clang::Make.

Referenced by getP1689ModuleDependencyFile().

◆ getTranslationUnitDependencies()

std::optional< TranslationUnitDeps > DependencyScanningTool::getTranslationUnitDependencies ( ArrayRef< std::string > CommandLine,
StringRef CWD,
DiagnosticConsumer & DiagConsumer,
const llvm::DenseSet< dependencies::ModuleID > & AlreadySeen,
dependencies::LookupModuleOutputCallback LookupModuleOutput,
std::optional< llvm::MemoryBufferRef > TUBuffer = std::nullopt )

Given a Clang driver command-line for a translation unit, gather the modular dependencies and return the information needed for explicit build.

Parameters
AlreadySeenThis stores modules which have previously been reported. Use the same instance for all calls to this function for a single DependencyScanningTool in a single build. Use a different one for different tools, and clear it between builds.
LookupModuleOutputThis function is called to fill in "-fmodule-file=", "-o" and other output arguments for dependencies.
TUBufferOptional memory buffer for translation unit input. If TUBuffer is nullopt, the input should be included in the Commandline already.
Returns
std::nullopt if errors occurred (reported to the DiagConsumer), translation unit dependencies otherwise.

Definition at line 146 of file DependencyScanningTool.cpp.

References clang::dependencies::FullDependencyConsumer::takeTranslationUnitDeps().

◆ getWorkerVFS()

llvm::vfs::FileSystem & clang::tooling::DependencyScanningTool::getWorkerVFS ( ) const
inline

Definition at line 156 of file DependencyScanningTool.h.

◆ initializeCompilerInstanceWithContextOrError()

llvm::Error DependencyScanningTool::initializeCompilerInstanceWithContextOrError ( StringRef CWD,
ArrayRef< std::string > CommandLine )

The following three methods provide a new interface to perform by name dependency scan.

The new interface's intention is to improve dependency scanning performance when a sequence of name is looked up with the same current working directory and the command line.

Initializing the context and the compiler instance. This method must be called before calling computeDependenciesByNameWithContext.

Parameters
CWDThe current working directory used during the scan.
CommandLineThe commandline used for the scan.
Returns
Error if the initializaiton fails.

Definition at line 247 of file DependencyScanningTool.cpp.

References initializeWorkerCIWithContextFromCommandline(), makeErrorFromDiagnosticsOS(), and clang::Result.

Referenced by getModuleDependencies().

◆ initializeWorkerCIWithContextFromCommandline()

bool DependencyScanningTool::initializeWorkerCIWithContextFromCommandline ( clang::dependencies::DependencyScanningWorker & Worker,
StringRef CWD,
ArrayRef< std::string > CommandLine,
DiagnosticConsumer & DC )
static

Initialize the worker's compiler instance from the commandline.

The compiler instance only takes a -cc1 job, so this method builds the -cc1 job from the CommandLine input.

Parameters
WorkerThe dependency scanning worker whose compiler instance with context is initialized.
CWDThe current working directory.
CommandLineThis command line may be a driver command or a cc1 command.
DCA diagnostics consumer to report error if the initialization fails.

Definition at line 219 of file DependencyScanningTool.cpp.

References getFirstCC1CommandLine(), and clang::dependencies::initVFSForByNameScanning().

Referenced by initializeCompilerInstanceWithContextOrError().


The documentation for this class was generated from the following files: