clang 24.0.0git
TUSummaryExtractorFrontendAction.cpp
Go to the documentation of this file.
1//===- TUSummaryExtractorFrontendAction.cpp -------------------------------===//
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
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/IOSandbox.h"
24#include "llvm/Support/Path.h"
25#include "llvm/TargetParser/Triple.h"
26#include <memory>
27#include <string>
28#include <vector>
29
30using namespace clang;
31using namespace ssaf;
32
33static std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
35 StringRef SSAFTUSummaryFile) {
36
37 StringRef Ext = llvm::sys::path::extension(SSAFTUSummaryFile);
38 StringRef FilePath = SSAFTUSummaryFile.drop_back(Ext.size());
39
40 if (!Ext.consume_front(".") || FilePath.empty()) {
41 Diags.Report(diag::warn_ssaf_extract_tu_summary_file_unknown_format)
42 << SSAFTUSummaryFile;
43 return std::nullopt;
44 }
45
46 if (!isFormatRegistered(Ext)) {
47 Diags.Report(diag::warn_ssaf_extract_tu_summary_file_unknown_output_format)
48 << Ext << SSAFTUSummaryFile;
49 return std::nullopt;
50 }
51
52 return std::pair{Ext, FilePath};
53}
54
55/// Return \c true if reported unrecognized extractors.
56static bool
58 ArrayRef<std::string> SSAFExtractSummaries) {
59 if (SSAFExtractSummaries.empty()) {
60 Diags.Report(diag::warn_ssaf_must_enable_summary_extractors);
61 return true;
62 }
63
64 std::vector<StringRef> UnrecognizedExtractorNames;
65 for (StringRef Name : SSAFExtractSummaries)
67 UnrecognizedExtractorNames.push_back(Name);
68
69 if (!UnrecognizedExtractorNames.empty()) {
70 Diags.Report(diag::warn_ssaf_extract_summary_unknown_extractor_name)
71 << UnrecognizedExtractorNames.size()
72 << llvm::join(UnrecognizedExtractorNames, ", ");
73 return true;
74 }
75
76 return false;
77}
78
79static std::vector<std::unique_ptr<ASTConsumer>>
81 ArrayRef<std::string> SSAFExtractSummaries) {
82 std::vector<std::unique_ptr<ASTConsumer>> Extractors;
83 Extractors.reserve(SSAFExtractSummaries.size());
84 for (StringRef Name : SSAFExtractSummaries) {
86 Extractors.push_back(makeTUSummaryExtractor(Name, Builder));
87 }
88 return Extractors;
89}
90
91namespace {
92
93/// Drives all extractor \c ASTConsumers and serializes the completed
94/// \c TUSummary.
95///
96/// Derives from \c MultiplexConsumer so every \c ASTConsumer virtual method is
97/// automatically forwarded to each extractor.
98class TUSummaryRunner final : public MultiplexConsumer {
99public:
100 static std::unique_ptr<TUSummaryRunner> create(CompilerInstance &CI);
101
102private:
103 TUSummaryRunner(llvm::Triple TargetTriple,
104 std::unique_ptr<SerializationFormat> Format,
105 const SSAFOptions &Opts);
106
107 void HandleTranslationUnit(ASTContext &Ctx) override;
108
109 TUSummary Summary;
110
111 /// Owned by the \c CompilerInstance.
112 const SSAFOptions &Opts;
113
114 TUSummaryBuilder Builder = TUSummaryBuilder(Summary, Opts);
115 std::unique_ptr<SerializationFormat> Format;
116};
117} // namespace
118
119std::unique_ptr<TUSummaryRunner> TUSummaryRunner::create(CompilerInstance &CI) {
120 const SSAFOptions &Opts = CI.getSSAFOpts();
121 DiagnosticsEngine &Diags = CI.getDiagnostics();
122
123 if (Opts.CompilationUnitId.empty()) {
124 Diags.Report(diag::warn_ssaf_tu_summary_requires_compilation_unit_id);
125 return nullptr;
126 }
127
128 auto MaybePair =
130 if (!MaybePair.has_value())
131 return nullptr;
132 auto [FormatName, OutputPath] = MaybePair.value();
133
135 return nullptr;
136
137 return std::unique_ptr<TUSummaryRunner>{new TUSummaryRunner{
138 CI.getTarget().getTriple(), makeFormat(FormatName), Opts}};
139}
140
141TUSummaryRunner::TUSummaryRunner(llvm::Triple TargetTriple,
142 std::unique_ptr<SerializationFormat> Format,
143 const SSAFOptions &Opts)
144 : MultiplexConsumer(std::vector<std::unique_ptr<ASTConsumer>>{}),
145 Summary(std::move(TargetTriple),
146 BuildNamespace(BuildNamespaceKind::CompilationUnit,
147 Opts.CompilationUnitId)),
148 Opts(Opts), Format(std::move(Format)) {
149 assert(this->Format);
150 assert(!Opts.CompilationUnitId.empty());
151
152 // Now the Summary and the builders are constructed, we can also construct the
153 // extractors.
154 auto Extractors = makeTUSummaryExtractors(Builder, Opts.ExtractSummaries);
155 assert(!Extractors.empty());
156
157 // We must initialize the Consumers here because our extractors need a
158 // Builder that holds a reference to the TUSummary, which would be only
159 // initialized after the MultiplexConsumer ctor. This is the only way we can
160 // avoid the use of the TUSummary before it starts its lifetime.
161 MultiplexConsumer::Consumers = std::move(Extractors);
162}
163
164void TUSummaryRunner::HandleTranslationUnit(ASTContext &Ctx) {
165 // First, invoke the Summary Extractors.
167
168 // FIXME(sandboxing): Remove this by adopting `llvm::vfs::OutputBackend`.
169 llvm::sys::sandbox::ScopedSetting Guard = llvm::sys::sandbox::scopedDisable();
170
171 // Then serialize the result.
172 if (auto Err = Format->writeTUSummary(Summary, Opts.TUSummaryFile)) {
173 Ctx.getDiagnostics().Report(diag::warn_ssaf_write_tu_summary_failed)
174 << Opts.TUSummaryFile << llvm::toString(std::move(Err));
175 }
176}
177
179
183
184std::unique_ptr<ASTConsumer>
186 StringRef InFile) {
187 auto WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
188 if (!WrappedConsumer)
189 return nullptr;
190
191 if (auto Runner = TUSummaryRunner::create(CI)) {
192 CI.getCodeGenOpts().ClearASTBeforeBackend = false;
193 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
194 Consumers.reserve(2);
195 Consumers.push_back(std::move(WrappedConsumer));
196 Consumers.push_back(std::move(Runner));
197 return std::make_unique<MultiplexConsumer>(std::move(Consumers));
198 }
199 return WrappedConsumer;
200}
Defines the clang::ASTContext interface.
static bool reportUnrecognizedExtractorNames(DiagnosticsEngine &Diags, ArrayRef< std::string > SSAFExtractSummaries)
Return true if reported unrecognized extractors.
static std::optional< std::pair< llvm::StringRef, llvm::StringRef > > parseOutputFileFormatAndPathOrReportError(DiagnosticsEngine &Diags, StringRef SSAFTUSummaryFile)
static std::vector< std::unique_ptr< ASTConsumer > > makeTUSummaryExtractors(TUSummaryBuilder &Builder, ArrayRef< std::string > SSAFExtractSummaries)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
DiagnosticsEngine & getDiagnostics() const
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
ssaf::SSAFOptions & getSSAFOpts()
TargetInfo & getTarget() const
CodeGenOptions & getCodeGenOpts()
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:234
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
std::vector< std::unique_ptr< ASTConsumer > > Consumers
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
WrapperFrontendAction(std::unique_ptr< FrontendAction > WrappedAction)
Construct a WrapperFrontendAction from an existing action, taking ownership of it.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::unique_ptr< FrontendAction > WrappedAction
Represents a single namespace in the build process.
std::string CompilationUnitId
Stable identifier used as the name of the CompilationUnit BuildNamespace of every produced TU summary...
Definition SSAFOptions.h:32
std::vector< std::string > ExtractSummaries
List of SSAF extractors to enable.
Definition SSAFOptions.h:22
std::string TUSummaryFile
The TU summary output file with the file extension representing the serialization format.
Definition SSAFOptions.h:27
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
TUSummaryExtractorFrontendAction(std::unique_ptr< FrontendAction > WrappedAction)
Defines the clang::TargetInfo interface.
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions &DiagOpts, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
std::unique_ptr< SerializationFormat > makeFormat(llvm::StringRef FormatName)
Try to instantiate a SerializationFormat with a given name.
std::unique_ptr< TUSummaryExtractor > makeTUSummaryExtractor(llvm::StringRef SummaryName, TUSummaryBuilder &Builder)
Try to instantiate a TUSummaryExtractor with a given name.
bool isFormatRegistered(llvm::StringRef FormatName)
Check if a SerializationFormat was registered with a given name.
bool isTUSummaryExtractorRegistered(llvm::StringRef SummaryName)
Check if a TUSummaryExtractor was registered with a given name.
Top level wrappers for InstallAPI frontend operations.