From: Simon Marchi Date: Mon, 14 Sep 2020 15:12:55 +0000 (-0400) Subject: gdb: change xml_fetch_another a function_view X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=8400a90d19c5c53f7100421c688fb896789048b9;p=binutils-gdb.git gdb: change xml_fetch_another a function_view The xml_fetch_another is currently a plain function pointer type, with a `void *` baton parameter. To improve type-safety, change this to a function_view. Any required data is captured by a lambda at the call site. gdb/ChangeLog: * xml-support.h (xml_fetch_another): Change type to be a function_view. (xml_process_xincludes): Remove baton parameter. (xml_fetch_content_from_file): Change baton parameter to dirname. * xml-support.c (struct xinclude_parsing_data) : Remove baton parameter. : Remove. (xinclude_start_include): Adjust. (xml_process_xincludes): Adjust. (xml_fetch_content_from_file): Replace baton parameter with dirname. * xml-syscall.c (syscall_parse_xml): Remove baton parameter. (xml_init_syscalls_info): Use a lambda. * xml-tdesc.c (tdesc_parse_xml): Remove baton parameter. (file_read_description_xml): Use a lambda. (fetch_available_features_from_target): Change baton parameter to target_ops. (target_read_description_xml): Use a lambda. (target_fetch_description_xml): Use a lambda. (string_read_description_xml): Update. Change-Id: I7ba4b8f5e97fc6a952c6c20ccc3be92a06cc2bd2 --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 0ccbb891924..8451cadcd5b 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,27 @@ +2020-09-14 Simon Marchi + + * xml-support.h (xml_fetch_another): Change type to be a + function_view. + (xml_process_xincludes): Remove baton parameter. + (xml_fetch_content_from_file): Change baton parameter to + dirname. + * xml-support.c (struct xinclude_parsing_data) + : Remove baton parameter. + : Remove. + (xinclude_start_include): Adjust. + (xml_process_xincludes): Adjust. + (xml_fetch_content_from_file): Replace baton parameter with + dirname. + * xml-syscall.c (syscall_parse_xml): Remove baton parameter. + (xml_init_syscalls_info): Use a lambda. + * xml-tdesc.c (tdesc_parse_xml): Remove baton parameter. + (file_read_description_xml): Use a lambda. + (fetch_available_features_from_target): Change baton parameter + to target_ops. + (target_read_description_xml): Use a lambda. + (target_fetch_description_xml): Use a lambda. + (string_read_description_xml): Update. + 2020-09-14 Simon Marchi * gdbtypes.h (TYPE_ENDIANITY_NOT_DEFAULT): Remove, replace all diff --git a/gdb/xml-support.c b/gdb/xml-support.c index c906b69fda0..8b698e511f1 100644 --- a/gdb/xml-support.c +++ b/gdb/xml-support.c @@ -745,13 +745,12 @@ gdb_xml_parse_attr_enum (struct gdb_xml_parser *parser, struct xinclude_parsing_data { xinclude_parsing_data (std::string &output_, - xml_fetch_another fetcher_, void *fetcher_baton_, + xml_fetch_another fetcher_, int include_depth_) : output (output_), skip_depth (0), include_depth (include_depth_), - fetcher (fetcher_), - fetcher_baton (fetcher_baton_) + fetcher (fetcher_) {} /* Where the output goes. */ @@ -770,7 +769,6 @@ struct xinclude_parsing_data /* A function to call to obtain additional features, and its baton. */ xml_fetch_another fetcher; - void *fetcher_baton; }; static void @@ -789,14 +787,12 @@ xinclude_start_include (struct gdb_xml_parser *parser, gdb_xml_error (parser, _("Maximum XInclude depth (%d) exceeded"), MAX_XINCLUDE_DEPTH); - gdb::optional text - = data->fetcher (href, data->fetcher_baton); + gdb::optional text = data->fetcher (href); if (!text) gdb_xml_error (parser, _("Could not load XML document \"%s\""), href); if (!xml_process_xincludes (data->output, parser->name (), text->data (), data->fetcher, - data->fetcher_baton, data->include_depth + 1)) gdb_xml_error (parser, _("Parsing \"%s\" failed"), href); @@ -878,10 +874,9 @@ const struct gdb_xml_element xinclude_elements[] = { bool xml_process_xincludes (std::string &result, const char *name, const char *text, - xml_fetch_another fetcher, void *fetcher_baton, - int depth) + xml_fetch_another fetcher, int depth) { - xinclude_parsing_data data (result, fetcher, fetcher_baton, depth); + xinclude_parsing_data data (result, fetcher, depth); gdb_xml_parser parser (name, xinclude_elements, &data); parser.set_is_xinclude (true); @@ -968,12 +963,11 @@ show_debug_xml (struct ui_file *file, int from_tty, } gdb::optional -xml_fetch_content_from_file (const char *filename, void *baton) +xml_fetch_content_from_file (const char *filename, const char *dirname) { - const char *dirname = (const char *) baton; gdb_file_up file; - if (dirname && *dirname) + if (dirname != nullptr && *dirname != '\0') { char *fullname = concat (dirname, "/", filename, (char *) NULL); diff --git a/gdb/xml-support.h b/gdb/xml-support.h index 7880aaa97fb..429d9ca2e99 100644 --- a/gdb/xml-support.h +++ b/gdb/xml-support.h @@ -25,6 +25,7 @@ #include "gdbsupport/xml-utils.h" #include "gdbsupport/byte-vector.h" #include "gdbsupport/gdb_optional.h" +#include "gdbsupport/function-view.h" struct gdb_xml_parser; struct gdb_xml_element; @@ -48,12 +49,12 @@ LONGEST xml_builtin_xfer_partial (const char *filename, /* Callback to fetch a new XML file, based on the provided HREF. */ -typedef gdb::optional (*xml_fetch_another) (const char *href, - void *baton); +using xml_fetch_another = gdb::function_view + (const char * /* href */)>; /* Append the expansion of TEXT after processing tags in - RESULT. FETCHER will be called (with FETCHER_BATON) to retrieve - any new files. DEPTH should be zero on the initial call. + RESULT. FETCHER will be called to retrieve any new files. DEPTH + should be zero on the initial call. On failure, this function uses NAME in a warning and returns false. It may throw an exception, but does not for XML parsing @@ -61,8 +62,7 @@ typedef gdb::optional (*xml_fetch_another) (const char *href, bool xml_process_xincludes (std::string &result, const char *name, const char *text, - xml_fetch_another fetcher, void *fetcher_baton, - int depth); + xml_fetch_another fetcher, int depth); /* Simplified XML parser infrastructure. */ @@ -231,6 +231,6 @@ ULONGEST gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, and warn. */ extern gdb::optional xml_fetch_content_from_file - (const char *filename, void *baton); + (const char *filename, const char *dirname); #endif diff --git a/gdb/xml-syscall.c b/gdb/xml-syscall.c index 8cca875c740..effb37960ee 100644 --- a/gdb/xml-syscall.c +++ b/gdb/xml-syscall.c @@ -287,8 +287,7 @@ static const struct gdb_xml_element syselements[] = { }; static struct syscalls_info * -syscall_parse_xml (const char *document, xml_fetch_another fetcher, - void *fetcher_baton) +syscall_parse_xml (const char *document, xml_fetch_another fetcher) { struct syscall_parsing_data data; syscalls_info_up sysinfo (new syscalls_info ()); @@ -322,9 +321,13 @@ xml_init_syscalls_info (const char *filename) if (!full_file) return NULL; - return syscall_parse_xml (full_file->data (), - xml_fetch_content_from_file, - (void *) ldirname (filename).c_str ()); + const std::string dirname = ldirname (filename); + auto fetch_another = [&dirname] (const char *name) + { + return xml_fetch_content_from_file (name, dirname.c_str ()); + }; + + return syscall_parse_xml (full_file->data (), fetch_another); } /* Initializes the syscalls_info structure according to the diff --git a/gdb/xml-tdesc.c b/gdb/xml-tdesc.c index 6f108a4e3ef..6e21ff42daf 100644 --- a/gdb/xml-tdesc.c +++ b/gdb/xml-tdesc.c @@ -617,8 +617,7 @@ static const struct gdb_xml_element tdesc_elements[] = { /* Parse DOCUMENT into a target description and return it. */ static struct target_desc * -tdesc_parse_xml (const char *document, xml_fetch_another fetcher, - void *fetcher_baton) +tdesc_parse_xml (const char *document, xml_fetch_another fetcher) { struct tdesc_parsing_data data; @@ -627,7 +626,7 @@ tdesc_parse_xml (const char *document, xml_fetch_another fetcher, if (!xml_process_xincludes (expanded_text, _("target description"), - document, fetcher, fetcher_baton, 0)) + document, fetcher, 0)) { warning (_("Could not load XML target description; ignoring")); return NULL; @@ -673,8 +672,13 @@ file_read_description_xml (const char *filename) return NULL; } - return tdesc_parse_xml (tdesc_str->data (), xml_fetch_content_from_file, - (void *) ldirname (filename).c_str ()); + const std::string dirname = ldirname (filename); + auto fetch_another = [&dirname] (const char *name) + { + return xml_fetch_content_from_file (name, dirname.c_str ()); + }; + + return tdesc_parse_xml (tdesc_str->data (), fetch_another); } /* Read a string representation of available features from the target, @@ -685,10 +689,8 @@ file_read_description_xml (const char *filename) for . */ static gdb::optional -fetch_available_features_from_target (const char *name, void *baton_) +fetch_available_features_from_target (const char *name, target_ops *ops) { - struct target_ops *ops = (struct target_ops *) baton_; - /* Read this object as a string. This ensures that a NUL terminator is added. */ return target_read_stralloc (ops, @@ -708,9 +710,12 @@ target_read_description_xml (struct target_ops *ops) if (!tdesc_str) return NULL; - return tdesc_parse_xml (tdesc_str->data (), - fetch_available_features_from_target, - ops); + auto fetch_another = [ops] (const char *name) + { + return fetch_available_features_from_target (name, ops); + }; + + return tdesc_parse_xml (tdesc_str->data (), fetch_another); } /* Fetches an XML target description using OPS, processing @@ -737,11 +742,14 @@ target_fetch_description_xml (struct target_ops *ops) if (!tdesc_str) return {}; + auto fetch_another = [ops] (const char *name) + { + return fetch_available_features_from_target (name, ops); + }; std::string output; if (!xml_process_xincludes (output, _("target description"), - tdesc_str->data (), - fetch_available_features_from_target, ops, 0)) + tdesc_str->data (), fetch_another, 0)) { warning (_("Could not load XML target description; ignoring")); return {}; @@ -755,9 +763,9 @@ target_fetch_description_xml (struct target_ops *ops) const struct target_desc * string_read_description_xml (const char *xml) { - return tdesc_parse_xml (xml, [] (const char *href, void *baton) + return tdesc_parse_xml (xml, [] (const char *href) { error (_("xincludes are unsupported with this method")); return gdb::optional (); - }, nullptr); + }); }