diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 89ff5d27a4143..3f30e524ab179 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -4191,6 +4191,31 @@ template bool Compiler::delegate(const Expr *E) { return this->Visit(E); } +static const Expr *stripCheckedDerivedToBaseCasts(const Expr *E) { + if (const auto *PE = dyn_cast(E)) + return stripCheckedDerivedToBaseCasts(PE->getSubExpr()); + + if (const auto *CE = dyn_cast(E); + CE && + (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp)) + return stripCheckedDerivedToBaseCasts(CE->getSubExpr()); + + return E; +} + +static const Expr *stripDerivedToBaseCasts(const Expr *E) { + if (const auto *PE = dyn_cast(E)) + return stripDerivedToBaseCasts(PE->getSubExpr()); + + if (const auto *CE = dyn_cast(E); + CE && (CE->getCastKind() == CK_DerivedToBase || + CE->getCastKind() == CK_UncheckedDerivedToBase || + CE->getCastKind() == CK_NoOp)) + return stripDerivedToBaseCasts(CE->getSubExpr()); + + return E; +} + template bool Compiler::visit(const Expr *E) { if (E->getType().isNull()) return false; @@ -4200,7 +4225,7 @@ template bool Compiler::visit(const Expr *E) { // Create local variable to hold the return value. if (!E->isGLValue() && !canClassify(E->getType())) { - UnsignedOrNone LocalIndex = allocateLocal(E); + UnsignedOrNone LocalIndex = allocateLocal(stripDerivedToBaseCasts(E)); if (!LocalIndex) return false; @@ -5078,18 +5103,6 @@ bool Compiler::VisitBuiltinCallExpr(const CallExpr *E, return true; } -static const Expr *stripDerivedToBaseCasts(const Expr *E) { - if (const auto *PE = dyn_cast(E)) - return stripDerivedToBaseCasts(PE->getSubExpr()); - - if (const auto *CE = dyn_cast(E); - CE && - (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp)) - return stripDerivedToBaseCasts(CE->getSubExpr()); - - return E; -} - template bool Compiler::VisitCallExpr(const CallExpr *E) { const FunctionDecl *FuncDecl = E->getDirectCallee(); @@ -5194,7 +5207,7 @@ bool Compiler::VisitCallExpr(const CallExpr *E) { const auto *InstancePtr = MC->getImplicitObjectArgument(); if (isa_and_nonnull(CompilingFunction) || isa_and_nonnull(CompilingFunction)) { - const auto *Stripped = stripDerivedToBaseCasts(InstancePtr); + const auto *Stripped = stripCheckedDerivedToBaseCasts(InstancePtr); if (isa(Stripped)) { FuncDecl = cast(FuncDecl)->getCorrespondingMethodInClass( diff --git a/clang/test/AST/ByteCode/cxx03.cpp b/clang/test/AST/ByteCode/cxx03.cpp index 70ae4134842b5..10e5232b9f873 100644 --- a/clang/test/AST/ByteCode/cxx03.cpp +++ b/clang/test/AST/ByteCode/cxx03.cpp @@ -29,3 +29,14 @@ void LambdaAccessingADummy() { int d; int a9[1] = {[d = 0] = 1}; // both-error {{is not an integral constant expression}} } + +const int p = 10; +struct B { + int a; + void *p; +}; +struct B2 : B { + void *q; +}; +_Static_assert(&(B2().a) == &p, ""); // both-error {{taking the address of a temporary object of type 'int'}} \ + // both-error {{not an integral constant expression}}