[llvm] [Object] Beginnings of SFrame parser and dumper (PR #147294)
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 14 02:09:32 PDT 2025
================
@@ -0,0 +1,56 @@
+//===- SFrameParser.cpp ---------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Object/SFrameParser.h"
+#include "llvm/BinaryFormat/SFrame.h"
+#include "llvm/Object/Error.h"
+#include "llvm/Support/FormatVariadic.h"
+
+using namespace llvm;
+using namespace llvm::object;
+
+template <typename T>
+static Expected<const T &> getDataSliceAs(ArrayRef<uint8_t> Data,
+ uint64_t Offset) {
+ static_assert(std::is_trivial_v<T>);
+ if (Data.size() < Offset + sizeof(T)) {
+ return make_error<GenericBinaryError>(
+ formatv("unexpected end of data at offset {0:x} while reading [{1:x}, "
+ "{2:x})",
+ Data.size(), Offset, Offset + sizeof(T))
+ .str(),
+ object_error::unexpected_eof);
+ }
+ return *reinterpret_cast<const T *>(Data.data() + Offset);
+}
+
+template <endianness E>
+Expected<SFrameParser<E>> SFrameParser<E>::create(ArrayRef<uint8_t> Contents) {
+ Expected<const sframe::Preamble<E> &> Preamble =
+ getDataSliceAs<sframe::Preamble<E>>(Contents, 0);
+ if (!Preamble)
+ return Preamble.takeError();
+
+ if (Preamble->Magic != sframe::Magic) {
----------------
jh7370 wrote:
Nit: here and below, these are single line ifs, so shouldn't have braces.
https://github.com/llvm/llvm-project/pull/147294
More information about the llvm-commits
mailing list