Skip to content

Commit cce93e3

Browse files
wangpc-ppvitalybuka
authored andcommitted
Reland "[clang] Enable sized deallocation by default in C++14 onwards" (#90373)
Since C++14 has been released for about nine years and most standard libraries have implemented sized deallocation functions, it's time to make this feature default again. This is another try of https://reviews.llvm.org/D112921. The original commit cf5a8b4 was reverted by 2e5035a due to some failures (see #83774). Fixes #60061
1 parent 768118d commit cce93e3

File tree

41 files changed

+513
-104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+513
-104
lines changed

clang-tools-extra/clangd/unittests/FindTargetTests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,9 @@ TEST_F(TargetDeclTest, OverloadExpr) {
839839
[[delete]] x;
840840
}
841841
)cpp";
842-
EXPECT_DECLS("CXXDeleteExpr", "void operator delete(void *) noexcept");
842+
// Sized deallocation is enabled by default in C++14 onwards.
843+
EXPECT_DECLS("CXXDeleteExpr",
844+
"void operator delete(void *, unsigned long) noexcept");
843845
}
844846

845847
TEST_F(TargetDeclTest, DependentExprs) {

clang-tools-extra/test/clang-tidy/checkers/misc/new-delete-overloads.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ struct S {
1212
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: declaration of 'operator new' has no matching declaration of 'operator delete' at the same scope
1313
void *operator new(size_t size) noexcept(false);
1414

15-
struct T {
16-
// Sized deallocations are not enabled by default, and so this new/delete pair
17-
// does not match. However, we expect only one warning, for the new, because
18-
// the operator delete is a placement delete and we do not warn on mismatching
19-
// placement operations.
20-
// CHECK-MESSAGES: :[[@LINE+1]]:9: warning: declaration of 'operator new' has no matching declaration of 'operator delete' at the same scope
21-
void *operator new(size_t size) noexcept;
22-
void operator delete(void *ptr, size_t) noexcept; // ok only if sized deallocation is enabled
23-
};
24-
2515
struct U {
2616
void *operator new(size_t size) noexcept;
2717
void operator delete(void *ptr) noexcept;

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ sections with improvements to Clang's support for those languages.
203203
C++ Language Changes
204204
--------------------
205205

206+
C++14 Feature Support
207+
^^^^^^^^^^^^^^^^^^^^^
208+
- Sized deallocation is enabled by default in C++14 onwards. The user may specify
209+
``-fno-sized-deallocation`` to disable it if there are some regressions.
210+
206211
C++20 Feature Support
207212
^^^^^^^^^^^^^^^^^^^^^
208213
- Implemented `P1907R1 <https://wg21.link/P1907R1>`_ which extends allowed non-type template argument

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ class MarshallingInfoVisibility<KeyPathAndMacro kpm, code default>
569569
// Key paths that are constant during parsing of options with the same key path prefix.
570570
defvar cplusplus = LangOpts<"CPlusPlus">;
571571
defvar cpp11 = LangOpts<"CPlusPlus11">;
572+
defvar cpp14 = LangOpts<"CPlusPlus14">;
572573
defvar cpp17 = LangOpts<"CPlusPlus17">;
573574
defvar cpp20 = LangOpts<"CPlusPlus20">;
574575
defvar c99 = LangOpts<"C99">;
@@ -3293,10 +3294,9 @@ defm relaxed_template_template_args : BoolFOption<"relaxed-template-template-arg
32933294
"Enable C++17 relaxed template template argument matching">,
32943295
NegFlag<SetFalse>>;
32953296
defm sized_deallocation : BoolFOption<"sized-deallocation",
3296-
LangOpts<"SizedDeallocation">, DefaultFalse,
3297-
PosFlag<SetTrue, [], [ClangOption, CC1Option],
3298-
"Enable C++14 sized global deallocation functions">,
3299-
NegFlag<SetFalse>>;
3297+
LangOpts<"SizedDeallocation">, Default<cpp14.KeyPath>,
3298+
PosFlag<SetTrue, [], [], "Enable C++14 sized global deallocation functions">,
3299+
NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;
33003300
defm aligned_allocation : BoolFOption<"aligned-allocation",
33013301
LangOpts<"AlignedAllocation">, Default<cpp17.KeyPath>,
33023302
PosFlag<SetTrue, [], [ClangOption], "Enable C++17 aligned allocation functions">,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7089,10 +7089,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
70897089
Args.addOptInFlag(CmdArgs, options::OPT_frelaxed_template_template_args,
70907090
options::OPT_fno_relaxed_template_template_args);
70917091

7092-
// -fsized-deallocation is off by default, as it is an ABI-breaking change for
7093-
// most platforms.
7094-
Args.addOptInFlag(CmdArgs, options::OPT_fsized_deallocation,
7095-
options::OPT_fno_sized_deallocation);
7092+
// -fsized-deallocation is on by default in C++14 onwards and otherwise off
7093+
// by default.
7094+
if (Arg *A = Args.getLastArg(options::OPT_fsized_deallocation,
7095+
options::OPT_fno_sized_deallocation)) {
7096+
if (A->getOption().matches(options::OPT_fno_sized_deallocation))
7097+
CmdArgs.push_back("-fno-sized-deallocation");
7098+
else
7099+
CmdArgs.push_back("-fsized-deallocation");
7100+
}
70967101

70977102
// -faligned-allocation is on by default in C++17 onwards and otherwise off
70987103
// by default.

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,16 +2932,68 @@ static bool sdkSupportsBuiltinModules(const Darwin::DarwinPlatformKind &TargetPl
29322932
}
29332933
}
29342934

2935-
void Darwin::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
2936-
llvm::opt::ArgStringList &CC1Args,
2937-
Action::OffloadKind DeviceOffloadKind) const {
2935+
static inline llvm::VersionTuple
2936+
sizedDeallocMinVersion(llvm::Triple::OSType OS) {
2937+
switch (OS) {
2938+
default:
2939+
break;
2940+
case llvm::Triple::Darwin:
2941+
case llvm::Triple::MacOSX: // Earliest supporting version is 10.12.
2942+
return llvm::VersionTuple(10U, 12U);
2943+
case llvm::Triple::IOS:
2944+
case llvm::Triple::TvOS: // Earliest supporting version is 10.0.0.
2945+
return llvm::VersionTuple(10U);
2946+
case llvm::Triple::WatchOS: // Earliest supporting version is 3.0.0.
2947+
return llvm::VersionTuple(3U);
2948+
}
2949+
2950+
llvm_unreachable("Unexpected OS");
2951+
}
2952+
2953+
bool Darwin::isSizedDeallocationUnavailable() const {
2954+
llvm::Triple::OSType OS;
2955+
2956+
if (isTargetMacCatalyst())
2957+
return TargetVersion < sizedDeallocMinVersion(llvm::Triple::MacOSX);
2958+
switch (TargetPlatform) {
2959+
case MacOS: // Earlier than 10.12.
2960+
OS = llvm::Triple::MacOSX;
2961+
break;
2962+
case IPhoneOS:
2963+
OS = llvm::Triple::IOS;
2964+
break;
2965+
case TvOS: // Earlier than 10.0.
2966+
OS = llvm::Triple::TvOS;
2967+
break;
2968+
case WatchOS: // Earlier than 3.0.
2969+
OS = llvm::Triple::WatchOS;
2970+
break;
2971+
case DriverKit:
2972+
case XROS:
2973+
// Always available.
2974+
return false;
2975+
}
2976+
2977+
return TargetVersion < sizedDeallocMinVersion(OS);
2978+
}
2979+
2980+
void Darwin::addClangTargetOptions(
2981+
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
2982+
Action::OffloadKind DeviceOffloadKind) const {
29382983
// Pass "-faligned-alloc-unavailable" only when the user hasn't manually
29392984
// enabled or disabled aligned allocations.
29402985
if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation,
29412986
options::OPT_fno_aligned_allocation) &&
29422987
isAlignedAllocationUnavailable())
29432988
CC1Args.push_back("-faligned-alloc-unavailable");
29442989

2990+
// Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
2991+
// or disabled sized deallocations.
2992+
if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation,
2993+
options::OPT_fno_sized_deallocation) &&
2994+
isSizedDeallocationUnavailable())
2995+
CC1Args.push_back("-fno-sized-deallocation");
2996+
29452997
addClangCC1ASTargetOptions(DriverArgs, CC1Args);
29462998

29472999
// Enable compatibility mode for NSItemProviderCompletionHandler in

clang/lib/Driver/ToolChains/Darwin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,10 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
511511
/// targeting.
512512
bool isAlignedAllocationUnavailable() const;
513513

514+
/// Return true if c++14 sized deallocation functions are not implemented in
515+
/// the c++ standard library of the deployment target we are targeting.
516+
bool isSizedDeallocationUnavailable() const;
517+
514518
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
515519
llvm::opt::ArgStringList &CC1Args,
516520
Action::OffloadKind DeviceOffloadKind) const override;

clang/lib/Driver/ToolChains/ZOS.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ void ZOS::addClangTargetOptions(const ArgList &DriverArgs,
3636
if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation,
3737
options::OPT_fno_aligned_allocation))
3838
CC1Args.push_back("-faligned-alloc-unavailable");
39+
40+
// Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
41+
// or disabled sized deallocations.
42+
if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation,
43+
options::OPT_fno_sized_deallocation))
44+
CC1Args.push_back("-fno-sized-deallocation");
3945
}
4046

4147
void zos::Assembler::ConstructJob(Compilation &C, const JobAction &JA,

clang/test/AST/ast-dump-expr-json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,7 @@ void TestNonADLCall3() {
23332333
// CHECK-NEXT: "kind": "FunctionDecl",
23342334
// CHECK-NEXT: "name": "operator delete",
23352335
// CHECK-NEXT: "type": {
2336-
// CHECK-NEXT: "qualType": "void (void *) noexcept"
2336+
// CHECK-NEXT: "qualType": "void (void *, unsigned long) noexcept"
23372337
// CHECK-NEXT: }
23382338
// CHECK-NEXT: },
23392339
// CHECK-NEXT: "inner": [

clang/test/AST/ast-dump-expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void UnaryExpressions(int *p) {
164164
// CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:8> 'int *' lvalue ParmVar 0x{{[^ ]*}} 'p' 'int *'
165165

166166
::delete p;
167-
// CHECK: CXXDeleteExpr 0x{{[^ ]*}} <line:[[@LINE-1]]:3, col:12> 'void' global Function 0x{{[^ ]*}} 'operator delete' 'void (void *) noexcept'
167+
// CHECK: CXXDeleteExpr 0x{{[^ ]*}} <line:[[@LINE-1]]:3, col:12> 'void' global Function 0x{{[^ ]*}} 'operator delete' 'void (void *, unsigned long) noexcept'
168168
// CHECK-NEXT: ImplicitCastExpr
169169
// CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:12> 'int *' lvalue ParmVar 0x{{[^ ]*}} 'p' 'int *'
170170

0 commit comments

Comments
 (0)