https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/147480
None >From e064acdb14edeb4e70463ccdb1726317e81a1a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com> Date: Tue, 8 Jul 2025 09:32:37 +0200 Subject: [PATCH] [clang][bytecode] Fix __builtin_is_within_lifetime in initializers --- clang/lib/AST/ByteCode/Compiler.cpp | 2 +- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 14 +++++++++++++- clang/test/AST/ByteCode/builtin-functions.cpp | 12 ++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index d1c93e4694667..1fb826b2f8dce 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -5008,7 +5008,7 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) { if (!this->emitCheckPseudoDtor(E)) return false; const Expr *Base = PD->getBase(); - if (!Base->isGLValue()) + if (classify(Base) != PT_Ptr) return this->discard(Base); if (!this->visit(Base)) return false; diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index db18ad2aa9bcb..1fe6d146b511d 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -2208,7 +2208,7 @@ static bool interp__builtin_is_within_lifetime(InterpState &S, CodePtr OpPC, if (Ptr.isOnePastEnd()) return Error(1); - bool Result = true; + bool Result = Ptr.getLifetime() != Lifetime::Ended; if (!Ptr.isActive()) { Result = false; } else { @@ -2216,7 +2216,19 @@ static bool interp__builtin_is_within_lifetime(InterpState &S, CodePtr OpPC, return false; if (!CheckMutable(S, OpPC, Ptr)) return false; + if (!CheckDummy(S, OpPC, Ptr, AK_Read)) + return false; + } + + // Check if we're currently running an initializer. + for (InterpFrame *Frame = S.Current; Frame; Frame = Frame->Caller) { + if (const Function *F = Frame->getFunction(); + F && F->isConstructor() && Frame->getThis().block() == Ptr.block()) { + return Error(2); + } } + if (S.EvaluatingDecl && Ptr.getDeclDesc()->asVarDecl() == S.EvaluatingDecl) + return Error(2); pushInteger(S, Result, Call->getType()); return true; diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp index 1bb6d22db9823..301c6213e05be 100644 --- a/clang/test/AST/ByteCode/builtin-functions.cpp +++ b/clang/test/AST/ByteCode/builtin-functions.cpp @@ -1738,6 +1738,18 @@ namespace WithinLifetime { // both-note {{'__builtin_is_within_lifetime' cannot be called with a one-past-the-end pointer}} \ // both-warning {{expression result unused}} } + + + constexpr bool self = __builtin_is_within_lifetime(&self); // both-error {{must be initialized by a constant expression}} \ + // both-note {{'__builtin_is_within_lifetime' cannot be called with a pointer to an object whose lifetime has not yet begun}} \ + // ref-error {{call to consteval function '__builtin_is_within_lifetime' is not a constant expression}} \ + // ref-note {{initializer of 'self' is not a constant expression}} \ + // ref-note {{declared here}} + + int nontCE(int p) { // both-note {{declared here}} + return __builtin_is_within_lifetime(&p); // both-error {{call to consteval function}} \ + // both-note {{function parameter 'p' with unknown value cannot be used in a constant expression}} + } } #ifdef __SIZEOF_INT128__ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits