-
Notifications
You must be signed in to change notification settings - Fork 15k
[lldb][windows] remove duplicate implementation of UTF8ToUTF16 #154424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[lldb][windows] remove duplicate implementation of UTF8ToUTF16 #154424
Conversation
@llvm/pr-subscribers-lldb Author: Charles Zablit (charles-zablit) Changes
Full diff: https://github.com/llvm/llvm-project/pull/154424.diff 1 Files Affected:
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);
}
|
lldb/source/Host/windows/Host.cpp
Outdated
@@ -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); |
There was a problem hiding this comment.
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)
.
There was a problem hiding this comment.
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
.
…154424) `std::wstring AnsiToUtf16(const std::string &ansi)` is a reimplementation of `llvm::sys::windows::UTF8ToUTF16`. This patch removes `AnsiToUtf16` and its usages entirely.
…154424) `std::wstring AnsiToUtf16(const std::string &ansi)` is a reimplementation of `llvm::sys::windows::UTF8ToUTF16`. This patch removes `AnsiToUtf16` and its usages entirely.
std::wstring AnsiToUtf16(const std::string &ansi)
is a reimplementation ofllvm::sys::windows::UTF8ToUTF16
. This patch removesAnsiToUtf16
and its usages entirely.