Author: Timm Bäder Date: 2024-05-12T07:26:03+02:00 New Revision: 63224d717108d927e998da8a67050a6cc5dd74a2
URL: https://github.com/llvm/llvm-project/commit/63224d717108d927e998da8a67050a6cc5dd74a2 DIFF: https://github.com/llvm/llvm-project/commit/63224d717108d927e998da8a67050a6cc5dd74a2.diff LOG: Reapply "Reapply "[clang][Interp] Create full type info for dummy pointers"" This reverts commit fb1c2dbc0a6ba641e0b39e0692ec6a4ee4444c4b. Added: Modified: clang/lib/AST/Interp/Descriptor.cpp clang/lib/AST/Interp/Descriptor.h clang/lib/AST/Interp/Interp.h clang/lib/AST/Interp/Program.cpp clang/test/AST/Interp/builtin-align-cxx.cpp clang/test/AST/Interp/c.c Removed: ################################################################################ diff --git a/clang/lib/AST/Interp/Descriptor.cpp b/clang/lib/AST/Interp/Descriptor.cpp index 954c58c8cb371..d0466902247b4 100644 --- a/clang/lib/AST/Interp/Descriptor.cpp +++ b/clang/lib/AST/Interp/Descriptor.cpp @@ -347,14 +347,6 @@ Descriptor::Descriptor(const DeclTy &D) assert(Source && "Missing source"); } -/// Dummy array. -Descriptor::Descriptor(const DeclTy &D, UnknownSize) - : Source(D), ElemSize(1), Size(UnknownSizeMark), MDSize(0), - AllocSize(MDSize), ElemRecord(nullptr), IsConst(true), IsMutable(false), - IsTemporary(false), IsArray(true), IsDummy(true) { - assert(Source && "Missing source"); -} - QualType Descriptor::getType() const { if (auto *E = asExpr()) return E->getType(); diff --git a/clang/lib/AST/Interp/Descriptor.h b/clang/lib/AST/Interp/Descriptor.h index cd20495c259c7..fcb14e76e7eb0 100644 --- a/clang/lib/AST/Interp/Descriptor.h +++ b/clang/lib/AST/Interp/Descriptor.h @@ -128,7 +128,7 @@ struct Descriptor final { /// Flag indicating if the block is an array. const bool IsArray = false; /// Flag indicating if this is a dummy descriptor. - const bool IsDummy = false; + bool IsDummy = false; /// Storage management methods. const BlockCtorFn CtorFn = nullptr; @@ -162,8 +162,8 @@ struct Descriptor final { /// Allocates a dummy descriptor. Descriptor(const DeclTy &D); - /// Allocates a dummy array descriptor. - Descriptor(const DeclTy &D, UnknownSize); + /// Make this descriptor a dummy descriptor. + void makeDummy() { IsDummy = true; } QualType getType() const; QualType getElemQualType() const; diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h index 66d30cc3fbaab..a0bf874300120 100644 --- a/clang/lib/AST/Interp/Interp.h +++ b/clang/lib/AST/Interp/Interp.h @@ -823,9 +823,9 @@ inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) { // element in the same array are NOT equal. They have the same Base value, // but a diff erent Offset. This is a pretty rare case, so we fix this here // by comparing pointers to the first elements. - if (!LHS.isZero() && !LHS.isDummy() && LHS.isArrayRoot()) + if (!LHS.isZero() && LHS.isArrayRoot()) VL = LHS.atIndex(0).getByteOffset(); - if (!RHS.isZero() && !RHS.isDummy() && RHS.isArrayRoot()) + if (!RHS.isZero() && RHS.isArrayRoot()) VR = RHS.atIndex(0).getByteOffset(); S.Stk.push<BoolT>(BoolT::from(Fn(Compare(VL, VR)))); @@ -1241,14 +1241,16 @@ inline bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) { !CheckNull(S, OpPC, Ptr, CSK_Field)) return false; - if (CheckDummy(S, OpPC, Ptr)) { - if (!CheckExtern(S, OpPC, Ptr)) - return false; - if (!CheckRange(S, OpPC, Ptr, CSK_Field)) - return false; - if (!CheckSubobject(S, OpPC, Ptr, CSK_Field)) - return false; - } + if (!CheckExtern(S, OpPC, Ptr)) + return false; + if (!CheckRange(S, OpPC, Ptr, CSK_Field)) + return false; + if (!CheckSubobject(S, OpPC, Ptr, CSK_Field)) + return false; + + if (Ptr.isBlockPointer() && Off > Ptr.block()->getSize()) + return false; + S.Stk.push<Pointer>(Ptr.atField(Off)); return true; } @@ -2034,11 +2036,6 @@ inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) { if (!Ptr.isZero()) { if (!CheckArray(S, OpPC, Ptr)) return false; - - if (Ptr.isDummy()) { - S.Stk.push<Pointer>(Ptr); - return true; - } } if (!OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) @@ -2055,11 +2052,6 @@ inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) { if (!Ptr.isZero()) { if (!CheckArray(S, OpPC, Ptr)) return false; - - if (Ptr.isDummy()) { - S.Stk.push<Pointer>(Ptr); - return true; - } } if (!OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) @@ -2113,12 +2105,12 @@ inline bool CopyArray(InterpState &S, CodePtr OpPC, uint32_t SrcIndex, uint32_t inline bool ArrayDecay(InterpState &S, CodePtr OpPC) { const Pointer &Ptr = S.Stk.pop<Pointer>(); - if (Ptr.isZero() || Ptr.isDummy()) { + if (Ptr.isZero()) { S.Stk.push<Pointer>(Ptr); return true; } - if (!Ptr.isUnknownSizeArray()) { + if (!Ptr.isUnknownSizeArray() || Ptr.isDummy()) { S.Stk.push<Pointer>(Ptr.atIndex(0)); return true; } diff --git a/clang/lib/AST/Interp/Program.cpp b/clang/lib/AST/Interp/Program.cpp index 02075c20cf556..87c767f85e799 100644 --- a/clang/lib/AST/Interp/Program.cpp +++ b/clang/lib/AST/Interp/Program.cpp @@ -144,17 +144,20 @@ std::optional<unsigned> Program::getOrCreateDummy(const ValueDecl *VD) { if (auto It = DummyVariables.find(VD); It != DummyVariables.end()) return It->second; - // Create dummy descriptor. - // We create desriptors of 'array of unknown size' if the type is an array - // type _and_ the size isn't known (it's not a ConstantArrayType). If the size - // is known however, we create a regular dummy pointer. Descriptor *Desc; - if (const auto *AT = VD->getType()->getAsArrayTypeUnsafe(); - AT && !isa<ConstantArrayType>(AT)) - Desc = allocateDescriptor(VD, Descriptor::UnknownSize{}); + if (std::optional<PrimType> T = Ctx.classify(VD->getType())) + Desc = createDescriptor(VD, *T, std::nullopt, true, false); else + Desc = createDescriptor(VD, VD->getType().getTypePtr(), std::nullopt, true, + false); + if (!Desc) Desc = allocateDescriptor(VD); + assert(Desc); + Desc->makeDummy(); + + assert(Desc->isDummy()); + // Allocate a block for storage. unsigned I = Globals.size(); @@ -310,8 +313,7 @@ Record *Program::getOrCreateRecord(const RecordDecl *RD) { for (const FieldDecl *FD : RD->fields()) { // Note that we DO create fields and descriptors // for unnamed bitfields here, even though we later ignore - // them everywhere. That's because so the FieldDecl's - // getFieldIndex() matches. + // them everywhere. That's so the FieldDecl's getFieldIndex() matches. // Reserve space for the field's descriptor and the offset. BaseSize += align(sizeof(InlineDescriptor)); @@ -344,6 +346,7 @@ Descriptor *Program::createDescriptor(const DeclTy &D, const Type *Ty, Descriptor::MetadataSize MDSize, bool IsConst, bool IsTemporary, bool IsMutable, const Expr *Init) { + // Classes and structures. if (const auto *RT = Ty->getAs<RecordType>()) { if (const auto *Record = getOrCreateRecord(RT->getDecl())) diff --git a/clang/test/AST/Interp/builtin-align-cxx.cpp b/clang/test/AST/Interp/builtin-align-cxx.cpp index 62d73dba929b2..c4103953df026 100644 --- a/clang/test/AST/Interp/builtin-align-cxx.cpp +++ b/clang/test/AST/Interp/builtin-align-cxx.cpp @@ -2,19 +2,6 @@ // RUN: %clang_cc1 -triple=x86_64-unknown-unknown -std=c++11 %s -fsyntax-only -verify=expected,both -fexperimental-new-constant-interpreter // RUN: %clang_cc1 -triple=x86_64-unknown-unknown -std=c++11 %s -fsyntax-only -verify=ref,both - -/// This is just a copy of the one from test/SemaCXX/ with some of the -/// diagnostic output adapted. -/// Also, align32array has an initializer now, which means it's not just -/// a dummy pointer for us and we do actually have type information for it. -/// In the future, we need to retain type information for dummy pointers as -/// well, so here is a test that will break once we do that: -namespace { - _Alignas(32) char heh[4]; - static_assert(!__builtin_is_aligned(&heh[1], 4), ""); // expected-error {{failed}} -} - - // Check that we don't crash when using dependent types in __builtin_align: template <typename a, a b> void *c(void *d) { // both-note{{candidate template ignored}} @@ -177,7 +164,7 @@ static_assert(wrap_align_up(static_cast<bool>(1), const_value(1 << 21)), ""); // // both-note@-1{{in instantiation of function template specialization 'wrap_align_up<bool>' requested here}} // Check constant evaluation for pointers: -_Alignas(32) char align32array[128] = {}; +_Alignas(32) char align32array[128]; static_assert(&align32array[0] == &align32array[0], ""); // __builtin_align_up/down can be constant evaluated as a no-op for values // that are known to have greater alignment: diff --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c index 207da5fe81260..2c675f4418ef3 100644 --- a/clang/test/AST/Interp/c.c +++ b/clang/test/AST/Interp/c.c @@ -270,3 +270,6 @@ int test3(void) { a[0] = test3; // all-error {{incompatible pointer to integer conversion assigning to 'int' from 'int (void)'}} return 0; } +/// This tests that we have full type info, even for values we cannot read. +int dummyarray[5]; +_Static_assert(&dummyarray[0] < &dummyarray[1], ""); // pedantic-warning {{GNU extension}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits