Skip to content

Commit 76a986b

Browse files
authored
Merge pull request #9518 from swiftlang/cherrypick-49277253f016268e4a10109f1db2e53c60d35881
[lldb] Use LLVM's helper for Unicode conversion (NFC) (llvm#112582)
2 parents 76c9c35 + aa675fd commit 76a986b

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

lldb/include/lldb/Host/Editline.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030

3131
#include "lldb/Host/Config.h"
3232

33-
#if LLDB_EDITLINE_USE_WCHAR
34-
#include <codecvt>
35-
#endif
3633
#include <locale>
3734
#include <sstream>
3835
#include <vector>
@@ -57,23 +54,6 @@
5754

5855
#include "llvm/ADT/FunctionExtras.h"
5956

60-
#if defined(__clang__) && defined(__has_warning)
61-
#if __has_warning("-Wdeprecated-declarations")
62-
#define LLDB_DEPRECATED_WARNING_DISABLE \
63-
_Pragma("clang diagnostic push") \
64-
_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
65-
#define LLDB_DEPRECATED_WARNING_RESTORE _Pragma("clang diagnostic pop")
66-
#endif
67-
#elif defined(__GNUC__) && __GNUC__ > 6
68-
#define LLDB_DEPRECATED_WARNING_DISABLE \
69-
_Pragma("GCC diagnostic push") \
70-
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
71-
#define LLDB_DEPRECATED_WARNING_RESTORE _Pragma("GCC diagnostic pop")
72-
#else
73-
#define LLDB_DEPRECATED_WARNING_DISABLE
74-
#define LLDB_DEPRECATED_WARNING_RESTORE
75-
#endif
76-
7757
namespace lldb_private {
7858
namespace line_editor {
7959

@@ -383,11 +363,6 @@ class Editline {
383363
void SetEditLinePromptCallback(EditlinePromptCallbackType callbackFn);
384364
void SetGetCharacterFunction(EditlineGetCharCallbackType callbackFn);
385365

386-
#if LLDB_EDITLINE_USE_WCHAR
387-
LLDB_DEPRECATED_WARNING_DISABLE
388-
std::wstring_convert<std::codecvt_utf8<wchar_t>> m_utf8conv;
389-
LLDB_DEPRECATED_WARNING_RESTORE
390-
#endif
391366
::EditLine *m_editline = nullptr;
392367
EditlineHistorySP m_history_sp;
393368
bool m_in_history = false;

lldb/source/Host/common/Editline.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
#include <iomanip>
1111
#include <optional>
1212

13-
#include "lldb/Host/Editline.h"
14-
1513
#include "lldb/Host/ConnectionFileDescriptor.h"
14+
#include "lldb/Host/Editline.h"
1615
#include "lldb/Host/FileSystem.h"
1716
#include "lldb/Host/Host.h"
1817
#include "lldb/Utility/CompletionRequest.h"
@@ -23,6 +22,7 @@
2322
#include "lldb/Utility/StreamString.h"
2423
#include "lldb/Utility/StringList.h"
2524
#include "lldb/Utility/Timeout.h"
25+
#include "llvm/Support/ConvertUTF.h"
2626

2727
#include "llvm/Support/FileSystem.h"
2828
#include "llvm/Support/Locale.h"
@@ -444,7 +444,9 @@ StringList Editline::GetInputAsStringList(int line_count) {
444444
if (line_count == 0)
445445
break;
446446
#if LLDB_EDITLINE_USE_WCHAR
447-
lines.AppendString(m_utf8conv.to_bytes(line));
447+
std::string buffer;
448+
llvm::convertWideToUTF8(line, buffer);
449+
lines.AppendString(buffer);
448450
#else
449451
lines.AppendString(line);
450452
#endif
@@ -636,7 +638,9 @@ unsigned char Editline::BreakLineCommand(int ch) {
636638
if (m_fix_indentation_callback) {
637639
StringList lines = GetInputAsStringList(m_current_line_index + 1);
638640
#if LLDB_EDITLINE_USE_WCHAR
639-
lines.AppendString(m_utf8conv.to_bytes(new_line_fragment));
641+
std::string buffer;
642+
llvm::convertWideToUTF8(new_line_fragment, buffer);
643+
lines.AppendString(buffer);
640644
#else
641645
lines.AppendString(new_line_fragment);
642646
#endif
@@ -684,8 +688,9 @@ unsigned char Editline::EndOrAddLineCommand(int ch) {
684688
m_input_lines.clear();
685689
for (unsigned index = 0; index < lines.GetSize(); index++) {
686690
#if LLDB_EDITLINE_USE_WCHAR
687-
m_input_lines.insert(m_input_lines.end(),
688-
m_utf8conv.from_bytes(lines[index]));
691+
std::wstring wbuffer;
692+
llvm::ConvertUTF8toWide(lines[index], wbuffer);
693+
m_input_lines.insert(m_input_lines.end(), wbuffer);
689694
#else
690695
m_input_lines.insert(m_input_lines.end(), lines[index]);
691696
#endif
@@ -869,7 +874,9 @@ unsigned char Editline::FixIndentationCommand(int ch) {
869874
currentLine = currentLine.erase(0, -indent_correction);
870875
}
871876
#if LLDB_EDITLINE_USE_WCHAR
872-
m_input_lines[m_current_line_index] = m_utf8conv.from_bytes(currentLine);
877+
std::wstring wbuffer;
878+
llvm::ConvertUTF8toWide(currentLine, wbuffer);
879+
m_input_lines[m_current_line_index] = wbuffer;
873880
#else
874881
m_input_lines[m_current_line_index] = currentLine;
875882
#endif
@@ -1502,7 +1509,7 @@ bool Editline::GetLine(std::string &line, bool &interrupted) {
15021509
} else {
15031510
m_history_sp->Enter(input);
15041511
#if LLDB_EDITLINE_USE_WCHAR
1505-
line = m_utf8conv.to_bytes(SplitLines(input)[0]);
1512+
llvm::convertWideToUTF8(SplitLines(input)[0], line);
15061513
#else
15071514
line = SplitLines(input)[0];
15081515
#endif
@@ -1574,25 +1581,22 @@ bool Editline::CompleteCharacter(char ch, EditLineGetCharType &out) {
15741581
out = (unsigned char)ch;
15751582
return true;
15761583
#else
1577-
LLDB_DEPRECATED_WARNING_DISABLE
1578-
std::codecvt_utf8<wchar_t> cvt;
1579-
LLDB_DEPRECATED_WARNING_RESTORE
15801584
llvm::SmallString<4> input;
15811585
for (;;) {
1582-
const char *from_next;
1583-
wchar_t *to_next;
1584-
std::mbstate_t state = std::mbstate_t();
15851586
input.push_back(ch);
1586-
switch (cvt.in(state, input.begin(), input.end(), from_next, &out, &out + 1,
1587-
to_next)) {
1588-
case std::codecvt_base::ok:
1587+
auto *cur_ptr = reinterpret_cast<const llvm::UTF8 *>(input.begin());
1588+
auto *end_ptr = reinterpret_cast<const llvm::UTF8 *>(input.end());
1589+
llvm::UTF32 code_point = 0;
1590+
llvm::ConversionResult cr = llvm::convertUTF8Sequence(
1591+
&cur_ptr, end_ptr, &code_point, llvm::lenientConversion);
1592+
switch (cr) {
1593+
case llvm::conversionOK:
1594+
out = code_point;
15891595
return out != (EditLineGetCharType)WEOF;
1590-
1591-
case std::codecvt_base::error:
1592-
case std::codecvt_base::noconv:
1596+
case llvm::targetExhausted:
1597+
case llvm::sourceIllegal:
15931598
return false;
1594-
1595-
case std::codecvt_base::partial:
1599+
case llvm::sourceExhausted:
15961600
lldb::ConnectionStatus status;
15971601
size_t read_count = m_input_connection.Read(
15981602
&ch, 1, std::chrono::seconds(0), status, nullptr);

0 commit comments

Comments
 (0)