Skip to content

Commit 933d1a7

Browse files
committed
[ADT] Just give up on GCC, I can't fix this.
While the memmove workaround fixed it for GCC 6.3. GCC 4.8 and GCC 7.1 are still broken. I have no clue what's going on, just blacklist GCC for now. Needless to say this code is ubsan, asan and msan-clean. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322862 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8845432 commit 933d1a7

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

include/llvm/ADT/Optional.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <algorithm>
2424
#include <cassert>
2525
#include <new>
26-
#include <cstring>
2726
#include <utility>
2827

2928
namespace llvm {
@@ -111,28 +110,24 @@ template <typename T, bool IsPodLike> struct OptionalStorage {
111110
}
112111
};
113112

113+
#if !defined(__GNUC__) || defined(__clang__) // GCC up to GCC7 miscompiles this.
114114
/// Storage for trivially copyable types only.
115115
template <typename T> struct OptionalStorage<T, true> {
116116
AlignedCharArrayUnion<T> storage;
117117
bool hasVal = false;
118118

119119
OptionalStorage() = default;
120120

121-
OptionalStorage(const T &y) : hasVal(true) {
122-
// We use memmove here because we know that T is trivially copyable and GCC
123-
// up to 7 miscompiles placement new.
124-
std::memmove(storage.buffer, &y, sizeof(y));
125-
}
121+
OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
126122
OptionalStorage &operator=(const T &y) {
123+
*reinterpret_cast<T *>(storage.buffer) = y;
127124
hasVal = true;
128-
// We use memmove here because we know that T is trivially copyable and GCC
129-
// up to 7 miscompiles placement new.
130-
std::memmove(storage.buffer, &y, sizeof(y));
131125
return *this;
132126
}
133127

134128
void reset() { hasVal = false; }
135129
};
130+
#endif
136131
} // namespace optional_detail
137132

138133
template <typename T> class Optional {

unittests/ADT/OptionalTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,7 @@ TEST_F(OptionalTest, OperatorGreaterEqual) {
518518
CheckRelation<GreaterEqual>(InequalityLhs, InequalityRhs, !IsLess);
519519
}
520520

521-
#if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \
522-
(defined(__GNUC__) && __GNUC__ >= 5)
521+
#if __has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)
523522
static_assert(std::is_trivially_copyable<Optional<int>>::value,
524523
"Should be trivially copyable");
525524
static_assert(

0 commit comments

Comments
 (0)