Author: Charles Zablit Date: 2025-12-04T17:08:59Z New Revision: 64e19916f9518709562ef41e236c1dba6c421782
URL: https://github.com/llvm/llvm-project/commit/64e19916f9518709562ef41e236c1dba6c421782 DIFF: https://github.com/llvm/llvm-project/commit/64e19916f9518709562ef41e236c1dba6c421782.diff LOG: [lldb] improve the heuristics for checking if a terminal supports Unicode (#168603) This patch improves the way lldb checks if the terminal it's opened in (if any) supports Unicode or not. On POSIX systems, we check if `LANG` contains `UTF-8`. On Windows, we always return `true` since we use the `WriteToConsoleW` api. Added: Modified: lldb/include/lldb/Host/Terminal.h lldb/source/Host/common/DiagnosticsRendering.cpp lldb/source/Host/common/Terminal.cpp Removed: ################################################################################ diff --git a/lldb/include/lldb/Host/Terminal.h b/lldb/include/lldb/Host/Terminal.h index da0d05e8bd265..3d66515c18812 100644 --- a/lldb/include/lldb/Host/Terminal.h +++ b/lldb/include/lldb/Host/Terminal.h @@ -68,6 +68,18 @@ class Terminal { llvm::Error SetHardwareFlowControl(bool enabled); + /// Returns whether or not the current terminal supports Unicode rendering. + /// + /// The value is cached after the first computation. + /// + /// On POSIX systems, we check if the LANG environment variable contains the + /// substring "UTF-8", case insensitive. + /// + /// On Windows, we always return true since we use the `WriteConsoleW` API + /// internally. Note that the default Windows codepage (437) does not support + /// all Unicode characters. This function does not check the codepage. + static bool SupportsUnicode(); + protected: struct Data; diff --git a/lldb/source/Host/common/DiagnosticsRendering.cpp b/lldb/source/Host/common/DiagnosticsRendering.cpp index f2cd3968967fb..375d1caa99f52 100644 --- a/lldb/source/Host/common/DiagnosticsRendering.cpp +++ b/lldb/source/Host/common/DiagnosticsRendering.cpp @@ -7,6 +7,8 @@ //===----------------------------------------------------------------------===// #include "lldb/Host/common/DiagnosticsRendering.h" +#include "lldb/Host/Terminal.h" + #include <cstdint> using namespace lldb_private; @@ -97,12 +99,8 @@ void RenderDiagnosticDetails(Stream &stream, return; } - // Since there is no other way to find this out, use the color - // attribute as a proxy for whether the terminal supports Unicode - // characters. In the future it might make sense to move this into - // Host so it can be customized for a specific platform. llvm::StringRef cursor, underline, vbar, joint, hbar, spacer; - if (stream.AsRawOstream().colors_enabled()) { + if (Terminal::SupportsUnicode()) { cursor = "˄"; underline = "˜"; vbar = "│"; diff --git a/lldb/source/Host/common/Terminal.cpp b/lldb/source/Host/common/Terminal.cpp index 436dfd8130d9b..dd1dc75133f45 100644 --- a/lldb/source/Host/common/Terminal.cpp +++ b/lldb/source/Host/common/Terminal.cpp @@ -400,6 +400,21 @@ llvm::Error Terminal::SetHardwareFlowControl(bool enabled) { #endif // LLDB_ENABLE_TERMIOS } +bool Terminal::SupportsUnicode() { + static std::optional<bool> result; + if (result) + return result.value(); +#ifdef _WIN32 + return true; +#else + const char *lang_var = std::getenv("LANG"); + if (!lang_var) + return false; + result = llvm::StringRef(lang_var).lower().find("utf-8") != std::string::npos; +#endif + return result.value(); +} + TerminalState::TerminalState(Terminal term, bool save_process_group) : m_tty(term) { Save(term, save_process_group); _______________________________________________ lldb-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
