gdb/testsuite/tui: Introduce check_box_contents
[deliverable/binutils-gdb.git] / gdb / python / py-linetable.c
index e83d46d70523371ce124c2fe11247303a41b75e6..858313bb22d033238c0a9862ac34edf50ab084c4 100644 (file)
@@ -1,6 +1,6 @@
 /* Python interface to line tables.
 
-   Copyright (C) 2013-2014 Free Software Foundation, Inc.
+   Copyright (C) 2013-2020 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -19,7 +19,6 @@
 
 #include "defs.h"
 #include "python-internal.h"
-#include "exceptions.h"
 
 typedef struct {
   PyObject_HEAD
@@ -29,7 +28,7 @@ typedef struct {
   CORE_ADDR pc;
 } linetable_entry_object;
 
-static PyTypeObject linetable_entry_object_type
+extern PyTypeObject linetable_entry_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("linetable_entry_object");
 
 typedef struct {
@@ -40,7 +39,7 @@ typedef struct {
   PyObject *symtab;
 } linetable_object;
 
-static PyTypeObject linetable_object_type
+extern PyTypeObject linetable_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("linetable_object");
 
 typedef struct {
@@ -53,10 +52,10 @@ typedef struct {
   PyObject *source;
 } ltpy_iterator_object;
 
-static PyTypeObject ltpy_iterator_object_type
+extern PyTypeObject ltpy_iterator_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("ltpy_iterator_object");
 
-/* Internal helper function to extract gdb.Symtab from a gdb.Linetable
+/* Internal helper function to extract gdb.Symtab from a gdb.LineTable
    object.  */
 
 static PyObject *
@@ -115,49 +114,37 @@ build_linetable_entry (int line, CORE_ADDR address)
   return (PyObject *) obj;
 }
 
-/* Internal helper function to build a Python Tuple from a GDB Vector.
+/* Internal helper function to build a Python Tuple from a vector.
    A line table entry can have multiple PCs for a given source line.
    Construct a Tuple of all entries for the given source line, LINE
-   from the line table VEC.  Construct one line table entry object per
+   from the line table PCS.  Construct one line table entry object per
    address.  */
 
 static PyObject *
-build_line_table_tuple_from_pcs (int line, VEC (CORE_ADDR) *vec)
+build_line_table_tuple_from_pcs (int line, const std::vector<CORE_ADDR> &pcs)
 {
-  int vec_len = 0;
-  PyObject *tuple;
-  CORE_ADDR pc;
   int i;
 
-  vec_len = VEC_length (CORE_ADDR, vec);
-  if (vec_len < 1)
+  if (pcs.size () < 1)
     Py_RETURN_NONE;
 
-  tuple = PyTuple_New (vec_len);
+  gdbpy_ref<> tuple (PyTuple_New (pcs.size ()));
 
   if (tuple == NULL)
     return NULL;
 
-  for (i = 0; VEC_iterate (CORE_ADDR, vec, i, pc); ++i)
+  for (i = 0; i < pcs.size (); ++i)
     {
-      PyObject *obj = build_linetable_entry (line, pc);
+      CORE_ADDR pc = pcs[i];
+      gdbpy_ref<> obj (build_linetable_entry (line, pc));
 
       if (obj == NULL)
-       {
-         Py_DECREF (tuple);
-         tuple = NULL;
-         break;
-       }
-      else if (PyTuple_SetItem (tuple, i, obj) != 0)
-       {
-         Py_DECREF (obj);
-         Py_DECREF (tuple);
-         tuple = NULL;
-         break;
-       }
+       return NULL;
+      else if (PyTuple_SetItem (tuple.get (), i, obj.release ()) != 0)
+       return NULL;
     }
 
-  return tuple;
+  return tuple.release ();
 }
 
 /* Implementation of gdb.LineTable.line (self) -> Tuple.  Returns a
@@ -168,28 +155,25 @@ static PyObject *
 ltpy_get_pcs_for_line (PyObject *self, PyObject *args)
 {
   struct symtab *symtab;
-  int py_line;
+  gdb_py_longest py_line;
   struct linetable_entry *best_entry = NULL;
-  linetable_entry_object *result;
-  VEC (CORE_ADDR) *pcs = NULL;
-  PyObject *tuple;
-  volatile struct gdb_exception except;
+  std::vector<CORE_ADDR> pcs;
 
   LTPY_REQUIRE_VALID (self, symtab);
 
   if (! PyArg_ParseTuple (args, GDB_PY_LL_ARG, &py_line))
     return NULL;
 
-  TRY_CATCH (except, RETURN_MASK_ALL)
+  try
     {
       pcs = find_pcs_for_symtab_line (symtab, py_line, &best_entry);
     }
-  GDB_PY_HANDLE_EXCEPTION (except);
-
-  tuple = build_line_table_tuple_from_pcs (py_line, pcs);
-  VEC_free (CORE_ADDR, pcs);
+  catch (const gdb_exception &except)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
 
-  return tuple;
+  return build_line_table_tuple_from_pcs (py_line, pcs);
 }
 
 /* Implementation of gdb.LineTable.has_line (self, line) -> Boolean.
@@ -200,7 +184,7 @@ static PyObject *
 ltpy_has_line (PyObject *self, PyObject *args)
 {
   struct symtab *symtab;
-  int py_line;
+  gdb_py_longest py_line;
   int index;
 
   LTPY_REQUIRE_VALID (self, symtab);
@@ -208,16 +192,16 @@ ltpy_has_line (PyObject *self, PyObject *args)
   if (! PyArg_ParseTuple (args, GDB_PY_LL_ARG, &py_line))
     return NULL;
 
-  if (LINETABLE (symtab) == NULL)
+  if (SYMTAB_LINETABLE (symtab) == NULL)
     {
       PyErr_SetString (PyExc_RuntimeError,
                       _("Linetable information not found in symbol table"));
       return NULL;
     }
 
-  for (index = 0; index < LINETABLE (symtab)->nitems; index++)
+  for (index = 0; index < SYMTAB_LINETABLE (symtab)->nitems; index++)
     {
-      struct linetable_entry *item = &(symtab->linetable->item[index]);
+      struct linetable_entry *item = &(SYMTAB_LINETABLE (symtab)->item[index]);
       if (item->line == py_line)
          Py_RETURN_TRUE;
     }
@@ -225,8 +209,8 @@ ltpy_has_line (PyObject *self, PyObject *args)
   Py_RETURN_FALSE;
 }
 
-/* Implementation of gdb.LineTable.source_lines (self) -> FrozenSet.
-   Returns a Python FrozenSet that contains source line entries in the
+/* Implementation of gdb.LineTable.source_lines (self) -> List.
+   Returns a Python List that contains source line entries in the
    line table.  This function will just return the source lines
    without corresponding addresses.  */
 
@@ -235,65 +219,49 @@ ltpy_get_all_source_lines (PyObject *self, PyObject *args)
 {
   struct symtab *symtab;
   Py_ssize_t index;
-  PyObject *source_list, *source_dict, *line;
   struct linetable_entry *item;
-  Py_ssize_t list_size;
 
   LTPY_REQUIRE_VALID (self, symtab);
 
-  if (LINETABLE (symtab) == NULL)
+  if (SYMTAB_LINETABLE (symtab) == NULL)
     {
       PyErr_SetString (PyExc_RuntimeError,
                       _("Linetable information not found in symbol table"));
       return NULL;
     }
 
-  source_dict = PyDict_New ();
+  gdbpy_ref<> source_dict (PyDict_New ());
   if (source_dict == NULL)
     return NULL;
 
-  for (index = 0; index < LINETABLE (symtab)->nitems; index++)
+  for (index = 0; index < SYMTAB_LINETABLE (symtab)->nitems; index++)
     {
-      item = &(LINETABLE (symtab)->item[index]);
+      item = &(SYMTAB_LINETABLE (symtab)->item[index]);
 
       /* 0 is used to signify end of line table information.  Do not
         include in the source set. */
       if (item->line > 0)
        {
-         line = gdb_py_object_from_longest (item->line);
+         gdbpy_ref<> line = gdb_py_object_from_longest (item->line);
 
          if (line == NULL)
-           {
-             Py_DECREF (source_dict);
-             return NULL;
-           }
-
-         if (PyDict_SetItem (source_dict, line, Py_None) == -1)
-           {
-             Py_DECREF (line);
-             Py_DECREF (source_dict);
-             return NULL;
-           }
-
-         Py_DECREF (line);
+           return NULL;
+
+         if (PyDict_SetItem (source_dict.get (), line.get (), Py_None) == -1)
+           return NULL;
        }
     }
 
-
-  source_list = PyDict_Keys (source_dict);
-  Py_DECREF (source_dict);
-
-  return source_list;
+  return PyDict_Keys (source_dict.get ());
 }
 
-/* Implementation of gdb.Linetable.is_valid (self) -> Boolean.
+/* Implementation of gdb.LineTable.is_valid (self) -> Boolean.
    Returns True if this line table object still exists in GDB.  */
 
 static PyObject *
 ltpy_is_valid (PyObject *self, PyObject *args)
 {
   struct symtab *symtab = NULL;
-  linetable_object *obj = (linetable_object *) self;
 
   symtab = symtab_object_to_symtab (get_symtab (self));
 
@@ -347,7 +315,7 @@ gdbpy_initialize_linetable (void)
   return 0;
 }
 
-/* Linetable entry object get functions.  */
+/* LineTable entry object get functions.  */
 
 /* Implementation of gdb.LineTableEntry.line (self) -> Long.  Returns
    a long integer associated with the line table entry.  */
@@ -357,7 +325,7 @@ ltpy_entry_get_line (PyObject *self, void *closure)
 {
   linetable_entry_object *obj = (linetable_entry_object *) self;
 
-  return gdb_py_object_from_longest (obj->line);
+  return gdb_py_object_from_longest (obj->line).release ();
 }
 
 /* Implementation of gdb.LineTableEntry.pc (self) -> Long.  Returns a
@@ -368,10 +336,10 @@ ltpy_entry_get_pc (PyObject *self, void *closure)
 {
   linetable_entry_object *obj = (linetable_entry_object *) self;
 
-  return  gdb_py_object_from_longest (obj->pc);
+  return  gdb_py_object_from_longest (obj->pc).release ();
 }
 
-/* Linetable iterator functions.  */
+/* LineTable iterator functions.  */
 
 /* Return a new line table iterator.  */
 
@@ -401,6 +369,7 @@ ltpy_iterator_dealloc (PyObject *obj)
   ltpy_iterator_object *iter_obj = (ltpy_iterator_object *) obj;
 
   Py_DECREF (iter_obj->source);
+  Py_TYPE (obj)->tp_free (obj);
 }
 
 /* Return a reference to the line table iterator.  */
@@ -425,16 +394,18 @@ ltpy_iternext (PyObject *self)
 {
   ltpy_iterator_object *iter_obj = (ltpy_iterator_object *) self;
   struct symtab *symtab;
-  int index;
   PyObject *obj;
   struct linetable_entry *item;
 
   LTPY_REQUIRE_VALID (iter_obj->source, symtab);
 
-  if (iter_obj->current_index >= LINETABLE (symtab)->nitems)
-    goto stop_iteration;
+  if (iter_obj->current_index >= SYMTAB_LINETABLE (symtab)->nitems)
+    {
+      PyErr_SetNone (PyExc_StopIteration);
+      return NULL;
+    }
 
-  item = &(LINETABLE (symtab)->item[iter_obj->current_index]);
+  item = &(SYMTAB_LINETABLE (symtab)->item[iter_obj->current_index]);
 
   /* Skip over internal entries such as 0.  0 signifies the end of
      line table data and is not useful to the API user.  */
@@ -443,22 +414,21 @@ ltpy_iternext (PyObject *self)
       iter_obj->current_index++;
 
       /* Exit if the internal value is the last item in the line table.  */
-      if (iter_obj->current_index >= symtab->linetable->nitems)
-       goto stop_iteration;
-      item = &(symtab->linetable->item[iter_obj->current_index]);
+      if (iter_obj->current_index >= SYMTAB_LINETABLE (symtab)->nitems)
+       {
+         PyErr_SetNone (PyExc_StopIteration);
+         return NULL;
+       }
+      item = &(SYMTAB_LINETABLE (symtab)->item[iter_obj->current_index]);
     }
 
   obj = build_linetable_entry (item->line, item->pc);
   iter_obj->current_index++;
 
   return obj;
-
- stop_iteration:
-  PyErr_SetNone (PyExc_StopIteration);
-  return NULL;
 }
 
-/* Implementation of gdb.LinetableIterator.is_valid (self) -> Boolean.
+/* Implementation of gdb.LineTableIterator.is_valid (self) -> Boolean.
    Returns True if this line table iterator object still exists in
    GDB.  */
 
@@ -486,15 +456,15 @@ Return executable locations for a given source line." },
     "has_line (lineno) -> Boolean\n\
 Return TRUE if this line has executable information, FALSE if not." },
   { "source_lines", ltpy_get_all_source_lines, METH_NOARGS,
-    "source_lines () -> FrozenSet\n\
-Return a frozen set of all executable source lines." },
+    "source_lines () -> List\n\
+Return a list of all executable source lines." },
   { "is_valid", ltpy_is_valid, METH_NOARGS,
     "is_valid () -> Boolean.\n\
-Return True if this Linetable is valid, False if not." },
+Return True if this LineTable is valid, False if not." },
   {NULL}  /* Sentinel */
 };
 
-static PyTypeObject linetable_object_type = {
+PyTypeObject linetable_object_type = {
   PyVarObject_HEAD_INIT (NULL, 0)
   "gdb.LineTable",               /*tp_name*/
   sizeof (linetable_object),     /*tp_basicsize*/
@@ -537,11 +507,11 @@ static PyTypeObject linetable_object_type = {
 static PyMethodDef ltpy_iterator_methods[] = {
   { "is_valid", ltpy_iter_is_valid, METH_NOARGS,
     "is_valid () -> Boolean.\n\
-Return True if this Linetable iterator is valid, False if not." },
+Return True if this LineTable iterator is valid, False if not." },
   {NULL}  /* Sentinel */
 };
 
-static PyTypeObject ltpy_iterator_object_type = {
+PyTypeObject ltpy_iterator_object_type = {
   PyVarObject_HEAD_INIT (NULL, 0)
   "gdb.LineTableIterator",               /*tp_name*/
   sizeof (ltpy_iterator_object),  /*tp_basicsize*/
@@ -573,7 +543,7 @@ static PyTypeObject ltpy_iterator_object_type = {
 };
 
 
-static PyGetSetDef linetable_entry_object_getset[] = {
+static gdb_PyGetSetDef linetable_entry_object_getset[] = {
   { "line", ltpy_entry_get_line, NULL,
     "The line number in the source file.", NULL },
   { "pc", ltpy_entry_get_pc, NULL,
@@ -581,7 +551,7 @@ static PyGetSetDef linetable_entry_object_getset[] = {
   { NULL }  /* Sentinel */
 };
 
-static PyTypeObject linetable_entry_object_type = {
+PyTypeObject linetable_entry_object_type = {
   PyVarObject_HEAD_INIT (NULL, 0)
   "gdb.LineTableEntry",                  /*tp_name*/
   sizeof (linetable_entry_object), /*tp_basicsize*/
This page took 0.029168 seconds and 4 git commands to generate.