diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -487,6 +487,10 @@ /// forward slash (/) elsewhere. bool UseTargetPathSeparator = false; + // Indicates whether we should keep all nullptr checks for pointers + // received as a result of a standard operator new (-fcheck-new) + bool CheckNew = false; + LangOptions(); /// Set language defaults for the given input language and diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -466,6 +466,8 @@ "avoid tearing the Lexer and etc. down). Controlled by " "-fincremental-extensions.") +BENIGN_LANGOPT(CheckNew, 1, 0, "Do not assume C++ operator new may not return NULL") + #undef LANGOPT #undef COMPATIBLE_LANGOPT #undef BENIGN_LANGOPT diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5038,7 +5038,11 @@ // ignore it for now to avoid breaking builds that use it. def fdiagnostics_show_location_EQ : Joined<["-"], "fdiagnostics-show-location=">, Group; -defm fcheck_new : BooleanFFlag<"check-new">, Group; +defm check_new : BoolOption<"f", "check-new", + LangOpts<"CheckNew">, DefaultFalse, + PosFlag, + NegFlag, BothFlags<[CC1Option]>>; + defm caller_saves : BooleanFFlag<"caller-saves">, Group; defm reorder_blocks : BooleanFFlag<"reorder-blocks">, Group; defm branch_count_reg : BooleanFFlag<"branch-count-reg">, Group; diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -276,6 +276,8 @@ } bool CXXNewExpr::shouldNullCheckAllocation() const { + if (getOperatorNew()->getLangOpts().CheckNew) + return true; return !getOperatorNew()->hasAttr() && getOperatorNew() ->getType() diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6149,6 +6149,9 @@ Triple.hasDefaultEmulatedTLS())) CmdArgs.push_back("-femulated-tls"); + Args.addOptInFlag(CmdArgs, options::OPT_fcheck_new, + options::OPT_fno_check_new); + if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) { // FIXME: There's no reason for this to be restricted to X86. The backend // code needs to be changed to include the appropriate function calls diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -16127,7 +16127,11 @@ // indicates failure by returning a null pointer value. Any other allocation // function never returns a null pointer value and indicates failure only by // throwing an exception [...] - if (!IsNothrow && !FD->hasAttr()) + // + // However, -fcheck-new invalidates this possible assumption, so don't add + // NonNull when that is enabled. + if (!IsNothrow && !FD->hasAttr() && + !getLangOpts().CheckNew) FD->addAttr(ReturnsNonNullAttr::CreateImplicit(Context, FD->getLocation())); // C++2a [basic.stc.dynamic.allocation]p2: diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -3157,7 +3157,8 @@ // Global allocation functions should always be visible. Alloc->setVisibleDespiteOwningModule(); - if (HasBadAllocExceptionSpec && getLangOpts().NewInfallible) + if (HasBadAllocExceptionSpec && getLangOpts().NewInfallible && + !getLangOpts().CheckNew) Alloc->addAttr( ReturnsNonNullAttr::CreateImplicit(Context, Alloc->getLocation())); diff --git a/clang/test/CodeGenCXX/fcheck-new.cpp b/clang/test/CodeGenCXX/fcheck-new.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGenCXX/fcheck-new.cpp @@ -0,0 +1,31 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py +// RUN: %clang_cc1 -fcheck-new -triple x86_64-linux-gnu -disable-O0-optnone \ +// RUN: -emit-llvm -o - %s | FileCheck %s +// + +struct A { A(); }; + +// CHECK-LABEL: @_Z5test0v( +// CHECK-NEXT: entry: +// CHECK-NEXT: [[CALL:%.*]] = call noalias noundef ptr @_Znwm(i64 noundef 1) +// CHECK-NEXT: [[NEW_ISNULL:%.*]] = icmp eq ptr [[CALL]], null +// CHECK-NEXT: br i1 [[NEW_ISNULL]], label [[NEW_CONT:%.*]], label [[NEW_NOTNULL:%.*]] +// CHECK: new.notnull: +// CHECK-NEXT: call void @_ZN1AC1Ev(ptr noundef nonnull align 1 dereferenceable(1) [[CALL]]) +// CHECK-NEXT: br label [[NEW_CONT]] +// CHECK: new.cont: +// CHECK-NEXT: [[TMP0:%.*]] = phi ptr [ [[CALL]], [[NEW_NOTNULL]] ], [ null, [[ENTRY:%.*]] ] +// CHECK-NEXT: ret ptr [[TMP0]] +// +A *test0() { + return new A(); +} + +// CHECK-LABEL: @_Z5test1v( +// CHECK-NEXT: entry: +// CHECK-NEXT: [[CALL:%.*]] = call noalias noundef ptr @_Znwm(i64 noundef 4) +// CHECK-NEXT: ret ptr [[CALL]] +// +int *test1() { + return new int; +} diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c --- a/clang/test/Driver/clang_f_opts.c +++ b/clang/test/Driver/clang_f_opts.c @@ -302,7 +302,6 @@ // RUN: -fno-reorder-blocks -freorder-blocks \ // RUN: -fno-schedule-insns2 -fschedule-insns2 \ // RUN: -fno-stack-check \ -// RUN: -fno-check-new -fcheck-new \ // RUN: -ffriend-injection \ // RUN: -fno-implement-inlines -fimplement-inlines \ // RUN: -fstack-check \