[llvm-branch-commits] [Remarks] BitstreamRemarkParser: Refactor error handling (PR #156511)
Florian Hahn via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Sep 3 10:48:46 PDT 2025
================
@@ -12,35 +12,75 @@
//===----------------------------------------------------------------------===//
#include "BitstreamRemarkParser.h"
-#include "llvm/Remarks/Remark.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
+#include <cassert>
#include <optional>
using namespace llvm;
using namespace llvm::remarks;
-static Error unknownRecord(const char *BlockName, unsigned RecordID) {
- return createStringError(
- std::make_error_code(std::errc::illegal_byte_sequence),
- "Error while parsing %s: unknown record entry (%lu).", BlockName,
- RecordID);
+namespace {
+
+template <typename... Ts> Error error(char const *Fmt, const Ts &...Vals) {
+ std::string Buffer;
+ raw_string_ostream OS(Buffer);
+ OS << formatv(Fmt, Vals...);
+ return make_error<StringError>(
+ Buffer, std::make_error_code(std::errc::illegal_byte_sequence));
+}
+
+} // namespace
+
+Error BitstreamBlockParserHelperBase::unknownRecord(unsigned AbbrevID) {
+ return error("Unknown record entry ({}).", AbbrevID);
}
-static Error malformedRecord(const char *BlockName, const char *RecordName) {
- return createStringError(
- std::make_error_code(std::errc::illegal_byte_sequence),
- "Error while parsing %s: malformed record entry (%s).", BlockName,
- RecordName);
+Error BitstreamBlockParserHelperBase::unexpectedRecord(StringRef RecordName) {
+ return error("Unexpected record entry ({}).", RecordName);
}
-BitstreamMetaParserHelper::BitstreamMetaParserHelper(
- BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo)
- : Stream(Stream), BlockInfo(BlockInfo) {}
+Error BitstreamBlockParserHelperBase::malformedRecord(StringRef RecordName) {
+ return error("Malformed record entry ({}).", RecordName);
+}
+
+Error BitstreamBlockParserHelperBase::unexpectedBlock(unsigned Code) {
+ return error("Unexpected subblock ({}).", Code);
+}
+
+static Expected<unsigned> expectSubBlock(BitstreamCursor &Stream) {
+ Expected<BitstreamEntry> Next = Stream.advance();
+ if (!Next)
+ return Next.takeError();
+ switch (Next->Kind) {
+ case BitstreamEntry::SubBlock:
+ return Next->ID;
+ case BitstreamEntry::Record:
+ case BitstreamEntry::EndBlock:
+ return error("Expected subblock, but got unexpected record.");
+ case BitstreamEntry::Error:
+ return error("Expected subblock, but got unexpected end of bitstream.");
+ }
+}
+
+Error BitstreamBlockParserHelperBase::expectBlock() {
+ auto MaybeBlockID = expectSubBlock(Stream);
+ if (!MaybeBlockID)
+ return MaybeBlockID.takeError();
+ if (*MaybeBlockID != BlockID)
+ return error("Expected {} block, but got unexpected block ({}).", BlockName,
+ *MaybeBlockID);
+ return Error::success();
+}
+
+Error BitstreamBlockParserHelperBase::enterBlock() {
+ if (Stream.EnterSubBlock(BlockID))
+ return error("Error while entering {} block.", BlockName);
+ return Error::success();
+}
/// Parse a record and fill in the fields in the parser.
----------------
fhahn wrote:
move comment to declaration?
https://github.com/llvm/llvm-project/pull/156511
More information about the llvm-branch-commits
mailing list