@@ -79,6 +79,19 @@ using namespace lldb_private::line_editor;
7979
8080#endif // #if LLDB_EDITLINE_USE_WCHAR
8181
82+ template <typename T> class ScopedOptional {
83+ public:
84+ template <typename ... Args>
85+ ScopedOptional (std::optional<T> &optional, Args &&...args)
86+ : m_optional(optional) {
87+ m_optional.emplace (std::forward<Args>(args)...);
88+ }
89+ ~ScopedOptional () { m_optional.reset (); }
90+
91+ private:
92+ std::optional<T> &m_optional;
93+ };
94+
8295bool IsOnlySpaces (const EditLineStringType &content) {
8396 for (wchar_t ch : content) {
8497 if (ch != EditLineCharType (' ' ))
@@ -541,9 +554,10 @@ int Editline::GetCharacter(EditLineGetCharType *c) {
541554 // Paint a ANSI formatted version of the desired prompt over the version
542555 // libedit draws. (will only be requested if colors are supported)
543556 if (m_needs_prompt_repaint) {
544- LockedStreamFile locked_stream = m_output_stream_sp->Lock ();
557+ ScopedOptional<LockedStreamFile> scope (m_locked_output,
558+ m_output_stream_sp->Lock ());
545559 MoveCursor (CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
546- fprintf (locked_stream. GetFile ().GetStream (),
560+ fprintf (m_locked_output-> GetFile ().GetStream (),
547561 " %s"
548562 " %s"
549563 " %s" ,
@@ -581,10 +595,10 @@ int Editline::GetCharacter(EditLineGetCharType *c) {
581595 // indefinitely. This gives a chance for someone to interrupt us. After
582596 // Read returns, immediately lock the mutex again and check if we were
583597 // interrupted.
584- m_output_stream_sp-> GetMutex (). unlock ();
598+ m_locked_output. reset ();
585599 int read_count =
586600 m_input_connection.Read (&ch, 1 , std::nullopt , status, nullptr );
587- m_output_stream_sp->GetMutex (). lock ( );
601+ m_locked_output. emplace ( m_output_stream_sp->Lock () );
588602 if (m_editor_status == EditorStatus::Interrupted) {
589603 while (read_count > 0 && status == lldb::eConnectionStatusSuccess)
590604 read_count =
@@ -1599,7 +1613,8 @@ bool Editline::GetLine(std::string &line, bool &interrupted) {
15991613 m_input_lines = std::vector<EditLineStringType>();
16001614 m_input_lines.insert (m_input_lines.begin (), EditLineConstString (" " ));
16011615
1602- LockedStreamFile locked_stream = m_output_stream_sp->Lock ();
1616+ ScopedOptional<LockedStreamFile> scope (m_locked_output,
1617+ m_output_stream_sp->Lock ());
16031618
16041619 lldbassert (m_editor_status != EditorStatus::Editing);
16051620 if (m_editor_status == EditorStatus::Interrupted) {
@@ -1619,7 +1634,7 @@ bool Editline::GetLine(std::string &line, bool &interrupted) {
16191634 interrupted = m_editor_status == EditorStatus::Interrupted;
16201635 if (!interrupted) {
16211636 if (input == nullptr ) {
1622- fprintf (locked_stream. GetFile ().GetStream (), " \n " );
1637+ fprintf (m_locked_output-> GetFile ().GetStream (), " \n " );
16231638 m_editor_status = EditorStatus::EndOfInput;
16241639 } else {
16251640 m_history_sp->Enter (input);
@@ -1644,7 +1659,8 @@ bool Editline::GetLines(int first_line_number, StringList &lines,
16441659 m_input_lines = std::vector<EditLineStringType>();
16451660 m_input_lines.insert (m_input_lines.begin (), EditLineConstString (" " ));
16461661
1647- LockedStreamFile locked_stream = m_output_stream_sp->Lock ();
1662+ ScopedOptional<LockedStreamFile> scope (m_locked_output,
1663+ m_output_stream_sp->Lock ());
16481664
16491665 // Begin the line editing loop
16501666 DisplayInput ();
0 commit comments