From patchwork Thu Oct 5 16:05:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 77179 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C31FF3882641 for ; Thu, 5 Oct 2023 16:06:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C31FF3882641 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1696521999; bh=WlbEv2mKgYQpwdiWMs1EOjwWJ9EHHYW2FQ7j0Mkpaoo=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=gORuc7Kx1jvPRvWTP/FE1LAyRR2Q6ltquUrMdslDCKE17P9EFaTT5ZzTe68V2S8gc vdDAZS9SmloJrIRq96UbXkM+nlLILPdiFgSWvxsQdnNa85tD+iqT5yTGDRCQxBeX3F 3mO9+SCtge+GALjDbv5q0SOY5IVmhLXe5Y3VVJdA= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by sourceware.org (Postfix) with ESMTPS id 2597E3885528 for ; Thu, 5 Oct 2023 16:05:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2597E3885528 Received: by mail-io1-xd34.google.com with SMTP id ca18e2360f4ac-79fa5d9f3a2so46997939f.3 for ; Thu, 05 Oct 2023 09:05:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696521954; x=1697126754; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WlbEv2mKgYQpwdiWMs1EOjwWJ9EHHYW2FQ7j0Mkpaoo=; b=uQk5Ce+OjXEXc+B0LtFXxdBRg9Abu+XDhvLdwWh189/EmXmMmiFtesK2oo2C0q4K1Z Uai9pKDt9WNQNOusICD0Ku6+jxqclI4vn8t7gxOEYEnG8I370BJLiu0mjAhpXCcqrmGV lnfDPXkZAhXSwsfOps3cv1UtvHMi6StuNfiG5TFAFBaAPSUIWKRjhCKh8DsZRa0PmTCU kyKlcgB1XquZswkjxCVJoUF3DM9DNqZ80s/U/Y5oJ3FayyjOCPZX0f+v3zbYv0jJ8HH+ nxyf46s0TzP9Qn4tgJHPI2b4ypQ/4FNviFuFWtf2cqLBuNrqC9S/oou63rM5+XLyJ3FC 14Zw== X-Gm-Message-State: AOJu0YxYVV4AfzMb00Z8KJ8kU/Soo9C7B9iRBC4qVmrpBpqfCOPjwqNv huGwNJp6PV3DBuQRz7Qr7h8iKCP7dtsmpFX3gXYAxQ== X-Google-Smtp-Source: AGHT+IEK6Sh2vvpaQDfMQ5az9cGozowXInOLuAIxY98X6+RVQq6rKqaxpFCsaPl7MRRUDaTd/CzvyQ== X-Received: by 2002:a5d:9f01:0:b0:792:8d16:91ea with SMTP id q1-20020a5d9f01000000b007928d1691eamr5476893iot.0.1696521954243; Thu, 05 Oct 2023 09:05:54 -0700 (PDT) Received: from localhost.localdomain (71-211-130-31.hlrn.qwest.net. [71.211.130.31]) by smtp.gmail.com with ESMTPSA id x1-20020a056638034100b00445630238cfsm25550jap.48.2023.10.05.09.05.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:05:53 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Use std::optional when available Date: Thu, 5 Oct 2023 10:05:43 -0600 Message-Id: <20231005160543.2627733-1-tromey@adacore.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" 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 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 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 + +namespace gdb +{ +template using optional = std::optional; + +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 */