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.
41 : Worker(Service, LocalFS) {}
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 std::nullopt if errors occurred (reported to the DiagConsumer),
48 /// dependency file contents otherwise.
49 std::optional<std::string>
50 getDependencyFile(ArrayRef<std::string> CommandLine, StringRef CWD,
52 DiagnosticConsumer &DiagConsumer);
53
54 /// Collect the module dependency in P1689 format for C++20 named modules.
55 ///
56 /// \param MakeformatOutput The output parameter for dependency information
57 /// in make format if the command line requires to generate make-format
58 /// dependency information by `-MD -MF <dep_file>`.
59 ///
60 /// \param MakeformatOutputPath The output parameter for the path to
61 /// \p MakeformatOutput.
62 ///
63 /// \returns std::nullopt if errors occurred (reported to the DiagConsumer),
64 /// P1689 dependency format rules otherwise.
65 std::optional<P1689Rule>
66 getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD,
67 std::string &MakeformatOutput,
68 std::string &MakeformatOutputPath,
69 DiagnosticConsumer &DiagConsumer);
70 std::optional<P1689Rule>
71 getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD,
72 DiagnosticConsumer &DiagConsumer) {
73 std::string MakeformatOutput;
74 std::string MakeformatOutputPath;
75
76 return getP1689ModuleDependencyFile(Command, CWD, MakeformatOutput,
77 MakeformatOutputPath, DiagConsumer);
78 }
79
80 /// Given a Clang driver command-line for a translation unit, gather the
81 /// modular dependencies and return the information needed for explicit build.
82 ///
83 /// \param AlreadySeen This stores modules which have previously been
84 /// reported. Use the same instance for all calls to this
85 /// function for a single \c DependencyScanningTool in a
86 /// single build. Use a different one for different tools,
87 /// and clear it between builds.
88 /// \param LookupModuleOutput This function is called to fill in
89 /// "-fmodule-file=", "-o" and other output
90 /// arguments for dependencies.
91 /// \param TUBuffer Optional memory buffer for translation unit input. If
92 /// std::nullopt, the input should be included
93 /// in the \p CommandLine already.
94 ///
95 /// \returns std::nullopt if errors occurred (reported to the DiagConsumer),
96 /// translation unit dependencies otherwise.
97 std::optional<dependencies::TranslationUnitDeps>
99 ArrayRef<std::string> CommandLine, StringRef CWD,
100 DiagnosticConsumer &DiagConsumer,
101 const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
103 std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);
104
105 /// By-name scanning given a Clang command-line. The scanning context is
106 /// initialized once and reused for each name pulled from
107 /// \p getNextName until it returns std::nullopt. Results flow to
108 /// \p DepConsumer (with per-name status via finishQuery); diagnostics flow to
109 /// \p DiagConsumer.
110 ///
111 /// \returns true if the scanning context initialized successfully and all
112 /// scans succeeded. Query the DepConsumer for the state of each individual
113 /// scan.
115 StringRef CWD, ArrayRef<std::string> CommandLine,
116 DiagnosticConsumer &DiagConsumer,
118 llvm::function_ref<std::optional<std::string>()> getNextName,
120
121 /// Returns the worker tracing VFS, if it was requested via the service.
122 llvm::vfs::TracingFileSystem *getWorkerTracingVFS() const {
123 return Worker.getTracingVFS();
124 }
125
126private:
128};
129
130/// Run the dependency scanning worker for the given driver or frontend
131/// command-line, and report the discovered dependencies to the provided
132/// consumer.
133///
134/// OverlayFS should be based on the Worker's dependency scanning file-system
135/// and can be used to provide any input specified on the command-line as
136/// in-memory file. If no overlay file-system is provided, the Worker's
137/// dependency scanning file-system is used instead.
138///
139/// \returns false if any errors occurred (with diagnostics reported to
140/// \p DiagConsumer), true otherwise.
142 dependencies::DependencyScanningWorker &Worker, StringRef WorkingDirectory,
143 ArrayRef<std::string> CommandLine,
146 DiagnosticConsumer &DiagConsumer,
148} // end namespace tooling
149} // end namespace clang
150
151#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, IntrusiveRefCntPtr< llvm::vfs::FileSystem > LocalFS=nullptr)
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...
Top level wrappers for InstallAPI frontend operations.
@ 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