Skip to content

[lldb] Use LLVM's helper for Unicode conversion (NFC) #112582

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

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions lldb/include/lldb/Host/Editline.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@

#include "lldb/Host/Config.h"

#if LLDB_EDITLINE_USE_WCHAR
#include <codecvt>
#endif
#include <locale>
#include <sstream>
#include <vector>
Expand All @@ -57,23 +54,6 @@

#include "llvm/ADT/FunctionExtras.h"

#if defined(__clang__) && defined(__has_warning)
#if __has_warning("-Wdeprecated-declarations")
#define LLDB_DEPRECATED_WARNING_DISABLE \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
#define LLDB_DEPRECATED_WARNING_RESTORE _Pragma("clang diagnostic pop")
#endif
#elif defined(__GNUC__) && __GNUC__ > 6
#define LLDB_DEPRECATED_WARNING_DISABLE \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
#define LLDB_DEPRECATED_WARNING_RESTORE _Pragma("GCC diagnostic pop")
#else
#define LLDB_DEPRECATED_WARNING_DISABLE
#define LLDB_DEPRECATED_WARNING_RESTORE
#endif

namespace lldb_private {
namespace line_editor {

Expand Down Expand Up @@ -383,11 +363,6 @@ class Editline {
void SetEditLinePromptCallback(EditlinePromptCallbackType callbackFn);
void SetGetCharacterFunction(EditlineGetCharCallbackType callbackFn);

#if LLDB_EDITLINE_USE_WCHAR
LLDB_DEPRECATED_WARNING_DISABLE
std::wstring_convert<std::codecvt_utf8<wchar_t>> m_utf8conv;
LLDB_DEPRECATED_WARNING_RESTORE
#endif
::EditLine *m_editline = nullptr;
EditlineHistorySP m_history_sp;
bool m_in_history = false;
Expand Down
48 changes: 26 additions & 22 deletions lldb/source/Host/common/Editline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
#include <iomanip>
#include <optional>

#include "lldb/Host/Editline.h"

#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/Editline.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Host.h"
#include "lldb/Utility/CompletionRequest.h"
Expand All @@ -23,6 +22,7 @@
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StringList.h"
#include "lldb/Utility/Timeout.h"
#include "llvm/Support/ConvertUTF.h"

#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Locale.h"
Expand Down Expand Up @@ -444,7 +444,9 @@ StringList Editline::GetInputAsStringList(int line_count) {
if (line_count == 0)
break;
#if LLDB_EDITLINE_USE_WCHAR
lines.AppendString(m_utf8conv.to_bytes(line));
std::string buffer;
llvm::convertWideToUTF8(line, buffer);
lines.AppendString(buffer);
#else
lines.AppendString(line);
#endif
Expand Down Expand Up @@ -636,7 +638,9 @@ unsigned char Editline::BreakLineCommand(int ch) {
if (m_fix_indentation_callback) {
StringList lines = GetInputAsStringList(m_current_line_index + 1);
#if LLDB_EDITLINE_USE_WCHAR
lines.AppendString(m_utf8conv.to_bytes(new_line_fragment));
std::string buffer;
llvm::convertWideToUTF8(new_line_fragment, buffer);
lines.AppendString(buffer);
#else
lines.AppendString(new_line_fragment);
#endif
Expand Down Expand Up @@ -684,8 +688,9 @@ unsigned char Editline::EndOrAddLineCommand(int ch) {
m_input_lines.clear();
for (unsigned index = 0; index < lines.GetSize(); index++) {
#if LLDB_EDITLINE_USE_WCHAR
m_input_lines.insert(m_input_lines.end(),
m_utf8conv.from_bytes(lines[index]));
std::wstring wbuffer;
llvm::ConvertUTF8toWide(lines[index], wbuffer);
m_input_lines.insert(m_input_lines.end(), wbuffer);
#else
m_input_lines.insert(m_input_lines.end(), lines[index]);
#endif
Expand Down Expand Up @@ -869,7 +874,9 @@ unsigned char Editline::FixIndentationCommand(int ch) {
currentLine = currentLine.erase(0, -indent_correction);
}
#if LLDB_EDITLINE_USE_WCHAR
m_input_lines[m_current_line_index] = m_utf8conv.from_bytes(currentLine);
std::wstring wbuffer;
llvm::ConvertUTF8toWide(currentLine, wbuffer);
m_input_lines[m_current_line_index] = wbuffer;
#else
m_input_lines[m_current_line_index] = currentLine;
#endif
Expand Down Expand Up @@ -1502,7 +1509,7 @@ bool Editline::GetLine(std::string &line, bool &interrupted) {
} else {
m_history_sp->Enter(input);
#if LLDB_EDITLINE_USE_WCHAR
line = m_utf8conv.to_bytes(SplitLines(input)[0]);
llvm::convertWideToUTF8(SplitLines(input)[0], line);
#else
line = SplitLines(input)[0];
#endif
Expand Down Expand Up @@ -1574,25 +1581,22 @@ bool Editline::CompleteCharacter(char ch, EditLineGetCharType &out) {
out = (unsigned char)ch;
return true;
#else
LLDB_DEPRECATED_WARNING_DISABLE
std::codecvt_utf8<wchar_t> cvt;
LLDB_DEPRECATED_WARNING_RESTORE
llvm::SmallString<4> input;
for (;;) {
const char *from_next;
wchar_t *to_next;
std::mbstate_t state = std::mbstate_t();
input.push_back(ch);
switch (cvt.in(state, input.begin(), input.end(), from_next, &out, &out + 1,
to_next)) {
case std::codecvt_base::ok:
auto *cur_ptr = reinterpret_cast<const llvm::UTF8 *>(input.begin());
auto *end_ptr = reinterpret_cast<const llvm::UTF8 *>(input.end());
llvm::UTF32 code_point = 0;
llvm::ConversionResult cr = llvm::convertUTF8Sequence(
&cur_ptr, end_ptr, &code_point, llvm::lenientConversion);
switch (cr) {
case llvm::conversionOK:
out = code_point;
return out != (EditLineGetCharType)WEOF;

case std::codecvt_base::error:
case std::codecvt_base::noconv:
case llvm::targetExhausted:
case llvm::sourceIllegal:
return false;

case std::codecvt_base::partial:
case llvm::sourceExhausted:
lldb::ConnectionStatus status;
size_t read_count = m_input_connection.Read(
&ch, 1, std::chrono::seconds(0), status, nullptr);
Expand Down
Loading