From: John McCall Date: Mon, 23 Aug 2010 01:17:59 +0000 (+0000) Subject: Extract a method to check whether a function is the global placement X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5172ed92b42f0bc6a022542a08f7b18af821bcb3;p=clang Extract a method to check whether a function is the global placement operator new[]. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111788 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index feaf467636..35c65ab0e6 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -340,6 +340,24 @@ static CharUnits CalculateCookiePadding(ASTContext &Ctx, QualType ElementType) { Ctx.getTypeAlignInChars(ElementType)); } +/// Check whether the given operator new[] is the global placement +/// operator new[]. +static bool IsPlacementOperatorNewArray(ASTContext &Ctx, + const FunctionDecl *Fn) { + // Must be in global scope. Note that allocation functions can't be + // declared in namespaces. + if (!Fn->getDeclContext()->getLookupContext()->isFileContext()) + return false; + + // Signature must be void *operator new[](size_t, void*). + // The size_t is common to all operator new[]s. + if (Fn->getNumParams() != 2) + return false; + + CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType()); + return (ParamType == Ctx.VoidPtrTy); +} + static CharUnits CalculateCookiePadding(ASTContext &Ctx, const CXXNewExpr *E) { if (!E->isArray()) return CharUnits::Zero(); @@ -347,16 +365,9 @@ static CharUnits CalculateCookiePadding(ASTContext &Ctx, const CXXNewExpr *E) { // No cookie is required if the new operator being used is // ::operator new[](size_t, void*). const FunctionDecl *OperatorNew = E->getOperatorNew(); - if (OperatorNew->getDeclContext()->getLookupContext()->isFileContext()) { - if (OperatorNew->getNumParams() == 2) { - CanQualType ParamType = - Ctx.getCanonicalType(OperatorNew->getParamDecl(1)->getType()); - - if (ParamType == Ctx.VoidPtrTy) - return CharUnits::Zero(); - } - } - + if (IsPlacementOperatorNewArray(Ctx, OperatorNew)) + return CharUnits::Zero(); + return CalculateCookiePadding(Ctx, E->getAllocatedType()); }