[Lldb-commits] [lldb] [lldb][NFC] Moved FindSchemeByProtocol() from Acceptor to Socket (PR #104439)

via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 15 07:07:20 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Dmitry Vasilyev (slydiman)

<details>
<summary>Changes</summary>

This is the prerequisite for #<!-- -->104238.

---
Full diff: https://github.com/llvm/llvm-project/pull/104439.diff


3 Files Affected:

- (modified) lldb/include/lldb/Host/Socket.h (+4) 
- (modified) lldb/source/Host/common/Socket.cpp (+32) 
- (modified) lldb/tools/lldb-server/Acceptor.cpp (+3-36) 


``````````diff
diff --git a/lldb/include/lldb/Host/Socket.h b/lldb/include/lldb/Host/Socket.h
index 4dad88e78faa11..573c881f727d8f 100644
--- a/lldb/include/lldb/Host/Socket.h
+++ b/lldb/include/lldb/Host/Socket.h
@@ -60,6 +60,10 @@ class Socket : public IOObject {
 
   ~Socket() override;
 
+  static const char *FindSchemeByProtocol(const SocketProtocol protocol);
+  static bool FindProtocolByScheme(const char *scheme,
+                                   SocketProtocol &protocol);
+
   static llvm::Error Initialize();
   static void Terminate();
 
diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp
index f9911cf136cbd0..7364a12280cfdd 100644
--- a/lldb/source/Host/common/Socket.cpp
+++ b/lldb/source/Host/common/Socket.cpp
@@ -70,6 +70,38 @@ static bool IsInterrupted() {
 #endif
 }
 
+struct SocketScheme {
+  const char *m_scheme;
+  const Socket::SocketProtocol m_protocol;
+};
+
+static SocketScheme socket_schemes[] = {
+    {"tcp", Socket::ProtocolTcp},
+    {"udp", Socket::ProtocolUdp},
+    {"unix", Socket::ProtocolUnixDomain},
+    {"unix-abstract", Socket::ProtocolUnixAbstract},
+};
+
+const char *
+Socket::FindSchemeByProtocol(const Socket::SocketProtocol protocol) {
+  for (auto s : socket_schemes) {
+    if (s.m_protocol == protocol)
+      return s.m_scheme;
+  }
+  return nullptr;
+}
+
+bool Socket::FindProtocolByScheme(const char *scheme,
+                                  Socket::SocketProtocol &protocol) {
+  for (auto s : socket_schemes) {
+    if (!strcmp(s.m_scheme, scheme)) {
+      protocol = s.m_protocol;
+      return true;
+    }
+  }
+  return false;
+}
+
 Socket::Socket(SocketProtocol protocol, bool should_close,
                bool child_processes_inherit)
     : IOObject(eFDTypeSocket), m_protocol(protocol),
diff --git a/lldb/tools/lldb-server/Acceptor.cpp b/lldb/tools/lldb-server/Acceptor.cpp
index 2037f1e0f62b9a..cf9a55c6c07da5 100644
--- a/lldb/tools/lldb-server/Acceptor.cpp
+++ b/lldb/tools/lldb-server/Acceptor.cpp
@@ -22,40 +22,6 @@ using namespace lldb_private;
 using namespace lldb_private::lldb_server;
 using namespace llvm;
 
-namespace {
-
-struct SocketScheme {
-  const char *m_scheme;
-  const Socket::SocketProtocol m_protocol;
-};
-
-SocketScheme socket_schemes[] = {
-    {"tcp", Socket::ProtocolTcp},
-    {"udp", Socket::ProtocolUdp},
-    {"unix", Socket::ProtocolUnixDomain},
-    {"unix-abstract", Socket::ProtocolUnixAbstract},
-};
-
-bool FindProtocolByScheme(const char *scheme,
-                          Socket::SocketProtocol &protocol) {
-  for (auto s : socket_schemes) {
-    if (!strcmp(s.m_scheme, scheme)) {
-      protocol = s.m_protocol;
-      return true;
-    }
-  }
-  return false;
-}
-
-const char *FindSchemeByProtocol(const Socket::SocketProtocol protocol) {
-  for (auto s : socket_schemes) {
-    if (s.m_protocol == protocol)
-      return s.m_scheme;
-  }
-  return nullptr;
-}
-}
-
 Status Acceptor::Listen(int backlog) {
   return m_listener_socket_up->Listen(StringRef(m_name), backlog);
 }
@@ -74,7 +40,7 @@ Socket::SocketProtocol Acceptor::GetSocketProtocol() const {
 }
 
 const char *Acceptor::GetSocketScheme() const {
-  return FindSchemeByProtocol(GetSocketProtocol());
+  return Socket::FindSchemeByProtocol(GetSocketProtocol());
 }
 
 std::string Acceptor::GetLocalSocketId() const { return m_local_socket_id(); }
@@ -87,7 +53,8 @@ std::unique_ptr<Acceptor> Acceptor::Create(StringRef name,
   Socket::SocketProtocol socket_protocol = Socket::ProtocolUnixDomain;
   // Try to match socket name as URL - e.g., tcp://localhost:5555
   if (std::optional<URI> res = URI::Parse(name)) {
-    if (!FindProtocolByScheme(res->scheme.str().c_str(), socket_protocol))
+    if (!Socket::FindProtocolByScheme(res->scheme.str().c_str(),
+                                      socket_protocol))
       error.SetErrorStringWithFormat("Unknown protocol scheme \"%s\"",
                                      res->scheme.str().c_str());
     else

``````````

</details>


https://github.com/llvm/llvm-project/pull/104439


More information about the lldb-commits mailing list