From patchwork Tue Apr 4 17:25:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 19833 Received: (qmail 118322 invoked by alias); 4 Apr 2017 17:26:20 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 117911 invoked by uid 89); 4 Apr 2017 17:26:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=convertible, 6437, OOTB X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 04 Apr 2017 17:26:12 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 30F274E4C6 for ; Tue, 4 Apr 2017 17:26:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 30F274E4C6 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 30F274E4C6 Received: from cascais.lan (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id ACD4478216 for ; Tue, 4 Apr 2017 17:26:11 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 14/18] -Wwrite-strings: Add a PyArg_ParseTupleAndKeywords "const char *" overload Date: Tue, 4 Apr 2017 18:25:47 +0100 Message-Id: <1491326751-16180-15-git-send-email-palves@redhat.com> In-Reply-To: <1491326751-16180-1-git-send-email-palves@redhat.com> References: <1491326751-16180-1-git-send-email-palves@redhat.com> -Wwrite-strings flags code like: static char *keywords[] = {"command", "from_tty", "to_string", NULL }; as needing "(char *)" casts, because string literals are "const char []". We can get rid of the casts by changing the array like this: - static char *keywords[] = {"command", "from_tty", "to_string", NULL }; + static const char *keywords[] = {"command", "from_tty", "to_string", NULL }; However, passing the array to PyArg_ParseTupleAndKeywords no longer works OOTB, because PyArg_ParseTupleAndKeywords expects a "char **": PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...); and "const char **" is not implicitly convertible to "char **". C++ is more tolerant that C here WRT aliasing, and a const_cast would be fine. However, to avoid having all callers add the 'const_cast' themselves, this commit define a PyArg_ParseTupleAndKeywords overload here with a corresponding 'keywords' parameter type that does the cast in a single place. As in the PyGetSetDef patch, I'd be fine with naming this gdb_PyArg_ParseTupleAndKeywords instead, if people would find having our own overload confusing. BTW, this API issue was discussed years ago in the python-dev list: https://mail.python.org/pipermail/python-dev/2006-February/060689.html ... and an overload for C++ was suggested in there somewhere. Unfortunately, it was never added. gdb/ChangeLog: yyyy-mm-dd Pedro Alves * python/py-arch.c (archpy_disassemble): Constify 'keywords' array. * python/py-cmd.c (cmdpy_init): Constify 'keywords' array. * python/py-finishbreakpoint.c (bpfinishpy_init): Constify 'keywords' array. * python/py-inferior.c (infpy_read_memory, infpy_write_memory) (infpy_search_memory): Constify 'keywords' array. * python/py-objfile.c (objfpy_add_separate_debug_file) (gdbpy_lookup_objfile): Constify 'keywords' array. * python/py-symbol.c (gdbpy_lookup_symbol) (gdbpy_lookup_global_symbol): Constify 'keywords' array. * python/py-type.c (gdbpy_lookup_type): Constify 'keywords' array. * python/py-value.c (valpy_lazy_string, valpy_string): Constify 'keywords' array. * python/python.c (execute_gdb_command, gdbpy_write, gdbpy_flush): Constify 'keywords' array. * python/python-internal.h (PyArg_ParseTupleAndKeywords): New function overload. --- gdb/python/py-arch.c | 2 +- gdb/python/py-breakpoint.c | 4 ++-- gdb/python/py-cmd.c | 6 +++--- gdb/python/py-finishbreakpoint.c | 2 +- gdb/python/py-inferior.c | 6 +++--- gdb/python/py-objfile.c | 4 ++-- gdb/python/py-symbol.c | 4 ++-- gdb/python/py-type.c | 2 +- gdb/python/py-value.c | 4 ++-- gdb/python/python-internal.h | 25 +++++++++++++++++++++++++ gdb/python/python.c | 6 +++--- 11 files changed, 45 insertions(+), 20 deletions(-) diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index 71d2989..48fae32 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -116,7 +116,7 @@ archpy_name (PyObject *self, PyObject *args) static PyObject * archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw) { - static char *keywords[] = { "start_pc", "end_pc", "count", NULL }; + static const char *keywords[] = { "start_pc", "end_pc", "count", NULL }; CORE_ADDR start, end = 0; CORE_ADDR pc; gdb_py_ulongest start_temp; diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index 724a7ed..2043d42 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -637,8 +637,8 @@ bppy_get_ignore_count (PyObject *self, void *closure) static int bppy_init (PyObject *self, PyObject *args, PyObject *kwargs) { - static char *keywords[] = { "spec", "type", "wp_class", "internal", - "temporary", NULL }; + static const char *keywords[] = { "spec", "type", "wp_class", "internal", + "temporary", NULL }; const char *spec; int type = bp_breakpoint; int access_type = hw_write; diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 3aaf7f9..8f61d9c 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -490,8 +490,8 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw) char *docstring = NULL; struct cmd_list_element **cmd_list; char *cmd_name, *pfx_name; - static char *keywords[] = { "name", "command_class", "completer_class", - "prefix", NULL }; + static const char *keywords[] = { "name", "command_class", "completer_class", + "prefix", NULL }; PyObject *is_prefix = NULL; int cmp; @@ -506,7 +506,7 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw) if (! PyArg_ParseTupleAndKeywords (args, kw, "si|iO", keywords, &name, &cmdtype, - &completetype, &is_prefix)) + &completetype, &is_prefix)) return -1; if (cmdtype != no_class && cmdtype != class_run diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpoint.c index 106fe34..9d43d55 100644 --- a/gdb/python/py-finishbreakpoint.c +++ b/gdb/python/py-finishbreakpoint.c @@ -156,7 +156,7 @@ bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj) static int bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs) { - static char *keywords[] = { "frame", "internal", NULL }; + static const char *keywords[] = { "frame", "internal", NULL }; struct finish_breakpoint_object *self_bpfinish = (struct finish_breakpoint_object *) self; PyObject *frame_obj = NULL; diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 46a0aad..8b4782b 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -435,7 +435,7 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) CORE_ADDR addr, length; gdb_byte *buffer = NULL; PyObject *addr_obj, *length_obj, *result; - static char *keywords[] = { "address", "length", NULL }; + static const char *keywords[] = { "address", "length", NULL }; if (! PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords, &addr_obj, &length_obj)) @@ -494,7 +494,7 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) const gdb_byte *buffer; CORE_ADDR addr, length; PyObject *addr_obj, *length_obj = NULL; - static char *keywords[] = { "address", "buffer", "length", NULL }; + static const char *keywords[] = { "address", "buffer", "length", NULL }; #ifdef IS_PY3K Py_buffer pybuf; @@ -643,7 +643,7 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) { struct gdb_exception except = exception_none; CORE_ADDR start_addr, length; - static char *keywords[] = { "address", "length", "pattern", NULL }; + static const char *keywords[] = { "address", "length", "pattern", NULL }; PyObject *start_addr_obj, *length_obj; Py_ssize_t pattern_size; const gdb_byte *buffer; diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index 105d88a..ad1ec4f 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -432,7 +432,7 @@ objfpy_is_valid (PyObject *self, PyObject *args) static PyObject * objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw) { - static char *keywords[] = { "file_name", NULL }; + static const char *keywords[] = { "file_name", NULL }; objfile_object *obj = (objfile_object *) self; const char *file_name; @@ -559,7 +559,7 @@ objfpy_lookup_objfile_by_build_id (const char *build_id) PyObject * gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw) { - static char *keywords[] = { "name", "by_build_id", NULL }; + static const char *keywords[] = { "name", "by_build_id", NULL }; const char *name; PyObject *by_build_id_obj = NULL; int by_build_id; diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index b71cfb4..de44c06 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -373,7 +373,7 @@ gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw) int domain = VAR_DOMAIN; struct field_of_this_result is_a_field_of_this; const char *name; - static char *keywords[] = { "name", "block", "domain", NULL }; + static const char *keywords[] = { "name", "block", "domain", NULL }; struct symbol *symbol = NULL; PyObject *block_obj = NULL, *sym_obj, *bool_obj; const struct block *block = NULL; @@ -443,7 +443,7 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw) { int domain = VAR_DOMAIN; const char *name; - static char *keywords[] = { "name", "domain", NULL }; + static const char *keywords[] = { "name", "domain", NULL }; struct symbol *symbol = NULL; PyObject *sym_obj; diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index f071006..4f4ede1 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -1347,7 +1347,7 @@ type_object_to_type (PyObject *obj) PyObject * gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw) { - static char *keywords[] = { "name", "block", NULL }; + static const char *keywords[] = { "name", "block", NULL }; const char *type_name = NULL; struct type *type = NULL; PyObject *block_obj = NULL; diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index bb42e8b..1a82af5 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -431,7 +431,7 @@ valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) gdb_py_longest length = -1; struct value *value = ((value_object *) self)->value; const char *user_encoding = NULL; - static char *keywords[] = { "encoding", "length", NULL }; + static const char *keywords[] = { "encoding", "length", NULL }; PyObject *str_obj = NULL; if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords, @@ -526,7 +526,7 @@ valpy_string (PyObject *self, PyObject *args, PyObject *kw) const char *user_encoding = NULL; const char *la_encoding = NULL; struct type *char_type; - static char *keywords[] = { "encoding", "errors", "length", NULL }; + static const char *keywords[] = { "encoding", "errors", "length", NULL }; if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords, &user_encoding, &errors, &length)) diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 8fc89cd..76f7443 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -318,6 +318,31 @@ struct gdb_PyGetSetDef : PyGetSetDef #define PyGetSetDef gdb_PyGetSetDef +/* The 'keywords' parameter of PyArg_ParseTupleAndKeywords has type + 'char **'. However, string literals are const in C++, and so to + avoid casting at every keyword array definition, we'll need to make + the keywords array an array of 'const char *'. To avoid having all + callers add a 'const_cast' themselves when passing such an + array through 'char **', we define a PyArg_ParseTupleAndKeywords + overload here with a corresponding 'keywords' parameter type that + does the cast in a single place. */ + +static inline int +PyArg_ParseTupleAndKeywords (PyObject *args, PyObject *kw, + const char *format, const char **keywords, ...) +{ + va_list ap; + int res; + + va_start (ap, keywords); + res = PyArg_VaParseTupleAndKeywords (args, kw, format, + const_cast (keywords), + ap); + va_end (ap); + + return res; +} + /* In order to be able to parse symtab_and_line_to_sal_object function a real symtab_and_line structure is needed. */ #include "symtab.h" diff --git a/gdb/python/python.c b/gdb/python/python.c index a7aff53..5a46493 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -572,7 +572,7 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw) const char *arg; PyObject *from_tty_obj = NULL, *to_string_obj = NULL; int from_tty, to_string; - static char *keywords[] = {"command", "from_tty", "to_string", NULL }; + static const char *keywords[] = { "command", "from_tty", "to_string", NULL }; if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg, &PyBool_Type, &from_tty_obj, @@ -1047,7 +1047,7 @@ static PyObject * gdbpy_write (PyObject *self, PyObject *args, PyObject *kw) { const char *arg; - static char *keywords[] = {"text", "stream", NULL }; + static const char *keywords[] = { "text", "stream", NULL }; int stream_type = 0; if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg, @@ -1088,7 +1088,7 @@ gdbpy_write (PyObject *self, PyObject *args, PyObject *kw) static PyObject * gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw) { - static char *keywords[] = {"stream", NULL }; + static const char *keywords[] = { "stream", NULL }; int stream_type = 0; if (! PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords,