From 351241c83ea67cf4cc31b3e30b952ec45c9736e1 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 7 Apr 2016 21:46:12 +0000 Subject: Replace Sema-level implementation of -fassume-sane-operator-new with a CodeGen-level implementation. Instead of adding an attribute to clang's FunctionDecl, add the IR attribute directly. This means a module built with this flag is now compatible with code built without it and vice versa. This change also results in the 'noalias' attribute no longer being added to calls to operator new in the IR; it's now only added to the declaration. It also fixes a bug where we failed to add the attribute to the 'nothrow' versions (because we didn't implicitly declare them, there was no good time to inject a fake attribute). llvm-svn: 265728 --- clang/lib/CodeGen/CodeGenModule.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index d70c05a..db1ed3c 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1076,12 +1076,22 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, if (const SectionAttr *SA = FD->getAttr()) F->setSection(SA->getName()); - // A replaceable global allocation function does not act like a builtin by - // default, only if it is invoked by a new-expression or delete-expression. - if (FD->isReplaceableGlobalAllocationFunction()) + if (FD->isReplaceableGlobalAllocationFunction()) { + // A replaceable global allocation function does not act like a builtin by + // default, only if it is invoked by a new-expression or delete-expression. F->addAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::NoBuiltin); + // A sane operator new returns a non-aliasing pointer. + // FIXME: Also add NonNull attribute to the return value + // for the non-nothrow forms? + auto Kind = FD->getDeclName().getCXXOverloadedOperator(); + if (getCodeGenOpts().AssumeSaneOperatorNew && + (Kind == OO_New || Kind == OO_Array_New)) + F->addAttribute(llvm::AttributeSet::ReturnIndex, + llvm::Attribute::NoAlias); + } + if (isa(FD) || isa(FD)) F->setUnnamedAddr(true); else if (const auto *MD = dyn_cast(FD)) -- cgit v1.1