Skip to content

Commit e5015ab

Browse files
committedAug 3, 2018
[Preprocessor] Allow libc++ to detect when aligned allocation is unavailable.
Libc++ needs to know when aligned allocation is supported by clang, but is otherwise unavailable at link time. Otherwise, libc++ will incorrectly end up generating calls to `__builtin_operator_new`/`__builtin_operator_delete` with alignment arguments. This patch implements the following changes: * The `__cpp_aligned_new` feature test macro to no longer be defined when aligned allocation is otherwise enabled but unavailable. * The Darwin driver no longer passes `-faligned-alloc-unavailable` when the user manually specifies `-faligned-allocation` or `-fno-aligned-allocation`. * Instead of a warning Clang now generates a hard error when an aligned allocation or deallocation function is referenced but unavailable. Patch by Eric Fiselier. Reviewers: rsmith, vsapsai, erik.pilkington, ahatanak, dexonsmith Reviewed By: rsmith Subscribers: Quuxplusone, cfe-commits Differential Revision: https://reviews.llvm.org/D45015 llvm-svn: 338934
1 parent 7c3492b commit e5015ab

File tree

7 files changed

+44
-9
lines changed

7 files changed

+44
-9
lines changed
 

Diff for: ‎clang/include/clang/Basic/DiagnosticGroups.td

-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ def NonVirtualDtor : DiagGroup<"non-virtual-dtor">;
364364
def NullPointerArithmetic : DiagGroup<"null-pointer-arithmetic">;
365365
def : DiagGroup<"effc++", [NonVirtualDtor]>;
366366
def OveralignedType : DiagGroup<"over-aligned">;
367-
def AlignedAllocationUnavailable : DiagGroup<"aligned-allocation-unavailable">;
368367
def OldStyleCast : DiagGroup<"old-style-cast">;
369368
def : DiagGroup<"old-style-definition">;
370369
def OutOfLineDeclaration : DiagGroup<"out-of-line-declaration">;

Diff for: ‎clang/include/clang/Basic/DiagnosticSemaKinds.td

+3-3
Original file line numberDiff line numberDiff line change
@@ -6465,12 +6465,12 @@ def warn_overaligned_type : Warning<
64656465
"type %0 requires %1 bytes of alignment and the default allocator only "
64666466
"guarantees %2 bytes">,
64676467
InGroup<OveralignedType>, DefaultIgnore;
6468-
def warn_aligned_allocation_unavailable :Warning<
6468+
def err_aligned_allocation_unavailable : Error<
64696469
"aligned %select{allocation|deallocation}0 function of type '%1' is only "
6470-
"available on %2 %3 or newer">, InGroup<AlignedAllocationUnavailable>, DefaultError;
6470+
"available on %2 %3 or newer">;
64716471
def note_silence_unligned_allocation_unavailable : Note<
64726472
"if you supply your own aligned allocation functions, use "
6473-
"-Wno-aligned-allocation-unavailable to silence this diagnostic">;
6473+
"-faligned-allocation to silence this diagnostic">;
64746474

64756475
def err_conditional_void_nonvoid : Error<
64766476
"%select{left|right}1 operand to ? is void, but %select{right|left}1 operand "

Diff for: ‎clang/lib/Driver/ToolChains/Darwin.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,11 @@ bool Darwin::isAlignedAllocationUnavailable() const {
20352035
void Darwin::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
20362036
llvm::opt::ArgStringList &CC1Args,
20372037
Action::OffloadKind DeviceOffloadKind) const {
2038-
if (isAlignedAllocationUnavailable())
2038+
// Pass "-faligned-alloc-unavailable" only when the user hasn't manually
2039+
// enabled or disabled aligned allocations.
2040+
if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation,
2041+
options::OPT_fno_aligned_allocation) &&
2042+
isAlignedAllocationUnavailable())
20392043
CC1Args.push_back("-faligned-alloc-unavailable");
20402044
}
20412045

Diff for: ‎clang/lib/Frontend/InitPreprocessor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
553553
Builder.defineMacro("__cpp_guaranteed_copy_elision", "201606L");
554554
Builder.defineMacro("__cpp_nontype_template_parameter_auto", "201606L");
555555
}
556-
if (LangOpts.AlignedAllocation)
556+
if (LangOpts.AlignedAllocation && !LangOpts.AlignedAllocationUnavailable)
557557
Builder.defineMacro("__cpp_aligned_new", "201606L");
558558
if (LangOpts.RelaxedTemplateTemplateArgs)
559559
Builder.defineMacro("__cpp_template_template_args", "201611L");

Diff for: ‎clang/lib/Sema/SemaExprCXX.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1741,9 +1741,9 @@ static void diagnoseUnavailableAlignedAllocation(const FunctionDecl &FD,
17411741
StringRef OSName = AvailabilityAttr::getPlatformNameSourceSpelling(
17421742
S.getASTContext().getTargetInfo().getPlatformName());
17431743

1744-
S.Diag(Loc, diag::warn_aligned_allocation_unavailable)
1745-
<< IsDelete << FD.getType().getAsString() << OSName
1746-
<< alignedAllocMinVersion(T.getOS()).getAsString();
1744+
S.Diag(Loc, diag::err_aligned_allocation_unavailable)
1745+
<< IsDelete << FD.getType().getAsString() << OSName
1746+
<< alignedAllocMinVersion(T.getOS()).getAsString();
17471747
S.Diag(Loc, diag::note_silence_unligned_allocation_unavailable);
17481748
}
17491749
}

Diff for: ‎clang/test/Driver/unavailable_aligned_allocation.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,13 @@
5151
// RUN: -c -### %s 2>&1 \
5252
// RUN: | FileCheck %s -check-prefix=AVAILABLE
5353
//
54+
// Check that passing -faligned-allocation or -fno-aligned-allocation stops the
55+
// driver from passing -faligned-alloc-unavailable to cc1.
56+
//
57+
// RUN: %clang -target x86_64-apple-macosx10.12 -faligned-allocation -c -### %s 2>&1 \
58+
// RUN: | FileCheck %s -check-prefix=AVAILABLE
59+
//
60+
// RUN: %clang -target x86_64-apple-macosx10.12 -fno-aligned-allocation -c -### %s 2>&1 \
61+
// RUN: | FileCheck %s -check-prefix=AVAILABLE
62+
5463
// AVAILABLE-NOT: "-faligned-alloc-unavailable"

Diff for: ‎clang/test/Lexer/aligned-allocation.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.12.0 -fexceptions -std=c++17 -verify %s \
2+
// RUN: -DEXPECT_DEFINED
3+
//
4+
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.12.0 -fexceptions -std=c++17 -verify %s \
5+
// RUN: -faligned-alloc-unavailable
6+
//
7+
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.12.0 -fexceptions -std=c++17 -verify %s \
8+
// RUN: -faligned-allocation -faligned-alloc-unavailable
9+
10+
// Test that __cpp_aligned_new is not defined when CC1 is passed
11+
// -faligned-alloc-unavailable by the Darwin driver, even when aligned
12+
// allocation is actually enabled.
13+
14+
// expected-no-diagnostics
15+
#ifdef EXPECT_DEFINED
16+
# ifndef __cpp_aligned_new
17+
# error "__cpp_aligned_new" should be defined
18+
# endif
19+
#else
20+
# ifdef __cpp_aligned_new
21+
# error "__cpp_aligned_new" should not be defined
22+
# endif
23+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.