Skip to content

Commit

Permalink
[ADT] Just give up on GCC, I can't fix this.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
d0k committed Jan 18, 2018
1 parent 8845432 commit 933d1a7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
13 changes: 4 additions & 9 deletions include/llvm/ADT/Optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <algorithm>
#include <cassert>
#include <new>
#include <cstring>
#include <utility>

namespace llvm {
Expand Down Expand Up @@ -111,28 +110,24 @@ template <typename T, bool IsPodLike> struct OptionalStorage {
}
};

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

OptionalStorage() = default;

OptionalStorage(const T &y) : hasVal(true) {
// We use memmove here because we know that T is trivially copyable and GCC
// up to 7 miscompiles placement new.
std::memmove(storage.buffer, &y, sizeof(y));
}
OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
OptionalStorage &operator=(const T &y) {
*reinterpret_cast<T *>(storage.buffer) = y;
hasVal = true;
// We use memmove here because we know that T is trivially copyable and GCC
// up to 7 miscompiles placement new.
std::memmove(storage.buffer, &y, sizeof(y));
return *this;
}

void reset() { hasVal = false; }
};
#endif
} // namespace optional_detail

template <typename T> class Optional {
Expand Down
3 changes: 1 addition & 2 deletions unittests/ADT/OptionalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@ TEST_F(OptionalTest, OperatorGreaterEqual) {
CheckRelation<GreaterEqual>(InequalityLhs, InequalityRhs, !IsLess);
}

#if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \
(defined(__GNUC__) && __GNUC__ >= 5)
#if __has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)
static_assert(std::is_trivially_copyable<Optional<int>>::value,
"Should be trivially copyable");
static_assert(
Expand Down

0 comments on commit 933d1a7

Please sign in to comment.