From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd41.google.com (mail-io1-xd41.google.com [IPv6:2607:f8b0:4864:20::d41]) by sourceware.org (Postfix) with ESMTPS id 65E83384A87E for ; Tue, 19 May 2020 18:55:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 65E83384A87E Received: by mail-io1-xd41.google.com with SMTP id w25so278966iol.12 for ; Tue, 19 May 2020 11:55:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=YnCNCFs9rQutfjXSQVJ3rG6wdKSd6AI+fhu22sPqbF8=; b=easOqapdhw1Wxz0CVbTC/b/4tdLNYtI7NQl5n361D4YxGXRuyoPfcX5InRrxHBTrgX gobqIGD/ujY9q5v73Sfk6uc1bIf4vFyE+wPxjzvsJ0sLVlpoI7nPCgm0L/kBaMHc/Ed4 b8lzWWM7SsFTWGovHU0RP7Pe8hr0mYluvtw9M9WzfffUTSCYY8kmAjcMzNqGMUZ24vMP CMzh0Ns81FQuM13nyt2S5Vy5eEF89TsvZMgy6W3XOpTUgyNnnuQQOpHC4yD+tGYn7WIm +mcuRGRVxgsIx45qbwg/IxDroW7TRzmnP1NLpsdbpYBeF7CSYawzvT3198WWgvW7T6ET rkAA== X-Gm-Message-State: AOAM531eE78n8OlKUseH5ebW1YVnzL0bHy6vuFCcoaXyY8aE0maVr4J6 DFvE0OAJ0KJ3NY5UTaEd5roGnbYe0RaXoA== X-Google-Smtp-Source: ABdhPJy2FKhkO840rjTRGdJvlZP5IafpSZr9+YaoXU4h8wIDsrDZWqSvtAobHmqhHP95531OZAQDew== X-Received: by 2002:a02:942a:: with SMTP id a39mr1099654jai.50.1589914540104; Tue, 19 May 2020 11:55:40 -0700 (PDT) Received: from cbiesinger.lan (75-6-176-145.lightspeed.mdsnwi.sbcglobal.net. [75.6.176.145]) by smtp.googlemail.com with ESMTPSA id k7sm124372ilr.41.2020.05.19.11.55.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 May 2020 11:55:39 -0700 (PDT) From: cbiesinger@chromium.org To: gdb-patches@sourceware.org Subject: [PATCH] Add gdb::nullopt, from C++17's std::nullopt Date: Tue, 19 May 2020 13:55:37 -0500 Message-Id: <20200519185537.87677-1-cbiesinger@chromium.org> X-Mailer: git-send-email 2.26.2.761.g0e0b3e54be-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 May 2020 18:55:43 -0000 From: Christian Biesinger This provides a clearer way to initialize a gdb::optional. E.g.: gdb::optional bar () { return gdb::nullopt; } or void foo (gdb::optional) { } foo (gdb::nullopt); Also updates all places I could find that use {} or optional() to instead use nullopt. gdb/ChangeLog: 2020-05-19 Christian Biesinger * cli/cli-option.c (struct option_def_and_value): Update to use nullopt. (parse_option): Likewise. * dwarf2/die.h (struct die_info): Likewise. * dwarf2/read.c (dw2_expand_symtabs_for_function): Likewise. (dw2_debug_names_expand_symtabs_for_function): Likewise. (lookup_dwo_id): Likewise. * fbsd-tdep.c (fbsd_make_note_desc): Likewise. * inf-child.c (inf_child_target::fileio_readlink): Likewise. * linux-nat.c (linux_nat_target::fileio_readlink): Likewise. * python/python.c (gdbpy_colorize): Likewise. * remote.c (remote_target::fileio_readlink): Likewise. * solib-aix.c (solib_aix_parse_libraries): Likewise. * solib-svr4.c (read_program_header): Likewise. (read_program_headers_from_bfd): Likewise. * symtab.h (struct general_symbol_info): Likewise. * target.c (target_read_alloc_1): Likewise. (target_read_stralloc): Likewise. (target_get_osdata): Likewise. (target_ops::fileio_readlink): Likewise. (target_fileio_readlink): Likewise. * xml-support.c (xml_fetch_content_from_file): Likewise. * xml-tdesc.c (target_fetch_description_xml): Likewise. (string_read_description_xml): Likewise. gdbsupport/ChangeLog: 2020-05-19 Christian Biesinger * gdb_optional.h (struct nullopt_t): Add. (nullopt): Add. (class optional): Add nullopt_t constructor. Change-Id: I371f05484a3b85cc6d54a8baead2b4f64e6ee308 --- gdb/cli/cli-option.c | 24 ++++++++++++------------ gdb/dwarf2/die.h | 2 +- gdb/dwarf2/read.c | 7 ++++--- gdb/fbsd-tdep.c | 2 +- gdb/inf-child.c | 4 ++-- gdb/linux-nat.c | 2 +- gdb/python/python.c | 20 ++++++++++---------- gdb/remote.c | 4 ++-- gdb/solib-aix.c | 4 ++-- gdb/solib-svr4.c | 24 ++++++++++++------------ gdb/symtab.h | 3 +-- gdb/target.c | 10 +++++----- gdb/xml-support.c | 4 ++-- gdb/xml-tdesc.c | 8 ++++---- gdbsupport/gdb_optional.h | 14 ++++++++++++++ 15 files changed, 73 insertions(+), 59 deletions(-) diff --git a/gdb/cli/cli-option.c b/gdb/cli/cli-option.c index a24eca457d..08457e4e03 100644 --- a/gdb/cli/cli-option.c +++ b/gdb/cli/cli-option.c @@ -62,7 +62,7 @@ struct option_def_and_value /* Constructor. */ option_def_and_value (const option_def &option_, void *ctx_, - gdb::optional &&value_ = {}) + gdb::optional &&value_ = gdb::nullopt) : option (option_), ctx (ctx_), value (std::move (value_)) @@ -185,15 +185,15 @@ parse_option (gdb::array_view options_group, parse_option_completion_info *completion = nullptr) { if (*args == nullptr) - return {}; + return gdb::nullopt; else if (**args != '-') { if (have_delimiter) error (_("Unrecognized option at: %s"), *args); - return {}; + return gdb::nullopt; } else if (check_for_argument (args, "--")) - return {}; + return gdb::nullopt; /* Skip the initial '-'. */ const char *arg = *args + 1; @@ -216,7 +216,7 @@ parse_option (gdb::array_view options_group, complete_on_options (options_group, completion->tracker, arg, completion->word); - return {}; + return gdb::nullopt; } error (_("Ambiguous option at: -%s"), arg); @@ -237,14 +237,14 @@ parse_option (gdb::array_view options_group, if (have_delimiter || mode != PROCESS_OPTIONS_UNKNOWN_IS_OPERAND) error (_("Unrecognized option at: %s"), *args); - return {}; + return gdb::nullopt; } if (completion != nullptr && arg[len] == '\0') { complete_on_options (options_group, completion->tracker, arg, completion->word); - return {}; + return gdb::nullopt; } *args += 1 + len; @@ -347,7 +347,7 @@ parse_option (gdb::array_view options_group, }; complete_on_enum (completion->tracker, all_boolean_enums, val_str, val_str); - return {}; + return gdb::nullopt; } } @@ -370,13 +370,13 @@ parse_option (gdb::array_view options_group, (make_unique_xstrdup ("NUMBER")); completion->tracker.add_completion (make_unique_xstrdup ("unlimited")); - return {}; + return gdb::nullopt; } else if (startswith ("unlimited", *args)) { completion->tracker.add_completion (make_unique_xstrdup ("unlimited")); - return {}; + return gdb::nullopt; } } @@ -403,7 +403,7 @@ parse_option (gdb::array_view options_group, complete_on_enum (completion->tracker, match->enums, *args, *args); if (completion->tracker.have_completions ()) - return {}; + return gdb::nullopt; /* If we don't have completions, let the non-completion path throw on invalid enum value @@ -448,7 +448,7 @@ parse_option (gdb::array_view options_group, gdb_assert_not_reached (_("option type not supported")); } - return {}; + return gdb::nullopt; } /* See cli-option.h. */ diff --git a/gdb/dwarf2/die.h b/gdb/dwarf2/die.h index 5522ebdf31..9f3a877e73 100644 --- a/gdb/dwarf2/die.h +++ b/gdb/dwarf2/die.h @@ -45,7 +45,7 @@ struct die_info /* If both exist, just use the first one. */ return DW_UNSND (&attrs[i]); } - return gdb::optional (); + return gdb::nullopt; } /* Return range lists base of the compile unit, which, if exists, is diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 719051bc5b..13c485b15d 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -3586,7 +3586,8 @@ dw2_expand_symtabs_for_function (struct objfile *objfile, struct dw2_symtab_iterator iter; struct dwarf2_per_cu_data *per_cu; - dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name); + dw2_symtab_iter_init (&iter, dwarf2_per_objfile, gdb::nullopt, VAR_DOMAIN, + func_name); while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL) dw2_instantiate_symtab (per_cu, false); @@ -5624,7 +5625,7 @@ dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile, { const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table; - dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name); + dw2_debug_names_iterator iter (map, gdb::nullopt, VAR_DOMAIN, func_name); struct dwarf2_per_cu_data *per_cu; while ((per_cu = iter.next ()) != NULL) @@ -6760,7 +6761,7 @@ lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die) struct attribute *attr; attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu); if (attr == nullptr) - return gdb::optional (); + return gdb::nullopt; return DW_UNSND (attr); } diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c index acf787c706..b8275814af 100644 --- a/gdb/fbsd-tdep.c +++ b/gdb/fbsd-tdep.c @@ -694,7 +694,7 @@ fbsd_make_note_desc (enum target_object object, uint32_t structsize) gdb::optional buf = target_read_alloc (current_top_target (), object, NULL); if (!buf || buf->empty ()) - return {}; + return gdb::nullopt; if (structsize == 0) return buf; diff --git a/gdb/inf-child.c b/gdb/inf-child.c index 4833094889..3420398f82 100644 --- a/gdb/inf-child.c +++ b/gdb/inf-child.c @@ -381,13 +381,13 @@ inf_child_target::fileio_readlink (struct inferior *inf, const char *filename, if (len < 0) { *target_errno = host_to_fileio_error (errno); - return {}; + return gdb::nullopt; } return std::string (buf, len); #else *target_errno = FILEIO_ENOSYS; - return {}; + return gdb::nullopt; #endif } diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 0a2bfdc57d..8de9501639 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -4480,7 +4480,7 @@ linux_nat_target::fileio_readlink (struct inferior *inf, const char *filename, if (len < 0) { *target_errno = host_to_fileio_error (errno); - return {}; + return gdb::nullopt; } return std::string (buf, len); diff --git a/gdb/python/python.c b/gdb/python/python.c index 67f362b852..c241a09f71 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1120,35 +1120,35 @@ static gdb::optional gdbpy_colorize (const std::string &filename, const std::string &contents) { if (!gdb_python_initialized) - return {}; + return gdb::nullopt; gdbpy_enter enter_py (get_current_arch (), current_language); if (gdb_python_module == nullptr || !PyObject_HasAttrString (gdb_python_module, "colorize")) - return {}; + return gdb::nullopt; gdbpy_ref<> hook (PyObject_GetAttrString (gdb_python_module, "colorize")); if (hook == nullptr) { gdbpy_print_stack (); - return {}; + return gdb::nullopt; } if (!PyCallable_Check (hook.get ())) - return {}; + return gdb::nullopt; gdbpy_ref<> fname_arg (PyString_FromString (filename.c_str ())); if (fname_arg == nullptr) { gdbpy_print_stack (); - return {}; + return gdb::nullopt; } gdbpy_ref<> contents_arg (PyString_FromString (contents.c_str ())); if (contents_arg == nullptr) { gdbpy_print_stack (); - return {}; + return gdb::nullopt; } gdbpy_ref<> result (PyObject_CallFunctionObjArgs (hook.get (), @@ -1158,17 +1158,17 @@ gdbpy_colorize (const std::string &filename, const std::string &contents) if (result == nullptr) { gdbpy_print_stack (); - return {}; + return gdb::nullopt; } if (!gdbpy_is_string (result.get ())) - return {}; + return gdb::nullopt; gdbpy_ref<> unic = python_string_to_unicode (result.get ()); if (unic == nullptr) { gdbpy_print_stack (); - return {}; + return gdb::nullopt; } gdbpy_ref<> host_str (PyUnicode_AsEncodedString (unic.get (), host_charset (), @@ -1176,7 +1176,7 @@ gdbpy_colorize (const std::string &filename, const std::string &contents) if (host_str == nullptr) { gdbpy_print_stack (); - return {}; + return gdb::nullopt; } return std::string (PyBytes_AsString (host_str.get ())); diff --git a/gdb/remote.c b/gdb/remote.c index 312a03c8fb..b910f7e533 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -12257,7 +12257,7 @@ remote_target::fileio_readlink (struct inferior *inf, const char *filename, int read_len; if (remote_hostio_set_filesystem (inf, remote_errno) != 0) - return {}; + return gdb::nullopt; remote_buffer_add_string (&p, &left, "vFile:readlink:"); @@ -12269,7 +12269,7 @@ remote_target::fileio_readlink (struct inferior *inf, const char *filename, &attachment_len); if (len < 0) - return {}; + return gdb::nullopt; std::string ret (len, '\0'); diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c index 125c8960c2..16cf235a20 100644 --- a/gdb/solib-aix.c +++ b/gdb/solib-aix.c @@ -108,7 +108,7 @@ solib_aix_parse_libraries (const char *library) "at compile time")); } - return {}; + return gdb::nullopt; } #else /* HAVE_LIBEXPAT */ @@ -215,7 +215,7 @@ solib_aix_parse_libraries (const char *library) library_list_elements, library, &result) == 0) return result; - return {}; + return gdb::nullopt; } #endif /* HAVE_LIBEXPAT */ diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c index 19d1105ae9..6df604cc2f 100644 --- a/gdb/solib-svr4.c +++ b/gdb/solib-svr4.c @@ -429,13 +429,13 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr) /* Get required auxv elements from target. */ if (target_auxv_search (current_top_target (), AT_PHDR, &at_phdr) <= 0) - return {}; + return gdb::nullopt; if (target_auxv_search (current_top_target (), AT_PHENT, &at_phent) <= 0) - return {}; + return gdb::nullopt; if (target_auxv_search (current_top_target (), AT_PHNUM, &at_phnum) <= 0) - return {}; + return gdb::nullopt; if (!at_phdr || !at_phnum) - return {}; + return gdb::nullopt; /* Determine ELF architecture type. */ if (at_phent == sizeof (Elf32_External_Phdr)) @@ -443,7 +443,7 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr) else if (at_phent == sizeof (Elf64_External_Phdr)) arch_size = 64; else - return {}; + return gdb::nullopt; /* Find the requested segment. */ if (type == -1) @@ -463,7 +463,7 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr) if (target_read_memory (at_phdr + i * sizeof (phdr), (gdb_byte *)&phdr, sizeof (phdr))) - return {}; + return gdb::nullopt; p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type, 4, byte_order); @@ -480,7 +480,7 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr) } if (i == at_phnum) - return {}; + return gdb::nullopt; /* Retrieve address and size. */ sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr, @@ -500,7 +500,7 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr) if (target_read_memory (at_phdr + i * sizeof (phdr), (gdb_byte *)&phdr, sizeof (phdr))) - return {}; + return gdb::nullopt; p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type, 4, byte_order); @@ -517,7 +517,7 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr) } if (i == at_phnum) - return {}; + return gdb::nullopt; /* Retrieve address and size. */ sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr, @@ -539,7 +539,7 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr) /* Read in requested program header. */ gdb::byte_vector buf (sect_size); if (target_read_memory (sect_addr, buf.data (), sect_size)) - return {}; + return gdb::nullopt; if (p_arch_size) *p_arch_size = arch_size; @@ -2510,12 +2510,12 @@ read_program_headers_from_bfd (bfd *abfd) Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); int phdrs_size = ehdr->e_phnum * ehdr->e_phentsize; if (phdrs_size == 0) - return {}; + return gdb::nullopt; gdb::byte_vector buf (phdrs_size); if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0 || bfd_bread (buf.data (), phdrs_size, abfd) != phdrs_size) - return {}; + return gdb::nullopt; return buf; } diff --git a/gdb/symtab.h b/gdb/symtab.h index 05e6a311b8..3a1200cfa1 100644 --- a/gdb/symtab.h +++ b/gdb/symtab.h @@ -471,8 +471,7 @@ struct general_symbol_info copy_name must be set to true. */ void compute_and_set_names (gdb::string_view linkage_name, bool copy_name, struct objfile_per_bfd_storage *per_bfd, - gdb::optional hash - = gdb::optional ()); + gdb::optional hash = gdb::nullopt); /* Name of the symbol. This is a required field. Storage for the name is allocated on the objfile_obstack for the associated diff --git a/gdb/target.c b/gdb/target.c index 6982a806e3..67e26fb46d 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -1781,7 +1781,7 @@ target_read_alloc_1 (struct target_ops *ops, enum target_object object, else if (status != TARGET_XFER_OK) { /* An error occurred. */ - return {}; + return gdb::nullopt; } buf_pos += xfered_len; @@ -1809,7 +1809,7 @@ target_read_stralloc (struct target_ops *ops, enum target_object object, = target_read_alloc_1 (ops, object, annex); if (!buf) - return {}; + return gdb::nullopt; if (buf->empty () || buf->back () != '\0') buf->push_back ('\0'); @@ -2536,7 +2536,7 @@ target_get_osdata (const char *type) t = find_default_run_target ("get OS data"); if (!t) - return {}; + return gdb::nullopt; return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type); } @@ -2772,7 +2772,7 @@ target_ops::fileio_readlink (struct inferior *inf, const char *filename, int *target_errno) { *target_errno = FILEIO_ENOSYS; - return {}; + return gdb::nullopt; } /* Helper for target_fileio_open and @@ -2984,7 +2984,7 @@ target_fileio_readlink (struct inferior *inf, const char *filename, } *target_errno = FILEIO_ENOSYS; - return {}; + return gdb::nullopt; } /* Like scoped_fd, but specific to target fileio. */ diff --git a/gdb/xml-support.c b/gdb/xml-support.c index c906b69fda..5177315be5 100644 --- a/gdb/xml-support.c +++ b/gdb/xml-support.c @@ -984,7 +984,7 @@ xml_fetch_content_from_file (const char *filename, void *baton) file = gdb_fopen_cloexec (filename, FOPEN_RB); if (file == NULL) - return {}; + return gdb::nullopt; /* Read in the whole file. */ @@ -1001,7 +1001,7 @@ xml_fetch_content_from_file (const char *filename, void *baton) || ferror (file.get ())) { warning (_("Read error from \"%s\""), filename); - return {}; + return gdb::nullopt; } text.back () = '\0'; diff --git a/gdb/xml-tdesc.c b/gdb/xml-tdesc.c index 6f108a4e3e..23b59e9d79 100644 --- a/gdb/xml-tdesc.c +++ b/gdb/xml-tdesc.c @@ -730,12 +730,12 @@ target_fetch_description_xml (struct target_ops *ops) "disabled at compile time")); } - return {}; + return gdb::nullopt; #else gdb::optional tdesc_str = fetch_available_features_from_target ("target.xml", ops); if (!tdesc_str) - return {}; + return gdb::nullopt; std::string output; if (!xml_process_xincludes (output, @@ -744,7 +744,7 @@ target_fetch_description_xml (struct target_ops *ops) fetch_available_features_from_target, ops, 0)) { warning (_("Could not load XML target description; ignoring")); - return {}; + return gdb::nullopt; } return output; #endif @@ -758,6 +758,6 @@ string_read_description_xml (const char *xml) return tdesc_parse_xml (xml, [] (const char *href, void *baton) { error (_("xincludes are unsupported with this method")); - return gdb::optional (); + return gdb::nullopt; }, nullptr); } diff --git a/gdbsupport/gdb_optional.h b/gdbsupport/gdb_optional.h index 02a87f6ee4..d1e70e4be9 100644 --- a/gdbsupport/gdb_optional.h +++ b/gdbsupport/gdb_optional.h @@ -25,12 +25,17 @@ namespace gdb { +struct nullopt_t { + explicit constexpr nullopt_t (int) {} +}; + struct in_place_t { explicit in_place_t () = default; }; constexpr gdb::in_place_t in_place {}; +constexpr nullopt_t nullopt {42}; /* This class attempts to be a compatible subset of std::optional, which is slated to be available in C++17. This class optionally @@ -55,6 +60,9 @@ class optional constexpr optional () : m_dummy () {} + constexpr optional (nullopt_t) + : m_dummy () + {} template constexpr optional (in_place_t, Args &&... args) @@ -93,6 +101,12 @@ class optional /* Assignment operators. */ + optional & + operator= (nullopt_t) + { + reset (); + } + optional & operator= (const optional &other) { -- 2.26.2.761.g0e0b3e54be-goog