clang 23.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
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/IOSandbox.h"
23#include "llvm/Support/Path.h"
24#include "llvm/TargetParser/Triple.h"
25#include <memory>
26#include <string>
27#include <vector>
28
29using namespace clang;
30using namespace ssaf;
31
32static std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
34 StringRef SSAFTUSummaryFile) {
35
36 StringRef Ext = llvm::sys::path::extension(SSAFTUSummaryFile);
37 StringRef FilePath = SSAFTUSummaryFile.drop_back(Ext.size());
38
39 if (!Ext.consume_front(".") || FilePath.empty()) {
40 Diags.Report(diag::warn_ssaf_extract_tu_summary_file_unknown_format)
41 << SSAFTUSummaryFile;
42 return std::nullopt;
43 }
44
45 if (!isFormatRegistered(Ext)) {
46 Diags.Report(diag::warn_ssaf_extract_tu_summary_file_unknown_output_format)
47 << Ext << SSAFTUSummaryFile;
48 return std::nullopt;
49 }
50
51 return std::pair{Ext, FilePath};
52}
53
54/// Return \c true if reported unrecognized extractors.
55static bool
57 ArrayRef<std::string> SSAFExtractSummaries) {
58 if (SSAFExtractSummaries.empty()) {
59 Diags.Report(diag::warn_ssaf_must_enable_summary_extractors);
60 return true;
61 }
62
63 std::vector<StringRef> UnrecognizedExtractorNames;
64 for (StringRef Name : SSAFExtractSummaries)
66 UnrecognizedExtractorNames.push_back(Name);
67
68 if (!UnrecognizedExtractorNames.empty()) {
69 Diags.Report(diag::warn_ssaf_extract_summary_unknown_extractor_name)
70 << UnrecognizedExtractorNames.size()
71 << llvm::join(UnrecognizedExtractorNames, ", ");
72 return true;
73 }
74
75 return false;
76}
77
78static std::vector<std::unique_ptr<ASTConsumer>>
80 ArrayRef<std::string> SSAFExtractSummaries) {
81 std::vector<std::unique_ptr<ASTConsumer>> Extractors;
82 Extractors.reserve(SSAFExtractSummaries.size());
83 for (StringRef Name : SSAFExtractSummaries) {
85 Extractors.push_back(makeTUSummaryExtractor(Name, Builder));
86 }
87 return Extractors;
88}
89
90namespace {
91
92/// Drives all extractor \c ASTConsumers and serializes the completed
93/// \c TUSummary.
94///
95/// Derives from \c MultiplexConsumer so every \c ASTConsumer virtual method is
96/// automatically forwarded to each extractor.
97class TUSummaryRunner final : public MultiplexConsumer {
98public:
99 static std::unique_ptr<TUSummaryRunner> create(CompilerInstance &CI);
100
101private:
102 TUSummaryRunner(llvm::Triple TargetTriple,
103 std::unique_ptr<SerializationFormat> Format,
104 const SSAFOptions &Opts);
105
106 void HandleTranslationUnit(ASTContext &Ctx) override;
107
108 TUSummary Summary;
109
110 /// Owned by the \c CompilerInstance.
111 const SSAFOptions &Opts;
112
113 TUSummaryBuilder Builder = TUSummaryBuilder(Summary, Opts);
114 std::unique_ptr<SerializationFormat> Format;
115};
116} // namespace
117
118std::unique_ptr<TUSummaryRunner> TUSummaryRunner::create(CompilerInstance &CI) {
119 const SSAFOptions &Opts = CI.getSSAFOpts();
120 DiagnosticsEngine &Diags = CI.getDiagnostics();
121
122 if (Opts.CompilationUnitId.empty()) {
123 Diags.Report(diag::warn_ssaf_tu_summary_requires_compilation_unit_id);
124 return nullptr;
125 }
126
127 auto MaybePair =
129 if (!MaybePair.has_value())
130 return nullptr;
131 auto [FormatName, OutputPath] = MaybePair.value();
132
134 return nullptr;
135
136 return std::unique_ptr<TUSummaryRunner>{new TUSummaryRunner{
137 CI.getTarget().getTriple(), makeFormat(FormatName), Opts}};
138}
139
140TUSummaryRunner::TUSummaryRunner(llvm::Triple TargetTriple,
141 std::unique_ptr<SerializationFormat> Format,
142 const SSAFOptions &Opts)
143 : MultiplexConsumer(std::vector<std::unique_ptr<ASTConsumer>>{}),
144 Summary(std::move(TargetTriple),
145 BuildNamespace(BuildNamespaceKind::CompilationUnit,
146 Opts.CompilationUnitId)),
147 Opts(Opts), Format(std::move(Format)) {
148 assert(this->Format);
149 assert(!Opts.CompilationUnitId.empty());
150
151 // Now the Summary and the builders are constructed, we can also construct the
152 // extractors.
153 auto Extractors = makeTUSummaryExtractors(Builder, Opts.ExtractSummaries);
154 assert(!Extractors.empty());
155
156 // We must initialize the Consumers here because our extractors need a
157 // Builder that holds a reference to the TUSummary, which would be only
158 // initialized after the MultiplexConsumer ctor. This is the only way we can
159 // avoid the use of the TUSummary before it starts its lifetime.
160 MultiplexConsumer::Consumers = std::move(Extractors);
161}
162
163void TUSummaryRunner::HandleTranslationUnit(ASTContext &Ctx) {
164 // First, invoke the Summary Extractors.
166
167 // FIXME(sandboxing): Remove this by adopting `llvm::vfs::OutputBackend`.
168 llvm::sys::sandbox::ScopedSetting Guard = llvm::sys::sandbox::scopedDisable();
169
170 // Then serialize the result.
171 if (auto Err = Format->writeTUSummary(Summary, Opts.TUSummaryFile)) {
172 Ctx.getDiagnostics().Report(diag::warn_ssaf_write_tu_summary_failed)
173 << Opts.TUSummaryFile << llvm::toString(std::move(Err));
174 }
175}
176
178
182
183std::unique_ptr<ASTConsumer>
185 StringRef InFile) {
186 auto WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
187 if (!WrappedConsumer)
188 return nullptr;
189
190 if (auto Runner = TUSummaryRunner::create(CI)) {
191 CI.getCodeGenOpts().ClearASTBeforeBackend = false;
192 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
193 Consumers.reserve(2);
194 Consumers.push_back(std::move(WrappedConsumer));
195 Consumers.push_back(std::move(Runner));
196 return std::make_unique<MultiplexConsumer>(std::move(Consumers));
197 }
198 return WrappedConsumer;
199}
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.
The JSON file list parser is used to communicate input to InstallAPI.