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