-Wwrite-strings: Add a PyArg_ParseTupleAndKeywords "const char *" overload
authorPedro Alves <palves@redhat.com>
Wed, 5 Apr 2017 18:21:36 +0000 (19:21 +0100)
committerPedro Alves <palves@redhat.com>
Wed, 5 Apr 2017 18:21:36 +0000 (19:21 +0100)
-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 type like this:

 -  static char *keywords[] = {"command", "from_tty", "to_string", NULL };
 +  static const char *keywords[] = {"command", "from_tty", "to_string", NULL };

However, passing the such 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<char **>
is fine.  However, to avoid having all callers do the cast themselves,
this commit defines a gdb_PyArg_ParseTupleAndKeywords function here
with a corresponding 'keywords' parameter type that does the cast in a
single place.

gdb/ChangeLog:
2017-04-05  Pedro Alves  <palves@redhat.com>

* python/python-internal.h (gdb_PyArg_ParseTupleAndKeywords): New
static inline function.
* python/py-arch.c (archpy_disassemble): Constify 'keywords'
array and use gdb_PyArg_ParseTupleAndKeywords.
* python/py-cmd.c (cmdpy_init): Likewise.
* python/py-finishbreakpoint.c (bpfinishpy_init): Likewise.
* python/py-inferior.c (infpy_read_memory, infpy_write_memory)
(infpy_search_memory): Likewise.
* python/py-objfile.c (objfpy_add_separate_debug_file)
(gdbpy_lookup_objfile): Likewise.
* python/py-symbol.c (gdbpy_lookup_symbol)
(gdbpy_lookup_global_symbol): Likewise.
* python/py-type.c (gdbpy_lookup_type): Likewise.
* python/py-value.c (valpy_lazy_string, valpy_string): Likewise.
* python/python.c (execute_gdb_command, gdbpy_write, gdbpy_flush):
Likewise.

12 files changed:
gdb/ChangeLog
gdb/python/py-arch.c
gdb/python/py-breakpoint.c
gdb/python/py-cmd.c
gdb/python/py-finishbreakpoint.c
gdb/python/py-inferior.c
gdb/python/py-objfile.c
gdb/python/py-symbol.c
gdb/python/py-type.c
gdb/python/py-value.c
gdb/python/python-internal.h
gdb/python/python.c

index 28557c2700502e895f9abcaec5b16f583ae714f2..610a1d04abf20abfa554c97a792b8a2d0e14457a 100644 (file)
@@ -1,3 +1,22 @@
+2017-04-05  Pedro Alves  <palves@redhat.com>
+
+       * python/python-internal.h (gdb_PyArg_ParseTupleAndKeywords): New
+       static inline function.
+       * python/py-arch.c (archpy_disassemble): Constify 'keywords'
+       array and use gdb_PyArg_ParseTupleAndKeywords.
+       * python/py-cmd.c (cmdpy_init): Likewise.
+       * python/py-finishbreakpoint.c (bpfinishpy_init): Likewise.
+       * python/py-inferior.c (infpy_read_memory, infpy_write_memory)
+       (infpy_search_memory): Likewise.
+       * python/py-objfile.c (objfpy_add_separate_debug_file)
+       (gdbpy_lookup_objfile): Likewise.
+       * python/py-symbol.c (gdbpy_lookup_symbol)
+       (gdbpy_lookup_global_symbol): Likewise.
+       * python/py-type.c (gdbpy_lookup_type): Likewise.
+       * python/py-value.c (valpy_lazy_string, valpy_string): Likewise.
+       * python/python.c (execute_gdb_command, gdbpy_write, gdbpy_flush):
+       Likewise.
+
 2017-04-05  Pedro Alves  <palves@redhat.com>
 
        * python/python-internal.h (gdb_PyGetSetDef): New type.
index 71d298973a018ea2df99132b3f6c6f8abf241946..cfadb47e43d2dce02e52afbeaec5527c16bdd6af 100644 (file)
@@ -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;
@@ -126,8 +126,9 @@ archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
 
   ARCHPY_REQUIRE_VALID (self, gdbarch);
 
-  if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords,
-                                    &start_temp, &end_obj, &count_obj))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO",
+                                       keywords, &start_temp, &end_obj,
+                                       &count_obj))
     return NULL;
 
   start = start_temp;
index 34f46fb7a42a3c3a5106ebace68399f8c44290f4..a08501ee2334a29707bb38a419f979441547074e 100644 (file)
@@ -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;
@@ -647,9 +647,9 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
   int internal_bp = 0;
   int temporary_bp = 0;
 
-  if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiOO", keywords,
-                                    &spec, &type, &access_type,
-                                    &internal, &temporary))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiOO", keywords,
+                                       &spec, &type, &access_type,
+                                       &internal, &temporary))
     return -1;
 
   if (internal)
index 3aaf7f943541fc584ba718c7a7ae0ac9737543b6..3e60673ca23f9f86d62b5cdacb2d7db32a30d210 100644 (file)
@@ -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;
 
@@ -504,9 +504,9 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
       return -1;
     }
 
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "si|iO",
-                                    keywords, &name, &cmdtype,
-                         &completetype, &is_prefix))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "si|iO",
+                                       keywords, &name, &cmdtype,
+                                       &completetype, &is_prefix))
     return -1;
 
   if (cmdtype != no_class && cmdtype != class_run
index 76189b8a1deb472eb36b8f191c2ff329b95d74a6..06f6ff90a01d3b971e931f315570796784e506cc 100644 (file)
@@ -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;
@@ -169,8 +169,8 @@ bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
   CORE_ADDR pc;
   struct symbol *function;
 
-  if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
-                                    &frame_obj, &internal))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
+                                       &frame_obj, &internal))
     return -1;
 
   TRY
index 77fc543dcabcb409c4241e04ce86b9aeda84e1c4..4b43c54e843e8fbc8493da036f8cab8506362e02 100644 (file)
@@ -435,10 +435,10 @@ 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))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords,
+                                       &addr_obj, &length_obj))
     return NULL;
 
   if (get_addr_from_python (addr_obj, &addr) < 0
@@ -494,21 +494,20 @@ 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;
 
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "Os*|O", keywords,
-                                    &addr_obj, &pybuf,
-                                    &length_obj))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "Os*|O", keywords,
+                                       &addr_obj, &pybuf, &length_obj))
     return NULL;
 
   buffer = (const gdb_byte *) pybuf.buf;
   buf_len = pybuf.len;
 #else
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords,
-                                    &addr_obj, &buffer, &buf_len,
-                                    &length_obj))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords,
+                                       &addr_obj, &buffer, &buf_len,
+                                       &length_obj))
     return NULL;
 
   buffer = (const gdb_byte *) buffer;
@@ -643,7 +642,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;
@@ -652,9 +651,9 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
 #ifdef IS_PY3K
   Py_buffer pybuf;
 
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "OOs*", keywords,
-                                    &start_addr_obj, &length_obj,
-                                    &pybuf))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OOs*", keywords,
+                                       &start_addr_obj, &length_obj,
+                                       &pybuf))
     return NULL;
 
   buffer = (const gdb_byte *) pybuf.buf;
@@ -663,9 +662,9 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
   PyObject *pattern;
   const void *vbuffer;
 
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
-                                    &start_addr_obj, &length_obj,
-                                    &pattern))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
+                                       &start_addr_obj, &length_obj,
+                                       &pattern))
      return NULL;
 
   if (!PyObject_CheckReadBuffer (pattern))
index 6a47c1703409fca8032ed007a7c32138428d6b9f..52f2e1f79268c516cfb53773817752502753d211 100644 (file)
@@ -432,13 +432,13 @@ 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;
 
   OBJFPY_REQUIRE_VALID (obj);
 
-  if (!PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
     return NULL;
 
   TRY
@@ -559,14 +559,14 @@ 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;
   struct objfile *objfile;
 
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
-                                    &name, &PyBool_Type, &by_build_id_obj))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
+                                       &name, &PyBool_Type, &by_build_id_obj))
     return NULL;
 
   by_build_id = 0;
index 05b002fe725cf6c002d5fedb73fab9f6fe408b6c..c9f83a3c23f89c63e1861934a478bbbb3ef8f2b9 100644 (file)
@@ -373,13 +373,14 @@ 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;
 
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
-                                    &block_object_type, &block_obj, &domain))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
+                                       &block_object_type, &block_obj,
+                                       &domain))
     return NULL;
 
   if (block_obj)
@@ -443,12 +444,12 @@ 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;
 
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
-                                    &domain))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
+                                       &domain))
     return NULL;
 
   TRY
index 12b631086d996d5953be1a95d80f304bc350be22..aa20d4ca82a761faf4ea919c7fe2ad8fa3c833a6 100644 (file)
@@ -1347,14 +1347,14 @@ 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;
   const struct block *block = NULL;
 
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
-                                    &type_name, &block_obj))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
+                                       &type_name, &block_obj))
     return NULL;
 
   if (block_obj)
index 9c0470f8127b9c906875e48f72e154d0a8c0c657..b5a0ef86d24aa0625dc4c5829057abbc2c2a4288 100644 (file)
@@ -431,11 +431,11 @@ 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,
-                                   &user_encoding, &length))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG,
+                                       keywords, &user_encoding, &length))
     return NULL;
 
   if (length < -1)
@@ -526,10 +526,10 @@ 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))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
+                                       &user_encoding, &errors, &length))
     return NULL;
 
   TRY
index 027faa59fa4b8ed0ed148f74e83e00a06b89ecb1..e84c8d29be71b14616c743f50c390bc313355ed5 100644 (file)
@@ -316,6 +316,34 @@ struct gdb_PyGetSetDef : 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<char **>' themselves when passing such an
+   array through 'char **', we define our own version of
+   PyArg_ParseTupleAndKeywords here with a corresponding 'keywords'
+   parameter type that does the cast in a single place.  (This is not
+   an overload of PyArg_ParseTupleAndKeywords in order to make it
+   clearer that we're calling our own function instead of a function
+   that exists in some newer Python version.)  */
+
+static inline int
+gdb_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<char **> (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"
index a7aff5336c92eada42e898928bfa5ea2558031ac..25f475ff39df7af8fbfc69b2393550179b3a2e7c 100644 (file)
@@ -572,11 +572,11 @@ 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,
-                                    &PyBool_Type, &to_string_obj))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
+                                       &PyBool_Type, &from_tty_obj,
+                                       &PyBool_Type, &to_string_obj))
     return NULL;
 
   from_tty = 0;
@@ -1047,11 +1047,11 @@ 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,
-                                    &stream_type))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg,
+                                       &stream_type))
     return NULL;
 
   TRY
@@ -1088,11 +1088,11 @@ 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,
-                                    &stream_type))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords,
+                                       &stream_type))
     return NULL;
 
   switch (stream_type)