From dfc62f22da53604f0ffd100da7a75ea2dd5855d1 Mon Sep 17 00:00:00 2001 From: wuhuiquan Date: Tue, 1 Jul 2025 10:46:13 +0800 Subject: [PATCH] [CodeSync] Synchronization of code differences related to ownedStruct --- clang/include/clang/Sema/Sema.h | 1 + clang/lib/Sema/BSC/SemaBSCDestructor.cpp | 16 ++++++++++++++++ clang/lib/Sema/SemaDecl.cpp | 10 +++++----- clang/lib/Sema/SemaExpr.cpp | 2 +- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 4f06eec6d8a0..c44992b77c90 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3226,6 +3226,7 @@ public: bool IsBSCCompatibleFutureType(QualType Ty); // BSC Destructor related. + bool IsCallDestructorExpr(Expr *E); BSCMethodDecl *getOrInsertBSCDestructor(RecordDecl *RD); void HandleBSCDestructorBody(RecordDecl *RD, BSCMethodDecl *Destructor, std::stack InstanceFields); diff --git a/clang/lib/Sema/BSC/SemaBSCDestructor.cpp b/clang/lib/Sema/BSC/SemaBSCDestructor.cpp index 7820409fd9cb..58fe0d4776dc 100644 --- a/clang/lib/Sema/BSC/SemaBSCDestructor.cpp +++ b/clang/lib/Sema/BSC/SemaBSCDestructor.cpp @@ -319,6 +319,7 @@ public: VarDecl::Create(SemaRef.Context, FD, D->getLocation(), D->getLocation(), &(SemaRef.Context.Idents).get(IName), SemaRef.Context.BoolTy, nullptr, SC_None); + FD->addDecl(VD); llvm::APInt Zero(SemaRef.Context.getTypeSize(SemaRef.Context.IntTy), 0); Expr *IInit = IntegerLiteral::Create( SemaRef.Context, Zero, SemaRef.Context.IntTy, SourceLocation()); @@ -740,6 +741,21 @@ public: } }; +bool Sema::IsCallDestructorExpr(Expr *E) { + Expr *NakedE = E->IgnoreParens(); + if (auto *CastExpr = llvm::dyn_cast(NakedE)) { + if (Expr *SubExpr = CastExpr->getSubExpr()) { + if (auto *DRE = llvm::dyn_cast(SubExpr)) { + NamedDecl *NDecl = DRE->getDecl(); + if (auto *MD = dyn_cast(NDecl)) { + return MD->isDestructor(); + } + } + } + } + return false; +} + void Sema::DesugarDestructorCall(FunctionDecl *FD) { if (!getLangOpts().BSC) return; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 89f05e90ced2..d75ba38da9be 100755 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -15983,11 +15983,6 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, // the declaration context below. Otherwise, we're unable to transform // 'this' expressions when transforming immediate context functions. - if (!IsInstantiation) - PopDeclContext(); - - PopFunctionScopeInfo(ActivePolicy, dcl); - #if ENABLE_BSC if (LangOpts.BSC) { if (auto FD = dyn_cast_or_null(dcl)) { @@ -16019,6 +16014,11 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, } #endif + if (!IsInstantiation) + PopDeclContext(); + + PopFunctionScopeInfo(ActivePolicy, dcl); + // If any errors have occurred, clear out any temporaries that may have // been leftover. This ensures that these temporaries won't be picked up for // deletion in some later function. diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 10c488b24d5b..392b8d8b049b 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -7492,7 +7492,7 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, TheCall->getCallee()->HasBSCScopeSpec = Fn->HasBSCScopeSpec; if (getLangOpts().BSC) { // unsafe function call is forbidden in the safe zone - if ((IsInSafeZone()) && + if (!IsCallDestructorExpr(Fn) && IsInSafeZone() && (Fn->getType()->checkFunctionProtoType(SZ_None) || Fn->getType()->checkFunctionProtoType(SZ_Unsafe))) { Diag(Fn->getBeginLoc(), diag::err_unsafe_action) -- Gitee