[clang] [clang][bytecode] Handle bitcasts involving bitfields (PR #116843)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 20 06:38:39 PST 2024


================
@@ -0,0 +1,66 @@
+//===--------------------- BitcastBuffer.h ----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_AST_INTERP_BITCAST_BUFFER_H
+#define LLVM_CLANG_AST_INTERP_BITCAST_BUFFER_H
+
+#include <cassert>
+#include <cstddef>
+#include <memory>
+
+namespace clang {
+namespace interp {
+
+enum class Endian { Little, Big };
+
+/// Returns the value of the bit in the given sequence of bytes.
+static inline bool bitof(const std::byte *B, unsigned BitIndex) {
+  return (B[BitIndex / 8] & (std::byte{1} << (BitIndex % 8))) != std::byte{0};
+}
+
+/// Returns whether \p N is a full byte offset or size.
+static inline bool fullByte(unsigned N) { return N % 8 == 0; }
+
+/// Track what bits have been initialized to known values and which ones
+/// have indeterminate value.
+/// All offsets are in bits.
----------------
AaronBallman wrote:

There's a small part of me that wonders if we should use a different type for representing bits. e.g., instead of using `size_t` to mean byte offset and bit offset depending on context, I wonder if we want a `BitOffset` datatype (strong enum? constexpr class?) to represent bit offsets and have no implicit conversions to/from `size_t` to help avoid accidentally mixing bit and byte offsets. WDYT?

https://github.com/llvm/llvm-project/pull/116843


More information about the cfe-commits mailing list