Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] [gdb] Use std::swap
@ 2026-11-25 13:38 Tom de Vries
  2026-11-25 13:38 ` [PATCH 2/2] [gdb] Use std::exchange Tom de Vries
  0 siblings, 1 reply; 2+ messages in thread
From: Tom de Vries @ 2026-11-25 13:38 UTC (permalink / raw)
  To: gdb-patches

Use std::swap in a few places to make code more readable and shorter,
replacing:
...
{
  auto tmp = a;
  a = b;
  b = tmp;
}
...
with:
...
std::swap (a, b);
...

Tested on x86_64-linux.
---
 gdb/gcore.c          | 9 ++-------
 gdb/prologue-value.c | 7 ++-----
 2 files changed, 4 insertions(+), 12 deletions(-)

diff --git a/gdb/gcore.c b/gdb/gcore.c
index b88085ad78b..53cb67977a3 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -38,6 +38,7 @@
 #include "gdbsupport/scope-exit.h"
 #include "auxv.h"
 #include "cli/cli-style.h"
+#include <utility>
 
 /* To generate sparse cores, we look at the data to write in chunks of
    this size when considering whether to skip the write.  Only if we
@@ -248,13 +249,7 @@ derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
   /* Now canonicalize their order, so that BOTTOM is a lower address
      (as opposed to a lower stack frame).  */
   if (*bottom > *top)
-    {
-      bfd_vma tmp_vma;
-
-      tmp_vma = *top;
-      *top = *bottom;
-      *bottom = tmp_vma;
-    }
+    std::swap (*top, *bottom);
 
   return 1;
 }
diff --git a/gdb/prologue-value.c b/gdb/prologue-value.c
index 00a62d466f7..31845124fe3 100644
--- a/gdb/prologue-value.c
+++ b/gdb/prologue-value.c
@@ -18,6 +18,7 @@
 
 #include "prologue-value.h"
 #include "regcache.h"
+#include <utility>
 
 \f
 /* Constructors.  */
@@ -69,11 +70,7 @@ constant_last (pv_t *a, pv_t *b)
 {
   if (a->kind == pvk_constant
       && b->kind != pvk_constant)
-    {
-      pv_t temp = *a;
-      *a = *b;
-      *b = temp;
-    }
+    std::swap (*a, *b);
 }
 
 

base-commit: 147e60cae54d6db8b466265354e8ee6571a24841
-- 
2.51.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] [gdb] Use std::exchange
  2026-11-25 13:38 [PATCH 1/2] [gdb] Use std::swap Tom de Vries
@ 2026-11-25 13:38 ` Tom de Vries
  0 siblings, 0 replies; 2+ messages in thread
From: Tom de Vries @ 2026-11-25 13:38 UTC (permalink / raw)
  To: gdb-patches

Use std::exchange in a few places to make code more readable and shorter,
replacing:
...
a = b;
b = c;
...
with:
...
a = std::exchange (b, c);
...

Tested on x86_64-linux.

Built on x86_64-linux using --host=x86_64-w64-mingw32.

That leaves the gdb/gnu-nat.c and gdb/darwin-nat.c changes neither tested nor
built.
---
 gdb/ada-exp-parser.c        |  4 ++--
 gdb/addrmap.h               |  7 +++----
 gdb/breakpoint.c            | 23 ++++++++---------------
 gdb/c-exp-parser.c          |  4 ++--
 gdb/charset.c               |  4 ++--
 gdb/cli-out.c               |  8 ++------
 gdb/cli/cli-style.c         |  4 ++--
 gdb/completer.c             |  5 ++---
 gdb/d-exp-parser.c          |  5 ++---
 gdb/darwin-nat.c            |  4 ++--
 gdb/event-top.c             |  4 ++--
 gdb/f-exp-parser.c          |  5 ++---
 gdb/f-valprint.c            |  4 ++--
 gdb/fork-child.c            |  4 ++--
 gdb/frame.c                 |  7 +++----
 gdb/gnu-nat.c               |  9 ++++-----
 gdb/go-exp-parser.c         |  5 ++---
 gdb/guile/scm-breakpoint.c  |  4 ++--
 gdb/infrun.c                | 21 +++++++--------------
 gdb/interps.c               |  9 ++++-----
 gdb/language.c              |  5 +++--
 gdb/linux-nat.c             |  4 ++--
 gdb/mingw-hdep.c            |  5 ++---
 gdb/parser-defs.h           |  4 ++--
 gdb/posix-hdep.c            |  5 ++---
 gdb/python/py-breakpoint.c  |  4 ++--
 gdb/record-btrace.c         |  7 +++----
 gdb/remote.c                |  5 ++---
 gdb/target.c                |  4 ++--
 gdb/top.c                   | 16 ++++++++--------
 gdb/windows-nat.c           | 12 ++++--------
 gdbserver/ax.cc             | 10 +++-------
 gdbserver/linux-low.cc      |  7 +++----
 gdbsupport/btrace-common.h  |  5 +++--
 gdbsupport/buildargv.h      | 11 ++++-------
 gdbsupport/gdb_ref_ptr.h    |  6 ++----
 gdbsupport/intrusive_list.h | 14 ++++++--------
 gdbsupport/scoped_fd.h      | 11 ++++-------
 gdbsupport/scoped_mmap.h    |  5 ++---
 39 files changed, 116 insertions(+), 164 deletions(-)

diff --git a/gdb/ada-exp-parser.c b/gdb/ada-exp-parser.c
index 955fa86dc26..b326333142a 100644
--- a/gdb/ada-exp-parser.c
+++ b/gdb/ada-exp-parser.c
@@ -27,6 +27,7 @@
 #include "gdbsupport/selftest.h"
 #include "parser-defs.h"
 #include "inferior.h"
+#include <utility>
 
 using namespace expr;
 
@@ -745,8 +746,7 @@ ada_parse_state::lex_single_quote ()
 {
   gdb_assert (*pstate->lexptr == '\'');
 
-  bool before_qual_quote = m_before_qual_quote;
-  m_before_qual_quote = false;
+  bool before_qual_quote = std::exchange (m_before_qual_quote, false);
 
   if (!before_qual_quote)
     {
diff --git a/gdb/addrmap.h b/gdb/addrmap.h
index a702341ef8f..89cbc3d264a 100644
--- a/gdb/addrmap.h
+++ b/gdb/addrmap.h
@@ -23,6 +23,7 @@
 #include "gdbsupport/function-view.h"
 #include "gdbsupport/gdb_obstack.h"
 #include "splay-tree.h"
+#include <utility>
 
 /* An address map is essentially a table mapping CORE_ADDRs onto GDB
    data structures, like blocks, symtabs, partial symtabs, and so on.
@@ -132,9 +133,8 @@ struct addrmap_mutable final : public addrmap
   DISABLE_COPY_AND_ASSIGN (addrmap_mutable);
 
   addrmap_mutable (addrmap_mutable &&other)
-    : tree (other.tree)
+    : tree (std::exchange (other.tree, nullptr))
   {
-    other.tree = nullptr;
   }
 
   addrmap_mutable &operator= (addrmap_mutable &&other)
@@ -143,8 +143,7 @@ struct addrmap_mutable final : public addrmap
     if (this != &other)
       {
 	clear ();
-	tree = other.tree;
-	other.tree = nullptr;
+	tree = std::exchange (other.tree, nullptr);
       }
     return *this;
   }
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index ca8ae98a7b7..f4ee1726e4f 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -82,6 +82,7 @@
 #include "gdbsupport/span.h"
 #include <optional>
 #include "gdbsupport/common-utils.h"
+#include <utility>
 
 /* Prototypes for local functions.  */
 
@@ -1177,8 +1178,7 @@ breakpoint_enabled (struct breakpoint *b)
 static void
 set_breakpoint_count (int num)
 {
-  prev_breakpoint_count = breakpoint_count;
-  breakpoint_count = num;
+  prev_breakpoint_count = std::exchange (breakpoint_count, num);
   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
 }
 
@@ -1898,9 +1898,7 @@ breakpoint_set_commands (struct breakpoint *b,
 void
 breakpoint_set_silent (struct breakpoint *b, int silent)
 {
-  int old_silent = b->silent;
-
-  b->silent = silent;
+  int old_silent = std::exchange (b->silent, silent);
   if (old_silent != silent)
     notify_breakpoint_modified (b);
 }
@@ -1918,8 +1916,7 @@ breakpoint_set_thread (struct breakpoint *b, int thread)
      already has task or inferior restriction.  */
   gdb_assert (thread == -1 || (b->task == -1 && b->inferior == -1));
 
-  int old_thread = b->thread;
-  b->thread = thread;
+  int old_thread = std::exchange (b->thread, thread);
   if (old_thread != thread)
     {
       /* If THREAD is in a different program_space than OLD_THREAD, or the
@@ -1976,8 +1973,7 @@ breakpoint_set_inferior (struct breakpoint *b, int inferior)
      already has a task or thread restriction.  */
   gdb_assert (inferior == -1 || (b->task == -1 && b->thread == -1));
 
-  int old_inferior = b->inferior;
-  b->inferior = inferior;
+  int old_inferior = std::exchange (b->inferior, inferior);
   if (old_inferior != inferior)
     {
       /* If INFERIOR is in a different program_space than OLD_INFERIOR, or
@@ -2033,8 +2029,7 @@ breakpoint_set_task (struct breakpoint *b, int task)
      already has a thread or inferior restriction.  */
   gdb_assert (task == -1 || (b->thread == -1 && b->inferior == -1));
 
-  int old_task = b->task;
-  b->task = task;
+  int old_task = std::exchange (b->task, task);
   if (old_task != task)
     notify_breakpoint_modified (b);
 }
@@ -5255,8 +5250,7 @@ bpstat_do_actions_1 (bpstat **bsp)
 	 commands are only executed once, we don't need to copy it; we
 	 can clear the pointer in the bpstat, and make sure we free
 	 the tree when we're done.  */
-      counted_command_line ccmd = bs->commands;
-      bs->commands = NULL;
+      counted_command_line ccmd = std::exchange (bs->commands, nullptr);
       if (ccmd != NULL)
 	cmd = ccmd.get ();
       if (command_line_is_silent (cmd))
@@ -14350,8 +14344,7 @@ enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
 	{
 	  watchpoint *w = gdb::checked_static_cast<watchpoint *> (bpt);
 
-	  orig_enable_state = bpt->enable_state;
-	  bpt->enable_state = bp_enabled;
+	  orig_enable_state = std::exchange (bpt->enable_state, bp_enabled);
 	  update_watchpoint (w, true /* reparse */);
 	}
       catch (const gdb_exception_error &e)
diff --git a/gdb/c-exp-parser.c b/gdb/c-exp-parser.c
index 783aeb1cb84..9de25f122e0 100644
--- a/gdb/c-exp-parser.c
+++ b/gdb/c-exp-parser.c
@@ -26,6 +26,7 @@
 #include "macroexp.h"
 #include "macroscope.h"
 #include "objc-lang.h"
+#include <utility>
 
 /* The entry point of the bison/yacc-generated parser, defined in
    c-exp-parser-gen.c.  Bison produces a declaration for c_yyparse in
@@ -680,9 +681,8 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
   int c;
   int namelen;
   const char *tokstart;
-  bool saw_structop = last_was_structop;
+  bool saw_structop = std::exchange (last_was_structop, false);
 
-  last_was_structop = false;
   *is_quoted_name = false;
 
  retry:
diff --git a/gdb/charset.c b/gdb/charset.c
index 4558625abf6..061b98c6921 100644
--- a/gdb/charset.c
+++ b/gdb/charset.c
@@ -25,6 +25,7 @@
 #include "charset-list.h"
 #include "gdbsupport/environ.h"
 #include "arch-utils.h"
+#include <utility>
 
 #ifdef USE_WIN32API
 #include <windows.h>
@@ -672,8 +673,7 @@ wchar_iterator::iterate (enum wchar_iterate_result *out_result,
 		 to see EOF.  */
 	      *out_result = wchar_iterate_incomplete;
 	      *ptr = m_input;
-	      *len = m_bytes;
-	      m_bytes = 0;
+	      *len = std::exchange (m_bytes, 0);
 	      return 0;
 
 	    default:
diff --git a/gdb/cli-out.c b/gdb/cli-out.c
index 0c339d0e819..b7a5841a890 100644
--- a/gdb/cli-out.c
+++ b/gdb/cli-out.c
@@ -28,6 +28,7 @@
 #include "ui.h"
 #include "cli/cli-cmds.h"
 #include "buffered-streams.h"
+#include <utility>
 
 /* These are the CLI output functions */
 
@@ -464,12 +465,7 @@ cli_ui_out::~cli_ui_out ()
 ui_file *
 cli_ui_out::set_stream (struct ui_file *stream)
 {
-  ui_file *old;
-
-  old = m_streams.back ();
-  m_streams.back () = stream;
-
-  return old;
+  return std::exchange (m_streams.back (), stream);
 }
 
 bool
diff --git a/gdb/cli/cli-style.c b/gdb/cli/cli-style.c
index cc22b861ca0..9ca1cca7b27 100644
--- a/gdb/cli/cli-style.c
+++ b/gdb/cli/cli-style.c
@@ -24,6 +24,7 @@
 #include "source-cache.h"
 #include "observable.h"
 #include "charset.h"
+#include <utility>
 
 /* True if styling is enabled.  */
 
@@ -64,8 +65,7 @@ static bool scoped_disable_styling_p = false;
 
 scoped_disable_styling::scoped_disable_styling ()
 {
-  m_old_value = scoped_disable_styling_p;
-  scoped_disable_styling_p = true;
+  m_old_value = std::exchange (scoped_disable_styling_p, true);
 }
 
 /* See cli/cli-style.h.  */
diff --git a/gdb/completer.c b/gdb/completer.c
index 09b1c8054d3..3d7e3271349 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -32,6 +32,7 @@
 #include "cli/cli-decode.h"
 #include "gdbsupport/gdb_tilde_expand.h"
 #include "readline/readline.h"
+#include <utility>
 
 /* FIXME: This is needed because of lookup_cmd_1 ().  We should be
    calling a hook instead so we eliminate the CLI dependency.  */
@@ -2726,9 +2727,7 @@ completion_result::completion_result (completion_result &&rhs) noexcept
 char **
 completion_result::release_match_list ()
 {
-  char **ret = match_list;
-  match_list = NULL;
-  return ret;
+  return std::exchange (match_list, nullptr);
 }
 
 /* See completer.h  */
diff --git a/gdb/d-exp-parser.c b/gdb/d-exp-parser.c
index 0fd01b49003..8d689d4dc0c 100644
--- a/gdb/d-exp-parser.c
+++ b/gdb/d-exp-parser.c
@@ -26,6 +26,7 @@
 #include "expression.h"
 #include "language.h"
 #include "value.h"
+#include <utility>
 
 /* The entry point of the bison/yacc-generated parser, defined in
    d-exp-parser-gen.c.  Bison produces a declaration for d_yyparse in
@@ -437,9 +438,7 @@ lex_one_token (struct parser_state *par_state)
   int c;
   int namelen;
   const char *tokstart;
-  int saw_structop = last_was_structop;
-
-  last_was_structop = 0;
+  int saw_structop = std::exchange (last_was_structop, 0);
 
  retry:
 
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index 312f67508c8..27c6e8a00aa 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -71,6 +71,7 @@
 #include "gdbsupport/scoped_restore.h"
 #include "nat/fork-inferior.h"
 #include "gdbsupport/eintr.h"
+#include <utility>
 
 /* Quick overview.
    Darwin kernel is Mach + BSD derived kernel.  Note that they share the
@@ -1202,8 +1203,7 @@ darwin_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *status)
   /* Handle fake stop events at first.  */
   if (darwin_inf_fake_stop != NULL)
     {
-      inferior *inf = darwin_inf_fake_stop;
-      darwin_inf_fake_stop = NULL;
+      inferior *inf = std::exchange (darwin_inf_fake_stop, nullptr);
 
       darwin_inferior *priv = get_darwin_inferior (inf);
 
diff --git a/gdb/event-top.c b/gdb/event-top.c
index 0fb6eafec57..046406d6498 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -45,6 +45,7 @@
 #include "async-event.h"
 #include "bt-utils.h"
 #include "pager.h"
+#include <utility>
 
 /* readline include files.  */
 #include "readline/readline.h"
@@ -1605,8 +1606,7 @@ gdb_disable_readline (void)
 
 scoped_segv_handler_restore::scoped_segv_handler_restore (segv_handler_t new_handler)
 {
-  m_old_handler = thread_local_segv_handler;
-  thread_local_segv_handler = new_handler;
+  m_old_handler = std::exchange (thread_local_segv_handler, new_handler);
 }
 
 scoped_segv_handler_restore::~scoped_segv_handler_restore()
diff --git a/gdb/f-exp-parser.c b/gdb/f-exp-parser.c
index 010561d83ba..bf1ee2a0dc9 100644
--- a/gdb/f-exp-parser.c
+++ b/gdb/f-exp-parser.c
@@ -26,6 +26,7 @@
 #include "parser-defs.h"
 #include "value.h"
 #include <string>
+#include <utility>
 
 /* The entry point of the bison/yacc-generated parser, defined in
    f-exp-parser-gen.c.  Bison produces a declaration for f_yyparse in
@@ -636,9 +637,7 @@ f_yylex (void)
   int namelen;
   unsigned int token;
   const char *tokstart;
-  bool saw_structop = last_was_structop;
-
-  last_was_structop = false;
+  bool saw_structop = std::exchange (last_was_structop, false);
 
  retry:
 
diff --git a/gdb/f-valprint.c b/gdb/f-valprint.c
index 2b54c8bf1c0..dfbdd466a39 100644
--- a/gdb/f-valprint.c
+++ b/gdb/f-valprint.c
@@ -37,6 +37,7 @@
 #include "gdbarch.h"
 #include "f-array-walker.h"
 #include "event-top.h"
+#include <utility>
 
 static void f77_get_dynamic_length_of_aggregate (struct type *);
 
@@ -206,9 +207,8 @@ class fortran_array_printer_impl : public fortran_array_walker_base_impl
       m_nrepeats++;
     if (!repeated || last_p)
       {
-	LONGEST nrepeats = m_nrepeats;
+	LONGEST nrepeats = std::exchange (m_nrepeats, 0);
 
-	m_nrepeats = 0;
 	if (nrepeats >= m_options.repeat_count_threshold)
 	  {
 	    annotate_elt_rep (nrepeats + 1);
diff --git a/gdb/fork-child.c b/gdb/fork-child.c
index a9d0bffb8e1..9ebcd390e98 100644
--- a/gdb/fork-child.c
+++ b/gdb/fork-child.c
@@ -28,6 +28,7 @@
 #include "gdbsupport/filestuff.h"
 #include "nat/fork-inferior.h"
 #include "gdbsupport/common-inferior.h"
+#include <utility>
 
 /* The exec-wrapper, if any, that will be used when starting the
    inferior.  */
@@ -80,8 +81,7 @@ postfork_hook (pid_t pid)
   inferior_appeared (inf, pid);
 
   gdb_assert (saved_ui != NULL);
-  current_ui = saved_ui;
-  saved_ui = NULL;
+  current_ui = std::exchange (saved_ui, nullptr);
 
   new_tty_postfork ();
 }
diff --git a/gdb/frame.c b/gdb/frame.c
index e24841bb8a1..83903adaa80 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -44,6 +44,7 @@
 #include "valprint.h"
 #include "cli/cli-option.h"
 #include "dwarf2/loc.h"
+#include <utility>
 
 /* Number of calls to reinit_frame_cache.  */
 static unsigned int frame_cache_generation = 0;
@@ -280,16 +281,14 @@ struct frame_info_stash_entry
   DISABLE_COPY_AND_ASSIGN (frame_info_stash_entry);
 
   frame_info_stash_entry (frame_info_stash_entry &&other)
-    : frame (other.frame)
+    : frame (std::exchange (other.frame, nullptr))
   {
-    other.frame = nullptr;
   }
 
   frame_info_stash_entry &operator= (frame_info_stash_entry &&other)
   {
     destroy ();
-    frame = other.frame;
-    other.frame = nullptr;
+    frame = std::exchange (other.frame, nullptr);
     return *this;
   }
 
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index c87675648ba..b54492a5278 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -22,6 +22,7 @@
 
 /* Include this first, to pick up the <mach.h> 'thread_info' diversion.  */
 #include "gnu-nat.h"
+#include <utility>
 
 /* Mach/Hurd headers are not yet ready for C++ compilation.  */
 extern "C"
@@ -2755,9 +2756,7 @@ static void
 set_task_pause_cmd (int arg, int from_tty)
 {
   struct inf *inf = cur_inf ();
-  int old_sc = inf->pause_sc;
-
-  inf->pause_sc = arg;
+  int old_sc = std::exchange (inf->pause_sc, arg);
 
   if (old_sc == 0 && inf->pause_sc != 0)
     /* If the task is currently unsuspended, immediately suspend it,
@@ -3261,9 +3260,9 @@ static void
 set_thread_pause_cmd (const char *args, int from_tty)
 {
   struct proc *thread = cur_thread ();
-  int old_sc = thread->pause_sc;
+  int old_sc = std::exchange (thread->pause_sc,
+			      parse_bool_arg (args, "set thread pause"));
 
-  thread->pause_sc = parse_bool_arg (args, "set thread pause");
   if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
     /* If the task is currently unsuspended, immediately suspend it,
        otherwise wait until the next time it gets control.  */
diff --git a/gdb/go-exp-parser.c b/gdb/go-exp-parser.c
index 457bfeee91d..6e1a5f56b76 100644
--- a/gdb/go-exp-parser.c
+++ b/gdb/go-exp-parser.c
@@ -27,6 +27,7 @@
 #include "language.h"
 #include "parser-defs.h"
 #include "value.h"
+#include <utility>
 
 /* The entry point of the bison/yacc-generated parser, defined in
    go-exp-parser-gen.c.  Bison produces a declaration for go_yyparse in
@@ -380,9 +381,7 @@ lex_one_token (struct parser_state *par_state)
   int c;
   int namelen;
   const char *tokstart;
-  int saw_structop = last_was_structop;
-
-  last_was_structop = 0;
+  int saw_structop = std::exchange (last_was_structop, 0);
 
  retry:
 
diff --git a/gdb/guile/scm-breakpoint.c b/gdb/guile/scm-breakpoint.c
index 5df2c00f5d3..3c715a09d04 100644
--- a/gdb/guile/scm-breakpoint.c
+++ b/gdb/guile/scm-breakpoint.c
@@ -31,6 +31,7 @@
 #include "language.h"
 #include "guile-internal.h"
 #include "location.h"
+#include <utility>
 
 /* The <gdb:breakpoint> smob.
    N.B.: The name of this struct is known to breakpoint.h.
@@ -1146,8 +1147,7 @@ bpscm_breakpoint_created (struct breakpoint *bp)
   /* Verify our caller error checked the user's request.  */
   gdb_assert (bpscm_want_scm_wrapper_p (bp, 1));
 
-  bp_scm = pending_breakpoint_scm;
-  pending_breakpoint_scm = SCM_BOOL_F;
+  bp_scm = std::exchange (pending_breakpoint_scm, SCM_BOOL_F);
 
   bpscm_attach_scm_to_breakpoint (bp, bp_scm);
 }
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 8f02c49cbcb..35efba151e9 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -77,6 +77,7 @@
 #include "interps.h"
 #include "finish-thread-state.h"
 #include "buffered-streams.h"
+#include <utility>
 
 /* Prototypes for local functions */
 
@@ -1083,9 +1084,8 @@ handle_vfork_child_exec_or_exit (int exec)
 
       /* This exec or exit marks the end of the shared memory region
 	 between the parent and the child.  Break the bonds.  */
-      inferior *vfork_parent = inf->vfork_parent;
-      inf->vfork_parent->vfork_child = nullptr;
-      inf->vfork_parent = nullptr;
+      inferior *vfork_parent = std::exchange (inf->vfork_parent, nullptr);
+      vfork_parent->vfork_child = nullptr;
 
       /* If the user wanted to detach from the parent, now is the
 	 time.  */
@@ -1115,8 +1115,7 @@ handle_vfork_child_exec_or_exit (int exec)
 	     the spaces has the desired effect.  Yes, this is "sort
 	     of" a hack.  */
 
-	  pspace = inf->pspace;
-	  inf->pspace = nullptr;
+	  pspace = std::exchange (inf->pspace, nullptr);
 	  address_space_ref_ptr aspace = std::move (inf->aspace);
 
 	  if (print_inferior_events)
@@ -9930,9 +9929,7 @@ signal_cache_update (int signo)
 int
 signal_stop_update (int signo, int state)
 {
-  int ret = signal_stop[signo];
-
-  signal_stop[signo] = state;
+  int ret = std::exchange (signal_stop[signo], state);
   signal_cache_update (signo);
   return ret;
 }
@@ -9940,9 +9937,7 @@ signal_stop_update (int signo, int state)
 int
 signal_print_update (int signo, int state)
 {
-  int ret = signal_print[signo];
-
-  signal_print[signo] = state;
+  int ret = std::exchange (signal_print[signo], state);
   signal_cache_update (signo);
   return ret;
 }
@@ -9950,9 +9945,7 @@ signal_print_update (int signo, int state)
 int
 signal_pass_update (int signo, int state)
 {
-  int ret = signal_program[signo];
-
-  signal_program[signo] = state;
+  int ret = std::exchange (signal_program[signo], state);
   signal_cache_update (signo);
   return ret;
 }
diff --git a/gdb/interps.c b/gdb/interps.c
index 2f0f03be106..0661912250b 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -39,6 +39,7 @@
 #include "main.h"
 #include "gdbsupport/buildargv.h"
 #include "gdbsupport/scope-exit.h"
+#include <utility>
 
 /* The magic initialization routine for this module.  */
 
@@ -209,12 +210,10 @@ struct interp *
 scoped_restore_interp::set_interp (const char *name)
 {
   struct interp *interp = interp_lookup (current_ui, name);
-  struct interp *old_interp = current_ui->current_interpreter;
-
-  if (interp)
-    current_ui->current_interpreter = interp;
+  if (interp == nullptr)
+    return current_ui->current_interpreter;
 
-  return old_interp;
+  return std::exchange (current_ui->current_interpreter, interp);
 }
 
 /* Returns true if the current interp is the passed in name.  */
diff --git a/gdb/language.c b/gdb/language.c
index d459461eb8c..fb0b9c015dc 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -45,6 +45,7 @@
 #include "c-exp-parser.h"
 #include <algorithm>
 #include "gdbarch.h"
+#include <utility>
 
 static void set_range_case (void);
 
@@ -94,8 +95,8 @@ get_current_language ()
     {
       /* Avoid recursive calls -- set_language refers to
 	 current_language.  */
-      lazily_set_language_ftype *call = lazy_language_setter;
-      lazy_language_setter = nullptr;
+      lazily_set_language_ftype *call
+	= std::exchange (lazy_language_setter, nullptr);
       call ();
     }
   return global_current_language;
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index ab9f6ade55d..650d88a3dc1 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -64,6 +64,7 @@
 #include "gdbsupport/gdb-sigmask.h"
 #include "gdbsupport/common-debug.h"
 #include "cli/cli-style.h"
+#include <utility>
 
 /* This comment documents high-level logic of this file.
 
@@ -3415,8 +3416,7 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
   gdb_assert (lp);
   gdb_assert (lp->stopped);
 
-  status = lp->status;
-  lp->status = 0;
+  status = std::exchange (lp->status, 0);
 
   if (!target_is_non_stop_p ())
     {
diff --git a/gdb/mingw-hdep.c b/gdb/mingw-hdep.c
index 90ebe252f57..cf797cf4d13 100644
--- a/gdb/mingw-hdep.c
+++ b/gdb/mingw-hdep.c
@@ -31,6 +31,7 @@
 
 #include <windows.h>
 #include <signal.h>
+#include <utility>
 
 /* Return an absolute file name of the running GDB, if possible, or
    ARGV0 if not.  The return value is in malloc'ed storage.  */
@@ -518,9 +519,7 @@ install_sigint_handler (c_c_handler_ftype *fn)
   SetConsoleCtrlHandler (ctrl_c_handler, TRUE);
   installed = true;
 
-  c_c_handler_ftype *result = current_handler;
-  current_handler = fn;
-  return result;
+  return std::exchange (current_handler, fn);
 }
 
 /* See terminal.h.  */
diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h
index 7e4f438e6f0..b0d69afd1f3 100644
--- a/gdb/parser-defs.h
+++ b/gdb/parser-defs.h
@@ -26,6 +26,7 @@
 #include "expression.h"
 #include "symtab.h"
 #include "expop.h"
+#include <utility>
 
 struct block;
 struct language_defn;
@@ -177,8 +178,7 @@ struct parser_state : public expr_builder
 
   int end_arglist ()
   {
-    int val = arglist_len;
-    arglist_len = m_funcall_chain.back ();
+    int val = std::exchange (arglist_len, m_funcall_chain.back ());
     m_funcall_chain.pop_back ();
     return val;
   }
diff --git a/gdb/posix-hdep.c b/gdb/posix-hdep.c
index b9aff9ede1f..1408eedd62d 100644
--- a/gdb/posix-hdep.c
+++ b/gdb/posix-hdep.c
@@ -23,6 +23,7 @@
 #include "ui-style.h"
 #include "gdb_curses.h"
 #include <signal.h>
+#include <utility>
 
 /* Wrapper for select.  Nothing special needed on POSIX platforms.  */
 
@@ -90,7 +91,5 @@ c_c_handler_ftype *
 install_sigint_handler (c_c_handler_ftype *fn)
 {
   signal (SIGINT, handler_wrapper);
-  c_c_handler_ftype *result = current_handler;
-  current_handler = fn;
-  return result;
+  return std::exchange (current_handler, fn);
 }
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 9621154224e..1b3d5a81ac6 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -34,6 +34,7 @@
 #include "py-event.h"
 #include "linespec.h"
 #include "gdbsupport/common-utils.h"
+#include <utility>
 
 extern PyTypeObject breakpoint_location_object_type;
 
@@ -1317,9 +1318,8 @@ gdbpy_breakpoint_created (struct breakpoint *bp)
 
   if (bppy_pending_object)
     {
-      newbp = bppy_pending_object;
+      newbp = std::exchange (bppy_pending_object, nullptr);
       Py_INCREF (newbp);
-      bppy_pending_object = NULL;
       pybp_debug_printf ("attaching existing breakpoint object");
     }
   else
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index dae243ee2db..75b89ab002c 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -47,6 +47,7 @@
 #include <forward_list>
 #include "objfiles.h"
 #include "interps.h"
+#include <utility>
 
 static const target_info record_btrace_target_info = {
   "record-btrace",
@@ -1542,8 +1543,7 @@ record_btrace_target::insert_breakpoint (struct gdbarch *gdbarch,
 
   /* Inserting breakpoints requires accessing memory.  Allow it for the
      duration of this function.  */
-  old = replay_memory_access;
-  replay_memory_access = replay_memory_access_read_write;
+  old = std::exchange (replay_memory_access, replay_memory_access_read_write);
 
   ret = 0;
   try
@@ -1572,8 +1572,7 @@ record_btrace_target::remove_breakpoint (struct gdbarch *gdbarch,
 
   /* Removing breakpoints requires accessing memory.  Allow it for the
      duration of this function.  */
-  old = replay_memory_access;
-  replay_memory_access = replay_memory_access_read_write;
+  old = std::exchange (replay_memory_access, replay_memory_access_read_write);
 
   ret = 0;
   try
diff --git a/gdb/remote.c b/gdb/remote.c
index abe6cf6d4be..a73d0d0781b 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -84,6 +84,7 @@
 #include "cli/cli-style.h"
 #include "gdbsupport/remote-args.h"
 #include "finish-thread-state.h"
+#include <utility>
 
 /* The remote target.  */
 
@@ -13869,9 +13870,7 @@ class scoped_remote_fd
   /* Release ownership of the file descriptor, and return it.  */
   ATTRIBUTE_UNUSED_RESULT int release () noexcept
   {
-    int fd = m_fd;
-    m_fd = -1;
-    return fd;
+    return std::exchange (m_fd, -1);
   }
 
   /* Return the owned file descriptor.  */
diff --git a/gdb/target.c b/gdb/target.c
index 64e4e7f95e4..178f753e380 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -55,6 +55,7 @@
 #include "valprint.h"
 #include "cli/cli-decode.h"
 #include "cli/cli-style.h"
+#include <utility>
 
 [[noreturn]] static void generic_tls_error (void);
 
@@ -3849,9 +3850,8 @@ void
 target_stop_and_wait (ptid_t ptid)
 {
   struct target_waitstatus status;
-  bool was_non_stop = non_stop;
+  bool was_non_stop = std::exchange (non_stop, true);
 
-  non_stop = true;
   target_stop (ptid);
 
   target_wait (ptid, &status, 0);
diff --git a/gdb/top.c b/gdb/top.c
index 18d4bfcd4fd..8a389912e13 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -74,6 +74,7 @@
 #include "cli-out.h"
 #include "tracepoint.h"
 #include "inf-loop.h"
+#include <utility>
 
 #if defined(TUI)
 # include "tui/tui.h"
@@ -789,10 +790,9 @@ void
 save_command_line (const char *cmd)
 {
   xfree (previous_saved_command_line);
-  previous_saved_command_line = saved_command_line;
-  previous_repeat_arguments = repeat_arguments;
-  saved_command_line = xstrdup (cmd);
-  repeat_arguments = NULL;
+  previous_saved_command_line
+    = std::exchange (saved_command_line, xstrdup (cmd));
+  previous_repeat_arguments = std::exchange (repeat_arguments, nullptr);
 }
 
 \f
@@ -994,8 +994,8 @@ gdb_readline_wrapper_line (gdb::unique_xmalloc_ptr<char> &&line)
   gdb_readline_wrapper_done = 1;
 
   /* Prevent operate-and-get-next from acting too early.  */
-  saved_after_char_processing_hook = after_char_processing_hook;
-  after_char_processing_hook = NULL;
+  saved_after_char_processing_hook
+    = std::exchange (after_char_processing_hook, nullptr);
 
 #if defined(TUI)
   if (tui_active)
@@ -1053,8 +1053,8 @@ class gdb_readline_wrapper_cleanup
     ui->secondary_prompt_depth--;
     gdb_assert (ui->secondary_prompt_depth >= 0);
 
-    after_char_processing_hook = saved_after_char_processing_hook;
-    saved_after_char_processing_hook = NULL;
+    after_char_processing_hook
+      = std::exchange (saved_after_char_processing_hook, nullptr);
 
     if (m_target_is_async_orig)
       target_async (true);
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index b3131435213..f19b9f38957 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -64,6 +64,7 @@
 #include "gdbsupport/pathstuff.h"
 #include "gdbsupport/symbol.h"
 #include "inf-loop.h"
+#include <utility>
 
 /* This comment documents high-level logic of this file.
 
@@ -1551,10 +1552,7 @@ windows_nat_target::get_windows_debug_event
 	  result_th->reply_later = 0;
 	}
       else
-	{
-	  continue_status = result_th->reply_later;
-	  result_th->reply_later = 0;
-	}
+	continue_status = std::exchange (result_th->reply_later, 0);
 
       /* Go back to waiting for the next event.  */
       continue_last_debug_event_main_thread
@@ -2317,8 +2315,7 @@ windows_nat_target::continue_status_for_event_detaching
      tracking thread creation events at this point.  */
   if (th != nullptr && th->reply_later != 0)
     {
-      DWORD res = th->reply_later;
-      th->reply_later = 0;
+      DWORD res = std::exchange (th->reply_later, 0);
       if (reply_later_events_left != nullptr)
 	(*reply_later_events_left)--;
       return res;
@@ -2986,8 +2983,7 @@ windows_nat_target::create_inferior (const char *exec_file,
       /* Reset all Win32 environment variables to avoid leftover on next run. */
       clear_win32_environment (environ);
       /* Prepare the environment vars for CreateProcess.  */
-      old_env = environ;
-      environ = in_env;
+      old_env = std::exchange (environ, in_env);
       cygwin_internal (CW_SYNC_WINENV);
       w32_env = NULL;
     }
diff --git a/gdbserver/ax.cc b/gdbserver/ax.cc
index f73a407f70a..b4fd1bda249 100644
--- a/gdbserver/ax.cc
+++ b/gdbserver/ax.cc
@@ -20,6 +20,7 @@
 #include "gdbsupport/format.h"
 #include "tracepoint.h"
 #include "gdbsupport/rsp-low.h"
+#include <utility>
 
 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
 
@@ -1262,13 +1263,8 @@ gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
 	  break;
 
 	case gdb_agent_op_rot:
-	  {
-	    ULONGEST tem = stack[sp - 1];
-
-	    stack[sp - 1] = stack[sp - 2];
-	    stack[sp - 2] = top;
-	    top = tem;
-	  }
+	  top
+	    = std::exchange (stack[sp - 1], std::exchange (stack[sp - 2], top));
 	  break;
 
 	case gdb_agent_op_zero_ext:
diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc
index c4daba0bf40..d9f75b9272d 100644
--- a/gdbserver/linux-low.cc
+++ b/gdbserver/linux-low.cc
@@ -61,6 +61,7 @@
 #include <elf.h>
 #endif
 #include "nat/linux-namespaces.h"
+#include <utility>
 
 #ifndef O_LARGEFILE
 #define O_LARGEFILE 0
@@ -2606,9 +2607,8 @@ linux_process_target::wait_for_event_filtered (ptid_t wait_ptid,
 			    event_thread->id.lwp (),
 			    event_child->status_pending);
 
-      *wstatp = event_child->status_pending;
+      *wstatp = std::exchange (event_child->status_pending, 0);
       event_child->status_pending_p = 0;
-      event_child->status_pending = 0;
       switch_to_thread (event_thread);
       return event_thread->id.lwp ();
     }
@@ -2682,9 +2682,8 @@ linux_process_target::wait_for_event_filtered (ptid_t wait_ptid,
       if (event_thread != NULL)
 	{
 	  event_child = get_thread_lwp (event_thread);
-	  *wstatp = event_child->status_pending;
+	  *wstatp = std::exchange (event_child->status_pending, 0);
 	  event_child->status_pending_p = 0;
-	  event_child->status_pending = 0;
 	  break;
 	}
 
diff --git a/gdbsupport/btrace-common.h b/gdbsupport/btrace-common.h
index 928e471de8f..263cf494b86 100644
--- a/gdbsupport/btrace-common.h
+++ b/gdbsupport/btrace-common.h
@@ -22,6 +22,8 @@
 #ifndef GDBSUPPORT_BTRACE_COMMON_H
 #define GDBSUPPORT_BTRACE_COMMON_H
 
+#include <utility>
+
 /* Branch tracing (btrace) is a per-thread control-flow execution trace of the
    inferior.  For presentation purposes, the branch trace is represented as a
    list of sequential control-flow blocks, one such list per thread.  */
@@ -191,9 +193,8 @@ struct btrace_data
     if (this != &other)
       {
 	fini ();
-	format = other.format;
+	format = std::exchange (other.format, BTRACE_FORMAT_NONE);
 	variant = other.variant;
-	other.format = BTRACE_FORMAT_NONE;
       }
     return *this;
   }
diff --git a/gdbsupport/buildargv.h b/gdbsupport/buildargv.h
index 0e2ca6a6594..ef65a350ee6 100644
--- a/gdbsupport/buildargv.h
+++ b/gdbsupport/buildargv.h
@@ -21,6 +21,7 @@
 #define GDBSUPPORT_BUILDARGV_H
 
 #include "libiberty.h"
+#include <utility>
 
 /* A wrapper for an array of char* that was allocated in the way that
    'buildargv' does, and should be freed with 'freeargv'.  */
@@ -57,15 +58,13 @@ class gdb_argv
   gdb_argv &operator= (gdb_argv &&other)
   {
     freeargv (m_argv);
-    m_argv = other.m_argv;
-    other.m_argv = nullptr;
+    m_argv = std::exchange (other.m_argv, nullptr);
     return *this;
   }
 
   gdb_argv (gdb_argv &&other)
   {
-    m_argv = other.m_argv;
-    other.m_argv = nullptr;
+    m_argv = std::exchange (other.m_argv, nullptr);
   }
 
   ~gdb_argv ()
@@ -104,9 +103,7 @@ class gdb_argv
 
   ATTRIBUTE_UNUSED_RESULT char **release ()
   {
-    char **result = m_argv;
-    m_argv = NULL;
-    return result;
+    return std::exchange (m_argv, nullptr);
   }
 
   /* Return the number of items in the array.  */
diff --git a/gdbsupport/gdb_ref_ptr.h b/gdbsupport/gdb_ref_ptr.h
index 757c3f395ba..6a195ac94a9 100644
--- a/gdbsupport/gdb_ref_ptr.h
+++ b/gdbsupport/gdb_ref_ptr.h
@@ -22,6 +22,7 @@
 
 #include <cstddef>
 #include "gdbsupport/traits.h"
+#include <utility>
 
 namespace gdb
 {
@@ -185,10 +186,7 @@ class ref_ptr
      the reference.  */
   ATTRIBUTE_UNUSED_RESULT T *release () noexcept
   {
-    T *result = m_obj;
-
-    m_obj = NULL;
-    return result;
+    return std::exchange (m_obj, nullptr);
   }
 
   /* Let users refer to members of the underlying pointer.  */
diff --git a/gdbsupport/intrusive_list.h b/gdbsupport/intrusive_list.h
index 8f5d2921441..1b67e08e389 100644
--- a/gdbsupport/intrusive_list.h
+++ b/gdbsupport/intrusive_list.h
@@ -19,6 +19,8 @@
 #ifndef GDBSUPPORT_INTRUSIVE_LIST_H
 #define GDBSUPPORT_INTRUSIVE_LIST_H
 
+#include <utility>
+
 #define INTRUSIVE_LIST_UNLINKED_VALUE ((T *) -1)
 
 /* A list node.  The elements put in an intrusive_list either inherit
@@ -251,19 +253,15 @@ class intrusive_list
   }
 
   intrusive_list (intrusive_list &&other) noexcept
-    : m_front (other.m_front),
-      m_back (other.m_back)
+    : m_front (std::exchange (other.m_front, nullptr)),
+      m_back (std::exchange (other.m_back, nullptr))
   {
-    other.m_front = nullptr;
-    other.m_back = nullptr;
   }
 
   intrusive_list &operator= (intrusive_list &&other) noexcept
   {
-    m_front = other.m_front;
-    m_back = other.m_back;
-    other.m_front = nullptr;
-    other.m_back = nullptr;
+    m_front = std::exchange (other.m_front, nullptr);
+    m_back = std::exchange (other.m_back, nullptr);
 
     return *this;
   }
diff --git a/gdbsupport/scoped_fd.h b/gdbsupport/scoped_fd.h
index 08102eda8ce..4fb1b2ab794 100644
--- a/gdbsupport/scoped_fd.h
+++ b/gdbsupport/scoped_fd.h
@@ -22,6 +22,7 @@
 
 #include <unistd.h>
 #include "gdb_file.h"
+#include <utility>
 
 /* A smart-pointer-like class to automatically close a file descriptor.  */
 
@@ -31,9 +32,8 @@ class scoped_fd
   explicit scoped_fd (int fd = -1) noexcept : m_fd (fd) {}
 
   scoped_fd (scoped_fd &&other) noexcept
-    : m_fd (other.m_fd)
+    : m_fd (std::exchange (other.m_fd, -1))
   {
-    other.m_fd = -1;
   }
 
   ~scoped_fd ()
@@ -48,8 +48,7 @@ class scoped_fd
       {
 	if (m_fd >= 0)
 	  close (m_fd);
-	m_fd = other.m_fd;
-	other.m_fd = -1;
+	m_fd = std::exchange (other.m_fd, -1);
       }
     return *this;
   }
@@ -58,9 +57,7 @@ class scoped_fd
 
   ATTRIBUTE_UNUSED_RESULT int release () noexcept
   {
-    int fd = m_fd;
-    m_fd = -1;
-    return fd;
+    return std::exchange (m_fd, -1);
   }
 
   /* Like release, but return a gdb_file_up that owns the file
diff --git a/gdbsupport/scoped_mmap.h b/gdbsupport/scoped_mmap.h
index 84ad2c38a15..c829a0b3dfb 100644
--- a/gdbsupport/scoped_mmap.h
+++ b/gdbsupport/scoped_mmap.h
@@ -23,6 +23,7 @@
 #ifdef HAVE_SYS_MMAN_H
 
 #include <sys/mman.h>
+#include <utility>
 
 /* A smart-pointer-like class to mmap() and automatically munmap() a memory
    mapping.  */
@@ -54,10 +55,8 @@ class scoped_mmap
 
   ATTRIBUTE_UNUSED_RESULT void *release () noexcept
   {
-    void *mem = m_mem;
-    m_mem = MAP_FAILED;
     m_length = 0;
-    return mem;
+    return std::exchange (m_mem, MAP_FAILED);
   }
 
   void reset (void *addr, size_t length, int prot, int flags, int fd,
-- 
2.51.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25 13:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 13:38 [PATCH 1/2] [gdb] Use std::swap Tom de Vries
2026-11-25 13:38 ` [PATCH 2/2] [gdb] Use std::exchange Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox