[Lldb-commits] [PATCH] D107704: [LLDB][NFC] Simplify IOHandler's getLine to avoid strange casts and redundant checks

Alf via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Aug 9 10:44:28 PDT 2021


gAlfonso-bit updated this revision to Diff 365220.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D107704/new/

https://reviews.llvm.org/D107704

Files:
  lldb/source/Core/IOHandler.cpp


Index: lldb/source/Core/IOHandler.cpp
===================================================================
--- lldb/source/Core/IOHandler.cpp
+++ lldb/source/Core/IOHandler.cpp
@@ -362,17 +362,19 @@
     if (prompt == nullptr)
       prompt = GetPrompt();
 
-    if (prompt && prompt[0]) {
-      if (m_output_sp) {
-        m_output_sp->Printf("%s", prompt);
-        m_output_sp->Flush();
-      }
+    if (prompt && prompt[0] && m_output_sp) {
+      m_output_sp->Printf("%s", prompt);
+      m_output_sp->Flush();
     }
   }
 
   Optional<std::string> got_line = SplitLine(m_line_buffer);
 
-  if (!got_line && !m_input_sp) {
+  if (got_line) {
+    goto gotLine;
+  }
+
+  if (!m_input_sp) {
     // No more input file, we are done...
     SetIsDone(true);
     return false;
@@ -381,24 +383,8 @@
   FILE *in = GetInputFILE();
   char buffer[256];
 
-  if (!got_line && !in && m_input_sp) {
-    // there is no FILE*, fall back on just reading bytes from the stream.
-    while (!got_line) {
-      size_t bytes_read = sizeof(buffer);
-      Status error = m_input_sp->Read((void *)buffer, bytes_read);
-      if (error.Success() && !bytes_read) {
-        got_line = SplitLineEOF(m_line_buffer);
-        break;
-      }
-      if (error.Fail())
-        break;
-      m_line_buffer += StringRef(buffer, bytes_read);
-      got_line = SplitLine(m_line_buffer);
-    }
-  }
-
-  if (!got_line && in) {
-    while (!got_line) {
+  if (in) {
+    do {
       char *r = fgets(buffer, sizeof(buffer), in);
 #ifdef _WIN32
       // ReadFile on Windows is supposed to set ERROR_OPERATION_ABORTED
@@ -422,16 +408,31 @@
       }
       m_line_buffer += buffer;
       got_line = SplitLine(m_line_buffer);
-    }
+    } while (!got_line);
+  } else {
+    // there is no FILE*, fall back on just reading bytes from the stream.
+    do {
+      size_t bytes_read = sizeof(buffer);
+      Status error = m_input_sp->Read((void *)buffer, bytes_read);
+      if (error.Success() && !bytes_read) {
+        got_line = SplitLineEOF(m_line_buffer);
+        break;
+      }
+      if (error.Fail())
+        return false;
+      m_line_buffer += StringRef(buffer, bytes_read);
+      got_line = SplitLine(m_line_buffer);
+    } while (!got_line);
   }
 
-  if (got_line) {
-    line = got_line.getValue();
-    if (m_data_recorder)
-      m_data_recorder->Record(line, true);
-  }
+  if (!got_line)
+    return false;
 
-  return (bool)got_line;
+gotLine:
+  line = got_line.getValue();
+  if (m_data_recorder)
+    m_data_recorder->Record(line, true);
+  return true;
 }
 
 #if LLDB_ENABLE_LIBEDIT


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107704.365220.patch
Type: text/x-patch
Size: 2588 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210809/a179311a/attachment.bin>


More information about the lldb-commits mailing list