================
@@ -3375,6 +3376,170 @@ void CastOperation::CheckCStyleCast() {
checkCastAlign();
}
+namespace {
+class BitCastBitClassifier {
+ const ASTContext &Ctx;
+
+ static void setRange(llvm::BitVector &BV, uint64_t Start, uint64_t Len) {
+ if (Len != 0)
+ BV.set(Start, Start + Len);
+ }
+
+public:
+ /// Bits in the value representation.
+ llvm::BitVector Value;
+ /// Bits enclosed by a union.
+ llvm::BitVector Union;
+ /// Bits enclosed by ``std::byte`` or unsigned ordinary character type.
+ llvm::BitVector ByteLike;
+
+ explicit BitCastBitClassifier(const ASTContext &Ctx) : Ctx(Ctx) {}
+
+ /// Classify the bits of \p T. Returns ``false`` if the type contains
+ /// constructs that this analysis does not model precisely (e.g.
bit-fields),
+ /// in which case the result is unusable.
+ [[nodiscard]] bool classify(QualType T) {
+ // TODO: Implement classification on big endian.
+ if (Ctx.getTargetInfo().isBigEndian())
+ return false;
+
+ uint64_t Bits = Ctx.getTypeSize(T);
+ Value.resize(Bits, false);
+ Union.resize(Bits, false);
+ ByteLike.resize(Bits, false);
+ return visit(T, 0);
+ }
+
+private:
+ [[nodiscard]] bool visit(QualType T, uint64_t StartBit) {
+ if (T.isNull() || T->isDependentType() || T->isFunctionType() ||
+ T->isReferenceType())
+ return false;
+
+ T = T.getCanonicalType().getUnqualifiedType();
+ uint64_t Bits = Ctx.getTypeSize(T);
+
+ // [bit.cast] permits casting padding to ``std::byte`` and to an unsigned
+ // ordinary character type.
+ {
+ QualType U = T;
+ bool IsByteLike = U->isStdByteType();
+ if (const auto *BT = U->getAs<BuiltinType>())
+ IsByteLike = BT->getKind() == BuiltinType::UChar ||
+ BT->getKind() == BuiltinType::Char_U;
----------------
cor3ntin wrote:
There is similar logic in emitBuiltinBitCast - it might be useful to extract it
in a function
https://github.com/llvm/llvm-project/pull/200362
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits