github-actions[bot] wrote: <!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning: <details> <summary> You can test this locally with the following command: </summary> ``````````bash git-clang-format --diff 2deb3a26fa47a4640962489e5473726d7a8bf12b 2da55a9d9db62e80cddd83f86e26d31537e571b5 --extensions h,cpp -- clang/test/SemaCXX/type-aware-new-constexpr.cpp clang/test/SemaCXX/type-aware-new-delete-arrays.cpp clang/test/SemaCXX/type-aware-new-delete-basic-free-declarations.cpp clang/test/SemaCXX/type-aware-new-delete-basic-in-class-declarations.cpp clang/test/SemaCXX/type-aware-new-delete-basic-resolution.cpp clang/include/clang/AST/Decl.h clang/include/clang/AST/ExprCXX.h clang/include/clang/AST/Stmt.h clang/include/clang/AST/Type.h clang/include/clang/Sema/Sema.h clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h clang/lib/AST/ASTImporter.cpp clang/lib/AST/Decl.cpp clang/lib/AST/DeclCXX.cpp clang/lib/AST/ExprCXX.cpp clang/lib/AST/Type.cpp clang/lib/CodeGen/CGExprCXX.cpp clang/lib/Driver/ToolChains/Clang.cpp clang/lib/Sema/SemaCoroutine.cpp clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaDeclCXX.cpp clang/lib/Sema/SemaExprCXX.cpp clang/lib/Sema/SemaTemplate.cpp clang/lib/Serialization/ASTReaderStmt.cpp clang/lib/Serialization/ASTWriterStmt.cpp clang/test/CodeGenCXX/new.cpp clang/test/CodeGenCoroutines/coro-alloc-2.cpp clang/test/CodeGenCoroutines/coro-alloc.cpp clang/test/Modules/new-delete.cpp clang/test/SemaCXX/coroutine-alloc-4.cpp clang/test/SemaCXX/coroutine-allocs.cpp clang/test/SemaCXX/delete.cpp `````````` </details> <details> <summary> View the diff from clang-format here. </summary> ``````````diff diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index 0c2ada9a3d..4a269ecc92 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -2304,8 +2304,8 @@ class CXXNewExpr final bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens, std::optional<Expr *> ArraySize, CXXNewInitializationStyle InitializationStyle, Expr *Initializer, - QualType Ty, TypeSourceInfo *AllocatedTypeInfo, - SourceRange Range, SourceRange DirectInitRange); + QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range, + SourceRange DirectInitRange); /// Build an empty c++ new expression. CXXNewExpr(EmptyShell Empty, bool IsArray, unsigned NumPlacementArgs, @@ -2451,7 +2451,9 @@ public: /// Indicates whether a type_identity tag should be implicitly passed to /// the allocation function. - bool passTypeIdentity() const { return CXXNewExprBits.ShouldPassTypeIdentity; } + bool passTypeIdentity() const { + return CXXNewExprBits.ShouldPassTypeIdentity; + } /// Answers whether the usual array deallocation function for the /// allocated type expects the size of the allocation as a @@ -2463,10 +2465,8 @@ public: /// Provides the full set of information about expected implicit /// parameters in this call ImplicitAllocationParameters implicitAllocationParameters() const { - return ImplicitAllocationParameters { - .PassTypeIdentity = passTypeIdentity(), - .PassAlignment = passAlignment() - }; + return ImplicitAllocationParameters{.PassTypeIdentity = passTypeIdentity(), + .PassAlignment = passAlignment()}; } using arg_iterator = ExprIterator; diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index 7d8bd7b70a..ad7dfe44ce 100644 --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -2635,8 +2635,8 @@ public: // C++14 decltype(auto) bool isTypedefNameType() const; // typedef or alias template - bool isTypeIdentitySpecialization() const; // std::type_identity<X> for any X - bool isDestroyingDeleteT() const; // std::destroying_delete_t + bool isTypeIdentitySpecialization() const; // std::type_identity<X> for any X + bool isDestroyingDeleteT() const; // std::destroying_delete_t #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ bool is##Id##Type() const; diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 501fc0ecb2..bd54d81255 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -4759,7 +4759,9 @@ public: bool IsTypeAwareOperatorNewOrDelete(const FunctionDecl *FnDecl) const; bool IsTypeAwareOperatorNewOrDelete(const FunctionTemplateDecl *FnDecl) const; bool IsTypeAwareOperatorNewOrDelete(const NamedDecl *FnDecl) const; - std::optional<FunctionDecl*> InstantiateTypeAwareUsualDelete(FunctionTemplateDecl *FnDecl, QualType AllocType); + std::optional<FunctionDecl *> + InstantiateTypeAwareUsualDelete(FunctionTemplateDecl *FnDecl, + QualType AllocType); ValueDecl *tryLookupUnambiguousFieldDecl(RecordDecl *ClassDecl, const IdentifierInfo *MemberOrBase); @@ -8153,14 +8155,12 @@ public: /// Finds the overloads of operator new and delete that are appropriate /// for the allocation. - bool FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, - AllocationFunctionScope NewScope, - AllocationFunctionScope DeleteScope, - QualType AllocType, bool IsArray, - ImplicitAllocationParameters &IAP, MultiExprArg PlaceArgs, - FunctionDecl *&OperatorNew, - FunctionDecl *&OperatorDelete, - bool Diagnose = true); + bool FindAllocationFunctions( + SourceLocation StartLoc, SourceRange Range, + AllocationFunctionScope NewScope, AllocationFunctionScope DeleteScope, + QualType AllocType, bool IsArray, ImplicitAllocationParameters &IAP, + MultiExprArg PlaceArgs, FunctionDecl *&OperatorNew, + FunctionDecl *&OperatorDelete, bool Diagnose = true); /// DeclareGlobalNewDelete - Declare the global forms of operator new and /// delete. These are: @@ -8190,10 +8190,11 @@ public: ArrayRef<QualType> Params); bool FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, - DeclarationName Name, FunctionDecl *&Operator, QualType DeallocType, - ImplicitDeallocationParameters, bool Diagnose = true); - FunctionDecl *FindUsualDeallocationFunction(QualType, - SourceLocation StartLoc, + DeclarationName Name, FunctionDecl *&Operator, + QualType DeallocType, + ImplicitDeallocationParameters, + bool Diagnose = true); + FunctionDecl *FindUsualDeallocationFunction(QualType, SourceLocation StartLoc, ImplicitDeallocationParameters, DeclarationName Name); FunctionDecl *FindDeallocationFunctionForDestructor(SourceLocation StartLoc, diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index 84a1018c58..5bddd0741b 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -8313,10 +8313,10 @@ ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) { return CXXNewExpr::Create( Importer.getToContext(), E->isGlobalNew(), ToOperatorNew, - ToOperatorDelete, E->implicitAllocationParameters(), E->doesUsualArrayDeleteWantSize(), - ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(), - ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange, - ToDirectInitRange); + ToOperatorDelete, E->implicitAllocationParameters(), + E->doesUsualArrayDeleteWantSize(), ToPlacementArgs, ToTypeIdParens, + ToArraySize, E->getInitializationStyle(), ToInitializer, ToType, + ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange); } ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index d594783ba2..08618f6b38 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3360,7 +3360,8 @@ bool FunctionDecl::isReservedGlobalPlacementOperator() const { const auto *proto = getType()->castAs<FunctionProtoType>(); if (proto->getNumParams() < 2) return false; - bool IsTypeAwareAllocator = proto->getParamType(0)->isTypeIdentitySpecialization(); + bool IsTypeAwareAllocator = + proto->getParamType(0)->isTypeIdentitySpecialization(); if (IsTypeAwareAllocator) return false; if (proto->getNumParams() != 2 || proto->isVariadic()) @@ -3503,7 +3504,8 @@ bool FunctionDecl::isDestroyingOperatorDelete() const { if (getNumParams() <= DestroyingDeleteTagParam) return false; - auto *RD = getParamDecl(DestroyingDeleteTagParam)->getType()->getAsCXXRecordDecl(); + auto *RD = + getParamDecl(DestroyingDeleteTagParam)->getType()->getAsCXXRecordDecl(); return RD && RD->isInStdNamespace() && RD->getIdentifier() && RD->getIdentifier()->isStr("destroying_delete_t"); } diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp index aa14241acd..464f59928d 100644 --- a/clang/lib/AST/DeclCXX.cpp +++ b/clang/lib/AST/DeclCXX.cpp @@ -2476,7 +2476,8 @@ bool CXXMethodDecl::isUsualDeallocationFunction( if (auto *PrimaryTemplate = getPrimaryTemplate()) { // Addendum: a template instance is a usual deallocation function if there // is a single template parameter, that parameter is a type, only the first - // parameter is dependent, and that parameter is a specialization of std::type_identity + // parameter is dependent, and that parameter is a specialization of + // std::type_identity if (!IsTypeAware) { // Stop early on if the specialization is not explicitly type aware return false; diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index d10b28e494..22307912ec 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -227,7 +227,8 @@ SourceLocation CXXScalarValueInitExpr::getBeginLoc() const { // CXXNewExpr CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew, - FunctionDecl *OperatorDelete, ImplicitAllocationParameters IAP, + FunctionDecl *OperatorDelete, + ImplicitAllocationParameters IAP, bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens, std::optional<Expr *> ArraySize, @@ -292,7 +293,8 @@ CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray, CXXNewExpr *CXXNewExpr::Create( const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew, - FunctionDecl *OperatorDelete, ImplicitAllocationParameters IAP, bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs, + FunctionDecl *OperatorDelete, ImplicitAllocationParameters IAP, + bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens, std::optional<Expr *> ArraySize, CXXNewInitializationStyle InitializationStyle, Expr *Initializer, QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range, @@ -305,10 +307,10 @@ CXXNewExpr *CXXNewExpr::Create( Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>( IsArray + HasInit + NumPlacementArgs, IsParenTypeId), alignof(CXXNewExpr)); - return new (Mem) - CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, IAP, UsualArrayDeleteWantsSize, - PlacementArgs, TypeIdParens, ArraySize, InitializationStyle, Initializer, - Ty, AllocatedTypeInfo, Range, DirectInitRange); + return new (Mem) CXXNewExpr( + IsGlobalNew, OperatorNew, OperatorDelete, IAP, UsualArrayDeleteWantsSize, + PlacementArgs, TypeIdParens, ArraySize, InitializationStyle, Initializer, + Ty, AllocatedTypeInfo, Range, DirectInitRange); } CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray, diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp index 238272b79f..fa4695e0e4 100644 --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -1549,7 +1549,8 @@ static void EnterNewDeleteCleanup(CodeGenFunction &CGF, DirectCleanup *Cleanup = CGF.EHStack.pushCleanupWithExtra<DirectCleanup>( EHCleanup, E->getNumPlacementArgs(), E->getOperatorDelete(), - NewPtr.emitRawPointer(CGF), AllocSize, E->passTypeIdentity(), E->passAlignment(), AllocAlign); + NewPtr.emitRawPointer(CGF), AllocSize, E->passTypeIdentity(), + E->passAlignment(), AllocAlign); for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) { auto &Arg = NewArgs[I + NumNonPlacementArgs]; Cleanup->setPlacementArg(I, Arg.getRValue(CGF), Arg.Ty); @@ -1573,15 +1574,11 @@ static void EnterNewDeleteCleanup(CodeGenFunction &CGF, }; typedef CallDeleteDuringNew<ConditionalCleanupTraits> ConditionalCleanup; - ConditionalCleanup *Cleanup = CGF.EHStack - .pushCleanupWithExtra<ConditionalCleanup>(EHCleanup, - E->getNumPlacementArgs(), - E->getOperatorDelete(), - SavedNewPtr, - SavedAllocSize, - E->passTypeIdentity(), - E->passAlignment(), - AllocAlign); + ConditionalCleanup *Cleanup = + CGF.EHStack.pushCleanupWithExtra<ConditionalCleanup>( + EHCleanup, E->getNumPlacementArgs(), E->getOperatorDelete(), + SavedNewPtr, SavedAllocSize, E->passTypeIdentity(), + E->passAlignment(), AllocAlign); for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) { auto &Arg = NewArgs[I + NumNonPlacementArgs]; Cleanup->setPlacementArg( @@ -1655,8 +1652,10 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { unsigned ParamsToSkip = 0; if (E->passTypeIdentity()) { QualType SpecializedTypeIdentity = allocatorType->getParamType(0); - CXXScalarValueInitExpr TypeIdentityParam(SpecializedTypeIdentity, nullptr, SourceLocation()); - allocatorArgs.add(EmitAnyExprToTemp(&TypeIdentityParam), SpecializedTypeIdentity); + CXXScalarValueInitExpr TypeIdentityParam(SpecializedTypeIdentity, nullptr, + SourceLocation()); + allocatorArgs.add(EmitAnyExprToTemp(&TypeIdentityParam), + SpecializedTypeIdentity); ++ParamsToSkip; ++IndexOfAlignArg; } @@ -1833,8 +1832,10 @@ void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::AllocaInst *TypeIdentityag = nullptr; if (Params.TypedAwareDelete) { QualType SpecializedTypeIdentity = *ParamTypeIt++; - CXXScalarValueInitExpr TypeIdentityParam(SpecializedTypeIdentity, nullptr, SourceLocation()); - DeleteArgs.add(EmitAnyExprToTemp(&TypeIdentityParam), SpecializedTypeIdentity); + CXXScalarValueInitExpr TypeIdentityParam(SpecializedTypeIdentity, nullptr, + SourceLocation()); + DeleteArgs.add(EmitAnyExprToTemp(&TypeIdentityParam), + SpecializedTypeIdentity); } // Pass the pointer itself. diff --git a/clang/lib/Sema/SemaCoroutine.cpp b/clang/lib/Sema/SemaCoroutine.cpp index 84d642436b..f8dad348b7 100644 --- a/clang/lib/Sema/SemaCoroutine.cpp +++ b/clang/lib/Sema/SemaCoroutine.cpp @@ -1112,13 +1112,12 @@ static bool findDeleteForPromise(Sema &S, SourceLocation Loc, QualType PromiseTy // The deallocation function's name is looked up by searching for it in the // scope of the promise type. If nothing is found, a search is performed in // the global scope. - ImplicitDeallocationParameters IDP = { - .PassTypeIdentity = S.AllowTypeAwareAllocators(), - .PassAlignment = Overaligned, - .PassSize = true - }; - if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete, PromiseType, - IDP, /*Diagnose*/ true)) + ImplicitDeallocationParameters IDP = {.PassTypeIdentity = + S.AllowTypeAwareAllocators(), + .PassAlignment = Overaligned, + .PassSize = true}; + if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete, + PromiseType, IDP, /*Diagnose*/ true)) return false; // [dcl.fct.def.coroutine]p12 @@ -1135,7 +1134,8 @@ static bool findDeleteForPromise(Sema &S, SourceLocation Loc, QualType PromiseTy // parameters first. It will return the deallocation function with one // parameter if failed. IDP.PassSize = CanProvideSize; - OperatorDelete = S.FindUsualDeallocationFunction(PromiseType, Loc, IDP, DeleteName); + OperatorDelete = + S.FindUsualDeallocationFunction(PromiseType, Loc, IDP, DeleteName); if (!OperatorDelete) return false; @@ -1426,36 +1426,35 @@ bool CoroutineStmtBuilder::makeNewAndDeleteExpr() { // Helper function to indicate whether the last lookup found the aligned // allocation function. ImplicitAllocationParameters IAP = { - .PassTypeIdentity = S.AllowTypeAwareAllocators(), - .PassAlignment = S.getLangOpts().CoroAlignedAllocation != 0 - }; - auto LookupAllocationFunction = [&](Sema::AllocationFunctionScope NewScope = - Sema::AFS_Both, - bool WithoutPlacementArgs = false, - bool ForceNonAligned = false) { - // [dcl.fct.def.coroutine]p9 - // The allocation function's name is looked up by searching for it in the - // scope of the promise type. - // - If any declarations are found, ... - // - If no declarations are found in the scope of the promise type, a search - // is performed in the global scope or via ADL for typed allocation. - if (NewScope == Sema::AFS_Both) - NewScope = PromiseContainsNew ? Sema::AFS_Class : Sema::AFS_Global; - - IAP = { .PassTypeIdentity = S.AllowTypeAwareAllocators(), - .PassAlignment = !ForceNonAligned && S.getLangOpts().CoroAlignedAllocation - }; - - FunctionDecl *UnusedResult = nullptr; - - S.FindAllocationFunctions(Loc, SourceRange(), NewScope, - /*DeleteScope*/ Sema::AFS_Both, PromiseType, - /*isArray*/ false, IAP, - WithoutPlacementArgs ? MultiExprArg{} - : PlacementArgs, - OperatorNew, UnusedResult, /*Diagnose*/ false); - }; + .PassAlignment = S.getLangOpts().CoroAlignedAllocation != 0}; + auto LookupAllocationFunction = + [&](Sema::AllocationFunctionScope NewScope = Sema::AFS_Both, + bool WithoutPlacementArgs = false, bool ForceNonAligned = false) { + // [dcl.fct.def.coroutine]p9 + // The allocation function's name is looked up by searching for it in + // the + // scope of the promise type. + // - If any declarations are found, ... + // - If no declarations are found in the scope of the promise type, a + // search is performed in the global scope or via ADL for typed + // allocation. + if (NewScope == Sema::AFS_Both) + NewScope = PromiseContainsNew ? Sema::AFS_Class : Sema::AFS_Global; + + IAP = {.PassTypeIdentity = S.AllowTypeAwareAllocators(), + .PassAlignment = + !ForceNonAligned && S.getLangOpts().CoroAlignedAllocation}; + + FunctionDecl *UnusedResult = nullptr; + + S.FindAllocationFunctions( + Loc, SourceRange(), NewScope, + /*DeleteScope*/ Sema::AFS_Both, PromiseType, + /*isArray*/ false, IAP, + WithoutPlacementArgs ? MultiExprArg{} : PlacementArgs, OperatorNew, + UnusedResult, /*Diagnose*/ false); + }; // We don't expect to call to global operator new with (size, p0, …, pn). // So if we choose to lookup the allocation function in global scope, we @@ -1600,14 +1599,17 @@ bool CoroutineStmtBuilder::makeNewAndDeleteExpr() { SmallVector<Expr *, 3> NewArgs; if (IAP.PassTypeIdentity) { - auto SpecializedTypeIdentity = S.InstantiateSpecializedTypeIdentity(PromiseType); - if (!SpecializedTypeIdentity) - return false; - auto *SpecializedTypeInfo = S.Context.getTrivialTypeSourceInfo(*SpecializedTypeIdentity, Loc); - auto TypeIdentity = S.BuildCXXTypeConstructExpr(SpecializedTypeInfo, Loc, {}, Loc, false); - if (TypeIdentity.isInvalid()) - return false; - NewArgs.push_back(TypeIdentity.get()); + auto SpecializedTypeIdentity = + S.InstantiateSpecializedTypeIdentity(PromiseType); + if (!SpecializedTypeIdentity) + return false; + auto *SpecializedTypeInfo = + S.Context.getTrivialTypeSourceInfo(*SpecializedTypeIdentity, Loc); + auto TypeIdentity = + S.BuildCXXTypeConstructExpr(SpecializedTypeInfo, Loc, {}, Loc, false); + if (TypeIdentity.isInvalid()) + return false; + NewArgs.push_back(TypeIdentity.get()); } NewArgs.push_back(FrameSize); if (S.getLangOpts().CoroAlignedAllocation && IAP.PassAlignment) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index b6fdfcd571..ad5372495b 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -17395,7 +17395,7 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, // e.g. something like // namespace std { struct align_val_t { explicit align_val_t(size_t); } }; // can result in an operator new/delete decl picking up this specified - // align_val_t struct, but the align_val_t constructed implicitly has the + // align_val_t struct, but the align_val_t constructed implicitly has the // mismatching internal definition. // // The correct course of action is probably to perform this logic at the point diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 2a0de5dc59..bdaec80a62 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -9715,13 +9715,13 @@ bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, auto DeallocType = Context.getRecordType(RD); DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Delete); - ImplicitDeallocationParameters IDP = { - .PassTypeIdentity = AllowTypeAwareAllocators(), - .PassAlignment = false, - .PassSize = false - }; + ImplicitDeallocationParameters IDP = {.PassTypeIdentity = + AllowTypeAwareAllocators(), + .PassAlignment = false, + .PassSize = false}; if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, - OperatorDelete, DeallocType, IDP, /*Diagnose*/false)) { + OperatorDelete, DeallocType, IDP, + /*Diagnose*/ false)) { if (Diagnose) Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); return true; @@ -10876,14 +10876,15 @@ bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { if (IsTypeAwareOperatorNewOrDelete(OperatorDelete)) { ++PointerParam; } - QualType ParamType = OperatorDelete->getParamDecl(PointerParam)->getType(); + QualType ParamType = + OperatorDelete->getParamDecl(PointerParam)->getType(); if (!declaresSameEntity(ParamType->getAsCXXRecordDecl(), RD)) { // C++ [class.dtor]p13: // ... as if for the expression 'delete this' appearing in a // non-virtual destructor of the destructor's class. ContextRAII SwitchContext(*this, Destructor); - ExprResult This = - ActOnCXXThis(OperatorDelete->getParamDecl(PointerParam)->getLocation()); + ExprResult This = ActOnCXXThis( + OperatorDelete->getParamDecl(PointerParam)->getLocation()); assert(!This.isInvalid() && "couldn't form 'this' expr in dtor?"); This = PerformImplicitConversion(This.get(), ParamType, AssignmentAction::Passing); @@ -11742,7 +11743,8 @@ NamespaceDecl *Sema::getStdNamespace() const { } ClassTemplateDecl *Sema::getStdTypeIdentity() const { - return cast_or_null<ClassTemplateDecl>(StdTypeIdentity.get(Context.getExternalSource())); + return cast_or_null<ClassTemplateDecl>( + StdTypeIdentity.get(Context.getExternalSource())); } namespace { @@ -16135,7 +16137,7 @@ bool Sema::IsTypeIdentitySpecialization(QualType Type) const { } bool Sema::IsTypeAwareOperatorNewOrDelete(const FunctionDecl *FnDecl) const { - // Type aware operators + // Type aware operators if (FnDecl->getNumParams() < 2) return false; const auto *ParamDecl = FnDecl->getParamDecl(0); @@ -16155,8 +16157,9 @@ bool Sema::IsTypeAwareOperatorNewOrDelete(const NamedDecl *ND) const { return false; } -std::optional<FunctionDecl*> -Sema::InstantiateTypeAwareUsualDelete(FunctionTemplateDecl *FnTemplateDecl, QualType DeallocType) { +std::optional<FunctionDecl *> +Sema::InstantiateTypeAwareUsualDelete(FunctionTemplateDecl *FnTemplateDecl, + QualType DeallocType) { if (!AllowTypeAwareAllocators()) return std::nullopt; @@ -16176,14 +16179,15 @@ Sema::InstantiateTypeAwareUsualDelete(FunctionTemplateDecl *FnTemplateDecl, Qual return std::nullopt; for (size_t Idx = 1; Idx < NumParams; ++Idx) { - // A type aware allocation is only usual if the only dependent parameter is the - // first parameter + // A type aware allocation is only usual if the only dependent parameter is + // the first parameter const auto *ParamDecl = FnDecl->getParamDecl(Idx); if (ParamDecl->getType()->isDependentType()) return std::nullopt; } - auto SpecializedTypeIdentity = InstantiateSpecializedTypeIdentity(DeallocType); + auto SpecializedTypeIdentity = + InstantiateSpecializedTypeIdentity(DeallocType); if (!SpecializedTypeIdentity) return std::nullopt; SmallVector<QualType, 4> ArgTypes; @@ -16217,17 +16221,19 @@ Sema::InstantiateTypeAwareUsualDelete(FunctionTemplateDecl *FnTemplateDecl, Qual return std::nullopt; FunctionProtoType::ExtProtoInfo EPI; - auto ExpectedFunctionType = Context.getFunctionType(Context.VoidTy, ArgTypes, EPI); + auto ExpectedFunctionType = + Context.getFunctionType(Context.VoidTy, ArgTypes, EPI); SourceLocation Loc; sema::TemplateDeductionInfo Info(Loc); FunctionDecl *Result; - if (DeduceTemplateArguments(FnTemplateDecl, nullptr, ExpectedFunctionType, Result, - Info) != TemplateDeductionResult::Success) + if (DeduceTemplateArguments(FnTemplateDecl, nullptr, ExpectedFunctionType, + Result, Info) != TemplateDeductionResult::Success) return std::nullopt; return Result; } -std::optional<QualType> Sema::InstantiateSpecializedTypeIdentity(QualType Subject) { +std::optional<QualType> +Sema::InstantiateSpecializedTypeIdentity(QualType Subject) { assert(AllowTypeAwareAllocators()); auto *TypeIdentity = getStdTypeIdentity(); if (!TypeIdentity) { @@ -16235,7 +16241,8 @@ std::optional<QualType> Sema::InstantiateSpecializedTypeIdentity(QualType Subjec } auto TN = TemplateName(TypeIdentity); TemplateArgumentListInfo Arguments; - Arguments.addArgument(getTrivialTemplateArgumentLoc(TemplateArgument(Subject), QualType(), SourceLocation())); + Arguments.addArgument(getTrivialTemplateArgumentLoc( + TemplateArgument(Subject), QualType(), SourceLocation())); auto Result = CheckTemplateIdType(TN, SourceLocation(), Arguments); if (Result.isNull()) return std::nullopt; @@ -16271,13 +16278,10 @@ static CanQualType RemoveAddressSpaceFromPtr(Sema &SemaRef, PtrTy->getPointeeType().getUnqualifiedType(), PtrQuals))); } -static inline bool -CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, - CanQualType ExpectedResultType, - CanQualType ExpectedFirstParamType, - unsigned DependentParamTypeDiag, - unsigned InvalidParamTypeDiag, - unsigned *MinimumNonDefaultArgs) { +static inline bool CheckOperatorNewDeleteTypes( + Sema &SemaRef, const FunctionDecl *FnDecl, CanQualType ExpectedResultType, + CanQualType ExpectedFirstParamType, unsigned DependentParamTypeDiag, + unsigned InvalidParamTypeDiag, unsigned *MinimumNonDefaultArgs) { auto NormalizeType = [&SemaRef](QualType T) { if (SemaRef.getLangOpts().OpenCLCPlusPlus) { // The operator is valid on any address space for OpenCL. @@ -16313,8 +16317,8 @@ CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, unsigned FirstNonTypeParam = 0; if (FnDecl->IsTypeAwareOperatorNewOrDelete()) { if (!SemaRef.getLangOpts().TypeAwareAllocators) { - return SemaRef.Diag(FnDecl->getLocation(), - diag::err_unsupported_type_aware_allocator); + return SemaRef.Diag(FnDecl->getLocation(), + diag::err_unsupported_type_aware_allocator); } ++FirstNonTypeParam; } @@ -16325,7 +16329,8 @@ CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, diag::err_operator_new_delete_too_few_parameters) << FnDecl->getDeclName(); - QualType FirstParamType = NormalizeType(FnDecl->getParamDecl(FirstNonTypeParam)->getType()); + QualType FirstParamType = + NormalizeType(FnDecl->getParamDecl(FirstNonTypeParam)->getType()); // Check that the first parameter type is what we expect. if (FirstParamType.getUnqualifiedType() != CanExpectedFirstParamType) { @@ -16358,11 +16363,10 @@ CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { // C++ [basic.stc.dynamic.allocation]p1: // The return type shall be void*. The first parameter shall have type // std::size_t. - if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, - SizeTy, - diag::err_operator_new_dependent_param_type, - diag::err_operator_new_param_type, - &MinimumNonDefaultArgs)) + if (CheckOperatorNewDeleteTypes( + SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, SizeTy, + diag::err_operator_new_dependent_param_type, + diag::err_operator_new_param_type, &MinimumNonDefaultArgs)) return true; assert(MinimumNonDefaultArgs > 0); // C++ [basic.stc.dynamic.allocation]p1: @@ -16372,7 +16376,7 @@ CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { if (ParamDecl->hasDefaultArg()) { return SemaRef.Diag(FnDecl->getLocation(), diag::err_operator_new_default_arg) - << FnDecl->getDeclName() << ParamDecl->getDefaultArgRange(); + << FnDecl->getDeclName() << ParamDecl->getDefaultArgRange(); } } return false; @@ -16405,8 +16409,7 @@ CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { if (CheckOperatorNewDeleteTypes( SemaRef, FnDecl, SemaRef.Context.VoidTy, ExpectedFirstParamType, diag::err_operator_delete_dependent_param_type, - diag::err_operator_delete_param_type, - &MinimumNonDefaultArgs)) + diag::err_operator_delete_param_type, &MinimumNonDefaultArgs)) return true; assert(MinimumNonDefaultArgs > 0); @@ -16419,7 +16422,8 @@ CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { diag::err_destroying_operator_delete_not_usual); return true; } - if (MD->IsTypeAwareOperatorNewOrDelete() && !SemaRef.getLangOpts().TypeAwareDestroyingDelete) { + if (MD->IsTypeAwareOperatorNewOrDelete() && + !SemaRef.getLangOpts().TypeAwareDestroyingDelete) { SemaRef.Diag(MD->getLocation(), diag::err_type_aware_destroying_operator_delete); return true; @@ -16430,7 +16434,7 @@ CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { if (ParamDecl->hasDefaultArg()) { return SemaRef.Diag(FnDecl->getLocation(), diag::err_operator_new_default_arg) - << FnDecl->getDeclName() << ParamDecl->getDefaultArgRange(); + << FnDecl->getDeclName() << ParamDecl->getDefaultArgRange(); } } return false; diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index c59ef76a70..19a6b424a6 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -1778,21 +1778,23 @@ namespace { UsualDeallocFnInfo() : Found(), FD(nullptr) {} UsualDeallocFnInfo(Sema &S, DeclAccessPair Found, QualType AllocType) : Found(Found), FD(dyn_cast<FunctionDecl>(Found->getUnderlyingDecl())), - Destroying(false), HasSizeT(false), HasAlignValT(false), HasTypeIdentity(false), - CUDAPref(SemaCUDA::CFP_Native) { - // A function template declaration is only a usual deallocation function if it is - // a typed delete + Destroying(false), HasSizeT(false), HasAlignValT(false), + HasTypeIdentity(false), CUDAPref(SemaCUDA::CFP_Native) { + // A function template declaration is only a usual deallocation function + // if it is a typed delete if (!FD) { auto *FTD = dyn_cast<FunctionTemplateDecl>(Found->getUnderlyingDecl()); if (!FTD) return; - auto InstantiatedDecl = S.InstantiateTypeAwareUsualDelete(FTD, AllocType); + auto InstantiatedDecl = + S.InstantiateTypeAwareUsualDelete(FTD, AllocType); if (!InstantiatedDecl) return; FD = *InstantiatedDecl; } unsigned NumBaseParams = 1; - if (S.IsTypeAwareOperatorNewOrDelete(FD) && S.AllowTypeAwareAllocators()) { + if (S.IsTypeAwareOperatorNewOrDelete(FD) && + S.AllowTypeAwareAllocators()) { auto TypeIdentityTag = FD->getParamDecl(0)->getType(); auto ExpectedTypeIdentityTag = S.InstantiateSpecializedTypeIdentity(AllocType); @@ -1835,7 +1837,8 @@ namespace { explicit operator bool() const { return FD; } - int Compare(Sema& S, const UsualDeallocFnInfo &Other, ImplicitDeallocationParameters IDP) const { + int Compare(Sema &S, const UsualDeallocFnInfo &Other, + ImplicitDeallocationParameters IDP) const { // C++ P0722: // A destroying operator delete is preferred over a non-destroying // operator delete. @@ -1865,23 +1868,17 @@ namespace { return OtherPrimaryTemplate ? 1 : -1; if (PrimaryTemplate && OtherPrimaryTemplate) { - const auto * DC = - dyn_cast<CXXRecordDecl>(Found->getDeclContext()); - const auto * OtherDC = + const auto *DC = dyn_cast<CXXRecordDecl>(Found->getDeclContext()); + const auto *OtherDC = dyn_cast<CXXRecordDecl>(Other.Found->getDeclContext()); unsigned ImplicitArgCount = 1 + Destroying + HasTypeIdentity + HasAlignValT + HasSizeT; - if (FunctionTemplateDecl *Best = - S.getMoreSpecializedTemplate( - PrimaryTemplate, OtherPrimaryTemplate, - SourceLocation(), TPOC_Call, - ImplicitArgCount, - DC ? QualType(DC->getTypeForDecl(), 0) - : QualType{}, - OtherDC - ? QualType(OtherDC->getTypeForDecl(), 0) - : QualType{}, - false)) { + if (FunctionTemplateDecl *Best = S.getMoreSpecializedTemplate( + PrimaryTemplate, OtherPrimaryTemplate, SourceLocation(), + TPOC_Call, ImplicitArgCount, + DC ? QualType(DC->getTypeForDecl(), 0) : QualType{}, + OtherDC ? QualType(OtherDC->getTypeForDecl(), 0) : QualType{}, + false)) { return Best == PrimaryTemplate ? 1 : -1; } } @@ -1912,8 +1909,10 @@ static bool hasNewExtendedAlignment(Sema &S, QualType AllocType) { S.getASTContext().getTargetInfo().getNewAlign(); } - -static bool CheckDeleteOperator(Sema &S, SourceLocation StartLoc, SourceRange Range, bool Diagnose, CXXRecordDecl *NamingClass, DeclAccessPair Decl, FunctionDecl* Operator) { +static bool CheckDeleteOperator(Sema &S, SourceLocation StartLoc, + SourceRange Range, bool Diagnose, + CXXRecordDecl *NamingClass, DeclAccessPair Decl, + FunctionDecl *Operator) { if (S.IsTypeAwareOperatorNewOrDelete(Operator)) { auto SelectedTypeIdentityParameter = Operator->getParamDecl(0)->getType(); if (S.RequireCompleteType(StartLoc, SelectedTypeIdentityParameter, @@ -1921,24 +1920,25 @@ static bool CheckDeleteOperator(Sema &S, SourceLocation StartLoc, SourceRange Ra return true; } - // FIXME: DiagnoseUseOfDecl? - if (Operator->isDeleted()) { - if (Diagnose) { - StringLiteral *Msg = Operator->getDeletedMessage(); - S.Diag(StartLoc, diag::err_deleted_function_use) - << (Msg != nullptr) << (Msg ? Msg->getString() : StringRef()); - S.NoteDeletedFunction(Operator); - } - return true; + // FIXME: DiagnoseUseOfDecl? + if (Operator->isDeleted()) { + if (Diagnose) { + StringLiteral *Msg = Operator->getDeletedMessage(); + S.Diag(StartLoc, diag::err_deleted_function_use) + << (Msg != nullptr) << (Msg ? Msg->getString() : StringRef()); + S.NoteDeletedFunction(Operator); } + return true; + } - return S.CheckAllocationAccess(StartLoc, Range, NamingClass, Decl, Diagnose); + return S.CheckAllocationAccess(StartLoc, Range, NamingClass, Decl, Diagnose); } /// Select the correct "usual" deallocation function to use from a selection of /// deallocation functions (either global or class-scope). static UsualDeallocFnInfo resolveDeallocationOverload( - Sema &S, LookupResult &R, ImplicitDeallocationParameters IDP, QualType DeallocType, + Sema &S, LookupResult &R, ImplicitDeallocationParameters IDP, + QualType DeallocType, llvm::SmallVectorImpl<UsualDeallocFnInfo> *BestFns = nullptr) { UsualDeallocFnInfo Best; @@ -1975,8 +1975,8 @@ static UsualDeallocFnInfo resolveDeallocationOverload( /// a member 'operator delete[]' with a 'size_t' parameter. This implies that /// we need to store the array size (even if the type is /// trivially-destructible). -static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, bool PassType, - QualType allocType) { +static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, + bool PassType, QualType allocType) { const RecordType *record = allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); if (!record) return false; @@ -2002,10 +2002,9 @@ static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, bool PassT // If the deallocation functions have class scope, the one without a // parameter of type std::size_t is selected. ImplicitDeallocationParameters IDP = { - .PassTypeIdentity = PassType, - .PassAlignment = hasNewExtendedAlignment(S, allocType), - .PassSize = false - }; + .PassTypeIdentity = PassType, + .PassAlignment = hasNewExtendedAlignment(S, allocType), + .PassSize = false}; auto Best = resolveDeallocationOverload(S, ops, IDP, allocType); return Best && Best.HasSizeT; } @@ -2420,9 +2419,9 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, AllocType->isDependentType() ? 0 : Context.getTypeAlign(AllocType); unsigned NewAlignment = Context.getTargetInfo().getNewAlign(); ImplicitAllocationParameters IAP = { - .PassTypeIdentity = AllowTypeAwareAllocators(), - .PassAlignment = getLangOpts().AlignedAllocation && Alignment > NewAlignment - }; + .PassTypeIdentity = AllowTypeAwareAllocators(), + .PassAlignment = + getLangOpts().AlignedAllocation && Alignment > NewAlignment}; if (CheckArgsForPlaceholders(PlacementArgs)) return ExprError(); @@ -2430,18 +2429,18 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, AllocationFunctionScope Scope = UseGlobal ? AFS_Global : AFS_Both; if (!AllocType->isDependentType() && !Expr::hasAnyTypeDependentArguments(PlacementArgs) && - FindAllocationFunctions( - StartLoc, SourceRange(PlacementLParen, PlacementRParen), Scope, Scope, - AllocType, ArraySize.has_value(), IAP, PlacementArgs, - OperatorNew, OperatorDelete)) + FindAllocationFunctions(StartLoc, + SourceRange(PlacementLParen, PlacementRParen), + Scope, Scope, AllocType, ArraySize.has_value(), + IAP, PlacementArgs, OperatorNew, OperatorDelete)) return ExprError(); // If this is an array allocation, compute whether the usual array // deallocation function for the type has a size_t parameter. bool UsualArrayDeleteWantsSize = false; if (ArraySize && !AllocType->isDependentType()) - UsualArrayDeleteWantsSize = - doesUsualArrayDeleteWantSize(*this, StartLoc, IAP.PassTypeIdentity, AllocType); + UsualArrayDeleteWantsSize = doesUsualArrayDeleteWantSize( + *this, StartLoc, IAP.PassTypeIdentity, AllocType); SmallVector<Expr *, 8> AllPlaceArgs; if (OperatorNew) { @@ -2622,10 +2621,9 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, } return CXXNewExpr::Create(Context, UseGlobal, OperatorNew, OperatorDelete, - IAP, UsualArrayDeleteWantsSize, - PlacementArgs, TypeIdParens, ArraySize, InitStyle, - Initializer, ResultType, AllocTypeInfo, Range, - DirectInitRange); + IAP, UsualArrayDeleteWantsSize, PlacementArgs, + TypeIdParens, ArraySize, InitStyle, Initializer, + ResultType, AllocTypeInfo, Range, DirectInitRange); } bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, @@ -2668,8 +2666,8 @@ bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, enum class ResolveMode { Typed, Untyped }; static bool resolveAllocationOverloadInterior( - Sema &S, LookupResult &R, SourceRange Range, ResolveMode Mode, SmallVectorImpl<Expr *> &Args, - bool &PassAlignment, FunctionDecl *&Operator, + Sema &S, LookupResult &R, SourceRange Range, ResolveMode Mode, + SmallVectorImpl<Expr *> &Args, bool &PassAlignment, FunctionDecl *&Operator, OverloadCandidateSet *AlignedCandidates, Expr *AlignArg, bool Diagnose) { unsigned NonTypeArgumentOffset = 0; if (Mode == ResolveMode::Typed) @@ -2721,9 +2719,9 @@ static bool resolveAllocationOverloadInterior( PassAlignment = false; AlignArg = Args[NonTypeArgumentOffset + 1]; Args.erase(Args.begin() + NonTypeArgumentOffset + 1); - return resolveAllocationOverloadInterior(S, R, Range, Mode, Args, PassAlignment, - Operator, &Candidates, AlignArg, - Diagnose); + return resolveAllocationOverloadInterior(S, R, Range, Mode, Args, + PassAlignment, Operator, + &Candidates, AlignArg, Diagnose); } // MSVC will fall back on trying to find a matching global operator new @@ -2738,9 +2736,10 @@ static bool resolveAllocationOverloadInterior( R.setLookupName(S.Context.DeclarationNames.getCXXOperatorName(OO_New)); S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); // FIXME: This will give bad diagnostics pointing at the wrong functions. - return resolveAllocationOverloadInterior(S, R, Range, Mode, Args, PassAlignment, - Operator, /*Candidates=*/nullptr, - /*AlignArg=*/nullptr, Diagnose); + return resolveAllocationOverloadInterior(S, R, Range, Mode, Args, + PassAlignment, Operator, + /*Candidates=*/nullptr, + /*AlignArg=*/nullptr, Diagnose); } if (Mode == ResolveMode::Typed) { // If we can't find a matching typed we don't consider this a failure. @@ -2773,7 +2772,9 @@ static bool resolveAllocationOverloadInterior( auto IsAligned = [NonTypeArgumentOffset](OverloadCandidate &C) { auto AlignArgOffset = NonTypeArgumentOffset + 1; return C.Function->getNumParams() > AlignArgOffset && - C.Function->getParamDecl(AlignArgOffset)->getType()->isAlignValT(); + C.Function->getParamDecl(AlignArgOffset) + ->getType() + ->isAlignValT(); }; auto IsUnaligned = [&](OverloadCandidate &C) { return !IsAligned(C); }; @@ -2781,7 +2782,8 @@ static bool resolveAllocationOverloadInterior( for (unsigned Idx = 0; Idx < NonTypeArgumentOffset + 1; ++Idx) AlignedArgs.push_back(Args[Idx]); AlignedArgs.push_back(AlignArg); - AlignedArgs.append(Args.begin() + NonTypeArgumentOffset + 1, Args.end()); + AlignedArgs.append(Args.begin() + NonTypeArgumentOffset + 1, + Args.end()); AlignedCands = AlignedCandidates->CompleteCandidates( S, OCD_AllCandidates, AlignedArgs, R.getNameLoc(), IsAligned); @@ -2821,12 +2823,7 @@ static bool resolveAllocationOverloadInterior( llvm_unreachable("Unreachable, bad result from BestViableFunction"); } - -enum class DeallocLookupMode { - Untyped, - OptionallyTyped, - RequireTyped -}; +enum class DeallocLookupMode { Untyped, OptionallyTyped, RequireTyped }; static void LookupGlobalDeallocationFunctions(Sema &S, SourceLocation Loc, LookupResult &FoundDelete, @@ -2849,7 +2846,7 @@ static void LookupGlobalDeallocationFunctions(Sema &S, SourceLocation Loc, static bool resolveAllocationOverload( Sema &S, LookupResult &R, SourceRange Range, SmallVectorImpl<Expr *> &Args, - ImplicitAllocationParameters& IAP, FunctionDecl *&Operator, + ImplicitAllocationParameters &IAP, FunctionDecl *&Operator, OverloadCandidateSet *AlignedCandidates, Expr *AlignArg, bool Diagnose) { Operator = nullptr; if (IAP.PassTypeIdentity) { @@ -2858,8 +2855,9 @@ static bool resolveAllocationOverload( UntypedParameters.reserve(Args.size() - 1); UntypedParameters.append(Args.begin() + 1, Args.end()); bool InitialAlignmentMode = IAP.PassAlignment; - if (resolveAllocationOverloadInterior(S, R, Range, ResolveMode::Typed, Args, IAP.PassAlignment, - Operator, AlignedCandidates, AlignArg, Diagnose)) + if (resolveAllocationOverloadInterior( + S, R, Range, ResolveMode::Typed, Args, IAP.PassAlignment, Operator, + AlignedCandidates, AlignArg, Diagnose)) return true; if (Operator) return false; @@ -2871,18 +2869,17 @@ static bool resolveAllocationOverload( Args = UntypedParameters; } assert(!Args[0]->getType()->isTypeIdentitySpecialization()); - return resolveAllocationOverloadInterior(S, R, Range, ResolveMode::Untyped, Args, IAP.PassAlignment, - Operator, AlignedCandidates, AlignArg, Diagnose); -} - -bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, - AllocationFunctionScope NewScope, - AllocationFunctionScope DeleteScope, - QualType AllocType, bool IsArray, - ImplicitAllocationParameters& IAP, MultiExprArg PlaceArgs, - FunctionDecl *&OperatorNew, - FunctionDecl *&OperatorDelete, - bool Diagnose) { + return resolveAllocationOverloadInterior( + S, R, Range, ResolveMode::Untyped, Args, IAP.PassAlignment, Operator, + AlignedCandidates, AlignArg, Diagnose); +} + +bool Sema::FindAllocationFunctions( + SourceLocation StartLoc, SourceRange Range, + AllocationFunctionScope NewScope, AllocationFunctionScope DeleteScope, + QualType AllocType, bool IsArray, ImplicitAllocationParameters &IAP, + MultiExprArg PlaceArgs, FunctionDecl *&OperatorNew, + FunctionDecl *&OperatorDelete, bool Diagnose) { // --- Choosing an allocation function --- // C++ 5.3.4p8 - 14 & 18 // 1) If looking in AFS_Global scope for allocation functions, only look in @@ -2897,7 +2894,6 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, unsigned ImplicitArgCount = 1 + IAP.PassAlignment + IAP.PassTypeIdentity; AllocArgs.reserve(ImplicitArgCount + PlaceArgs.size()); - // C++ [expr.new]p8: // If the allocated type is a non-array type, the allocation // function's name is operator new and the deallocation function's @@ -2918,7 +2914,8 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, // expr on the stack QualType TypeIdentity = Context.getSizeType(); if (IAP.PassTypeIdentity) { - if (auto SpecializedTypeIdentity = InstantiateSpecializedTypeIdentity(AllocElemType)) { + if (auto SpecializedTypeIdentity = + InstantiateSpecializedTypeIdentity(AllocElemType)) { TypeIdentity = *SpecializedTypeIdentity; } else { IAP.PassTypeIdentity = false; @@ -2929,11 +2926,11 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, CXXScalarValueInitExpr TypeIdentityParam(TypeIdentity, nullptr, StartLoc); if (IAP.PassTypeIdentity) AllocArgs.push_back(&TypeIdentityParam); - + QualType SizeTy = Context.getSizeType(); unsigned SizeTyWidth = Context.getTypeSize(SizeTy); IntegerLiteral Size(Context, llvm::APInt::getZero(SizeTyWidth), SizeTy, - SourceLocation()); + SourceLocation()); AllocArgs.push_back(&Size); QualType AlignValT = Context.VoidTy; @@ -2987,10 +2984,10 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, // We do our own custom access checks below. R.suppressDiagnostics(); - if (resolveAllocationOverload(*this, R, Range, AllocArgs, IAP, - OperatorNew, /*Candidates=*/nullptr, + if (resolveAllocationOverload(*this, R, Range, AllocArgs, IAP, OperatorNew, + /*Candidates=*/nullptr, /*AlignArg=*/nullptr, Diagnose)) - return true; + return true; } // We don't need an operator delete if we're running under -fno-exceptions. @@ -3046,7 +3043,9 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, return true; DeclareGlobalNewDelete(); - auto LookupMode = OriginalTypeAwareState ? DeallocLookupMode::OptionallyTyped : DeallocLookupMode::Untyped; + auto LookupMode = OriginalTypeAwareState + ? DeallocLookupMode::OptionallyTyped + : DeallocLookupMode::Untyped; LookupGlobalDeallocationFunctions(*this, StartLoc, FoundDelete, LookupMode, DeleteName, AllocElemType); } @@ -3072,7 +3071,8 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, unsigned NonPlacementNewArgCount = 1; // size parameter if (IAP.PassTypeIdentity) ++NonPlacementNewArgCount; - bool isPlacementNew = !PlaceArgs.empty() || OperatorNew->param_size() != NonPlacementNewArgCount || + bool isPlacementNew = !PlaceArgs.empty() || + OperatorNew->param_size() != NonPlacementNewArgCount || OperatorNew->isVariadic(); if (isPlacementNew) { @@ -3106,8 +3106,8 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, } for (LookupResult::iterator D = FoundDelete.begin(), - DEnd = FoundDelete.end(); - D != DEnd; ++D) { + DEnd = FoundDelete.end(); + D != DEnd; ++D) { FunctionDecl *Fn = nullptr; if (FunctionTemplateDecl *FnTmpl = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { @@ -3140,11 +3140,11 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, // with a size_t where possible (which it always is in this case). llvm::SmallVector<UsualDeallocFnInfo, 4> BestDeallocFns; ImplicitDeallocationParameters IDP = { - .PassTypeIdentity = OriginalTypeAwareState, - .PassAlignment = hasNewExtendedAlignment(*this, AllocElemType), - .PassSize = FoundGlobalDelete - }; - UsualDeallocFnInfo Selected = resolveDeallocationOverload(*this, FoundDelete, IDP, AllocElemType, &BestDeallocFns); + .PassTypeIdentity = OriginalTypeAwareState, + .PassAlignment = hasNewExtendedAlignment(*this, AllocElemType), + .PassSize = FoundGlobalDelete}; + UsualDeallocFnInfo Selected = resolveDeallocationOverload( + *this, FoundDelete, IDP, AllocElemType, &BestDeallocFns); if (Selected && BestDeallocFns.empty()) Matches.push_back(std::make_pair(Selected.Found, Selected.FD)); else { @@ -3153,7 +3153,6 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, for (auto Fn : BestDeallocFns) Matches.push_back(std::make_pair(Fn.Found, Fn.FD)); } - } // C++ [expr.new]p20: @@ -3162,17 +3161,28 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, // deallocation function will be called. if (Matches.size() == 1) { OperatorDelete = Matches[0].second; - if (IsTypeAwareOperatorNewOrDelete(OperatorDelete) != IAP.PassTypeIdentity) { + if (IsTypeAwareOperatorNewOrDelete(OperatorDelete) != + IAP.PassTypeIdentity) { Diag(StartLoc, diag::warn_mismatching_type_aware_cleanup_deallocator); int NewDiagIndex = IsTypeAwareOperatorNewOrDelete(OperatorNew) ? 0 : 1; - int DeleteDiagIndex = IsTypeAwareOperatorNewOrDelete(OperatorDelete) ? 0 : 1; - Diag(OperatorNew->getLocation(), diag::note_type_aware_operator_declared) << NewDiagIndex << OperatorNew->getDeclName(); - Diag(OperatorDelete->getLocation(), diag::note_type_aware_operator_declared) << DeleteDiagIndex << OperatorDelete->getDeclName(); - } - if (IAP.PassTypeIdentity && OperatorDelete->getDeclContext() != OperatorNew->getDeclContext()) { - Diag(StartLoc, diag::err_no_matching_type_aware_cleanup_deallocator_mismatch) << OperatorNew->getDeclName() << DeleteName << OperatorNew->getDeclContext(); - Diag(OperatorNew->getLocation(), diag::err_type_aware_operator_found) << OperatorNew->getDeclName() << OperatorNew->getDeclContext(); - Diag(OperatorDelete->getLocation(), diag::err_type_aware_operator_found) << OperatorDelete->getDeclName() << OperatorDelete->getDeclContext(); + int DeleteDiagIndex = + IsTypeAwareOperatorNewOrDelete(OperatorDelete) ? 0 : 1; + Diag(OperatorNew->getLocation(), diag::note_type_aware_operator_declared) + << NewDiagIndex << OperatorNew->getDeclName(); + Diag(OperatorDelete->getLocation(), + diag::note_type_aware_operator_declared) + << DeleteDiagIndex << OperatorDelete->getDeclName(); + } + if (IAP.PassTypeIdentity && + OperatorDelete->getDeclContext() != OperatorNew->getDeclContext()) { + Diag(StartLoc, + diag::err_no_matching_type_aware_cleanup_deallocator_mismatch) + << OperatorNew->getDeclName() << DeleteName + << OperatorNew->getDeclContext(); + Diag(OperatorNew->getLocation(), diag::err_type_aware_operator_found) + << OperatorNew->getDeclName() << OperatorNew->getDeclContext(); + Diag(OperatorDelete->getLocation(), diag::err_type_aware_operator_found) + << OperatorDelete->getDeclName() << OperatorDelete->getDeclContext(); } // C++1z [expr.new]p23: @@ -3184,7 +3194,8 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, if (getLangOpts().CPlusPlus11 && isPlacementNew && isNonPlacementDeallocationFunction(*this, OperatorDelete)) { UsualDeallocFnInfo Info(*this, - DeclAccessPair::make(OperatorDelete, AS_public), AllocElemType); + DeclAccessPair::make(OperatorDelete, AS_public), + AllocElemType); // Core issue, per mail to core reflector, 2016-10-09: // If this is a member operator delete, and there is a corresponding // non-sized member operator delete, this isn't /really/ a sized @@ -3192,12 +3203,11 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, bool IsSizedDelete = Info.HasSizeT; if (IsSizedDelete && !FoundGlobalDelete) { ImplicitDeallocationParameters SizeTestingIDP = { - .PassTypeIdentity = Info.HasTypeIdentity, - .PassAlignment = Info.HasAlignValT, - .PassSize = false - }; - auto NonSizedDelete = - resolveDeallocationOverload(*this, FoundDelete, SizeTestingIDP, AllocElemType); + .PassTypeIdentity = Info.HasTypeIdentity, + .PassAlignment = Info.HasAlignValT, + .PassSize = false}; + auto NonSizedDelete = resolveDeallocationOverload( + *this, FoundDelete, SizeTestingIDP, AllocElemType); if (NonSizedDelete && !NonSizedDelete.HasSizeT && NonSizedDelete.HasAlignValT == Info.HasAlignValT) IsSizedDelete = false; @@ -3214,7 +3224,9 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, << DeleteName; } } - if (CheckDeleteOperator(*this, StartLoc, Range, /* Diagnose */ true, FoundDelete.getNamingClass(), Matches[0].first, Matches[0].second)) + if (CheckDeleteOperator(*this, StartLoc, Range, /* Diagnose */ true, + FoundDelete.getNamingClass(), Matches[0].first, + Matches[0].second)) return true; } else if (!Matches.empty()) { @@ -3477,14 +3489,15 @@ void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, } } -FunctionDecl *Sema::FindUsualDeallocationFunction(QualType DeallocType, - SourceLocation StartLoc, - ImplicitDeallocationParameters IDP, - DeclarationName Name) { +FunctionDecl *Sema::FindUsualDeallocationFunction( + QualType DeallocType, SourceLocation StartLoc, + ImplicitDeallocationParameters IDP, DeclarationName Name) { DeclareGlobalNewDelete(); LookupResult FoundDelete(*this, Name, StartLoc, LookupOrdinaryName); - auto LookupMode = AllowTypeAwareAllocators() ? DeallocLookupMode::OptionallyTyped : DeallocLookupMode::Untyped; + auto LookupMode = AllowTypeAwareAllocators() + ? DeallocLookupMode::OptionallyTyped + : DeallocLookupMode::Untyped; LookupGlobalDeallocationFunctions(*this, StartLoc, FoundDelete, LookupMode, Name, DeallocType); @@ -3492,11 +3505,14 @@ FunctionDecl *Sema::FindUsualDeallocationFunction(QualType DeallocType, // user-declared variadic operator delete or the enable_if attribute. We // should probably not consider those cases to be usual deallocation // functions. But for now we just make an arbitrary choice in that case. - auto Result = resolveDeallocationOverload(*this, FoundDelete, IDP, DeallocType); + auto Result = + resolveDeallocationOverload(*this, FoundDelete, IDP, DeallocType); if (!Result) return nullptr; - if (CheckDeleteOperator(*this, StartLoc, StartLoc, /* Diagnose */ true, FoundDelete.getNamingClass(), Result.Found, Result.FD)) + if (CheckDeleteOperator(*this, StartLoc, StartLoc, /* Diagnose */ true, + FoundDelete.getNamingClass(), Result.Found, + Result.FD)) return nullptr; assert(Result.FD && "operator delete missing from global scope?"); @@ -3509,11 +3525,10 @@ FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc, FunctionDecl *OperatorDelete = nullptr; auto DeallocType = Context.getRecordType(RD); - ImplicitDeallocationParameters IDP = { - .PassTypeIdentity = AllowTypeAwareAllocators(), - .PassAlignment = false, - .PassSize = false - }; + ImplicitDeallocationParameters IDP = {.PassTypeIdentity = + AllowTypeAwareAllocators(), + .PassAlignment = false, + .PassSize = false}; if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete, DeallocType, IDP)) return nullptr; @@ -3531,7 +3546,9 @@ FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc, bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, DeclarationName Name, - FunctionDecl *&Operator, QualType DeallocType, ImplicitDeallocationParameters IDP, + FunctionDecl *&Operator, + QualType DeallocType, + ImplicitDeallocationParameters IDP, bool Diagnose) { LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); // Try to find operator delete/operator delete[] in class scope. @@ -3542,7 +3559,8 @@ bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, Found.suppressDiagnostics(); - IDP.PassAlignment |= hasNewExtendedAlignment(*this, Context.getRecordType(RD)); + IDP.PassAlignment |= + hasNewExtendedAlignment(*this, Context.getRecordType(RD)); // C++17 [expr.delete]p10: // If the deallocation functions have class scope, the one without a @@ -3553,7 +3571,9 @@ bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, // If we could find an overload, use it. if (Matches.size() == 1) { Operator = cast<CXXMethodDecl>(Matches[0].FD); - return CheckDeleteOperator(*this, StartLoc, StartLoc, Diagnose, Found.getNamingClass(), Matches[0].Found, Operator); + return CheckDeleteOperator(*this, StartLoc, StartLoc, Diagnose, + Found.getNamingClass(), Matches[0].Found, + Operator); } // We found multiple suitable operators; complain about the ambiguity. @@ -3976,13 +3996,13 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, ArrayForm ? OO_Array_Delete : OO_Delete); if (PointeeRD) { - ImplicitDeallocationParameters IDP = { - .PassTypeIdentity = AllowTypeAwareAllocators(), - .PassAlignment = false, - .PassSize = false - }; + ImplicitDeallocationParameters IDP = {.PassTypeIdentity = + AllowTypeAwareAllocators(), + .PassAlignment = false, + .PassSize = false}; if (!UseGlobal && - FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, OperatorDelete, Pointee, IDP)) + FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, + OperatorDelete, Pointee, IDP)) return ExprError(); // If we're allocating an array of records, check whether the @@ -3991,16 +4011,17 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, // If the user specifically asked to use the global allocator, // we'll need to do the lookup into the class. if (UseGlobal) - UsualArrayDeleteWantsSize = - doesUsualArrayDeleteWantSize(*this, StartLoc, IDP.PassTypeIdentity, PointeeElem); + UsualArrayDeleteWantsSize = doesUsualArrayDeleteWantSize( + *this, StartLoc, IDP.PassTypeIdentity, PointeeElem); // Otherwise, the usual operator delete[] should be the // function we just found. else if (isa_and_nonnull<CXXMethodDecl>(OperatorDelete)) UsualArrayDeleteWantsSize = - UsualDeallocFnInfo(*this, - DeclAccessPair::make(OperatorDelete, AS_public), Pointee) - .HasSizeT; + UsualDeallocFnInfo( + *this, DeclAccessPair::make(OperatorDelete, AS_public), + Pointee) + .HasSizeT; } if (!PointeeRD->hasIrrelevantDestructor()) @@ -4030,12 +4051,12 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, bool Overaligned = hasNewExtendedAlignment(*this, Pointee); // Look for a global declaration. - ImplicitDeallocationParameters IDP = { - .PassTypeIdentity = AllowTypeAwareAllocators(), - .PassAlignment = Overaligned, - .PassSize = CanProvideSize - }; - OperatorDelete = FindUsualDeallocationFunction(Pointee, StartLoc, IDP, DeleteName); + ImplicitDeallocationParameters IDP = {.PassTypeIdentity = + AllowTypeAwareAllocators(), + .PassAlignment = Overaligned, + .PassSize = CanProvideSize}; + OperatorDelete = + FindUsualDeallocationFunction(Pointee, StartLoc, IDP, DeleteName); if (!OperatorDelete) return ExprError(); } diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 0b3dee8298..c0cad6f933 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -2065,7 +2065,8 @@ DeclResult Sema::CheckClassTemplate( if (ShouldAddRedecl) NewTemplate->setPreviousDecl(PrevClassTemplate); - if (isStdTypeIdentity && (!StdTypeIdentity || getStdTypeIdentity()->isImplicit())) + if (isStdTypeIdentity && + (!StdTypeIdentity || getStdTypeIdentity()->isImplicit())) StdTypeIdentity = NewTemplate; NewClass->setDescribedClassTemplate(NewTemplate); `````````` </details> https://github.com/llvm/llvm-project/pull/113510 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits