diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 7cf2519d6a71f..eb619e65a7bfc 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -1642,22 +1642,8 @@ bool Compiler::VisitImplicitValueInitExpr( if (QT->isIncompleteArrayType()) return true; - if (QT->isArrayType()) { - const ArrayType *AT = QT->getAsArrayTypeUnsafe(); - assert(AT); - const auto *CAT = cast(AT); - size_t NumElems = CAT->getZExtSize(); - PrimType ElemT = classifyPrim(CAT->getElementType()); - - for (size_t I = 0; I != NumElems; ++I) { - if (!this->visitZeroInitializer(ElemT, CAT->getElementType(), E)) - return false; - if (!this->emitInitElem(ElemT, I, E)) - return false; - } - - return true; - } + if (QT->isArrayType()) + return this->visitZeroArrayInitializer(QT, E); if (const auto *ComplexTy = E->getType()->getAs()) { assert(Initializing); @@ -3916,18 +3902,9 @@ bool Compiler::visitZeroRecordInitializer(const Record *R, return false; } } else if (D->isCompositeArray()) { - const Record *ElemRecord = D->ElemDesc->ElemRecord; - assert(D->ElemDesc->ElemRecord); - for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) { - if (!this->emitConstUint32(I, E)) - return false; - if (!this->emitArrayElemPtr(PT_Uint32, E)) - return false; - if (!this->visitZeroRecordInitializer(ElemRecord, E)) - return false; - if (!this->emitPopPtr(E)) - return false; - } + // Can't be a vector or complex field. + if (!this->visitZeroArrayInitializer(D->getType(), E)) + return false; } else if (D->isRecord()) { if (!this->visitZeroRecordInitializer(D->ElemRecord, E)) return false; @@ -3958,6 +3935,52 @@ bool Compiler::visitZeroRecordInitializer(const Record *R, return true; } +template +bool Compiler::visitZeroArrayInitializer(QualType T, const Expr *E) { + assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType()); + const ArrayType *AT = T->getAsArrayTypeUnsafe(); + QualType ElemType = AT->getElementType(); + size_t NumElems = cast(AT)->getZExtSize(); + + if (std::optional ElemT = classify(ElemType)) { + for (size_t I = 0; I != NumElems; ++I) { + if (!this->visitZeroInitializer(*ElemT, ElemType, E)) + return false; + if (!this->emitInitElem(*ElemT, I, E)) + return false; + } + return true; + } else if (ElemType->isRecordType()) { + const Record *R = getRecord(ElemType); + + for (size_t I = 0; I != NumElems; ++I) { + if (!this->emitConstUint32(I, E)) + return false; + if (!this->emitArrayElemPtr(PT_Uint32, E)) + return false; + if (!this->visitZeroRecordInitializer(R, E)) + return false; + if (!this->emitPopPtr(E)) + return false; + } + return true; + } else if (ElemType->isArrayType()) { + for (size_t I = 0; I != NumElems; ++I) { + if (!this->emitConstUint32(I, E)) + return false; + if (!this->emitArrayElemPtr(PT_Uint32, E)) + return false; + if (!this->visitZeroArrayInitializer(ElemType, E)) + return false; + if (!this->emitPopPtr(E)) + return false; + } + return true; + } + + return false; +} + template template bool Compiler::emitConst(T Value, PrimType Ty, const Expr *E) { diff --git a/clang/lib/AST/ByteCode/Compiler.h b/clang/lib/AST/ByteCode/Compiler.h index d1b624daba6b9..2a94f5ec76b6c 100644 --- a/clang/lib/AST/ByteCode/Compiler.h +++ b/clang/lib/AST/ByteCode/Compiler.h @@ -325,6 +325,7 @@ class Compiler : public ConstStmtVisitor, bool>, /// Emits a zero initializer. bool visitZeroInitializer(PrimType T, QualType QT, const Expr *E); bool visitZeroRecordInitializer(const Record *R, const Expr *E); + bool visitZeroArrayInitializer(QualType T, const Expr *E); /// Emits an APSInt constant. bool emitConst(const llvm::APSInt &Value, PrimType Ty, const Expr *E); diff --git a/clang/lib/AST/ByteCode/InterpFrame.cpp b/clang/lib/AST/ByteCode/InterpFrame.cpp index 7f02464a1c0f1..20f67d9b1fd42 100644 --- a/clang/lib/AST/ByteCode/InterpFrame.cpp +++ b/clang/lib/AST/ByteCode/InterpFrame.cpp @@ -234,7 +234,12 @@ SourceInfo InterpFrame::getSource(CodePtr PC) const { if (Func && !funcHasUsableBody(Func) && Caller) return Caller->getSource(RetPC); - return S.getSource(Func, PC); + // Similarly, if the resulting source location is invalid anyway, + // point to the caller instead. + SourceInfo Result = S.getSource(Func, PC); + if (Result.getLoc().isInvalid() && Caller) + return Caller->getSource(RetPC); + return Result; } const Expr *InterpFrame::getExpr(CodePtr PC) const { diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp index 56f54ff168f3e..7a4fc89a27dac 100644 --- a/clang/test/AST/ByteCode/placement-new.cpp +++ b/clang/test/AST/ByteCode/placement-new.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -fexperimental-new-constant-interpreter -verify=expected,both %s +// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -fexperimental-new-constant-interpreter -verify=expected,both %s -DBYTECODE // RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -verify=ref,both %s namespace std { @@ -338,3 +338,17 @@ namespace PR48606 { } static_assert(f()); } + +#ifdef BYTECODE +constexpr int N = [] // expected-error {{must be initialized by a constant expression}} \ + // expected-note {{assignment to dereferenced one-past-the-end pointer is not allowed in a constant expression}} \ + // expected-note {{in call to}} +{ + struct S { + int a[1]; + }; + S s; + ::new (s.a) int[1][2][3][4](); + return s.a[0]; +}(); +#endif