clang 24.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.
40
41 /// Print out the dependency information into a string using the dependency
42 /// file format that is specified in the options (-MD is the default) and
43 /// return it.
44 ///
45 /// \returns std::nullopt if errors occurred (reported to the DiagConsumer),
46 /// dependency file contents otherwise.
47 std::optional<std::string>
48 getDependencyFile(ArrayRef<std::string> CommandLine, StringRef CWD,
50 DiagnosticConsumer &DiagConsumer);
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 std::nullopt if errors occurred (reported to the DiagConsumer),
62 /// P1689 dependency format rules otherwise.
63 std::optional<P1689Rule>
64 getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD,
65 std::string &MakeformatOutput,
66 std::string &MakeformatOutputPath,
67 DiagnosticConsumer &DiagConsumer);
68 std::optional<P1689Rule>
69 getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD,
70 DiagnosticConsumer &DiagConsumer) {
71 std::string MakeformatOutput;
72 std::string MakeformatOutputPath;
73
74 return getP1689ModuleDependencyFile(Command, CWD, MakeformatOutput,
75 MakeformatOutputPath, DiagConsumer);
76 }
77
78 /// Given a Clang driver command-line for a translation unit, gather the
79 /// modular dependencies and return the information needed for explicit build.
80 ///
81 /// \param AlreadySeen This stores modules which have previously been
82 /// reported. Use the same instance for all calls to this
83 /// function for a single \c DependencyScanningTool in a
84 /// single build. Use a different one for different tools,
85 /// and clear it between builds.
86 /// \param LookupModuleOutput This function is called to fill in
87 /// "-fmodule-file=", "-o" and other output
88 /// arguments for dependencies.
89 /// \param TUBuffer Optional memory buffer for translation unit input. If
90 /// TUBuffer is nullopt, the input should be included in the
91 /// Commandline already.
92 ///
93 /// \returns std::nullopt if errors occurred (reported to the DiagConsumer),
94 /// translation unit dependencies otherwise.
95 std::optional<dependencies::TranslationUnitDeps>
97 ArrayRef<std::string> CommandLine, StringRef CWD,
98 DiagnosticConsumer &DiagConsumer,
99 const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
101 std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);
102
103 /// By-name scanning given a Clang command-line. The scanning context is
104 /// initialized once and reused for each name pulled from
105 /// \p getNextName until it returns std::nullopt. Results flow to
106 /// \p DepConsumer (with per-name status via finishQuery); diagnostics flow to
107 /// \p DiagConsumer.
108 ///
109 /// \returns true if the scanning context initialized successfully and all
110 /// scans succeeded. Query the DepConsumer for the state of each individual
111 /// scan.
113 StringRef CWD, ArrayRef<std::string> CommandLine,
114 DiagnosticConsumer &DiagConsumer,
116 llvm::function_ref<std::optional<std::string>()> getNextName,
118
119 /// Returns the worker tracing VFS, if it was requested via the service.
120 llvm::vfs::TracingFileSystem *getWorkerTracingVFS() const {
121 return Worker.getTracingVFS();
122 }
123
124private:
126};
127
128/// Run the dependency scanning worker for the given driver or frontend
129/// command-line, and report the discovered dependencies to the provided
130/// consumer.
131///
132/// OverlayFS should be based on the Worker's dependency scanning file-system
133/// and can be used to provide any input specified on the command-line as
134/// in-memory file. If no overlay file-system is provided, the Worker's
135/// dependency scanning file-system is used instead.
136///
137/// \returns false if any errors occurred (with diagnostics reported to
138/// \c DiagConsumer), true otherwise.
140 dependencies::DependencyScanningWorker &Worker, StringRef WorkingDirectory,
141 ArrayRef<std::string> CommandLine,
144 DiagnosticConsumer &DiagConsumer,
146} // end namespace tooling
147} // end namespace clang
148
149#endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNINGTOOL_H
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
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.
std::optional< std::string > getDependencyFile(ArrayRef< std::string > CommandLine, StringRef CWD, dependencies::LookupModuleOutputCallback LookupModuleOutput, DiagnosticConsumer &DiagConsumer)
Print out the dependency information into a string using the dependency file format that is specified...
std::optional< dependencies::TranslationUnitDeps > 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 ...
llvm::vfs::TracingFileSystem * getWorkerTracingVFS() const
Returns the worker tracing VFS, if it was requested via the service.
std::optional< P1689Rule > getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD, DiagnosticConsumer &DiagConsumer)
bool getByNameDependencies(StringRef CWD, ArrayRef< std::string > CommandLine, DiagnosticConsumer &DiagConsumer, dependencies::DependencyActionController &Controller, llvm::function_ref< std::optional< std::string >()> getNextName, dependencies::DependencyConsumer &DepConsumer)
By-name scanning given a Clang command-line.
std::optional< P1689Rule > 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.
DependencyScanningTool(dependencies::DependencyScanningService &Service)
Construct a dependency scanning tool.
llvm::function_ref< std::string(const ModuleDeps &, ModuleOutputKind)> LookupModuleOutputCallback
A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
bool computeDependencies(dependencies::DependencyScanningWorker &Worker, StringRef WorkingDirectory, ArrayRef< std::string > CommandLine, dependencies::DependencyConsumer &Consumer, dependencies::DependencyActionController &Controller, DiagnosticConsumer &DiagConsumer, IntrusiveRefCntPtr< llvm::vfs::FileSystem > OverlayFS=nullptr)
Run the dependency scanning worker for the given driver or frontend command-line, and report the disc...
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