[Lldb-commits] [lldb] [NFC][lldb][windows] Replace C-style casts with static_cast / reinterpret_cast (PR #199039)

Charles Zablit via lldb-commits lldb-commits at lists.llvm.org
Thu May 21 07:14:58 PDT 2026


https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/199039

>From 6a8fa4632e286a1de958d0d9e9655b980ca1627b Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 21 May 2026 11:52:33 +0100
Subject: [PATCH 1/2] [NFC][lldb][windows] Replace C-style casts with
 static_cast / reinterpret_cast

---
 lldb/source/Host/windows/PipeWindows.cpp                      | 3 ++-
 lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp | 2 +-
 .../Plugins/Process/Windows/Common/NativeThreadWindows.cpp    | 4 ++--
 .../Plugins/Process/Windows/Common/RegisterContextWindows.cpp | 4 ++--
 .../Plugins/Process/Windows/Common/TargetThreadWindows.cpp    | 2 +-
 5 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/lldb/source/Host/windows/PipeWindows.cpp b/lldb/source/Host/windows/PipeWindows.cpp
index 001396fafde04..77ad7fa311a94 100644
--- a/lldb/source/Host/windows/PipeWindows.cpp
+++ b/lldb/source/Host/windows/PipeWindows.cpp
@@ -34,7 +34,8 @@ PipeWindows::PipeWindows()
 }
 
 PipeWindows::PipeWindows(pipe_t read, pipe_t write)
-    : m_read((HANDLE)read), m_write((HANDLE)write),
+    : m_read(reinterpret_cast<HANDLE>(read)),
+      m_write(reinterpret_cast<HANDLE>(write)),
       m_read_fd(PipeWindows::kInvalidDescriptor),
       m_write_fd(PipeWindows::kInvalidDescriptor) {
   assert(read != LLDB_INVALID_PIPE || write != LLDB_INVALID_PIPE);
diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index e47771b18b59f..0e2156e4f8c99 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -156,7 +156,7 @@ lldb::thread_result_t DebuggerThread::DebuggerThreadAttachRoutine(
   LLDB_LOG(log, "preparing to attach to process '{0}' on background thread.",
            pid);
 
-  if (!DebugActiveProcess((DWORD)pid)) {
+  if (!DebugActiveProcess(static_cast<DWORD>(pid))) {
     Status error(::GetLastError(), eErrorTypeWin32);
     m_debug_delegate->OnDebuggerError(error, 0);
     return {};
diff --git a/lldb/source/Plugins/Process/Windows/Common/NativeThreadWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/NativeThreadWindows.cpp
index d76e9a51fdace..442af86af40d2 100644
--- a/lldb/source/Plugins/Process/Windows/Common/NativeThreadWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/NativeThreadWindows.cpp
@@ -35,7 +35,7 @@ Status NativeThreadWindows::DoStop() {
   if (m_state != eStateStopped) {
     DWORD previous_suspend_count =
         ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
-    if (previous_suspend_count == (DWORD)-1)
+    if (previous_suspend_count == static_cast<DWORD>(-1))
       return Status(::GetLastError(), eErrorTypeWin32);
 
     // Invalidate cached register values on every stop.
@@ -88,7 +88,7 @@ Status NativeThreadWindows::DoResume(lldb::StateType resume_state) {
       // explicitly compare with -1.
       previous_suspend_count = ::ResumeThread(thread_handle);
 
-      if (previous_suspend_count == (DWORD)-1)
+      if (previous_suspend_count == static_cast<DWORD>(-1))
         return Status(::GetLastError(), eErrorTypeWin32);
 
     } while (previous_suspend_count > 1);
diff --git a/lldb/source/Plugins/Process/Windows/Common/RegisterContextWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/RegisterContextWindows.cpp
index 25b67bec3e3cf..8e03c5f73cecb 100644
--- a/lldb/source/Plugins/Process/Windows/Common/RegisterContextWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/RegisterContextWindows.cpp
@@ -158,7 +158,7 @@ bool RegisterContextWindows::CacheAllRegisterValues() {
   m_context.ContextFlags = kWinContextFlags;
   if (::SuspendThread(
           wthread.GetHostThread().GetNativeThread().GetSystemHandle()) ==
-      (DWORD)-1) {
+      static_cast<DWORD>(-1)) {
     return false;
   }
   if (!::GetThreadContext(
@@ -172,7 +172,7 @@ bool RegisterContextWindows::CacheAllRegisterValues() {
   }
   if (::ResumeThread(
           wthread.GetHostThread().GetNativeThread().GetSystemHandle()) ==
-      (DWORD)-1) {
+      static_cast<DWORD>(-1)) {
     return false;
   }
   LLDB_LOG(log, "successfully updated the register values.");
diff --git a/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp
index b2b66f2927644..bd6db55446cd6 100644
--- a/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp
@@ -166,7 +166,7 @@ Status TargetThreadWindows::DoResume() {
       // explicitly compare with -1.
       previous_suspend_count = ::ResumeThread(thread_handle);
 
-      if (previous_suspend_count == (DWORD)-1)
+      if (previous_suspend_count == static_cast<DWORD>(-1))
         return Status(::GetLastError(), eErrorTypeWin32);
 
     } while (previous_suspend_count > 1);

>From b9166641980ab390275dc9e64fddee1e563267ef Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 21 May 2026 11:53:48 +0100
Subject: [PATCH 2/2] [NFC][lldb][windows] Replace ((HANDLE)(long long)-1) with
 reinterpret_cast

---
 lldb/include/lldb/Host/windows/PseudoConsole.h | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/lldb/include/lldb/Host/windows/PseudoConsole.h b/lldb/include/lldb/Host/windows/PseudoConsole.h
index 0e927e3a433ec..e0f6546b7561b 100644
--- a/lldb/include/lldb/Host/windows/PseudoConsole.h
+++ b/lldb/include/lldb/Host/windows/PseudoConsole.h
@@ -128,12 +128,18 @@ class PseudoConsole {
   void SetStopping(bool value) { m_stopping = value; };
 
 protected:
-  HANDLE m_conpty_handle = ((HANDLE)(long long)-1);
-  HANDLE m_conpty_output = ((HANDLE)(long long)-1);
-  HANDLE m_conpty_input = ((HANDLE)(long long)-1);
+  // Equivalent to INVALID_HANDLE_VALUE, but spelled without pulling in
+  // <windows.h> from a public header. HANDLE is `void *` (typedef'd above);
+  // reinterpret_cast cannot appear in a constant expression (C++17), so this
+  // is a runtime initializer evaluated by every constructor.
+  HANDLE m_conpty_handle = reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
+  HANDLE m_conpty_output = reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
+  HANDLE m_conpty_input = reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
   // Pipe mode: child-side handles passed to CreateProcessW, closed after launch
-  HANDLE m_pipe_child_stdin = ((HANDLE)(long long)-1);
-  HANDLE m_pipe_child_stdout = ((HANDLE)(long long)-1);
+  HANDLE m_pipe_child_stdin =
+      reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
+  HANDLE m_pipe_child_stdout =
+      reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
   Mode m_mode = Mode::None;
   std::mutex m_mutex{};
   std::condition_variable m_cv{};



More information about the lldb-commits mailing list