The libstdc++ version of optional contains some runtime checks enabled
when _GLIBCXX_DEBUG is defined. I think it would be useful if our
version contained similar checks.
Add checks in the two `get` methods, also conditional on _GLIBCXX_DEBUG.
I think it's simpler to use that macro rather than introducing a new
GDB-specific one, as I think that if somebody is interested in enabling
these runtime checks, they'll also be interested in enabling the
libstdc++ runtime checks (and vice-versa).
I implemented these checks using gdb_assert. Note that gdb_assert
throws (after querying the user), and we are in noexcept methods. That
means that std::terminate / abort will immediately be called. I think
this is ok, since if those were "real" _GLIBCXX_DEBUG checks, abort
would be called straight away.
If I add a dummy failure, it looks like so:
$ ./gdb -q -nx --data-directory=data-directory
/home/simark/src/binutils-gdb/gdb/../gdbsupport/gdb_optional.h:206: internal-error: T& gdb::optional<T>::get() [with T = int]: Assertion `this->has_value ()' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Quit this debugging session? (y or n) n
[1] 658767 abort (core dumped) ./gdb -q -nx --data-directory=data-directory
Change-Id: Iadfdcd131425bd2ca6a2de30d7b22e9b3cc67793
}
/* The get operations have m_instantiated as a precondition. */
- T &get () noexcept { return m_item; }
- constexpr const T &get () const noexcept { return m_item; }
+ T &get () noexcept
+ {
+#if defined(_GLIBCXX_DEBUG)
+ gdb_assert (this->has_value ());
+#endif
+ return m_item;
+ }
+ constexpr const T &get () const noexcept
+ {
+#if defined(_GLIBCXX_DEBUG)
+ gdb_assert (this->has_value ());
+#endif
+ return m_item;
+ }
/* The object. */
union