nealsid created this revision. nealsid requested review of this revision. Herald added a project: LLDB. Herald added a subscriber: lldb-commits.
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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D101250 Files: lldb/include/lldb/Host/Editline.h lldb/source/Host/common/Editline.cpp
Index: lldb/source/Host/common/Editline.cpp =================================================================== --- lldb/source/Host/common/Editline.cpp +++ lldb/source/Host/common/Editline.cpp @@ -1102,6 +1102,21 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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) { Index: lldb/include/lldb/Host/Editline.h =================================================================== --- lldb/include/lldb/Host/Editline.h +++ lldb/include/lldb/Host/Editline.h @@ -346,6 +346,16 @@ 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
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits