Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 2/4] gdb/python: collapse some nested ifs
Date: Mon, 14 Sep 2026 15:19:02 -0400	[thread overview]
Message-ID: <20260914191908.557014-2-simon.marchi@efficios.com> (raw)
In-Reply-To: <20260914191908.557014-1-simon.marchi@efficios.com>

While reviewing another patch in gdb/python, I found that some nested
"ifs" could be collapsed into one.  I had Claude hunt for more cases
like that throughout gdb/python, and this patch changes them.

Change-Id: I7cc50a08ec18d85106c2dfebca803a1e354a6582
---
 gdb/python/py-breakpoint.c  | 31 +++++++++---------------
 gdb/python/py-connection.c  |  6 ++---
 gdb/python/py-framefilter.c | 48 ++++++++++++++++---------------------
 gdb/python/py-function.c    | 14 ++++-------
 gdb/python/py-param.c       | 10 ++++----
 gdb/python/py-prettyprint.c | 18 +++++++-------
 gdb/python/py-progspace.c   |  8 +++----
 gdb/python/python.c         | 12 ++++------
 8 files changed, 59 insertions(+), 88 deletions(-)

diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 2d8076ad2a73..2db756111d68 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -1263,11 +1263,9 @@ gdbpy_breakpoint_created (struct breakpoint *bp)
       gdbpy_print_stack ();
     }
 
-  if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
-    {
-      if (evpy_emit_event (newbp, gdb_py_events.breakpoint_created) < 0)
-	gdbpy_print_stack ();
-    }
+  if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created)
+      && evpy_emit_event (newbp, gdb_py_events.breakpoint_created) < 0)
+    gdbpy_print_stack ();
 }
 
 /* Callback that is used when a breakpoint is deleted.  This will
@@ -1291,12 +1289,10 @@ gdbpy_breakpoint_deleted (struct breakpoint *b)
 	  if (bp_obj->is_finish_bp)
 	    bpfinishpy_pre_delete_hook (bp_obj.get ());
 
-	  if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
-	    {
-	      if (evpy_emit_event (bp_obj,
-				   gdb_py_events.breakpoint_deleted) < 0)
-		gdbpy_print_stack ();
-	    }
+	  if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted)
+	      && evpy_emit_event (bp_obj,
+				  gdb_py_events.breakpoint_deleted) < 0)
+	    gdbpy_print_stack ();
 
 	  bp_obj->bp = NULL;
 	  --bppy_live;
@@ -1320,15 +1316,10 @@ gdbpy_breakpoint_modified (struct breakpoint *b)
       gdbpy_enter enter_py (b->gdbarch);
 
       PyObject *bp_obj = bp->py_bp_object;
-      if (bp_obj)
-	{
-	  if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
-	    {
-	      if (evpy_emit_event (bp_obj,
-				   gdb_py_events.breakpoint_modified) < 0)
-		gdbpy_print_stack ();
-	    }
-	}
+      if (bp_obj != nullptr
+	  && !evregpy_no_listeners_p (gdb_py_events.breakpoint_modified)
+	  && evpy_emit_event (bp_obj, gdb_py_events.breakpoint_modified) < 0)
+	gdbpy_print_stack ();
     }
 }
 
diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index e2769871d3d7..8f476d4461d9 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -160,9 +160,9 @@ connpy_connection_removed (process_stratum_target *target)
 
   gdbpy_enter enter_py;
 
-  if (!evregpy_no_listeners_p (gdb_py_events.connection_removed))
-    if (emit_connection_event (target, gdb_py_events.connection_removed) < 0)
-      gdbpy_print_stack ();
+  if (!evregpy_no_listeners_p (gdb_py_events.connection_removed)
+      && emit_connection_event (target, gdb_py_events.connection_removed) < 0)
+    gdbpy_print_stack ();
 
   auto conn_obj_iter = all_connection_objects.find (target);
   if (conn_obj_iter != all_connection_objects.end ())
diff --git a/gdb/python/py-framefilter.c b/gdb/python/py-framefilter.c
index 2777b9440974..c3eee22653a1 100644
--- a/gdb/python/py-framefilter.c
+++ b/gdb/python/py-framefilter.c
@@ -329,11 +329,9 @@ py_print_single_arg (struct ui_out *out,
       if the value is a frame argument.  This is denoted in this
       function with PRINT_ARGS_FIELD which is flag from the caller to
       emit the ARGS field.  */
-  if (out->is_mi_like_p ())
-    {
-      if (print_args_field || args_type != NO_VALUES)
-	maybe_tuple.emplace (out, nullptr);
-    }
+  if (out->is_mi_like_p ()
+      && (print_args_field || args_type != NO_VALUES))
+    maybe_tuple.emplace (out, nullptr);
 
   annotate_arg_begin ();
 
@@ -585,11 +583,9 @@ enumerate_locals (PyObject *iter,
       /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
 	 each output contains only one field.  The exception is
 	 -stack-list-variables, which always provides a tuple.  */
-      if (out->is_mi_like_p ())
-	{
-	  if (print_args_field || args_type != NO_VALUES)
-	    tuple.emplace (out, nullptr);
-	}
+      if (out->is_mi_like_p ()
+	  && (print_args_field || args_type != NO_VALUES))
+	tuple.emplace (out, nullptr);
 
       /* If the output is not MI we indent locals.  */
       out->spaces (local_indent);
@@ -886,19 +882,17 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
     {
       /* Print address to the address field.  If an address is not provided,
 	 print nothing.  */
-      if (opts.addressprint && has_addr)
-	{
-	  if (!sal.symtab
+      if (opts.addressprint && has_addr
+	  && (!sal.symtab
 	      || frame_show_address (frame, sal)
-	      || print_what == LOC_AND_ADDRESS)
-	    {
-	      annotate_frame_address ();
-	      out->field_core_addr ("addr", gdbarch, address);
-	      if (get_frame_pc_masked (frame))
-		out->field_string ("pac", " [PAC]");
-	      annotate_frame_address_end ();
-	      out->text (" in ");
-	    }
+	      || print_what == LOC_AND_ADDRESS))
+	{
+	  annotate_frame_address ();
+	  out->field_core_addr ("addr", gdbarch, address);
+	  if (get_frame_pc_masked (frame))
+	    out->field_string ("pac", " [PAC]");
+	  annotate_frame_address_end ();
+	  out->text (" in ");
 	}
 
       /* Print frame function name.  */
@@ -1034,12 +1028,10 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
 	out->text ("\n");
     }
 
-  if (print_locals)
-    {
-      if (py_print_locals (filter, out, args_type, indent,
-			   frame) == EXT_LANG_BT_ERROR)
-	return EXT_LANG_BT_ERROR;
-    }
+  if (print_locals
+      && py_print_locals (filter, out, args_type, indent,
+			  frame) == EXT_LANG_BT_ERROR)
+    return EXT_LANG_BT_ERROR;
 
   if ((flags & PRINT_HIDE) == 0)
     {
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 23e0be0ea432..1e8be6658216 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -113,16 +113,12 @@ fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
   if (PyObject_HasAttrString (self, "__doc__"))
     {
       gdbpy_ref<> ds_obj (PyObject_GetAttrString (self, "__doc__"));
-      if (ds_obj != NULL)
+      if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
 	{
-	  if (gdbpy_is_string (ds_obj.get ()))
-	    {
-	      docstring = python_string_to_host_string (ds_obj.get ());
-	      if (docstring == NULL)
-		return -1;
-	      docstring
-		= gdbpy_fix_doc_string_indentation (std::move (docstring));
-	    }
+	  docstring = python_string_to_host_string (ds_obj.get ());
+	  if (docstring == NULL)
+	    return -1;
+	  docstring = gdbpy_fix_doc_string_indentation (std::move (docstring));
 	}
     }
   if (! docstring)
diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c
index c5f7ff57e1a8..3cc55e49157f 100644
--- a/gdb/python/py-param.c
+++ b/gdb/python/py-param.c
@@ -376,15 +376,13 @@ set_parameter_value (parmpy_object *self, PyObject *value)
 		}
 	    }
 
-	if (allowed == TRIBOOL_UNKNOWN)
-	  {
-	    if (val > UINT_MAX || val < INT_MIN
+	if (allowed == TRIBOOL_UNKNOWN
+	    && (val > UINT_MAX || val < INT_MIN
 		|| (var_type == var_uinteger && val < 0)
 		|| (var_type == var_integer && val > INT_MAX)
 		|| (var_type == var_pinteger && val < 0)
-		|| (var_type == var_pinteger && val > INT_MAX))
-	      allowed = TRIBOOL_FALSE;
-	  }
+		|| (var_type == var_pinteger && val > INT_MAX)))
+	  allowed = TRIBOOL_FALSE;
 	if (allowed == TRIBOOL_FALSE)
 	  {
 	    PyErr_SetString (PyExc_RuntimeError,
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index ce6ed699f3e5..c81db67e93ff 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -204,17 +204,15 @@ pretty_print_one_value (PyObject *printer, struct value **out_value)
 	{
 	  result.reset (PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst,
 						    NULL));
-	  if (result != NULL)
+	  if (result != NULL
+	      && !gdbpy_is_string (result.get ())
+	      && !gdbpy_is_lazy_string (result.get ())
+	      && result != Py_None)
 	    {
-	      if (! gdbpy_is_string (result.get ())
-		  && ! gdbpy_is_lazy_string (result.get ())
-		  && result != Py_None)
-		{
-		  *out_value = convert_value_from_python (result.get ());
-		  if (PyErr_Occurred ())
-		    *out_value = NULL;
-		  result = NULL;
-		}
+	      *out_value = convert_value_from_python (result.get ());
+	      if (PyErr_Occurred ())
+		*out_value = NULL;
+	      result = NULL;
 	    }
 	}
     }
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index de6bfc653462..b27950c172c4 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -662,10 +662,10 @@ gdbpy_executable_changed (struct program_space *pspace, bool reload_p)
 
   gdbpy_enter enter_py;
 
-  if (!evregpy_no_listeners_p (gdb_py_events.executable_changed))
-    if (emit_executable_changed_event (gdb_py_events.executable_changed,
-				       pspace, reload_p) < 0)
-      gdbpy_print_stack ();
+  if (!evregpy_no_listeners_p (gdb_py_events.executable_changed)
+      && emit_executable_changed_event (gdb_py_events.executable_changed,
+					pspace, reload_p) < 0)
+    gdbpy_print_stack ();
 }
 
 /* Helper function to emit NewProgspaceEvent (when ADDING_P is true) or
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 01f4aa275ef9..08ee471000cb 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -905,11 +905,8 @@ gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw)
   for (const symbol_search &p : symbols)
     {
       /* Minimal symbols included?  */
-      if (minsyms_p)
-	{
-	  if (p.msymbol.minsym != NULL)
-	    count++;
-	}
+      if (minsyms_p && p.msymbol.minsym != NULL)
+	count++;
 
       if (p.symbol != NULL)
 	count++;
@@ -936,9 +933,8 @@ gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw)
       std::string symbol_name;
 
       /* Skipping minimal symbols?  */
-      if (minsyms_p == 0)
-	if (p.msymbol.minsym != NULL)
-	  continue;
+      if (minsyms_p == 0 && p.msymbol.minsym != NULL)
+	continue;
 
       if (p.msymbol.minsym == NULL)
 	{
-- 
2.55.0


  reply	other threads:[~2026-09-14 19:19 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-14 19:19 [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Simon Marchi
2026-09-14 19:19 ` Simon Marchi [this message]
2026-09-14 19:35   ` [PATCH 2/4] gdb/python: collapse some nested ifs Tom Tromey
2026-09-14 19:46     ` Simon Marchi
2026-09-14 19:19 ` [PATCH 3/4] gdb/python: rename evregpy_no_listeners_p to evregpy_has_listeners_p Simon Marchi
2026-09-14 19:38   ` Tom Tromey
2026-09-14 19:19 ` [PATCH 4/4] gdb/python: remove more useless casts Simon Marchi
2026-09-14 19:39   ` Tom Tromey
2026-09-14 19:47     ` Simon Marchi
2026-09-14 19:24 ` [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Tom Tromey
2026-09-14 19:43   ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260914191908.557014-2-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox