Commit 676035fe authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook Github Bot

Fix optional default initialization

Summary: Initialize Optional internal memory, so that "un-initialized memory" tools don't flag its callsites.

Reviewed By: yfeldblum

Differential Revision: D3960462

fbshipit-source-id: 3bd0109959fb93e040fa2e874f586b3508e46dd2
parent a30215d9
...@@ -67,17 +67,6 @@ typedef int detail::NoneHelper::*None; ...@@ -67,17 +67,6 @@ typedef int detail::NoneHelper::*None;
const None none = nullptr; const None none = nullptr;
/**
* gcc-4.7 warns about use of uninitialized memory around the use of storage_
* even though this is explicitly initialized at each point.
*/
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wuninitialized"
# pragma GCC diagnostic ignored "-Wpragmas"
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // __GNUC__
class OptionalEmptyException : public std::runtime_error { class OptionalEmptyException : public std::runtime_error {
public: public:
OptionalEmptyException() OptionalEmptyException()
...@@ -273,9 +262,18 @@ class Optional { ...@@ -273,9 +262,18 @@ class Optional {
} }
struct StorageTriviallyDestructible { struct StorageTriviallyDestructible {
// uninitialized // The union trick allows to initialize the Optional's memory,
union { Value value; }; // so that compiler/tools don't complain about unitialized memory,
bool hasValue; // without actually calling Value's default constructor.
// The rest of the implementation enforces that hasValue/value are
// synchronized.
union {
bool hasValue;
struct {
bool paddingForHasValue_[1];
Value value;
};
};
StorageTriviallyDestructible() : hasValue{false} {} StorageTriviallyDestructible() : hasValue{false} {}
...@@ -285,12 +283,16 @@ class Optional { ...@@ -285,12 +283,16 @@ class Optional {
}; };
struct StorageNonTriviallyDestructible { struct StorageNonTriviallyDestructible {
// uninitialized // See StorageTriviallyDestructible's union
union { Value value; }; union {
bool hasValue; bool hasValue;
struct {
bool paddingForHasValue_[1];
Value value;
};
};
StorageNonTriviallyDestructible() : hasValue{false} {} StorageNonTriviallyDestructible() : hasValue{false} {}
~StorageNonTriviallyDestructible() { ~StorageNonTriviallyDestructible() {
clear(); clear();
} }
...@@ -311,10 +313,6 @@ class Optional { ...@@ -311,10 +313,6 @@ class Optional {
Storage storage_; Storage storage_;
}; };
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
template<class T> template<class T>
const T* get_pointer(const Optional<T>& opt) { const T* get_pointer(const Optional<T>& opt) {
return opt.get_pointer(); return opt.get_pointer();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment