From: Douglas Gregor Date: Wed, 18 Aug 2010 15:06:25 +0000 (+0000) Subject: Make sure to add MallocAttr to explicitly-declared operator new/new[] X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7b86862ff555a9d848ac7abf6042d192b6d5a04d;p=clang Make sure to add MallocAttr to explicitly-declared operator new/new[] when -fassume-sane-operator-new. Patch by Tom Jablin! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111363 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 5e46090c05..8599a146c6 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -1257,8 +1257,11 @@ void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, Context.getCanonicalType( Func->getParamDecl(0)->getType().getUnqualifiedType()); // FIXME: Do we need to check for default arguments here? - if (Func->getNumParams() == 1 && InitialParamType == Argument) + if (Func->getNumParams() == 1 && InitialParamType == Argument) { + if(AddMallocAttr && !Func->hasAttr()) + Func->addAttr(::new (Context) MallocAttr()); return; + } } } } diff --git a/test/CodeGenCXX/new.cpp b/test/CodeGenCXX/new.cpp index 3f22e89f2a..61a5a153ca 100644 --- a/test/CodeGenCXX/new.cpp +++ b/test/CodeGenCXX/new.cpp @@ -73,6 +73,10 @@ void t8(int n) { new U[n]; } +// noalias +// CHECK: declare noalias i8* @_Znam +void *operator new[](size_t); + void t9() { bool b;