mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2025-03-07 13:39:43 +08:00
Commit ecfb656c37
("dwarf2read.c: Make dir_index and
file_name_index strong typedefs") added a use of gdb::optional that
triggers bogus -Wmaybe-uninitialized warnings:
GCC trunk is complaining like this:
../../binutils-gdb/gdb/dwarf2read.c: In function void read_formatted_entries(bfd*, const gdb_byte**, line_header*, const comp_unit_head*, void (*)(line_header*, const char*, dir_index, unsigned int, unsigned int)):
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: fe.file_entry::length may be used uninitialized in this function [-Werror=maybe-uninitialized]
callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
^
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: *((void*)& fe +8) may be used uninitialized in this function [-Werror=maybe-uninitialized]
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: fe.file_entry::mod_time may be used uninitialized in this function [-Werror=maybe-uninitialized]
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: fe.file_entry::name may be used uninitialized in this function [-Werror=maybe-uninitialized]
While some older GCCs are complaining like this:
../../binutils-gdb/gdb/dwarf2read.c: In function void read_formatted_entries(bfd*, const gdb_byte**, line_header*, const comp_unit_head*, void (*)(line_header*, const char*, dir_index, unsigned int, unsigned int)):
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: uint may be used uninitialized in this function [-Werror=maybe-uninitialized]
callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
Looking around the web, I see that boost's optional implementation
triggers this kind of issue often too. See:
http://www.boost.org/doc/libs/1_63_0/libs/optional/doc/html/boost_optional/tutorial/gotchas/false_positive_with__wmaybe_uninitialized.html
I noticed that replacing the gdb::optional uses with real C++17
std::optional uses against GCC 7/trunk makes the warnings go away.
Looking at the implementation, AFAICS, libstdc++ always initializes
its "empty" union payload member (_M_empty, which is defined as an
empty class, like ours). I.e., all payload types have this ctor:
struct _Optional_payload.....
{
constexpr _Optional_payload()
: _M_empty() {}
The constexpr makes a diference too. Without it, GCC7 still warns.
So I'm applying the same treatment to our gdb::optional.
gdb/ChangeLog:
2017-04-05 Pedro Alves <palves@redhat.com>
* common/gdb_optional.h (optional::optional): Make constexpr and
initialize m_dummy.
118 lines
2.9 KiB
C++
118 lines
2.9 KiB
C++
/* An optional object.
|
|
|
|
Copyright (C) 2017 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#ifndef GDB_OPTIONAL_H
|
|
#define GDB_OPTIONAL_H
|
|
|
|
namespace gdb
|
|
{
|
|
|
|
/* This class attempts to be a compatible subset of std::optional,
|
|
which is slated to be available in C++17. This class optionally
|
|
holds an object of some type -- by default it is constructed not
|
|
holding an object, but later the object can be "emplaced". This is
|
|
similar to using std::unique_ptr, but in-object allocation is
|
|
guaranteed. */
|
|
template<typename T>
|
|
class optional
|
|
{
|
|
public:
|
|
|
|
constexpr optional ()
|
|
: m_dummy (),
|
|
m_instantiated (false)
|
|
{
|
|
}
|
|
|
|
~optional ()
|
|
{
|
|
if (m_instantiated)
|
|
destroy ();
|
|
}
|
|
|
|
/* These aren't deleted in std::optional, but it was simpler to
|
|
delete them here, because currently the users of this class don't
|
|
need them, and making them depend on the definition of T is
|
|
somewhat complicated. */
|
|
optional (const optional &other) = delete;
|
|
optional<T> &operator= (const optional &other) = delete;
|
|
|
|
template<typename... Args>
|
|
void emplace (Args &&... args)
|
|
{
|
|
if (m_instantiated)
|
|
destroy ();
|
|
new (&m_item) T (std::forward<Args>(args)...);
|
|
m_instantiated = true;
|
|
}
|
|
|
|
/* Observers. */
|
|
constexpr const T *operator-> () const
|
|
{ return std::addressof (this->get ()); }
|
|
|
|
T *operator-> ()
|
|
{ return std::addressof (this->get ()); }
|
|
|
|
constexpr const T &operator* () const &
|
|
{ return this->get (); }
|
|
|
|
T &operator* () &
|
|
{ return this->get (); }
|
|
|
|
T &&operator* () &&
|
|
{ return std::move (this->get ()); }
|
|
|
|
constexpr const T &&operator* () const &&
|
|
{ return std::move (this->get ()); }
|
|
|
|
constexpr explicit operator bool () const noexcept
|
|
{ return m_instantiated; }
|
|
|
|
constexpr bool has_value () const noexcept
|
|
{ return m_instantiated; }
|
|
|
|
private:
|
|
|
|
/* Destroy the object. */
|
|
void destroy ()
|
|
{
|
|
gdb_assert (m_instantiated);
|
|
m_instantiated = false;
|
|
m_item.~T ();
|
|
}
|
|
|
|
/* The get operations have m_instantiated as a precondition. */
|
|
T &get () noexcept { return m_item; }
|
|
constexpr const T &get () const noexcept { return m_item; }
|
|
|
|
/* The object. */
|
|
union
|
|
{
|
|
struct { } m_dummy;
|
|
T m_item;
|
|
};
|
|
|
|
/* True if the object was ever emplaced. */
|
|
bool m_instantiated;
|
|
};
|
|
|
|
}
|
|
|
|
#endif /* GDB_OPTIONAL_H */
|