public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/3] gdbsupport: change scoped_fd::to_file to a free function
@ 2021-07-22 19:37 Simon Marchi
  2021-07-22 19:37 ` [PATCH 2/3] gdbsupport: make gdb_open_cloexec return scoped_fd Simon Marchi
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Simon Marchi @ 2021-07-22 19:37 UTC (permalink / raw)
  To: gdb-patches

The following patch wants to chagne gdb_fopen_cloexec to return a
scoped_fd.  Doing this causes a cyclic include between scoped_fd.h and
filestuff.h.  scoped_fd.h includes filestuff.h because of the
scoped_fd::to_file method.  To fix that, change scoped_fd::to_file to
be a free function in filestuff.h.

Change-Id: Ic82a48914b2aacee8f14af535b7469245f88b93d
---
 gdb/dwarf2/index-write.c            |  2 +-
 gdb/source.c                        |  2 +-
 gdb/unittests/scoped_fd-selftests.c |  2 +-
 gdbsupport/filestuff.h              | 17 +++++++++++++++++
 gdbsupport/scoped_fd.h              | 13 -------------
 5 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index 4e00c716d910..59a6aa4264d5 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -1544,7 +1544,7 @@ struct index_wip_file
     if (out_file_fd.get () == -1)
       perror_with_name (("mkstemp"));
 
-    out_file = out_file_fd.to_file ("wb");
+    out_file = to_file (out_file_fd, "wb");
 
     if (out_file == nullptr)
       error (_("Can't open `%s' for writing"), filename_temp.data ());
diff --git a/gdb/source.c b/gdb/source.c
index 7d1934bd6c9b..0e3308048dc3 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -1620,7 +1620,7 @@ search_command_helper (const char *regex, int from_tty, bool forward)
   if (lseek (desc.get (), (*offsets)[line - 1], 0) < 0)
     perror_with_name (symtab_to_filename_for_display (loc->symtab ()));
 
-  gdb_file_up stream = desc.to_file (FDOPEN_MODE);
+  gdb_file_up stream = to_file (desc, FDOPEN_MODE);
   clearerr (stream.get ());
 
   gdb::def_vector<char> buf;
diff --git a/gdb/unittests/scoped_fd-selftests.c b/gdb/unittests/scoped_fd-selftests.c
index d1803aaf8da2..1a361f00a2e3 100644
--- a/gdb/unittests/scoped_fd-selftests.c
+++ b/gdb/unittests/scoped_fd-selftests.c
@@ -76,7 +76,7 @@ test_to_file ()
 
   unlink (filename);
   
-  gdb_file_up file = sfd.to_file ("rw");
+  gdb_file_up file = to_file (sfd, "rw");
   SELF_CHECK (file != nullptr);
   SELF_CHECK (sfd.get () == -1);
 }
diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h
index 7f4cfb2388b1..658e1df4e558 100644
--- a/gdbsupport/filestuff.h
+++ b/gdbsupport/filestuff.h
@@ -21,6 +21,7 @@
 
 #include <dirent.h>
 #include <fcntl.h>
+#include "scoped_fd.h"
 
 /* Note all the file descriptors which are open when this is called.
    These file descriptors will not be closed by close_most_fds.  */
@@ -96,6 +97,22 @@ gdb_fopen_cloexec (const std::string &filename, const char *opentype)
   return gdb_fopen_cloexec (filename.c_str (), opentype);
 }
 
+/* Converts FD into a gdb_file_up.
+
+   On success, the file descriptor owned by FD is released, meaning that FD no
+   longer contains nor owns it.  */
+
+static inline
+gdb_file_up to_file (scoped_fd &fd, const char *mode) noexcept
+{
+  gdb_file_up result (fdopen (fd.get (), mode));
+
+  if (result != nullptr)
+    int unused ATTRIBUTE_UNUSED = fd.release ();
+
+  return result;
+}
+
 /* Like 'socketpair', but ensures that the returned file descriptors
    have the close-on-exec flag set.  */
 
diff --git a/gdbsupport/scoped_fd.h b/gdbsupport/scoped_fd.h
index a1aad8493998..a571729c951c 100644
--- a/gdbsupport/scoped_fd.h
+++ b/gdbsupport/scoped_fd.h
@@ -21,7 +21,6 @@
 #define COMMON_SCOPED_FD_H
 
 #include <unistd.h>
-#include "filestuff.h"
 
 /* A smart-pointer-like class to automatically close a file descriptor.  */
 
@@ -63,18 +62,6 @@ class scoped_fd
     return fd;
   }
 
-  /* Like release, but return a gdb_file_up that owns the file
-     descriptor.  On success, this scoped_fd will be released.  On
-     failure, return NULL and leave this scoped_fd in possession of
-     the fd.  */
-  gdb_file_up to_file (const char *mode) noexcept
-  {
-    gdb_file_up result (fdopen (m_fd, mode));
-    if (result != nullptr)
-      m_fd = -1;
-    return result;
-  }
-
   int get () const noexcept
   {
     return m_fd;
-- 
2.32.0


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

end of thread, other threads:[~2021-09-30 19:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-22 19:37 [PATCH 1/3] gdbsupport: change scoped_fd::to_file to a free function Simon Marchi
2021-07-22 19:37 ` [PATCH 2/3] gdbsupport: make gdb_open_cloexec return scoped_fd Simon Marchi
2021-07-22 19:37 ` [PATCH 3/3] gdbsupport: make gdb_mkostemp_cloexec return a scoped_fd Simon Marchi
2021-07-30 13:47 ` [PATCH 1/3] gdbsupport: change scoped_fd::to_file to a free function Tom Tromey
2021-09-30 16:17   ` Simon Marchi
2021-09-30 17:48     ` Tom Tromey
2021-09-30 19:29       ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).