llvm.org GIT mirror llvm / ce4ba23
[Remarks][NFC] Combine ParserFormat and SerializerFormat It's useless to have both. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366216 91177308-0d34-0410-b5e6-96231b3b80d8 Francis Visoiu Mistrih a month ago
13 changed file(s) with 100 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
8989 using RemarkSetupErrorInfo::RemarkSetupErrorInfo;
9090 };
9191
92 enum class RemarksSerializerFormat { Unknown, YAML };
93
94 Expected parseSerializerFormat(StringRef Format);
95
9692 /// Setup optimization remarks.
9793 Expected>
9894 setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
2323 namespace remarks {
2424
2525 constexpr uint64_t Version = 0;
26 constexpr StringRef Magic("REMARKS", 7);
2726
2827 /// The debug location used to track a remark back to the source file.
2928 struct RemarkLocation {
0 //===-- llvm/Remarks/RemarkFormat.h - The format of remarks -----*- C++/-*-===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file defines utilities to deal with the format of remarks.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_REMARKS_REMARK_FORMAT_H
13 #define LLVM_REMARKS_REMARK_FORMAT_H
14
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Error.h"
17
18 namespace llvm {
19 namespace remarks {
20
21 constexpr StringRef Magic("REMARKS", 7);
22
23 /// The format used for serializing/deserializing remarks.
24 enum class Format { Unknown, YAML };
25
26 /// Parse and validate a string for the remark format.
27 Expected parseFormat(StringRef FormatStr);
28
29 } // end namespace remarks
30 } // end namespace llvm
31
32 #endif /* LLVM_REMARKS_REMARK_FORMAT_H */
1515 #include "llvm/ADT/SmallVector.h"
1616 #include "llvm/ADT/StringRef.h"
1717 #include "llvm/Remarks/Remark.h"
18 #include "llvm/Remarks/RemarkFormat.h"
1819 #include "llvm/Support/Error.h"
1920 #include
2021
2425 struct ParserImpl;
2526 struct ParsedStringTable;
2627
27 enum class ParserFormat { YAML };
28
2928 /// Parser used to parse a raw buffer to remarks::Remark objects.
3029 struct Parser {
3130 /// The hidden implementation of the parser.
3433 /// Create a parser parsing \p Buffer to Remark objects.
3534 /// This constructor should be only used for parsing remarks without a string
3635 /// table.
37 Parser(ParserFormat Format, StringRef Buffer);
36 Parser(Format ParserFormat, StringRef Buffer);
3837
3938 /// Create a parser parsing \p Buffer to Remark objects, using \p StrTab as a
4039 /// string table.
41 Parser(ParserFormat Format, StringRef Buffer,
40 Parser(Format ParserFormat, StringRef Buffer,
4241 const ParsedStringTable &StrTab);
4342
4443 // Needed because ParserImpl is an incomplete type.
9999 #include "llvm/MC/SectionKind.h"
100100 #include "llvm/Pass.h"
101101 #include "llvm/Remarks/Remark.h"
102 #include "llvm/Remarks/RemarkFormat.h"
102103 #include "llvm/Remarks/RemarkStringTable.h"
103104 #include "llvm/Support/Casting.h"
104105 #include "llvm/Support/CommandLine.h"
1414 #include "llvm/IR/DiagnosticInfo.h"
1515 #include "llvm/IR/Function.h"
1616 #include "llvm/IR/GlobalValue.h"
17 #include "llvm/Remarks/RemarkFormat.h"
1718
1819 using namespace llvm;
1920
111112 char RemarkSetupFormatError::ID = 0;
112113
113114 static std::unique_ptr
114 formatToSerializer(RemarksSerializerFormat RemarksFormat, raw_ostream &OS) {
115 formatToSerializer(remarks::Format RemarksFormat, raw_ostream &OS) {
115116 switch (RemarksFormat) {
116117 default:
117118 llvm_unreachable("Unknown remark serializer format.");
118119 return nullptr;
119 case RemarksSerializerFormat::YAML:
120 case remarks::Format::YAML:
120121 return llvm::make_unique(OS);
121122 };
122 }
123
124 Expected
125 llvm::parseSerializerFormat(StringRef StrFormat) {
126 auto Format = StringSwitch(StrFormat)
127 .Cases("", "yaml", RemarksSerializerFormat::YAML)
128 .Default(RemarksSerializerFormat::Unknown);
129
130 if (Format == RemarksSerializerFormat::Unknown)
131 return createStringError(std::make_error_code(std::errc::invalid_argument),
132 "Unknown remark serializer format: '%s'",
133 StrFormat.data());
134
135 return Format;
136123 }
137124
138125 Expected>
157144 if (EC)
158145 return make_error(errorCodeToError(EC));
159146
160 Expected Format =
161 parseSerializerFormat(RemarksFormat);
147 Expected Format = remarks::parseFormat(RemarksFormat);
162148 if (Error E = Format.takeError())
163149 return make_error(std::move(E));
164150
0 add_llvm_library(LLVMRemarks
11 Remark.cpp
2 RemarkFormat.cpp
23 RemarkParser.cpp
34 RemarkStringTable.cpp
45 YAMLRemarkParser.cpp
0 //===- RemarkFormat.cpp --------------------------------------------------===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Implementation of utilities to handle the different remark formats.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "llvm/Remarks/RemarkFormat.h"
13 #include "llvm/ADT/StringSwitch.h"
14
15 using namespace llvm;
16 using namespace llvm::remarks;
17
18 Expected llvm::remarks::parseFormat(StringRef FormatStr) {
19 auto Result = StringSwitch(FormatStr)
20 .Cases("", "yaml", Format::YAML)
21 .Default(Format::Unknown);
22
23 if (Result == Format::Unknown)
24 return createStringError(std::make_error_code(std::errc::invalid_argument),
25 "Unknown remark serializer format: '%s'",
26 FormatStr.data());
27
28 return Result;
29 }
1919 using namespace llvm;
2020 using namespace llvm::remarks;
2121
22 static std::unique_ptr formatToParserImpl(ParserFormat Format,
22 static std::unique_ptr formatToParserImpl(Format ParserFormat,
2323 StringRef Buf) {
24 switch (Format) {
25 case ParserFormat::YAML:
24 switch (ParserFormat) {
25 case Format::YAML:
2626 return llvm::make_unique(Buf);
27 case Format::Unknown:
28 llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum");
29 return nullptr;
2730 };
28 llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum");
2931 }
3032
3133 static std::unique_ptr
32 formatToParserImpl(ParserFormat Format, StringRef Buf,
34 formatToParserImpl(Format ParserFormat, StringRef Buf,
3335 const ParsedStringTable &StrTab) {
34 switch (Format) {
35 case ParserFormat::YAML:
36 switch (ParserFormat) {
37 case Format::YAML:
3638 return llvm::make_unique(Buf, &StrTab);
39 case Format::Unknown:
40 llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum");
41 return nullptr;
3742 };
38 llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum");
3943 }
4044
41 Parser::Parser(ParserFormat Format, StringRef Buf)
42 : Impl(formatToParserImpl(Format, Buf)) {}
45 Parser::Parser(Format ParserFormat, StringRef Buf)
46 : Impl(formatToParserImpl(ParserFormat, Buf)) {}
4347
44 Parser::Parser(ParserFormat Format, StringRef Buf,
48 Parser::Parser(Format ParserFormat, StringRef Buf,
4549 const ParsedStringTable &StrTab)
46 : Impl(formatToParserImpl(Format, Buf, StrTab)) {}
50 : Impl(formatToParserImpl(ParserFormat, Buf, StrTab)) {}
4751
4852 Parser::~Parser() = default;
4953
109113
110114 extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
111115 uint64_t Size) {
112 return wrap(
113 new remarks::Parser(remarks::ParserFormat::YAML,
114 StringRef(static_cast(Buf), Size)));
116 return wrap(new remarks::Parser(
117 remarks::Format::YAML, StringRef(static_cast(Buf), Size)));
115118 }
116119
117120 static void handleYAMLError(remarks::YAMLParserImpl &Impl, Error E) {
1818 namespace remarks {
1919 /// This is used as a base for any parser implementation.
2020 struct ParserImpl {
21 explicit ParserImpl(ParserFormat Format) : Format(Format) {}
21 explicit ParserImpl(Format ParserFormat) : ParserFormat(ParserFormat) {}
2222 // Virtual destructor prevents mismatched deletes
2323 virtual ~ParserImpl() {}
2424
2525 // The parser format. This is used as a tag to safely cast between
2626 // implementations.
27 ParserFormat Format;
27 Format ParserFormat;
2828 };
2929 } // end namespace remarks
3030 } // end namespace llvm
126126
127127 YAMLParserImpl(StringRef Buf,
128128 Optional StrTab = None)
129 : ParserImpl{ParserFormat::YAML}, YAMLParser(Buf, StrTab),
129 : ParserImpl{Format::YAML}, YAMLParser(Buf, StrTab),
130130 YAMLIt(YAMLParser.Stream.begin()), HasErrors(false) {}
131131
132132 static bool classof(const ParserImpl *PI) {
133 return PI->Format == ParserFormat::YAML;
133 return PI->ParserFormat == Format::YAML;
134134 }
135135 };
136136 } // end namespace remarks
149149 return false;
150150 }
151151
152 remarks::Parser Parser(remarks::ParserFormat::YAML, (*Buf)->getBuffer());
152 remarks::Parser Parser(remarks::Format::YAML, (*Buf)->getBuffer());
153153
154154 while (true) {
155155 Expected RemarkOrErr = Parser.getNext();
1313 using namespace llvm;
1414
1515 template void parseGood(const char (&Buf)[N]) {
16 remarks::Parser Parser(remarks::ParserFormat::YAML, {Buf, N - 1});
16 remarks::Parser Parser(remarks::Format::YAML, {Buf, N - 1});
1717 Expected Remark = Parser.getNext();
1818 EXPECT_FALSE(errorToBool(Remark.takeError())); // Check for parsing errors.
1919 EXPECT_TRUE(*Remark != nullptr); // At least one remark.
2424
2525 template
2626 bool parseExpectError(const char (&Buf)[N], const char *Error) {
27 remarks::Parser Parser(remarks::ParserFormat::YAML, {Buf, N - 1});
27 remarks::Parser Parser(remarks::Format::YAML, {Buf, N - 1});
2828 Expected Remark = Parser.getNext();
2929 EXPECT_FALSE(Remark); // Expect an error here.
3030
353353 " - String: ' because its definition is unavailable'\n"
354354 "\n";
355355
356 remarks::Parser Parser(remarks::ParserFormat::YAML, Buf);
356 remarks::Parser Parser(remarks::Format::YAML, Buf);
357357 Expected RemarkOrErr = Parser.getNext();
358358 EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
359359 EXPECT_TRUE(*RemarkOrErr != nullptr);
515515 115);
516516
517517 remarks::ParsedStringTable StrTab(StrTabBuf);
518 remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab);
518 remarks::Parser Parser(remarks::Format::YAML, Buf, StrTab);
519519 Expected RemarkOrErr = Parser.getNext();
520520 EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
521521 EXPECT_TRUE(*RemarkOrErr != nullptr);
583583 StringRef StrTabBuf = StringRef("inline");
584584
585585 remarks::ParsedStringTable StrTab(StrTabBuf);
586 remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab);
586 remarks::Parser Parser(remarks::Format::YAML, Buf, StrTab);
587587 Expected Remark = Parser.getNext();
588588 EXPECT_FALSE(Remark); // Expect an error here.
589589