Skip to content

Commit a18f57e

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

File tree

42 files changed

+523
-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

+523
-113
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,9 @@ TEST_F(TargetDeclTest, OverloadExpr) {
836836
[[delete]] x;
837837
}
838838
)cpp";
839-
EXPECT_DECLS("CXXDeleteExpr", "void operator delete(void *) noexcept");
839+
// Sized deallocation is enabled by default in C++14 onwards.
840+
EXPECT_DECLS("CXXDeleteExpr",
841+
"void operator delete(void *, unsigned long) noexcept");
840842
}
841843

842844
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
@@ -155,6 +155,11 @@ C++17 Feature Support
155155
files because they may not be stable across multiple TUs (the values may vary
156156
based on compiler version as well as CPU tuning). #GH60174
157157

158+
C++14 Feature Support
159+
^^^^^^^^^^^^^^^^^^^^^
160+
- Sized deallocation is enabled by default in C++14 onwards. The user may specify
161+
``-fno-sized-deallocation`` to disable it if there are some regressions.
162+
158163
C++20 Feature Support
159164
^^^^^^^^^^^^^^^^^^^^^
160165

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">;
@@ -3388,10 +3389,9 @@ defm relaxed_template_template_args : BoolFOption<"relaxed-template-template-arg
33883389
NegFlag<SetFalse, [], [CC1Option], "Disable">,
33893390
BothFlags<[], [ClangOption], " C++17 relaxed template template argument matching">>;
33903391
defm sized_deallocation : BoolFOption<"sized-deallocation",
3391-
LangOpts<"SizedDeallocation">, DefaultFalse,
3392-
PosFlag<SetTrue, [], [ClangOption, CC1Option],
3393-
"Enable C++14 sized global deallocation functions">,
3394-
NegFlag<SetFalse>>;
3392+
LangOpts<"SizedDeallocation">, Default<cpp14.KeyPath>,
3393+
PosFlag<SetTrue, [], [], "Enable C++14 sized global deallocation functions">,
3394+
NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;
33953395
defm aligned_allocation : BoolFOption<"aligned-allocation",
33963396
LangOpts<"AlignedAllocation">, Default<cpp17.KeyPath>,
33973397
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
@@ -7263,10 +7263,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
72637263
}
72647264
}
72657265

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

72717276
// -faligned-allocation is on by default in C++17 onwards and otherwise off
72727277
// 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)