From c442912d736946703b8a9278cb2e323c51dbefcb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Timm=20B=C3=A4der?= Date: Fri, 30 Jun 2023 08:26:36 +0200 Subject: [PATCH] [clang][Interp][NFC] Add type checks to InterpStack::peek() --- clang/lib/AST/Interp/InterpStack.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/clang/lib/AST/Interp/InterpStack.h b/clang/lib/AST/Interp/InterpStack.h index 435120d..fa2f9d5 100644 --- a/clang/lib/AST/Interp/InterpStack.h +++ b/clang/lib/AST/Interp/InterpStack.h @@ -44,7 +44,7 @@ public: assert(ItemTypes.back() == toPrimType()); ItemTypes.pop_back(); #endif - auto *Ptr = &peek(); + auto *Ptr = &peekInternal(); auto Value = std::move(*Ptr); Ptr->~T(); shrink(aligned_size()); @@ -57,14 +57,18 @@ public: assert(ItemTypes.back() == toPrimType()); ItemTypes.pop_back(); #endif - auto *Ptr = &peek(); + auto *Ptr = &peekInternal(); Ptr->~T(); shrink(aligned_size()); } /// Returns a reference to the value on the top of the stack. template T &peek() const { - return *reinterpret_cast(peekData(aligned_size())); +#ifndef NDEBUG + assert(!ItemTypes.empty()); + assert(ItemTypes.back() == toPrimType()); +#endif + return peekInternal(); } template T &peek(size_t Offset) const { @@ -92,6 +96,11 @@ private: return ((sizeof(T) + PtrAlign - 1) / PtrAlign) * PtrAlign; } + /// Like the public peek(), but without the debug type checks. + template T &peekInternal() const { + return *reinterpret_cast(peekData(aligned_size())); + } + /// Grows the stack to accommodate a value and returns a pointer to it. void *grow(size_t Size); /// Returns a pointer from the top of the stack. -- 2.7.4