Skip to content

Commit fca82bd

Browse files
[lldb][windows] remove duplicate implementation of UTF8ToUTF16 (llvm#154424)
`std::wstring AnsiToUtf16(const std::string &ansi)` is a reimplementation of `llvm::sys::windows::UTF8ToUTF16`. This patch removes `AnsiToUtf16` and its usages entirely.
1 parent fd3865c commit fca82bd

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

lldb/source/Host/windows/Host.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "lldb/Utility/StreamString.h"
2323
#include "lldb/Utility/StructuredData.h"
2424

25+
#include "llvm/ADT/SmallVector.h"
2526
#include "llvm/ADT/StringRef.h"
2627
#include "llvm/Support/ConvertUTF.h"
2728
#include "llvm/Support/ManagedStatic.h"
@@ -32,6 +33,8 @@
3233
using namespace lldb;
3334
using namespace lldb_private;
3435

36+
using llvm::sys::windows::UTF8ToUTF16;
37+
3538
static bool GetTripleForProcess(const FileSpec &executable,
3639
llvm::Triple &triple) {
3740
// Open the PE File as a binary file, and parse just enough information to
@@ -325,31 +328,18 @@ class WindowsEventLog {
325328

326329
static llvm::ManagedStatic<WindowsEventLog> event_log;
327330

328-
static std::wstring AnsiToUtf16(const std::string &ansi) {
329-
if (ansi.empty())
330-
return {};
331-
332-
const int unicode_length =
333-
MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, nullptr, 0);
334-
if (unicode_length == 0)
335-
return {};
336-
337-
std::wstring unicode(unicode_length, L'\0');
338-
MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, &unicode[0], unicode_length);
339-
return unicode;
340-
}
341-
342331
void Host::SystemLog(Severity severity, llvm::StringRef message) {
332+
if (message.empty())
333+
return;
334+
343335
HANDLE h = event_log->GetHandle();
344336
if (!h)
345337
return;
346338

347-
std::wstring wide_message = AnsiToUtf16(message.str());
348-
if (wide_message.empty())
339+
llvm::SmallVector<wchar_t, 1> argsUTF16;
340+
if (UTF8ToUTF16(message.str(), argsUTF16))
349341
return;
350342

351-
LPCWSTR msg_ptr = wide_message.c_str();
352-
353343
WORD event_type;
354344
switch (severity) {
355345
case lldb::eSeverityWarning:
@@ -363,5 +353,7 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) {
363353
event_type = EVENTLOG_INFORMATION_TYPE;
364354
}
365355

366-
ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, &msg_ptr, nullptr);
356+
LPCWSTR messages[1] = {argsUTF16.data()};
357+
ReportEventW(h, event_type, 0, 0, nullptr, std::size(messages), 0, messages,
358+
nullptr);
367359
}

0 commit comments

Comments
 (0)