From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Use std::optional when available
Date: Thu, 5 Oct 2023 10:05:43 -0600 [thread overview]
Message-ID: <20231005160543.2627733-1-tromey@adacore.com> (raw)
This patch changes gdb_optional.h to use std::optional when it is
available.
Note I had to comment out some of the tests. These give a
set-but-not-used warning when built with the libstdc++ optional. I
think this change is harmless, particularly because these tests will
be removed entirely once gdb switches to C++17.
Regression tested on x86-64 Fedora 36.
---
gdb/unittests/optional-selftests.c | 2 ++
gdb/unittests/optional/cons/copy.cc | 4 ++++
gdb/unittests/optional/cons/move.cc | 4 ++++
gdbsupport/gdb_optional.h | 17 +++++++++++++++++
4 files changed, 27 insertions(+)
diff --git a/gdb/unittests/optional-selftests.c b/gdb/unittests/optional-selftests.c
index 8a727c02159..ea8447f4454 100644
--- a/gdb/unittests/optional-selftests.c
+++ b/gdb/unittests/optional-selftests.c
@@ -30,9 +30,11 @@
/* libstdc++'s testsuite uses VERIFY. */
#define VERIFY SELF_CHECK
+#if __cplusplus < 201703L
/* Used to disable testing features not supported by
gdb::optional. */
#define GDB_OPTIONAL
+#endif
namespace selftests {
namespace optional {
diff --git a/gdb/unittests/optional/cons/copy.cc b/gdb/unittests/optional/cons/copy.cc
index 87a08f9a52b..4b9837dd3c5 100644
--- a/gdb/unittests/optional/cons/copy.cc
+++ b/gdb/unittests/optional/cons/copy.cc
@@ -89,6 +89,9 @@ test ()
enum outcome { nothrow, caught, bad_catch };
+ /* These tests give an unused-but-set warning with libstdc++
+ optional. */
+#ifdef GDB_OPTIONAL
{
outcome result = nothrow;
gdb::optional<throwing_copy> o;
@@ -120,6 +123,7 @@ test ()
VERIFY( result == caught );
}
+#endif
VERIFY( tracker::count == 0 );
}
diff --git a/gdb/unittests/optional/cons/move.cc b/gdb/unittests/optional/cons/move.cc
index 398784ae7ec..072c1b83b97 100644
--- a/gdb/unittests/optional/cons/move.cc
+++ b/gdb/unittests/optional/cons/move.cc
@@ -87,6 +87,9 @@ test ()
enum outcome { nothrow, caught, bad_catch };
+ /* These tests give an unused-but-set warning with libstdc++
+ optional. */
+#ifdef GDB_OPTIONAL
{
outcome result = nothrow;
gdb::optional<throwing_move> o;
@@ -118,6 +121,7 @@ test ()
VERIFY( result == caught );
}
+#endif
VERIFY( tracker::count == 0 );
}
diff --git a/gdbsupport/gdb_optional.h b/gdbsupport/gdb_optional.h
index 9b7b7b2f7f4..c177691bb65 100644
--- a/gdbsupport/gdb_optional.h
+++ b/gdbsupport/gdb_optional.h
@@ -20,6 +20,21 @@
#ifndef COMMON_GDB_OPTIONAL_H
#define COMMON_GDB_OPTIONAL_H
+#if __cplusplus >= 201703L
+
+#include <optional>
+
+namespace gdb
+{
+template<typename T> using optional = std::optional<T>;
+
+using in_place_t = std::in_place_t;
+
+inline constexpr in_place_t in_place{};
+}
+
+#else /* __cplusplus < 201703L */
+
#include "gdbsupport/traits.h"
namespace gdb
@@ -230,4 +245,6 @@ class optional
}
+#endif /* __cplusplus < 201703L */
+
#endif /* COMMON_GDB_OPTIONAL_H */
--
2.40.1
next reply other threads:[~2023-10-05 16:05 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-05 16:05 Tom Tromey [this message]
2023-10-09 12:40 ` Andrew Burgess
2023-10-10 16:49 ` Tom Tromey
2023-10-12 18:44 ` Pedro Alves
2023-10-16 14:13 ` Andrew Burgess
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231005160543.2627733-1-tromey@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).