Skip to content

Conversation

charles-zablit
Copy link
Contributor

std::wstring AnsiToUtf16(const std::string &ansi) is a reimplementation of llvm::sys::windows::UTF8ToUTF16. This patch removes AnsiToUtf16 and its usages entirely.

@llvmbot
Copy link
Member

llvmbot commented Aug 19, 2025

@llvm/pr-subscribers-lldb

Author: Charles Zablit (charles-zablit)

Changes

std::wstring AnsiToUtf16(const std::string &ansi) is a reimplementation of llvm::sys::windows::UTF8ToUTF16. This patch removes AnsiToUtf16 and its usages entirely.


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

1 Files Affected:

  • (modified) lldb/source/Host/windows/Host.cpp (+9-18)
diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp
index 4e747f77afc0e..8b76b21f33b29 100644
--- a/lldb/source/Host/windows/Host.cpp
+++ b/lldb/source/Host/windows/Host.cpp
@@ -22,6 +22,7 @@
 #include "lldb/Utility/StreamString.h"
 #include "lldb/Utility/StructuredData.h"
 
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/ConvertUTF.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -32,6 +33,8 @@
 using namespace lldb;
 using namespace lldb_private;
 
+using llvm::sys::windows::UTF8ToUTF16;
+
 static bool GetTripleForProcess(const FileSpec &executable,
                                 llvm::Triple &triple) {
   // Open the PE File as a binary file, and parse just enough information to
@@ -325,30 +328,17 @@ class WindowsEventLog {
 
 static llvm::ManagedStatic<WindowsEventLog> event_log;
 
-static std::wstring AnsiToUtf16(const std::string &ansi) {
-  if (ansi.empty())
-    return {};
-
-  const int unicode_length =
-      MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, nullptr, 0);
-  if (unicode_length == 0)
-    return {};
-
-  std::wstring unicode(unicode_length, L'\0');
-  MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, &unicode[0], unicode_length);
-  return unicode;
-}
-
 void Host::SystemLog(Severity severity, llvm::StringRef message) {
   HANDLE h = event_log->GetHandle();
   if (!h)
     return;
 
-  std::wstring wide_message = AnsiToUtf16(message.str());
-  if (wide_message.empty())
+  llvm::SmallVector<wchar_t, 1> argsUTF16;
+  if (UTF8ToUTF16(message.str(), argsUTF16))
     return;
 
-  LPCWSTR msg_ptr = wide_message.c_str();
+  if (argsUTF16.empty())
+    return;
 
   WORD event_type;
   switch (severity) {
@@ -363,5 +353,6 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) {
     event_type = EVENTLOG_INFORMATION_TYPE;
   }
 
-  ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, &msg_ptr, nullptr);
+  LPCWSTR messages[] = {argsUTF16.data()};
+  ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, messages, nullptr);
 }

@@ -363,5 +353,6 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) {
event_type = EVENTLOG_INFORMATION_TYPE;
}

ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, &msg_ptr, nullptr);
LPCWSTR messages[] = {argsUTF16.data()};
ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, messages, nullptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider retaining this as &argsUTF16.data() instead of the LPCWSTR[]. Or if you want to use the explicit array, consider replacing the 1 with countof(messages).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could not get it to compile with the &argsUTF16.data(), because of the following error: cannot convert argument 8 from 'wchar_t **' to 'LPCWSTR *'.

I used a fixed size array and std::size.

@charles-zablit charles-zablit merged commit af5f16b into llvm:main Aug 20, 2025
9 checks passed
charles-zablit added a commit to charles-zablit/llvm-project that referenced this pull request Sep 2, 2025
…154424)

`std::wstring AnsiToUtf16(const std::string &ansi)` is a
reimplementation of `llvm::sys::windows::UTF8ToUTF16`. This patch
removes `AnsiToUtf16` and its usages entirely.
charles-zablit added a commit to charles-zablit/llvm-project that referenced this pull request Sep 4, 2025
…154424)

`std::wstring AnsiToUtf16(const std::string &ansi)` is a
reimplementation of `llvm::sys::windows::UTF8ToUTF16`. This patch
removes `AnsiToUtf16` and its usages entirely.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants