[llvm] Raw socket stream (PR #84710)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 10 20:42:55 PDT 2024
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 60dda1fc6ef82c5d7fe54000e6c0a21e7bafdeb5 002ef00c8eddd903d76230ef65e6ef9d703992ba -- llvm/include/llvm/Support/raw_socket_stream.h llvm/lib/Support/raw_socket_stream.cpp llvm/unittests/Support/raw_socket_stream_test.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/llvm/lib/Support/raw_socket_stream.cpp b/llvm/lib/Support/raw_socket_stream.cpp
index 0e9731e622..5cda83d2f8 100644
--- a/llvm/lib/Support/raw_socket_stream.cpp
+++ b/llvm/lib/Support/raw_socket_stream.cpp
@@ -21,9 +21,9 @@
#include <thread>
#ifndef _WIN32
+#include <poll.h>
#include <sys/socket.h>
#include <sys/un.h>
-#include <poll.h>
#else
#include "llvm/Support/Windows/WindowsSupport.h"
// winsock2.h must be included before afunix.h. Briefly turn off clang-format to
@@ -98,8 +98,8 @@ ListeningSocket::ListeningSocket(int SocketFD, StringRef SocketPath,
: FD(SocketFD), SocketPath(SocketPath), PipeFD{PipeFD[0], PipeFD[1]} {}
ListeningSocket::ListeningSocket(ListeningSocket &&LS)
- : FD(LS.FD.load()),
- SocketPath(LS.SocketPath), PipeFD{LS.PipeFD[0], LS.PipeFD[1]} {
+ : FD(LS.FD.load()), SocketPath(LS.SocketPath),
+ PipeFD{LS.PipeFD[0], LS.PipeFD[1]} {
LS.FD = -1;
LS.SocketPath.clear();
@@ -148,41 +148,41 @@ ListeningSocket::createListeningUnixSocket(StringRef SocketPath,
return llvm::make_error<StringError>(getLastSocketErrorCode(),
"socket create failed");
- struct sockaddr_un Addr = setSocketAddr(SocketPath);
- if (::bind(Socket, (struct sockaddr *)&Addr, sizeof(Addr)) == -1) {
- // Grab error code from call to ::bind before calling ::close
- std::error_code EC = getLastSocketErrorCode();
- ::close(Socket);
- return llvm::make_error<StringError>(EC, "Bind error");
- }
+ struct sockaddr_un Addr = setSocketAddr(SocketPath);
+ if (::bind(Socket, (struct sockaddr *)&Addr, sizeof(Addr)) == -1) {
+ // Grab error code from call to ::bind before calling ::close
+ std::error_code EC = getLastSocketErrorCode();
+ ::close(Socket);
+ return llvm::make_error<StringError>(EC, "Bind error");
+ }
- // Mark socket as passive so incoming connections can be accepted
- if (::listen(Socket, MaxBacklog) == -1)
- return llvm::make_error<StringError>(getLastSocketErrorCode(),
- "Listen error");
+ // Mark socket as passive so incoming connections can be accepted
+ if (::listen(Socket, MaxBacklog) == -1)
+ return llvm::make_error<StringError>(getLastSocketErrorCode(),
+ "Listen error");
- int PipeFD[2];
+ int PipeFD[2];
#ifdef _WIN32
- // Reserve 1 byte for the pipe and use default textmode
- if (::_pipe(PipeFD, 1, 0) == -1)
+ // Reserve 1 byte for the pipe and use default textmode
+ if (::_pipe(PipeFD, 1, 0) == -1)
#else
if (::pipe(PipeFD) == -1)
#endif // _WIN32
- return llvm::make_error<StringError>(getLastSocketErrorCode(),
- "pipe failed");
+ return llvm::make_error<StringError>(getLastSocketErrorCode(),
+ "pipe failed");
#ifdef _WIN32
- return ListeningSocket{_open_osfhandle(Socket, 0), SocketPath, PipeFD};
+ return ListeningSocket{_open_osfhandle(Socket, 0), SocketPath, PipeFD};
#else
return ListeningSocket{Socket, SocketPath, PipeFD};
#endif // _WIN32
-}
+ }
-Expected<std::unique_ptr<raw_socket_stream>>
-ListeningSocket::accept(std::optional<std::chrono::milliseconds> Timeout) {
+ Expected<std::unique_ptr<raw_socket_stream>> ListeningSocket::accept(
+ std::optional<std::chrono::milliseconds> Timeout) {
- struct pollfd FDs[2];
- FDs[0].events = POLLIN;
+ struct pollfd FDs[2];
+ FDs[0].events = POLLIN;
#ifdef _WIN32
SOCKET WinServerSock = _get_osfhandle(FD);
FDs[0].fd = WinServerSock;
@@ -211,50 +211,50 @@ ListeningSocket::accept(std::optional<std::chrono::milliseconds> Timeout) {
int AcceptFD;
#ifdef _WIN32
- SOCKET WinAcceptSock = ::accept(WinServerSock, NULL, NULL);
- AcceptFD = _open_osfhandle(WinAcceptSock, 0);
+ SOCKET WinAcceptSock = ::accept(WinServerSock, NULL, NULL);
+ AcceptFD = _open_osfhandle(WinAcceptSock, 0);
#else
- AcceptFD = ::accept(FD, NULL, NULL);
+ AcceptFD = ::accept(FD, NULL, NULL);
#endif
if (AcceptFD == -1)
return llvm::make_error<StringError>(getLastSocketErrorCode(),
"accept failed");
return std::make_unique<raw_socket_stream>(AcceptFD);
-}
+ }
-void ListeningSocket::shutdown() {
- if (FD == -1)
- return;
- ::close(FD);
- ::unlink(SocketPath.c_str());
+ void ListeningSocket::shutdown() {
+ if (FD == -1)
+ return;
+ ::close(FD);
+ ::unlink(SocketPath.c_str());
- // Ensure ::poll returns if shutdown is called by a seperate thread
- char Byte = 'A';
- ::write(PipeFD[1], &Byte, 1);
+ // Ensure ::poll returns if shutdown is called by a seperate thread
+ char Byte = 'A';
+ ::write(PipeFD[1], &Byte, 1);
- FD = -1;
-}
+ FD = -1;
+ }
-ListeningSocket::~ListeningSocket() {
- shutdown();
+ ListeningSocket::~ListeningSocket() {
+ shutdown();
- // Close the pipe's FDs in the destructor instead of within
- // ListeningSocket::shutdown to avoid unnecessary synchronization issues that
- // would occur as PipeFD's values would have to be changed to -1
- ::close(PipeFD[0]);
- ::close(PipeFD[1]);
-}
+ // Close the pipe's FDs in the destructor instead of within
+ // ListeningSocket::shutdown to avoid unnecessary synchronization issues
+ // that would occur as PipeFD's values would have to be changed to -1
+ ::close(PipeFD[0]);
+ ::close(PipeFD[1]);
+ }
-//===----------------------------------------------------------------------===//
-// raw_socket_stream
-//===----------------------------------------------------------------------===//
+ //===----------------------------------------------------------------------===//
+ // raw_socket_stream
+ //===----------------------------------------------------------------------===//
-raw_socket_stream::raw_socket_stream(int SocketFD)
- : raw_fd_stream(SocketFD, true) {}
+ raw_socket_stream::raw_socket_stream(int SocketFD)
+ : raw_fd_stream(SocketFD, true) {}
-Expected<std::unique_ptr<raw_socket_stream>>
-raw_socket_stream::createConnectedUnixSocket(StringRef SocketPath) {
+ Expected<std::unique_ptr<raw_socket_stream>>
+ raw_socket_stream::createConnectedUnixSocket(StringRef SocketPath) {
#ifdef _WIN32
WSABalancer _;
#endif // _WIN32
@@ -262,6 +262,6 @@ raw_socket_stream::createConnectedUnixSocket(StringRef SocketPath) {
if (!FD)
return FD.takeError();
return std::make_unique<raw_socket_stream>(*FD);
-}
+ }
-raw_socket_stream::~raw_socket_stream() {}
+ raw_socket_stream::~raw_socket_stream() {}
``````````
</details>
https://github.com/llvm/llvm-project/pull/84710
More information about the llvm-commits
mailing list