Skip to content

Commit de19f43

Browse files
committed
[lldb] Use LLVM's helper for Unicode conversion (NFC)
The codecvt header has been deprecated in C++17. Use LLVM's unicode helpers to convert between UTF-8 and UTF-16.
1 parent 40ea92c commit de19f43

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
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: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "lldb/Host/Editline.h"
1414

15+
#include <codecvt>
16+
1517
#include "lldb/Host/ConnectionFileDescriptor.h"
1618
#include "lldb/Host/FileSystem.h"
1719
#include "lldb/Host/Host.h"
@@ -23,6 +25,7 @@
2325
#include "lldb/Utility/StreamString.h"
2426
#include "lldb/Utility/StringList.h"
2527
#include "lldb/Utility/Timeout.h"
28+
#include "llvm/Support/ConvertUTF.h"
2629

2730
#include "llvm/Support/FileSystem.h"
2831
#include "llvm/Support/Locale.h"
@@ -444,7 +447,9 @@ StringList Editline::GetInputAsStringList(int line_count) {
444447
if (line_count == 0)
445448
break;
446449
#if LLDB_EDITLINE_USE_WCHAR
447-
lines.AppendString(m_utf8conv.to_bytes(line));
450+
std::string buffer;
451+
llvm::convertWideToUTF8(line, buffer);
452+
lines.AppendString(buffer);
448453
#else
449454
lines.AppendString(line);
450455
#endif
@@ -636,7 +641,9 @@ unsigned char Editline::BreakLineCommand(int ch) {
636641
if (m_fix_indentation_callback) {
637642
StringList lines = GetInputAsStringList(m_current_line_index + 1);
638643
#if LLDB_EDITLINE_USE_WCHAR
639-
lines.AppendString(m_utf8conv.to_bytes(new_line_fragment));
644+
std::string buffer;
645+
llvm::convertWideToUTF8(new_line_fragment, buffer);
646+
lines.AppendString(buffer);
640647
#else
641648
lines.AppendString(new_line_fragment);
642649
#endif
@@ -684,8 +691,9 @@ unsigned char Editline::EndOrAddLineCommand(int ch) {
684691
m_input_lines.clear();
685692
for (unsigned index = 0; index < lines.GetSize(); index++) {
686693
#if LLDB_EDITLINE_USE_WCHAR
687-
m_input_lines.insert(m_input_lines.end(),
688-
m_utf8conv.from_bytes(lines[index]));
694+
std::wstring wbuffer;
695+
llvm::ConvertUTF8toWide(lines[index], wbuffer);
696+
m_input_lines.insert(m_input_lines.end(), wbuffer);
689697
#else
690698
m_input_lines.insert(m_input_lines.end(), lines[index]);
691699
#endif
@@ -869,7 +877,9 @@ unsigned char Editline::FixIndentationCommand(int ch) {
869877
currentLine = currentLine.erase(0, -indent_correction);
870878
}
871879
#if LLDB_EDITLINE_USE_WCHAR
872-
m_input_lines[m_current_line_index] = m_utf8conv.from_bytes(currentLine);
880+
std::wstring wbuffer;
881+
llvm::ConvertUTF8toWide(currentLine, wbuffer);
882+
m_input_lines[m_current_line_index] = wbuffer;
873883
#else
874884
m_input_lines[m_current_line_index] = currentLine;
875885
#endif
@@ -1502,7 +1512,7 @@ bool Editline::GetLine(std::string &line, bool &interrupted) {
15021512
} else {
15031513
m_history_sp->Enter(input);
15041514
#if LLDB_EDITLINE_USE_WCHAR
1505-
line = m_utf8conv.to_bytes(SplitLines(input)[0]);
1515+
llvm::convertWideToUTF8(SplitLines(input)[0], line);
15061516
#else
15071517
line = SplitLines(input)[0];
15081518
#endif
@@ -1574,25 +1584,23 @@ bool Editline::CompleteCharacter(char ch, EditLineGetCharType &out) {
15741584
out = (unsigned char)ch;
15751585
return true;
15761586
#else
1577-
LLDB_DEPRECATED_WARNING_DISABLE
1578-
std::codecvt_utf8<wchar_t> cvt;
1579-
LLDB_DEPRECATED_WARNING_RESTORE
15801587
llvm::SmallString<4> input;
15811588
for (;;) {
1582-
const char *from_next;
1583-
wchar_t *to_next;
1584-
std::mbstate_t state = std::mbstate_t();
15851589
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:
1590+
const char *cur_ptr = input.begin();
1591+
const char *end_ptr = input.end();
1592+
llvm::UTF32 code_point = 0;
1593+
llvm::ConversionResult cr = llvm::convertUTF8Sequence(
1594+
(const llvm::UTF8 **)&cur_ptr, (const llvm::UTF8 *)end_ptr, &code_point,
1595+
llvm::lenientConversion);
1596+
switch (cr) {
1597+
case llvm::conversionOK:
1598+
out = code_point;
15891599
return out != (EditLineGetCharType)WEOF;
1590-
1591-
case std::codecvt_base::error:
1592-
case std::codecvt_base::noconv:
1600+
case llvm::targetExhausted:
1601+
case llvm::sourceIllegal:
15931602
return false;
1594-
1595-
case std::codecvt_base::partial:
1603+
case llvm::sourceExhausted:
15961604
lldb::ConnectionStatus status;
15971605
size_t read_count = m_input_connection.Read(
15981606
&ch, 1, std::chrono::seconds(0), status, nullptr);

0 commit comments

Comments
 (0)