llvm-project
143 строки · 5.2 Кб
1//===- RemarkConvert.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// Convert remarks from bitstream to yaml and the other way around.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RemarkUtilHelpers.h"14#include "RemarkUtilRegistry.h"15
16using namespace llvm;17using namespace remarks;18using namespace llvm::remarkutil;19
20extern ExitOnError ExitOnErr;21static cl::SubCommand22YAML2Bitstream("yaml2bitstream",23"Convert YAML remarks to bitstream remarks");24static cl::SubCommand25Bitstream2YAML("bitstream2yaml",26"Convert bitstream remarks to YAML remarks");27
28namespace yaml2bitstream {29/// Remark format to parse.
30static constexpr Format InputFormat = Format::YAML;31/// Remark format to output.
32static constexpr Format OutputFormat = Format::Bitstream;33INPUT_OUTPUT_COMMAND_LINE_OPTIONS(YAML2Bitstream)34} // namespace yaml2bitstream35
36namespace bitstream2yaml {37/// Remark format to parse.
38static constexpr Format InputFormat = Format::Bitstream;39/// Remark format to output.
40static constexpr Format OutputFormat = Format::YAML;41INPUT_OUTPUT_COMMAND_LINE_OPTIONS(Bitstream2YAML)42} // namespace bitstream2yaml43
44namespace yaml2bitstream {45/// Parses all remarks in the input YAML file.
46/// \p [out] ParsedRemarks - Filled with remarks parsed from the input file.
47/// \p [out] StrTab - A string table populated for later remark serialization.
48/// \returns Error::success() if all remarks were successfully parsed, and an
49/// Error otherwise.
50static Error51tryParseRemarksFromYAMLFile(std::vector<std::unique_ptr<Remark>> &ParsedRemarks,52StringTable &StrTab) {53auto MaybeBuf = getInputMemoryBuffer(InputFileName);54if (!MaybeBuf)55return MaybeBuf.takeError();56auto MaybeParser = createRemarkParser(InputFormat, (*MaybeBuf)->getBuffer());57if (!MaybeParser)58return MaybeParser.takeError();59auto &Parser = **MaybeParser;60auto MaybeRemark = Parser.next();61for (; MaybeRemark; MaybeRemark = Parser.next()) {62StrTab.internalize(**MaybeRemark);63ParsedRemarks.push_back(std::move(*MaybeRemark));64}65auto E = MaybeRemark.takeError();66if (!E.isA<EndOfFileError>())67return E;68consumeError(std::move(E));69return Error::success();70}
71
72/// Reserialize a list of parsed YAML remarks into bitstream remarks.
73/// \p ParsedRemarks - A list of remarks.
74/// \p StrTab - The string table for the remarks.
75/// \returns Error::success() on success.
76static Error tryReserializeYAML2Bitstream(77const std::vector<std::unique_ptr<Remark>> &ParsedRemarks,78StringTable &StrTab) {79auto MaybeOF = getOutputFileForRemarks(OutputFileName, OutputFormat);80if (!MaybeOF)81return MaybeOF.takeError();82auto OF = std::move(*MaybeOF);83auto MaybeSerializer = createRemarkSerializer(84OutputFormat, SerializerMode::Standalone, OF->os(), std::move(StrTab));85if (!MaybeSerializer)86return MaybeSerializer.takeError();87auto Serializer = std::move(*MaybeSerializer);88for (const auto &Remark : ParsedRemarks)89Serializer->emit(*Remark);90OF->keep();91return Error::success();92}
93
94/// Parse YAML remarks and reserialize as bitstream remarks.
95/// \returns Error::success() on success, and an Error otherwise.
96static Error tryYAML2Bitstream() {97StringTable StrTab;98std::vector<std::unique_ptr<Remark>> ParsedRemarks;99ExitOnErr(tryParseRemarksFromYAMLFile(ParsedRemarks, StrTab));100return tryReserializeYAML2Bitstream(ParsedRemarks, StrTab);101}
102} // namespace yaml2bitstream103
104namespace bitstream2yaml {105/// Parse bitstream remarks and reserialize as YAML remarks.
106/// \returns An Error if reserialization fails, or Error::success() on success.
107static Error tryBitstream2YAML() {108// Create the serializer.109auto MaybeOF = getOutputFileForRemarks(OutputFileName, OutputFormat);110if (!MaybeOF)111return MaybeOF.takeError();112auto OF = std::move(*MaybeOF);113auto MaybeSerializer = createRemarkSerializer(114OutputFormat, SerializerMode::Standalone, OF->os());115if (!MaybeSerializer)116return MaybeSerializer.takeError();117
118// Create the parser.119auto MaybeBuf = getInputMemoryBuffer(InputFileName);120if (!MaybeBuf)121return MaybeBuf.takeError();122auto Serializer = std::move(*MaybeSerializer);123auto MaybeParser = createRemarkParser(InputFormat, (*MaybeBuf)->getBuffer());124if (!MaybeParser)125return MaybeParser.takeError();126auto &Parser = **MaybeParser;127
128// Parse + reserialize all remarks.129auto MaybeRemark = Parser.next();130for (; MaybeRemark; MaybeRemark = Parser.next())131Serializer->emit(**MaybeRemark);132auto E = MaybeRemark.takeError();133if (!E.isA<EndOfFileError>())134return E;135consumeError(std::move(E));136return Error::success();137}
138} // namespace bitstream2yaml139
140static CommandRegistration Bitstream2YamlReg(&Bitstream2YAML,141bitstream2yaml::tryBitstream2YAML);142static CommandRegistration Yaml2Bitstream(&YAML2Bitstream,143yaml2bitstream::tryYAML2Bitstream);144