From 47f230ba2c8ff0e45e025163862ad699746b0ab0 Mon Sep 17 00:00:00 2001 From: modimo Date: Tue, 30 Nov 2021 17:18:58 -0800 Subject: [PATCH] Add toggling for -fnew-infallible/-fno-new-infallible Allow toggling of -fnew-infallible so last instance takes precedence Testing: ninja check-all Reviewed By: bruno Differential Revision: https://reviews.llvm.org/D113523 --- clang/docs/ClangCommandLineReference.rst | 4 ++-- clang/include/clang/Driver/Options.td | 9 +++++---- clang/lib/Driver/ToolChains/Clang.cpp | 5 ++++- clang/test/CodeGenCXX/new-infallible.cpp | 9 +++++++++ clang/test/Driver/new-infallible.cpp | 5 +++++ 5 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 clang/test/Driver/new-infallible.cpp diff --git a/clang/docs/ClangCommandLineReference.rst b/clang/docs/ClangCommandLineReference.rst index 94eb3fec8a23..8d4ffa6a38fc 100644 --- a/clang/docs/ClangCommandLineReference.rst +++ b/clang/docs/ClangCommandLineReference.rst @@ -1941,9 +1941,9 @@ Microsoft compiler version number to report in \_MSC\_VER (0 = don't define it ( Specifies the largest alignment guaranteed by '::operator new(size\_t)' -.. option:: -fnew-infallible +.. option:: -fnew-infallible, -fno-new-infallible -Treats throwing global C++ operator new as always returning valid memory (annotates with \_\_attribute\_\_((returns\_nonnull)) and throw()). This is detectable in source. +Enable treating throwing global C++ operator new as always returning valid memory (annotates with \_\_attribute\_\_((returns\_nonnull)) and throw()). This is detectable in source. .. option:: -fnext-runtime diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index a6932b3fa4b8..4e6dd2050344 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2789,10 +2789,11 @@ def fvisibility_ms_compat : Flag<["-"], "fvisibility-ms-compat">, Group def fvisibility_global_new_delete_hidden : Flag<["-"], "fvisibility-global-new-delete-hidden">, Group, HelpText<"Give global C++ operator new and delete declarations hidden visibility">, Flags<[CC1Option]>, MarshallingInfoFlag>; -def fnew_infallible : Flag<["-"], "fnew-infallible">, Group, - HelpText<"Treats throwing global C++ operator new as always returning valid memory " - "(annotates with __attribute__((returns_nonnull)) and throw()). This is detectable in source.">, - Flags<[CC1Option]>, MarshallingInfoFlag>; +defm new_infallible : BoolFOption<"new-infallible", + LangOpts<"NewInfallible">, DefaultFalse, + PosFlag, NegFlag, + BothFlags<[CC1Option], " treating throwing global C++ operator new as always returning valid memory " + "(annotates with __attribute__((returns_nonnull)) and throw()). This is detectable in source.">>; defm whole_program_vtables : BoolFOption<"whole-program-vtables", CodeGenOpts<"WholeProgramVTables">, DefaultFalse, PosFlag, diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 5b773621a4af..21b8d730b6bf 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5821,9 +5821,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var, options::OPT_fno_visibility_inlines_hidden_static_local_var); Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden); - Args.AddLastArg(CmdArgs, options::OPT_fnew_infallible); Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); + if (Args.hasFlag(options::OPT_fnew_infallible, + options::OPT_fno_new_infallible, false)) + CmdArgs.push_back("-fnew-infallible"); + if (Args.hasFlag(options::OPT_fno_operator_names, options::OPT_foperator_names, false)) CmdArgs.push_back("-fno-operator-names"); diff --git a/clang/test/CodeGenCXX/new-infallible.cpp b/clang/test/CodeGenCXX/new-infallible.cpp index 94bcc886d396..505aee081c56 100644 --- a/clang/test/CodeGenCXX/new-infallible.cpp +++ b/clang/test/CodeGenCXX/new-infallible.cpp @@ -1,7 +1,16 @@ // RUN: %clang_cc1 -emit-llvm -triple x86_64-linux-gnu -fnew-infallible -o - %s | FileCheck %s +// RUN: %clang_cc1 -emit-llvm -triple x86_64-linux-gnu -fno-new-infallible -fnew-infallible -o - %s | FileCheck %s +// RUN: %clang_cc1 -emit-llvm -triple x86_64-linux-gnu -fno-new-infallible -o - %s | FileCheck %s --check-prefix=NO-NEW-INFALLIBLE +// RUN: %clang_cc1 -emit-llvm -triple x86_64-linux-gnu -fnew-infallible -fno-new-infallible -o - %s | FileCheck %s --check-prefix=NO-NEW-INFALLIBLE // CHECK: call noalias nonnull i8* @_Znwm(i64 4) // CHECK: ; Function Attrs: nobuiltin nounwind allocsize(0) // CHECK-NEXT: declare nonnull i8* @_Znwm(i64) + +// NO-NEW-INFALLIBLE: call noalias nonnull i8* @_Znwm(i64 4) + +// NO-NEW-INFALLIBLE: ; Function Attrs: nobuiltin allocsize(0) +// NO-NEW-INFALLIBLE-NEXT: declare nonnull i8* @_Znwm(i64) + int *new_infallible = new int; diff --git a/clang/test/Driver/new-infallible.cpp b/clang/test/Driver/new-infallible.cpp new file mode 100644 index 000000000000..5a9be234a2f9 --- /dev/null +++ b/clang/test/Driver/new-infallible.cpp @@ -0,0 +1,5 @@ +// RUN: %clang -### -S -fno-new-infallible -fnew-infallible %s 2>&1 | FileCheck --check-prefix=NEW-INFALLIBLE %s +// NEW-INFALLIBLE: "-fnew-infallible" + +// RUN: %clang -### -S -fnew-infallible -fno-new-infallible %s 2>&1 | FileCheck --check-prefix=NO-NEW-INFALLIBLE %s +// NO-NEW-INFALLIBLE-NOT: "-fnew-infallible" \ No newline at end of file