[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:39:11 PDT 2021


gAlfonso-bit updated this revision to Diff 365218.

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
@@ -372,7 +372,11 @@
 
   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 +385,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 +410,32 @@
       }
       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) {
+  gotLine:
     line = got_line.getValue();
     if (m_data_recorder)
       m_data_recorder->Record(line, true);
+    return true;
   }
 
-  return (bool)got_line;
+  return false;
 }
 
 #if LLDB_ENABLE_LIBEDIT


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


More information about the lldb-commits mailing list