[Lldb-commits] [lldb] 2f9cd63 - [LLDB] Fix IOHandlerEditline::GetCurrentLines()

walter erquinigo via lldb-commits lldb-commits at lists.llvm.org
Fri Sep 1 17:52:05 PDT 2023


Author: walter erquinigo
Date: 2023-09-01T20:52:00-04:00
New Revision: 2f9cd6377f3d972ed8cf814225a02a353d6cc545

URL: https://github.com/llvm/llvm-project/commit/2f9cd6377f3d972ed8cf814225a02a353d6cc545
DIFF: https://github.com/llvm/llvm-project/commit/2f9cd6377f3d972ed8cf814225a02a353d6cc545.diff

LOG: [LLDB] Fix IOHandlerEditline::GetCurrentLines()

This method was working as expected if LLDB_ENABLE_LIBEDIT is false, however, if it was true, then the variable m_current_lines_ptr was always pointing to an empty list, because Editline only updates its contents once the full input has been completed (see https://github.com/llvm/llvm-project/blob/main/lldb/source/Host/common/Editline.cpp#L1576).

A simple fix is to invoke Editline::GetInputAsStringList() from GetCurrentLines(), which is already used in many places as the common way to get the full input list.

Added: 
    

Modified: 
    lldb/include/lldb/Core/IOHandler.h
    lldb/include/lldb/Host/Editline.h
    lldb/source/Core/IOHandler.cpp
    lldb/source/Expression/REPL.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/IOHandler.h b/lldb/include/lldb/Core/IOHandler.h
index 0eef77e81ccb83..bb0fb58175a6bc 100644
--- a/lldb/include/lldb/Core/IOHandler.h
+++ b/lldb/include/lldb/Core/IOHandler.h
@@ -403,7 +403,7 @@ class IOHandlerEditline : public IOHandler {
 
   void SetInterruptExits(bool b) { m_interrupt_exits = b; }
 
-  const StringList *GetCurrentLines() const { return m_current_lines_ptr; }
+  StringList GetCurrentLines() const;
 
   uint32_t GetCurrentLineIndex() const;
 

diff  --git a/lldb/include/lldb/Host/Editline.h b/lldb/include/lldb/Host/Editline.h
index 4e2a26e04fa672..35fd179abb509c 100644
--- a/lldb/include/lldb/Host/Editline.h
+++ b/lldb/include/lldb/Host/Editline.h
@@ -227,6 +227,9 @@ class Editline {
 
   void PrintAsync(Stream *stream, const char *s, size_t len);
 
+  /// Convert the current input lines into a UTF8 StringList
+  StringList GetInputAsStringList(int line_count = UINT32_MAX);
+
 private:
   /// Sets the lowest line number for multi-line editing sessions.  A value of
   /// zero suppresses
@@ -282,9 +285,6 @@ class Editline {
   /// Save the line currently being edited
   void SaveEditedLine();
 
-  /// Convert the current input lines into a UTF8 StringList
-  StringList GetInputAsStringList(int line_count = UINT32_MAX);
-
   /// Replaces the current multi-line session with the next entry from history.
   unsigned char RecallHistory(HistoryOperation op);
 

diff  --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp
index 4591d9985b9616..ac9a9bb2844594 100644
--- a/lldb/source/Core/IOHandler.cpp
+++ b/lldb/source/Core/IOHandler.cpp
@@ -508,6 +508,21 @@ uint32_t IOHandlerEditline::GetCurrentLineIndex() const {
   return m_curr_line_idx;
 }
 
+StringList IOHandlerEditline::GetCurrentLines() const {
+#if LLDB_ENABLE_LIBEDIT
+  if (m_editline_up)
+    return m_editline_up->GetInputAsStringList();
+#endif
+  // When libedit is not used, the current lines can be gotten from
+  // `m_current_lines_ptr`, which is updated whenever a new line is processed.
+  // This doesn't happen when libedit is used, in which case
+  // `m_current_lines_ptr` is only updated when the full input is terminated.
+
+  if (m_current_lines_ptr)
+    return *m_current_lines_ptr;
+  return StringList();
+}
+
 bool IOHandlerEditline::GetLines(StringList &lines, bool &interrupted) {
   m_current_lines_ptr = &lines;
 

diff  --git a/lldb/source/Expression/REPL.cpp b/lldb/source/Expression/REPL.cpp
index 7e48c8f293d3b9..07d5b5b3dd9346 100644
--- a/lldb/source/Expression/REPL.cpp
+++ b/lldb/source/Expression/REPL.cpp
@@ -528,17 +528,15 @@ void REPL::IOHandlerComplete(IOHandler &io_handler,
   current_code.append(m_code.CopyList());
 
   IOHandlerEditline &editline = static_cast<IOHandlerEditline &>(io_handler);
-  const StringList *current_lines = editline.GetCurrentLines();
-  if (current_lines) {
-    const uint32_t current_line_idx = editline.GetCurrentLineIndex();
-
-    if (current_line_idx < current_lines->GetSize()) {
-      for (uint32_t i = 0; i < current_line_idx; ++i) {
-        const char *line_cstr = current_lines->GetStringAtIndex(i);
-        if (line_cstr) {
-          current_code.append("\n");
-          current_code.append(line_cstr);
-        }
+  StringList current_lines = editline.GetCurrentLines();
+  const uint32_t current_line_idx = editline.GetCurrentLineIndex();
+
+  if (current_line_idx < current_lines.GetSize()) {
+    for (uint32_t i = 0; i < current_line_idx; ++i) {
+      const char *line_cstr = current_lines.GetStringAtIndex(i);
+      if (line_cstr) {
+        current_code.append("\n");
+        current_code.append(line_cstr);
       }
     }
   }


        


More information about the lldb-commits mailing list