Skip to content

Commit d457621

Browse files
authored
[lldb] Fixing warnings / win32 builds in MainLoop. (#146632)
Commit 1a7b7e2 introduced a few casting warnings and a build issue in Win32 platforms. Trying to correct the casts to c++ style casts instead of C style casts.
1 parent ae9990e commit d457621

File tree

6 files changed

+48
-32
lines changed

6 files changed

+48
-32
lines changed

lldb/include/lldb/Host/windows/MainLoopWindows.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace lldb_private {
1919

20+
using handle_t = void *;
21+
2022
// Windows-specific implementation of the MainLoopBase class. It can monitor
2123
// socket descriptors for readability using WSAEventSelect. Non-socket file
2224
// descriptors are not supported.
@@ -33,15 +35,15 @@ class MainLoopWindows : public MainLoopBase {
3335

3436
class IOEvent {
3537
public:
36-
IOEvent(IOObject::WaitableHandle event) : m_event(event) {}
38+
IOEvent(handle_t event) : m_event(event) {}
3739
virtual ~IOEvent() {}
3840
virtual void WillPoll() {}
3941
virtual void DidPoll() {}
4042
virtual void Disarm() {}
41-
IOObject::WaitableHandle GetHandle() { return m_event; }
43+
handle_t GetHandle() { return m_event; }
4244

4345
protected:
44-
IOObject::WaitableHandle m_event;
46+
handle_t m_event;
4547
};
4648
using IOEventUP = std::unique_ptr<IOEvent>;
4749

lldb/include/lldb/lldb-types.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ typedef void *rwlock_t;
4242
typedef void *process_t; // Process type is HANDLE
4343
typedef void *thread_t; // Host thread type
4444
typedef void *file_t; // Host file type
45-
typedef unsigned int __w64 socket_t; // Host socket type
45+
typedef uintptr_t socket_t; // Host socket type
4646
typedef void *thread_arg_t; // Host thread argument type
4747
typedef unsigned thread_result_t; // Host thread result type
4848
typedef thread_result_t (*thread_func_t)(void *); // Host thread function type
4949
typedef void *pipe_t; // Host pipe type is HANDLE
5050

51+
// printf macro for file_t
52+
#define PRIuFT PRIuPTR
53+
5154
#else
5255

5356
#include <pthread.h>
@@ -63,6 +66,9 @@ typedef void *thread_result_t; // Host thread result type
6366
typedef void *(*thread_func_t)(void *); // Host thread function type
6467
typedef int pipe_t; // Host pipe type
6568

69+
// printf macro for file_t
70+
#define PRIuFT PRIi32
71+
6672
#endif // _WIN32
6773

6874
#define LLDB_INVALID_PROCESS ((lldb::process_t)-1)

lldb/source/Host/common/JSONTransport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ ReadFull(IOObject &descriptor, size_t length,
4242
if (timeout && timeout_supported) {
4343
SelectHelper sh;
4444
sh.SetTimeout(*timeout);
45-
sh.FDSetRead((lldb::socket_t)descriptor.GetWaitableHandle());
45+
sh.FDSetRead(
46+
reinterpret_cast<lldb::socket_t>(descriptor.GetWaitableHandle()));
4647
Status status = sh.Select();
4748
if (status.Fail()) {
4849
// Convert timeouts into a specific error.

lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
273273

274274
if (log) {
275275
LLDB_LOGF(log,
276-
"%p ConnectionFileDescriptor::Read() fd = %" PRIu64
276+
"%p ConnectionFileDescriptor::Read() fd = %" PRIuFT
277277
", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
278278
static_cast<void *>(this),
279-
static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),
279+
static_cast<file_t>(m_io_sp->GetWaitableHandle()),
280280
static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
281281
static_cast<uint64_t>(bytes_read), error.AsCString());
282282
}
@@ -377,10 +377,10 @@ size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
377377

378378
if (log) {
379379
LLDB_LOGF(log,
380-
"%p ConnectionFileDescriptor::Write(fd = %" PRIu64
380+
"%p ConnectionFileDescriptor::Write(fd = %" PRIuFT
381381
", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",
382382
static_cast<void *>(this),
383-
static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),
383+
static_cast<file_t>(m_io_sp->GetWaitableHandle()),
384384
static_cast<const void *>(src), static_cast<uint64_t>(src_len),
385385
static_cast<uint64_t>(bytes_sent), error.AsCString());
386386
}
@@ -452,7 +452,7 @@ ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
452452
select_helper.SetTimeout(*timeout);
453453

454454
// FIXME: Migrate to MainLoop.
455-
select_helper.FDSetRead((lldb::socket_t)handle);
455+
select_helper.FDSetRead(reinterpret_cast<socket_t>(handle));
456456
#if defined(_WIN32)
457457
// select() won't accept pipes on Windows. The entire Windows codepath
458458
// needs to be converted over to using WaitForMultipleObjects and event

lldb/source/Host/windows/MainLoopWindows.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lldb/Host/windows/MainLoopWindows.h"
1010
#include "lldb/Host/Config.h"
1111
#include "lldb/Host/Socket.h"
12+
#include "lldb/Host/windows/windows.h"
1213
#include "lldb/Utility/Status.h"
1314
#include "llvm/Config/llvm-config.h"
1415
#include "llvm/Support/Casting.h"
@@ -18,6 +19,8 @@
1819
#include <cerrno>
1920
#include <csignal>
2021
#include <ctime>
22+
#include <io.h>
23+
#include <thread>
2124
#include <vector>
2225
#include <winsock2.h>
2326

@@ -39,9 +42,8 @@ namespace {
3942
class PipeEvent : public MainLoopWindows::IOEvent {
4043
public:
4144
explicit PipeEvent(HANDLE handle)
42-
: IOEvent((IOObject::WaitableHandle)CreateEventW(
43-
NULL, /*bManualReset=*/FALSE,
44-
/*bInitialState=*/FALSE, NULL)),
45+
: IOEvent(CreateEventW(NULL, /*bManualReset=*/FALSE,
46+
/*bInitialState=*/FALSE, NULL)),
4547
m_handle(handle), m_ready(CreateEventW(NULL, /*bManualReset=*/FALSE,
4648
/*bInitialState=*/FALSE, NULL)) {
4749
assert(m_event && m_ready);
@@ -53,12 +55,12 @@ class PipeEvent : public MainLoopWindows::IOEvent {
5355
SetEvent(m_ready);
5456
// Keep trying to cancel ReadFile() until the thread exits.
5557
do {
56-
CancelIoEx((HANDLE)m_handle, /*lpOverlapped=*/NULL);
58+
CancelIoEx(m_handle, /*lpOverlapped=*/NULL);
5759
} while (WaitForSingleObject(m_monitor_thread.native_handle(), 1) ==
5860
WAIT_TIMEOUT);
5961
m_monitor_thread.join();
6062
}
61-
CloseHandle((HANDLE)m_event);
63+
CloseHandle(m_event);
6264
CloseHandle(m_ready);
6365
}
6466

@@ -107,7 +109,7 @@ class PipeEvent : public MainLoopWindows::IOEvent {
107109
continue;
108110
}
109111

110-
SetEvent((HANDLE)m_event);
112+
SetEvent(m_event);
111113

112114
// Wait until the current read is consumed before doing the next read.
113115
WaitForSingleObject(m_ready, INFINITE);
@@ -124,15 +126,15 @@ class PipeEvent : public MainLoopWindows::IOEvent {
124126
class SocketEvent : public MainLoopWindows::IOEvent {
125127
public:
126128
explicit SocketEvent(SOCKET socket)
127-
: IOEvent((IOObject::WaitableHandle)WSACreateEvent()), m_socket(socket) {
129+
: IOEvent(WSACreateEvent()), m_socket(socket) {
128130
assert(m_event != WSA_INVALID_EVENT);
129131
}
130132

131-
~SocketEvent() override { WSACloseEvent((HANDLE)m_event); }
133+
~SocketEvent() override { WSACloseEvent(m_event); }
132134

133135
void WillPoll() {
134-
int result = WSAEventSelect(m_socket, (HANDLE)m_event,
135-
FD_READ | FD_ACCEPT | FD_CLOSE);
136+
int result =
137+
WSAEventSelect(m_socket, m_event, FD_READ | FD_ACCEPT | FD_CLOSE);
136138
assert(result == 0);
137139
UNUSED_IF_ASSERT_DISABLED(result);
138140
}
@@ -143,7 +145,7 @@ class SocketEvent : public MainLoopWindows::IOEvent {
143145
UNUSED_IF_ASSERT_DISABLED(result);
144146
}
145147

146-
void Disarm() override { WSAResetEvent((HANDLE)m_event); }
148+
void Disarm() override { WSAResetEvent(m_event); }
147149

148150
SOCKET m_socket;
149151
};
@@ -167,7 +169,7 @@ llvm::Expected<size_t> MainLoopWindows::Poll() {
167169
events.reserve(m_read_fds.size() + 1);
168170
for (auto &[_, fd_info] : m_read_fds) {
169171
fd_info.event->WillPoll();
170-
events.push_back((HANDLE)fd_info.event->GetHandle());
172+
events.push_back(fd_info.event->GetHandle());
171173
}
172174
events.push_back(m_interrupt_event);
173175

@@ -206,16 +208,21 @@ MainLoopWindows::RegisterReadObject(const IOObjectSP &object_sp,
206208
return nullptr;
207209
}
208210

209-
if (object_sp->GetFdType() == IOObject::eFDTypeSocket)
211+
if (object_sp->GetFdType() == IOObject::eFDTypeSocket) {
210212
m_read_fds[waitable_handle] = {
211-
std::make_unique<SocketEvent>((SOCKET)waitable_handle), callback};
212-
else if (GetFileType(waitable_handle) == FILE_TYPE_PIPE)
213-
m_read_fds[waitable_handle] = {
214-
std::make_unique<PipeEvent>((HANDLE)waitable_handle), callback};
215-
else {
216-
error = Status::FromErrorStringWithFormat("Unsupported file type %d",
217-
GetFileType(waitable_handle));
218-
return nullptr;
213+
std::make_unique<SocketEvent>(
214+
reinterpret_cast<SOCKET>(waitable_handle)),
215+
callback};
216+
} else {
217+
DWORD file_type = GetFileType(waitable_handle);
218+
if (file_type != FILE_TYPE_PIPE) {
219+
error = Status::FromErrorStringWithFormat("Unsupported file type %d",
220+
file_type);
221+
return nullptr;
222+
}
223+
224+
m_read_fds[waitable_handle] = {std::make_unique<PipeEvent>(waitable_handle),
225+
callback};
219226
}
220227

221228
return CreateReadHandle(object_sp);

lldb/source/Utility/SelectHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ lldb_private::Status SelectHelper::Select() {
177177
#endif
178178
// Set the FD bits in the fdsets for read/write/error
179179
for (auto &pair : m_fd_map) {
180-
const lldb::socket_t fd = pair.first;
180+
lldb::socket_t fd = pair.first;
181181

182182
if (pair.second.read_set)
183183
FD_SET(fd, read_fdset_ptr);

0 commit comments

Comments
 (0)