From 6dabc75940d66b32052fc93d1c9904e5fd65fcad Mon Sep 17 00:00:00 2001 From: JF Bastien Date: Mon, 29 Jul 2019 23:12:48 +0000 Subject: [PATCH] [NFC] avoid AlignedCharArray in clang As discussed in D65249, don't use AlignedCharArray or std::aligned_storage. Just use alignas(X) char Buf[Size];. This will allow me to remove AlignedCharArray entirely, and works on the current minimum version of Visual Studio. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@367274 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Expr.h | 5 ++--- include/clang/Sema/Overload.h | 4 ++-- lib/CodeGen/CGCleanup.cpp | 7 ++++--- .../linux/DirectoryWatcher-linux.cpp | 11 ++++++----- lib/Sema/SemaOverload.cpp | 5 ++--- lib/Sema/TypeLocBuilder.cpp | 2 +- lib/Sema/TypeLocBuilder.h | 12 +++++------- 7 files changed, 22 insertions(+), 24 deletions(-) diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index d44a815c86..e6b16455c3 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -2619,9 +2619,8 @@ public: /// + sizeof(Stmt *) bytes of storage, aligned to alignof(CallExpr): /// /// \code{.cpp} - /// llvm::AlignedCharArray Buffer; - /// CallExpr *TheCall = CallExpr::CreateTemporary(Buffer.buffer, etc); + /// alignas(CallExpr) char Buffer[sizeof(CallExpr) + sizeof(Stmt *)]; + /// CallExpr *TheCall = CallExpr::CreateTemporary(Buffer, etc); /// \endcode static CallExpr *CreateTemporary(void *Mem, Expr *Fn, QualType Ty, ExprValueKind VK, SourceLocation RParenLoc, diff --git a/include/clang/Sema/Overload.h b/include/clang/Sema/Overload.h index 96aadeac2b..e9280103a3 100644 --- a/include/clang/Sema/Overload.h +++ b/include/clang/Sema/Overload.h @@ -881,7 +881,7 @@ class Sema; constexpr static unsigned NumInlineBytes = 24 * sizeof(ImplicitConversionSequence); unsigned NumInlineBytesUsed = 0; - llvm::AlignedCharArray InlineSpace; + alignas(void *) char InlineSpace[NumInlineBytes]; // Address space of the object being constructed. LangAS DestAS = LangAS::Default; @@ -904,7 +904,7 @@ class Sema; unsigned NBytes = sizeof(T) * N; if (NBytes > NumInlineBytes - NumInlineBytesUsed) return SlabAllocator.Allocate(N); - char *FreeSpaceStart = InlineSpace.buffer + NumInlineBytesUsed; + char *FreeSpaceStart = InlineSpace + NumInlineBytesUsed; assert(uintptr_t(FreeSpaceStart) % alignof(void *) == 0 && "Misaligned storage!"); diff --git a/lib/CodeGen/CGCleanup.cpp b/lib/CodeGen/CGCleanup.cpp index 5594f30302..3cf366e463 100644 --- a/lib/CodeGen/CGCleanup.cpp +++ b/lib/CodeGen/CGCleanup.cpp @@ -740,14 +740,15 @@ void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) { // here. Unfortunately, if you ask for a SmallVector, the // alignment isn't sufficient. auto *CleanupSource = reinterpret_cast(Scope.getCleanupBuffer()); - llvm::AlignedCharArray CleanupBufferStack; + alignas(EHScopeStack::ScopeStackAlignment) char + CleanupBufferStack[8 * sizeof(void *)]; std::unique_ptr CleanupBufferHeap; size_t CleanupSize = Scope.getCleanupSize(); EHScopeStack::Cleanup *Fn; if (CleanupSize <= sizeof(CleanupBufferStack)) { - memcpy(CleanupBufferStack.buffer, CleanupSource, CleanupSize); - Fn = reinterpret_cast(CleanupBufferStack.buffer); + memcpy(CleanupBufferStack, CleanupSource, CleanupSize); + Fn = reinterpret_cast(CleanupBufferStack); } else { CleanupBufferHeap.reset(new char[CleanupSize]); memcpy(CleanupBufferHeap.get(), CleanupSource, CleanupSize); diff --git a/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp b/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp index 6d7d69da4d..114d0815ba 100644 --- a/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp +++ b/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp @@ -184,10 +184,11 @@ void DirectoryWatcherLinux::InotifyPollingLoop() { // the inotify file descriptor should have the same alignment as // struct inotify_event. - auto ManagedBuffer = - llvm::make_unique>(); - char *const Buf = ManagedBuffer->buffer; + struct Buffer { + alignas(struct inotify_event) char buffer[EventBufferLength]; + }; + auto ManagedBuffer = llvm::make_unique(); + char *const Buf = ManagedBuffer.buffer; const int EpollFD = epoll_create1(EPOLL_CLOEXEC); if (EpollFD == -1) { @@ -350,4 +351,4 @@ std::unique_ptr clang::DirectoryWatcher::create( return llvm::make_unique( Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD, std::move(*InotifyPollingStopper)); -} \ No newline at end of file +} diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index f632a4d3bd..3e2657b46c 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -7052,10 +7052,9 @@ void Sema::AddConversionCandidate( // allocator). QualType CallResultType = ConversionType.getNonLValueExprType(Context); - llvm::AlignedCharArray - Buffer; + alignas(CallExpr) char Buffer[sizeof(CallExpr) + sizeof(Stmt *)]; CallExpr *TheTemporaryCall = CallExpr::CreateTemporary( - Buffer.buffer, &ConversionFn, CallResultType, VK, From->getBeginLoc()); + Buffer, &ConversionFn, CallResultType, VK, From->getBeginLoc()); ImplicitConversionSequence ICS = TryCopyInitialization(*this, TheTemporaryCall, ToType, diff --git a/lib/Sema/TypeLocBuilder.cpp b/lib/Sema/TypeLocBuilder.cpp index b451403544..2dcbbd83c6 100644 --- a/lib/Sema/TypeLocBuilder.cpp +++ b/lib/Sema/TypeLocBuilder.cpp @@ -51,7 +51,7 @@ void TypeLocBuilder::grow(size_t NewCapacity) { &Buffer[Index], Capacity - Index); - if (Buffer != InlineBuffer.buffer) + if (Buffer != InlineBuffer) delete[] Buffer; Buffer = NewBuffer; diff --git a/lib/Sema/TypeLocBuilder.h b/lib/Sema/TypeLocBuilder.h index 1e6883926a..738f731c9f 100644 --- a/lib/Sema/TypeLocBuilder.h +++ b/lib/Sema/TypeLocBuilder.h @@ -39,18 +39,16 @@ class TypeLocBuilder { /// The inline buffer. enum { BufferMaxAlignment = alignof(void *) }; - llvm::AlignedCharArray InlineBuffer; + alignas(BufferMaxAlignment) char InlineBuffer[InlineCapacity]; unsigned NumBytesAtAlign4, NumBytesAtAlign8; - public: +public: TypeLocBuilder() - : Buffer(InlineBuffer.buffer), Capacity(InlineCapacity), - Index(InlineCapacity), NumBytesAtAlign4(0), NumBytesAtAlign8(0) - { - } + : Buffer(InlineBuffer), Capacity(InlineCapacity), Index(InlineCapacity), + NumBytesAtAlign4(0), NumBytesAtAlign8(0) {} ~TypeLocBuilder() { - if (Buffer != InlineBuffer.buffer) + if (Buffer != InlineBuffer) delete[] Buffer; } -- 2.40.0