| 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 | |
| 9 | #include "clang/ScalableStaticAnalysisFramework/Frontend/TUSummaryExtractorFrontendAction.h" |
| 10 | #include "clang/AST/ASTConsumer.h" |
| 11 | #include "clang/Basic/DiagnosticFrontend.h" |
| 12 | #include "clang/Frontend/MultiplexConsumer.h" |
| 13 | #include "clang/ScalableStaticAnalysisFramework/Core/Serialization/SerializationFormatRegistry.h" |
| 14 | #include "clang/ScalableStaticAnalysisFramework/Core/TUSummary/ExtractorRegistry.h" |
| 15 | #include "clang/ScalableStaticAnalysisFramework/Core/TUSummary/TUSummary.h" |
| 16 | #include "clang/ScalableStaticAnalysisFramework/Core/TUSummary/TUSummaryBuilder.h" |
| 17 | #include "clang/ScalableStaticAnalysisFramework/Core/TUSummary/TUSummaryExtractor.h" |
| 18 | #include "llvm/ADT/StringExtras.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/Support/Path.h" |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | using namespace clang; |
| 26 | using namespace ssaf; |
| 27 | |
| 28 | static std::optional<std::pair<llvm::StringRef, llvm::StringRef>> |
| 29 | parseOutputFileFormatAndPathOrReportError(DiagnosticsEngine &Diags, |
| 30 | StringRef SSAFTUSummaryFile) { |
| 31 | |
| 32 | StringRef Ext = llvm::sys::path::extension(path: SSAFTUSummaryFile); |
| 33 | StringRef FilePath = SSAFTUSummaryFile.drop_back(N: Ext.size()); |
| 34 | |
| 35 | if (!Ext.consume_front(Prefix: "." ) || FilePath.empty()) { |
| 36 | Diags.Report(DiagID: diag::warn_ssaf_extract_tu_summary_file_unknown_format) |
| 37 | << SSAFTUSummaryFile; |
| 38 | return std::nullopt; |
| 39 | } |
| 40 | |
| 41 | if (!isFormatRegistered(FormatName: Ext)) { |
| 42 | Diags.Report(DiagID: diag::warn_ssaf_extract_tu_summary_file_unknown_output_format) |
| 43 | << Ext << SSAFTUSummaryFile; |
| 44 | return std::nullopt; |
| 45 | } |
| 46 | |
| 47 | return std::pair{Ext, FilePath}; |
| 48 | } |
| 49 | |
| 50 | /// Return \c true if reported unrecognized extractors. |
| 51 | static bool |
| 52 | (DiagnosticsEngine &Diags, |
| 53 | ArrayRef<std::string> ) { |
| 54 | if (SSAFExtractSummaries.empty()) { |
| 55 | Diags.Report(DiagID: diag::warn_ssaf_must_enable_summary_extractors); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | std::vector<StringRef> ; |
| 60 | for (StringRef Name : SSAFExtractSummaries) |
| 61 | if (!isTUSummaryExtractorRegistered(SummaryName: Name)) |
| 62 | UnrecognizedExtractorNames.push_back(x: Name); |
| 63 | |
| 64 | if (!UnrecognizedExtractorNames.empty()) { |
| 65 | Diags.Report(DiagID: diag::warn_ssaf_extract_summary_unknown_extractor_name) |
| 66 | << UnrecognizedExtractorNames.size() |
| 67 | << llvm::join(R&: UnrecognizedExtractorNames, Separator: ", " ); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | static std::vector<std::unique_ptr<ASTConsumer>> |
| 75 | (TUSummaryBuilder &Builder, |
| 76 | ArrayRef<std::string> ) { |
| 77 | std::vector<std::unique_ptr<ASTConsumer>> ; |
| 78 | Extractors.reserve(n: SSAFExtractSummaries.size()); |
| 79 | for (StringRef Name : SSAFExtractSummaries) { |
| 80 | assert(isTUSummaryExtractorRegistered(Name)); |
| 81 | Extractors.push_back(x: makeTUSummaryExtractor(SummaryName: Name, Builder)); |
| 82 | } |
| 83 | return Extractors; |
| 84 | } |
| 85 | |
| 86 | namespace { |
| 87 | |
| 88 | /// Drives all extractor \c ASTConsumers and serializes the completed |
| 89 | /// \c TUSummary. |
| 90 | /// |
| 91 | /// Derives from \c MultiplexConsumer so every \c ASTConsumer virtual method is |
| 92 | /// automatically forwarded to each extractor. |
| 93 | class TUSummaryRunner final : public MultiplexConsumer { |
| 94 | public: |
| 95 | static std::unique_ptr<TUSummaryRunner> create(CompilerInstance &CI, |
| 96 | StringRef InFile); |
| 97 | |
| 98 | private: |
| 99 | TUSummaryRunner(StringRef InFile, std::unique_ptr<SerializationFormat> Format, |
| 100 | const FrontendOptions &Opts); |
| 101 | |
| 102 | void HandleTranslationUnit(ASTContext &Ctx) override; |
| 103 | |
| 104 | TUSummary Summary; |
| 105 | TUSummaryBuilder Builder = TUSummaryBuilder(Summary); |
| 106 | std::unique_ptr<SerializationFormat> Format; |
| 107 | const FrontendOptions &Opts; |
| 108 | }; |
| 109 | } // namespace |
| 110 | |
| 111 | std::unique_ptr<TUSummaryRunner> TUSummaryRunner::create(CompilerInstance &CI, |
| 112 | StringRef InFile) { |
| 113 | const FrontendOptions &Opts = CI.getFrontendOpts(); |
| 114 | DiagnosticsEngine &Diags = CI.getDiagnostics(); |
| 115 | |
| 116 | auto MaybePair = |
| 117 | parseOutputFileFormatAndPathOrReportError(Diags, SSAFTUSummaryFile: Opts.SSAFTUSummaryFile); |
| 118 | if (!MaybePair.has_value()) |
| 119 | return nullptr; |
| 120 | auto [FormatName, OutputPath] = MaybePair.value(); |
| 121 | |
| 122 | if (reportUnrecognizedExtractorNames(Diags, SSAFExtractSummaries: Opts.SSAFExtractSummaries)) |
| 123 | return nullptr; |
| 124 | |
| 125 | return std::unique_ptr<TUSummaryRunner>{ |
| 126 | new TUSummaryRunner{InFile, makeFormat(FormatName), Opts}}; |
| 127 | } |
| 128 | |
| 129 | TUSummaryRunner::TUSummaryRunner(StringRef InFile, |
| 130 | std::unique_ptr<SerializationFormat> Format, |
| 131 | const FrontendOptions &Opts) |
| 132 | : MultiplexConsumer(std::vector<std::unique_ptr<ASTConsumer>>{}), |
| 133 | Summary(BuildNamespace(BuildNamespaceKind::CompilationUnit, InFile)), |
| 134 | Format(std::move(Format)), Opts(Opts) { |
| 135 | assert(this->Format); |
| 136 | |
| 137 | // Now the Summary and the builders are constructed, we can also construct the |
| 138 | // extractors. |
| 139 | auto = makeTUSummaryExtractors(Builder, SSAFExtractSummaries: Opts.SSAFExtractSummaries); |
| 140 | assert(!Extractors.empty()); |
| 141 | |
| 142 | // We must initialize the Consumers here because our extractors need a |
| 143 | // Builder that holds a reference to the TUSummary, which would be only |
| 144 | // initialized after the MultiplexConsumer ctor. This is the only way we can |
| 145 | // avoid the use of the TUSummary before it starts its lifetime. |
| 146 | MultiplexConsumer::Consumers = std::move(Extractors); |
| 147 | } |
| 148 | |
| 149 | void TUSummaryRunner::HandleTranslationUnit(ASTContext &Ctx) { |
| 150 | // First, invoke the Summary Extractors. |
| 151 | MultiplexConsumer::HandleTranslationUnit(Ctx); |
| 152 | |
| 153 | // Then serialize the result. |
| 154 | if (auto Err = Format->writeTUSummary(Summary, Path: Opts.SSAFTUSummaryFile)) { |
| 155 | Ctx.getDiagnostics().Report(DiagID: diag::warn_ssaf_write_tu_summary_failed) |
| 156 | << Opts.SSAFTUSummaryFile << llvm::toString(E: std::move(Err)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | TUSummaryExtractorFrontendAction::() = default; |
| 161 | |
| 162 | TUSummaryExtractorFrontendAction::( |
| 163 | std::unique_ptr<FrontendAction> WrappedAction) |
| 164 | : WrapperFrontendAction(std::move(WrappedAction)) {} |
| 165 | |
| 166 | std::unique_ptr<ASTConsumer> |
| 167 | TUSummaryExtractorFrontendAction::(CompilerInstance &CI, |
| 168 | StringRef InFile) { |
| 169 | auto WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); |
| 170 | if (!WrappedConsumer) |
| 171 | return nullptr; |
| 172 | |
| 173 | if (auto Runner = TUSummaryRunner::create(CI, InFile)) { |
| 174 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
| 175 | Consumers.reserve(n: 2); |
| 176 | Consumers.push_back(x: std::move(WrappedConsumer)); |
| 177 | Consumers.push_back(x: std::move(Runner)); |
| 178 | return std::make_unique<MultiplexConsumer>(args: std::move(Consumers)); |
| 179 | } |
| 180 | return WrappedConsumer; |
| 181 | } |
| 182 | |