clang 22.0.0git
DependencyScanningTool.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNINGTOOL_H
10#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNINGTOOL_H
11
17#include "llvm/ADT/DenseSet.h"
18#include <optional>
19#include <string>
20#include <vector>
21
22namespace clang {
23namespace tooling {
24
25struct P1689Rule {
26 std::string PrimaryOutput;
27 std::optional<dependencies::P1689ModuleInfo> Provides;
28 std::vector<dependencies::P1689ModuleInfo> Requires;
29};
30
31/// The high-level implementation of the dependency discovery tool that runs on
32/// an individual worker thread.
34public:
35 /// Construct a dependency scanning tool.
36 ///
37 /// @param Service The parent service. Must outlive the tool.
38 /// @param FS The filesystem for the tool to use. Defaults to the physical FS.
41 llvm::vfs::createPhysicalFileSystem());
42
43 /// Print out the dependency information into a string using the dependency
44 /// file format that is specified in the options (-MD is the default) and
45 /// return it.
46 ///
47 /// \returns A \c StringError with the diagnostic output if clang errors
48 /// occurred, dependency file contents otherwise.
50 getDependencyFile(ArrayRef<std::string> CommandLine, StringRef CWD);
51
52 /// Collect the module dependency in P1689 format for C++20 named modules.
53 ///
54 /// \param MakeformatOutput The output parameter for dependency information
55 /// in make format if the command line requires to generate make-format
56 /// dependency information by `-MD -MF <dep_file>`.
57 ///
58 /// \param MakeformatOutputPath The output parameter for the path to
59 /// \param MakeformatOutput.
60 ///
61 /// \returns A \c StringError with the diagnostic output if clang errors
62 /// occurred, P1689 dependency format rules otherwise.
64 getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD,
65 std::string &MakeformatOutput,
66 std::string &MakeformatOutputPath);
68 getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD) {
69 std::string MakeformatOutput;
70 std::string MakeformatOutputPath;
71
72 return getP1689ModuleDependencyFile(Command, CWD, MakeformatOutput,
73 MakeformatOutputPath);
74 }
75
76 /// Given a Clang driver command-line for a translation unit, gather the
77 /// modular dependencies and return the information needed for explicit build.
78 ///
79 /// \param AlreadySeen This stores modules which have previously been
80 /// reported. Use the same instance for all calls to this
81 /// function for a single \c DependencyScanningTool in a
82 /// single build. Use a different one for different tools,
83 /// and clear it between builds.
84 /// \param LookupModuleOutput This function is called to fill in
85 /// "-fmodule-file=", "-o" and other output
86 /// arguments for dependencies.
87 /// \param TUBuffer Optional memory buffer for translation unit input. If
88 /// TUBuffer is nullopt, the input should be included in the
89 /// Commandline already.
90 ///
91 /// \returns a \c StringError with the diagnostic output if clang errors
92 /// occurred, \c TranslationUnitDeps otherwise.
95 ArrayRef<std::string> CommandLine, StringRef CWD,
96 const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
98 std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);
99
100 /// Given a compilation context specified via the Clang driver command-line,
101 /// gather modular dependencies of module with the given name, and return the
102 /// information needed for explicit build.
103 /// TODO: this method should be removed as soon as Swift and our C-APIs adopt
104 /// CompilerInstanceWithContext. We are keeping it here so that it is easier
105 /// to coordinate with Swift and C-API changes.
107 StringRef ModuleName, ArrayRef<std::string> CommandLine, StringRef CWD,
108 const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
110
111 /// The following three methods provide a new interface to perform
112 /// by name dependency scan. The new interface's intention is to improve
113 /// dependency scanning performance when a sequence of name is looked up
114 /// with the same current working directory and the command line.
115
116 /// @brief Initializing the context and the compiler instance.
117 /// This method must be called before calling
118 /// computeDependenciesByNameWithContext.
119 /// @param CWD The current working directory used during the scan.
120 /// @param CommandLine The commandline used for the scan.
121 /// @return Error if the initializaiton fails.
122 llvm::Error
124 ArrayRef<std::string> CommandLine);
125
126 /// @brief Computes the dependeny for the module named ModuleName.
127 /// @param ModuleName The name of the module for which this method computes
128 ///. dependencies.
129 /// @param AlreadySeen This stores modules which have previously been
130 /// reported. Use the same instance for all calls to this
131 /// function for a single \c DependencyScanningTool in a
132 /// single build. Note that this parameter is not part of
133 /// the context because it can be shared across different
134 /// worker threads and each worker thread may update it.
135 /// @param LookupModuleOutput This function is called to fill in
136 /// "-fmodule-file=", "-o" and other output
137 /// arguments for dependencies.
138 /// @return An instance of \c TranslationUnitDeps if the scan is successful.
139 /// Otherwise it returns an error.
142 StringRef ModuleName,
143 const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
145
146 /// @brief This method finializes the compiler instance. It finalizes the
147 /// diagnostics and deletes the compiler instance. Call this method
148 /// once all names for a same commandline are scanned.
149 /// @return Error if an error occured during finalization.
151
152 llvm::vfs::FileSystem &getWorkerVFS() const { return Worker.getVFS(); }
153
154private:
156 std::unique_ptr<dependencies::TextDiagnosticsPrinterWithOutput>
157 DiagPrinterWithOS;
158};
159
160} // end namespace tooling
161} // end namespace clang
162
163#endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNINGTOOL_H
The dependency scanning service contains shared configuration and state that is used by the individua...
An individual dependency scanning worker that is able to run on its own thread.
llvm::Error initializeCompilerInstanceWithContext(StringRef CWD, ArrayRef< std::string > CommandLine)
The following three methods provide a new interface to perform by name dependency scan.
DependencyScanningTool(dependencies::DependencyScanningService &Service, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=llvm::vfs::createPhysicalFileSystem())
Construct a dependency scanning tool.
llvm::Expected< dependencies::TranslationUnitDeps > getTranslationUnitDependencies(ArrayRef< std::string > CommandLine, StringRef CWD, 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 ...
llvm::Expected< P1689Rule > getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD)
llvm::Error finalizeCompilerInstanceWithContext()
This method finializes the compiler instance.
llvm::Expected< dependencies::TranslationUnitDeps > 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 ...
llvm::Expected< dependencies::TranslationUnitDeps > computeDependenciesByNameWithContext(StringRef ModuleName, const llvm::DenseSet< dependencies::ModuleID > &AlreadySeen, dependencies::LookupModuleOutputCallback LookupModuleOutput)
Computes the dependeny for the module named ModuleName.
llvm::vfs::FileSystem & getWorkerVFS() const
llvm::Expected< P1689Rule > getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD, std::string &MakeformatOutput, std::string &MakeformatOutputPath)
Collect the module dependency in P1689 format for C++20 named modules.
llvm::Expected< std::string > getDependencyFile(ArrayRef< std::string > CommandLine, StringRef CWD)
Print out the dependency information into a string using the dependency file format that is specified...
llvm::function_ref< std::string(const ModuleDeps &, ModuleOutputKind)> LookupModuleOutputCallback
A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
The JSON file list parser is used to communicate input to InstallAPI.
@ Worker
'worker' clause, allowed on 'loop', Combined, and 'routine' directives.
Specifies the working directory and command of a compilation.
std::vector< dependencies::P1689ModuleInfo > Requires
std::optional< dependencies::P1689ModuleInfo > Provides