Author: Timm Baeder Date: 2025-09-26T16:59:16+02:00 New Revision: 23473247c2aad142b8259ed70e569e932357c962
URL: https://github.com/llvm/llvm-project/commit/23473247c2aad142b8259ed70e569e932357c962 DIFF: https://github.com/llvm/llvm-project/commit/23473247c2aad142b8259ed70e569e932357c962.diff LOG: [clang][bytecode][NFC] Use switches for pointer type distinction (#160879) In the important places. They are all fully covered switch statements so we know where to add code when adding a new pointer type. Added: Modified: clang/lib/AST/ByteCode/Pointer.cpp clang/lib/AST/ByteCode/Pointer.h Removed: ################################################################################ diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp index 81d4ce14f9310..663134c8696de 100644 --- a/clang/lib/AST/ByteCode/Pointer.cpp +++ b/clang/lib/AST/ByteCode/Pointer.cpp @@ -110,19 +110,21 @@ Pointer &Pointer::operator=(const Pointer &P) { StorageKind = P.StorageKind; Offset = P.Offset; - if (P.isBlockPointer()) { + switch (StorageKind) { + case Storage::Int: + Int = P.Int; + break; + case Storage::Block: BS = P.BS; if (BS.Pointee) BS.Pointee->addPointer(this); - } else if (P.isIntegralPointer()) { - Int = P.Int; - } else if (P.isFunctionPointer()) { + break; + case Storage::Fn: Fn = P.Fn; - } else if (P.isTypeidPointer()) { + break; + case Storage::Typeid: Typeid = P.Typeid; - } else { - assert(false && "Unhandled storage kind"); } return *this; } @@ -147,19 +149,21 @@ Pointer &Pointer::operator=(Pointer &&P) { StorageKind = P.StorageKind; Offset = P.Offset; - if (P.isBlockPointer()) { + switch (StorageKind) { + case Storage::Int: + Int = P.Int; + break; + case Storage::Block: BS = P.BS; if (BS.Pointee) BS.Pointee->addPointer(this); - } else if (P.isIntegralPointer()) { - Int = P.Int; - } else if (P.isFunctionPointer()) { + break; + case Storage::Fn: Fn = P.Fn; - } else if (P.isTypeidPointer()) { + break; + case Storage::Typeid: Typeid = P.Typeid; - } else { - assert(false && "Unhandled storage kind"); } return *this; } @@ -358,13 +362,17 @@ void Pointer::print(llvm::raw_ostream &OS) const { } size_t Pointer::computeOffsetForComparison() const { - if (isIntegralPointer()) - return asIntPointer().Value + Offset; - if (isTypeidPointer()) + switch (StorageKind) { + case Storage::Int: + return Int.Value + Offset; + case Storage::Block: + // See below. + break; + case Storage::Fn: + return Fn.getIntegerRepresentation() + Offset; + case Storage::Typeid: return reinterpret_cast<uintptr_t>(asTypeidPointer().TypePtr) + Offset; - - if (!isBlockPointer()) - return Offset; + } size_t Result = 0; Pointer P = *this; diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h index bbf20801ce923..af89b66e9f875 100644 --- a/clang/lib/AST/ByteCode/Pointer.h +++ b/clang/lib/AST/ByteCode/Pointer.h @@ -56,7 +56,7 @@ struct TypeidPointer { const Type *TypeInfoType; }; -enum class Storage { Block, Int, Fn, Typeid }; +enum class Storage { Int, Block, Fn, Typeid }; /// A pointer to a memory block, live or dead. /// @@ -252,14 +252,16 @@ class Pointer { /// Checks if the pointer is null. bool isZero() const { - if (isBlockPointer()) + switch (StorageKind) { + case Storage::Int: + return Int.Value == 0 && Offset == 0; + case Storage::Block: return BS.Pointee == nullptr; - if (isFunctionPointer()) + case Storage::Fn: return Fn.isZero(); - if (isTypeidPointer()) + case Storage::Typeid: return false; - assert(isIntegralPointer()); - return Int.Value == 0 && Offset == 0; + } } /// Checks if the pointer is live. bool isLive() const { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
