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 @@ -452,6 +452,10 @@ /// The seed used by the randomize structure layout feature. std::string RandstructSeed; + // 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 @@ -443,6 +443,8 @@ VALUE_LANGOPT(FuchsiaAPILevel, 32, 0, "Fuchsia API level") +BENIGN_LANGOPT(CheckNew, 1, 0, "Do not eliminate null pointer checks for storage allocated by new") + #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 @@ -4579,7 +4579,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 : BoolFOption<"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 @@ -275,6 +275,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 @@ -5981,6 +5981,9 @@ options::OPT_fno_emulated_tls); Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); + if (Args.hasFlag(options::OPT_fcheck_new, options::OPT_fno_check_new, false)) + CmdArgs.push_back("-fcheck-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/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -4113,6 +4113,9 @@ if (const Arg *A = Args.getLastArg(OPT_frandomize_layout_seed_EQ)) Opts.RandstructSeed = A->getValue(0); + if (const Arg *A = Args.getLastArg(OPT_fcheck_new)) + Opts.CheckNew = true; + return Diags.getNumErrors() == NumErrorsBefore; } 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 @@ -15436,7 +15436,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 violates 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 @@ -3104,7 +3104,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,10 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py +// RUN: %clang -fcheck-new --target=x86_64-linux-gnu -emit-llvm -S %s -o - | opt -S -mem2reg | FileCheck %s + +// CHECK-LABEL: @_Z5test0v( +// CHECK-NEXT: [[TMP1:%.*]] = call noalias noundef ptr @_Znwm(i64 noundef 4) #[[ATTR2:[0-9]+]] +// CHECK-NEXT: ret ptr [[TMP1]] +// +int *test0() { + 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 @@ -298,7 +298,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 \