[Lldb-commits] [lldb] fd89af6 - Wrap edit line configuration calls into helper functions

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Fri Apr 30 03:32:54 PDT 2021


Author: Neal (nealsid)
Date: 2021-04-30T12:32:29+02:00
New Revision: fd89af6880f33ead708abe2f7d88ecb687d4e0d2

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

LOG: Wrap edit line configuration calls into helper functions

Currently we call el_set directly to configure the editor in the libedit
wrapper.  There are some cases in which this causes extra casting, but we pass
captureless lambdas as function pointers, which should work out of the box.
Since el_set takes varargs, if the cast is incorrect or if the cast is not
present, it causes a run time failure rather than compile error.  This change
makes it so a few different types of configuration is done inside a helper
function to provide type safety and eliminate that casting.  I didn't do all
edit line configuration because I'm not sure how important it was in other cases
and it might require something more general keep up with libedit's signature.
I'm open to suggestions, though.

Reviewed By: teemperor, JDevlieghere

Differential Revision: https://reviews.llvm.org/D101250

Added: 
    

Modified: 
    lldb/include/lldb/Host/Editline.h
    lldb/source/Host/common/Editline.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Host/Editline.h b/lldb/include/lldb/Host/Editline.h
index 3295c9a2080a6..04da25edf632a 100644
--- a/lldb/include/lldb/Host/Editline.h
+++ b/lldb/include/lldb/Host/Editline.h
@@ -346,6 +346,16 @@ class Editline {
 
   void ApplyTerminalSizeChange();
 
+  // The following set various editline parameters.  It's not any less
+  // verbose to put the editline calls into a function, but it
+  // provides type safety, since the editline functions take varargs
+  // parameters.
+  void AddFunctionToEditLine(const EditLineCharType *command,
+                             const EditLineCharType *helptext,
+                             EditlineCommandCallbackType callbackFn);
+  void SetEditLinePromptCallback(EditlinePromptCallbackType callbackFn);
+  void SetGetCharacterFunction(EditlineGetCharCallbackType callbackFn);
+
 #if LLDB_EDITLINE_USE_WCHAR
   std::wstring_convert<std::codecvt_utf8<wchar_t>> m_utf8conv;
 #endif

diff  --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp
index 85c62b4288a97..2cfcf2ab1432e 100644
--- a/lldb/source/Host/common/Editline.cpp
+++ b/lldb/source/Host/common/Editline.cpp
@@ -1102,6 +1102,21 @@ unsigned char Editline::TypedCharacter(int ch) {
   return CC_REDISPLAY;
 }
 
+void Editline::AddFunctionToEditLine(const EditLineCharType *command,
+                                     const EditLineCharType *helptext,
+                                     EditlineCommandCallbackType callbackFn) {
+  el_wset(m_editline, EL_ADDFN, command, helptext, callbackFn);
+}
+
+void Editline::SetEditLinePromptCallback(
+    EditlinePromptCallbackType callbackFn) {
+  el_set(m_editline, EL_PROMPT, callbackFn);
+}
+
+void Editline::SetGetCharacterFunction(EditlineGetCharCallbackType callbackFn) {
+  el_wset(m_editline, EL_GETCFN, callbackFn);
+}
+
 void Editline::ConfigureEditor(bool multiline) {
   if (m_editline && m_multiline_enabled == multiline)
     return;
@@ -1128,74 +1143,83 @@ void Editline::ConfigureEditor(bool multiline) {
   el_set(m_editline, EL_CLIENTDATA, this);
   el_set(m_editline, EL_SIGNAL, 0);
   el_set(m_editline, EL_EDITOR, "emacs");
-  el_set(m_editline, EL_PROMPT,
-         (EditlinePromptCallbackType)([](EditLine *editline) {
-           return Editline::InstanceFor(editline)->Prompt();
-         }));
 
-  el_wset(m_editline, EL_GETCFN, (EditlineGetCharCallbackType)([](
-                                     EditLine *editline, EditLineGetCharType *c) {
-            return Editline::InstanceFor(editline)->GetCharacter(c);
-          }));
+  SetGetCharacterFunction([](EditLine *editline, EditLineGetCharType *c) {
+    return Editline::InstanceFor(editline)->GetCharacter(c);
+  });
+
+  SetEditLinePromptCallback([](EditLine *editline) {
+    return Editline::InstanceFor(editline)->Prompt();
+  });
 
   // Commands used for multiline support, registered whether or not they're
   // used
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-break-line"),
-          EditLineConstString("Insert a line break"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->BreakLineCommand(ch);
-          }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-end-or-add-line"),
-          EditLineConstString("End editing or continue when incomplete"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->EndOrAddLineCommand(ch);
-          }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-delete-next-char"),
-          EditLineConstString("Delete next character"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->DeleteNextCharCommand(ch);
-          }));
-  el_wset(
-      m_editline, EL_ADDFN, EditLineConstString("lldb-delete-previous-char"),
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-break-line"),
+      EditLineConstString("Insert a line break"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->BreakLineCommand(ch);
+      });
+
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-end-or-add-line"),
+      EditLineConstString("End editing or continue when incomplete"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->EndOrAddLineCommand(ch);
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-delete-next-char"),
+      EditLineConstString("Delete next character"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->DeleteNextCharCommand(ch);
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-delete-previous-char"),
       EditLineConstString("Delete previous character"),
-      (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
+      [](EditLine *editline, int ch) {
         return Editline::InstanceFor(editline)->DeletePreviousCharCommand(ch);
-      }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-previous-line"),
-          EditLineConstString("Move to previous line"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->PreviousLineCommand(ch);
-          }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-next-line"),
-          EditLineConstString("Move to next line"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->NextLineCommand(ch);
-          }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-previous-history"),
-          EditLineConstString("Move to previous history"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->PreviousHistoryCommand(ch);
-          }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-next-history"),
-          EditLineConstString("Move to next history"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->NextHistoryCommand(ch);
-          }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-buffer-start"),
-          EditLineConstString("Move to start of buffer"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->BufferStartCommand(ch);
-          }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-buffer-end"),
-          EditLineConstString("Move to end of buffer"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->BufferEndCommand(ch);
-          }));
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-fix-indentation"),
-          EditLineConstString("Fix line indentation"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->FixIndentationCommand(ch);
-          }));
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-previous-line"),
+      EditLineConstString("Move to previous line"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->PreviousLineCommand(ch);
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-next-line"),
+      EditLineConstString("Move to next line"), [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->NextLineCommand(ch);
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-previous-history"),
+      EditLineConstString("Move to previous history"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->PreviousHistoryCommand(ch);
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-next-history"),
+      EditLineConstString("Move to next history"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->NextHistoryCommand(ch);
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-buffer-start"),
+      EditLineConstString("Move to start of buffer"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->BufferStartCommand(ch);
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-buffer-end"),
+      EditLineConstString("Move to end of buffer"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->BufferEndCommand(ch);
+      });
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-fix-indentation"),
+      EditLineConstString("Fix line indentation"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->FixIndentationCommand(ch);
+      });
 
   // Register the complete callback under two names for compatibility with
   // older clients using custom .editrc files (largely because libedit has a
@@ -1206,10 +1230,12 @@ void Editline::ConfigureEditor(bool multiline) {
                                                      int ch) {
     return Editline::InstanceFor(editline)->TabCommand(ch);
   };
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-complete"),
-          EditLineConstString("Invoke completion"), complete_callback);
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb_complete"),
-          EditLineConstString("Invoke completion"), complete_callback);
+  AddFunctionToEditLine(EditLineConstString("lldb-complete"),
+                        EditLineConstString("Invoke completion"),
+                        complete_callback);
+  AddFunctionToEditLine(EditLineConstString("lldb_complete"),
+                        EditLineConstString("Invoke completion"),
+                        complete_callback);
 
   // General bindings we don't mind being overridden
   if (!multiline) {
@@ -1217,21 +1243,22 @@ void Editline::ConfigureEditor(bool multiline) {
            NULL); // Cycle through backwards search, entering string
 
     if (m_suggestion_callback) {
-      el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-apply-complete"),
-              EditLineConstString("Adopt autocompletion"),
-              (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-                return Editline::InstanceFor(editline)->ApplyAutosuggestCommand(
-                    ch);
-              }));
+      AddFunctionToEditLine(
+          EditLineConstString("lldb-apply-complete"),
+          EditLineConstString("Adopt autocompletion"),
+          [](EditLine *editline, int ch) {
+            return Editline::InstanceFor(editline)->ApplyAutosuggestCommand(ch);
+          });
 
       el_set(m_editline, EL_BIND, "^f", "lldb-apply-complete",
              NULL); // Apply a part that is suggested automatically
 
-      el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-typed-character"),
-              EditLineConstString("Typed character"),
-              (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-                return Editline::InstanceFor(editline)->TypedCharacter(ch);
-              }));
+      AddFunctionToEditLine(
+          EditLineConstString("lldb-typed-character"),
+          EditLineConstString("Typed character"),
+          [](EditLine *editline, int ch) {
+            return Editline::InstanceFor(editline)->TypedCharacter(ch);
+          });
 
       char bind_key[2] = {0, 0};
       llvm::StringRef ascii_chars =
@@ -1266,11 +1293,12 @@ void Editline::ConfigureEditor(bool multiline) {
   el_source(m_editline, nullptr);
 
   // Register an internal binding that external developers shouldn't use
-  el_wset(m_editline, EL_ADDFN, EditLineConstString("lldb-revert-line"),
-          EditLineConstString("Revert line to saved state"),
-          (EditlineCommandCallbackType)([](EditLine *editline, int ch) {
-            return Editline::InstanceFor(editline)->RevertLineCommand(ch);
-          }));
+  AddFunctionToEditLine(
+      EditLineConstString("lldb-revert-line"),
+      EditLineConstString("Revert line to saved state"),
+      [](EditLine *editline, int ch) {
+        return Editline::InstanceFor(editline)->RevertLineCommand(ch);
+      });
 
   // Register keys that perform auto-indent correction
   if (m_fix_indentation_callback && m_fix_indentation_callback_chars) {


        


More information about the lldb-commits mailing list