Skip to content

Commit cf5a8b4

Browse files
authored
[clang] Enable sized deallocation by default in C++14 onwards (#83774)
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. Fixes #60061
1 parent 1a343c9 commit cf5a8b4

File tree

42 files changed

+555
-113
lines changed

Some content is hidden

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

42 files changed

+555
-113
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
@@ -90,6 +90,11 @@ C++ Language Changes
9090
--------------------
9191
- Implemented ``_BitInt`` literal suffixes ``__wb`` or ``__WB`` as a Clang extension with ``unsigned`` modifiers also allowed. (#GH85223).
9292

93+
C++14 Feature Support
94+
^^^^^^^^^^^^^^^^^^^^^
95+
- Sized deallocation is enabled by default in C++14 onwards. The user may specify
96+
``-fno-sized-deallocation`` to disable it if there are some regressions.
97+
9398
C++20 Feature Support
9499
^^^^^^^^^^^^^^^^^^^^^
95100

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ class MarshallingInfoVisibility<KeyPathAndMacro kpm, code default>
603603
// Key paths that are constant during parsing of options with the same key path prefix.
604604
defvar cplusplus = LangOpts<"CPlusPlus">;
605605
defvar cpp11 = LangOpts<"CPlusPlus11">;
606+
defvar cpp14 = LangOpts<"CPlusPlus14">;
606607
defvar cpp17 = LangOpts<"CPlusPlus17">;
607608
defvar cpp20 = LangOpts<"CPlusPlus20">;
608609
defvar c99 = LangOpts<"C99">;
@@ -3370,10 +3371,9 @@ defm relaxed_template_template_args : BoolFOption<"relaxed-template-template-arg
33703371
"Enable C++17 relaxed template template argument matching">,
33713372
NegFlag<SetFalse>>;
33723373
defm sized_deallocation : BoolFOption<"sized-deallocation",
3373-
LangOpts<"SizedDeallocation">, DefaultFalse,
3374-
PosFlag<SetTrue, [], [ClangOption, CC1Option],
3375-
"Enable C++14 sized global deallocation functions">,
3376-
NegFlag<SetFalse>>;
3374+
LangOpts<"SizedDeallocation">, Default<cpp14.KeyPath>,
3375+
PosFlag<SetTrue, [], [], "Enable C++14 sized global deallocation functions">,
3376+
NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;
33773377
defm aligned_allocation : BoolFOption<"aligned-allocation",
33783378
LangOpts<"AlignedAllocation">, Default<cpp17.KeyPath>,
33793379
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
@@ -7262,10 +7262,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
72627262
Args.addOptInFlag(CmdArgs, options::OPT_frelaxed_template_template_args,
72637263
options::OPT_fno_relaxed_template_template_args);
72647264

7265-
// -fsized-deallocation is off by default, as it is an ABI-breaking change for
7266-
// most platforms.
7267-
Args.addOptInFlag(CmdArgs, options::OPT_fsized_deallocation,
7268-
options::OPT_fno_sized_deallocation);
7265+
// -fsized-deallocation is on by default in C++14 onwards and otherwise off
7266+
// by default.
7267+
if (Arg *A = Args.getLastArg(options::OPT_fsized_deallocation,
7268+
options::OPT_fno_sized_deallocation)) {
7269+
if (A->getOption().matches(options::OPT_fno_sized_deallocation))
7270+
CmdArgs.push_back("-fno-sized-deallocation");
7271+
else
7272+
CmdArgs.push_back("-fsized-deallocation");
7273+
}
72697274

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

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,16 +2912,68 @@ static bool sdkSupportsBuiltinModules(const Darwin::DarwinPlatformKind &TargetPl
29122912
}
29132913
}
29142914

2915-
void Darwin::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
2916-
llvm::opt::ArgStringList &CC1Args,
2917-
Action::OffloadKind DeviceOffloadKind) const {
2915+
static inline llvm::VersionTuple
2916+
sizedDeallocMinVersion(llvm::Triple::OSType OS) {
2917+
switch (OS) {
2918+
default:
2919+
break;
2920+
case llvm::Triple::Darwin:
2921+
case llvm::Triple::MacOSX: // Earliest supporting version is 10.12.
2922+
return llvm::VersionTuple(10U, 12U);
2923+
case llvm::Triple::IOS:
2924+
case llvm::Triple::TvOS: // Earliest supporting version is 10.0.0.
2925+
return llvm::VersionTuple(10U);
2926+
case llvm::Triple::WatchOS: // Earliest supporting version is 3.0.0.
2927+
return llvm::VersionTuple(3U);
2928+
}
2929+
2930+
llvm_unreachable("Unexpected OS");
2931+
}
2932+
2933+
bool Darwin::isSizedDeallocationUnavailable() const {
2934+
llvm::Triple::OSType OS;
2935+
2936+
if (isTargetMacCatalyst())
2937+
return TargetVersion < sizedDeallocMinVersion(llvm::Triple::MacOSX);
2938+
switch (TargetPlatform) {
2939+
case MacOS: // Earlier than 10.12.
2940+
OS = llvm::Triple::MacOSX;
2941+
break;
2942+
case IPhoneOS:
2943+
OS = llvm::Triple::IOS;
2944+
break;
2945+
case TvOS: // Earlier than 10.0.
2946+
OS = llvm::Triple::TvOS;
2947+
break;
2948+
case WatchOS: // Earlier than 3.0.
2949+
OS = llvm::Triple::WatchOS;
2950+
break;
2951+
case DriverKit:
2952+
case XROS:
2953+
// Always available.
2954+
return false;
2955+
}
2956+
2957+
return TargetVersion < sizedDeallocMinVersion(OS);
2958+
}
2959+
2960+
void Darwin::addClangTargetOptions(
2961+
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
2962+
Action::OffloadKind DeviceOffloadKind) const {
29182963
// Pass "-faligned-alloc-unavailable" only when the user hasn't manually
29192964
// enabled or disabled aligned allocations.
29202965
if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation,
29212966
options::OPT_fno_aligned_allocation) &&
29222967
isAlignedAllocationUnavailable())
29232968
CC1Args.push_back("-faligned-alloc-unavailable");
29242969

2970+
// Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
2971+
// or disabled sized deallocations.
2972+
if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation,
2973+
options::OPT_fno_sized_deallocation) &&
2974+
isSizedDeallocationUnavailable())
2975+
CC1Args.push_back("-fno-sized-deallocation");
2976+
29252977
addClangCC1ASTargetOptions(DriverArgs, CC1Args);
29262978

29272979
// 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)