Don't allow direct access to StreamString's internal buffer.

This is a large API change that removes the two functions from
StreamString that return a std::string& and a const std::string&,
and instead provide one function which returns a StringRef.

Direct access to the underlying buffer violates the concept of
a "stream" which is intended to provide forward only access,
and makes porting to llvm::raw_ostream more difficult in the
future.

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

llvm-svn: 287152
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
index 6019428..19fc78d 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -200,7 +200,9 @@
 
     Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
     ConnectionStatus status = eConnectionStatusSuccess;
-    const char *packet_data = packet.GetData();
+    // TODO: Don't shimmy through a std::string, just use StringRef.
+    std::string packet_str = packet.GetString();
+    const char *packet_data = packet_str.c_str();
     const size_t packet_length = packet.GetSize();
     size_t bytes_written = Write(packet_data, packet_length, status, NULL);
     if (log) {
@@ -236,7 +238,7 @@
           strm.Printf("\\x%2.2x", *p);
         // Print the checksum
         strm.Printf("%*s", (int)3, p);
-        log->PutCString(strm.GetString().c_str());
+        log->PutString(strm.GetString());
       } else
         log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written,
                     (int)packet_length, packet_data);
@@ -867,7 +869,7 @@
           // Packet footer...
           strm.Printf("%c%c%c", m_bytes[total_length - 3],
                       m_bytes[total_length - 2], m_bytes[total_length - 1]);
-          log->PutCString(strm.GetString().c_str());
+          log->PutString(strm.GetString());
         } else {
           if (CompressionIsEnabled())
             log->Printf("<%4" PRIu64 ":%" PRIu64 "> read packet: %.*s",
@@ -1250,7 +1252,7 @@
       Platform *const platform = nullptr;
       launch_info.Dump(string_stream, platform);
       log->Printf("launch info for gdb-remote stub:\n%s",
-                  string_stream.GetString().c_str());
+                  string_stream.GetData());
     }
     error = Host::LaunchProcess(launch_info);
 
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
index 49733a5..1e5067a 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
@@ -349,7 +349,7 @@
   }
 
   StringExtractorGDBRemote response;
-  if (SendPacketAndWaitForResponse(packet.GetData(), response,
+  if (SendPacketAndWaitForResponse(packet.GetString(), response,
                                    /*send_async=*/false) ==
       PacketResult::Success) {
     const char *response_cstr = response.GetStringRef().c_str();
@@ -518,7 +518,7 @@
             GDBR_LOG_PROCESS | GDBR_LOG_PACKETS))
       log->Printf("GDBRemoteCommunicationClient::%s: Didn't get sequence mutex "
                   "for %s packet.",
-                  __FUNCTION__, payload.GetString().c_str());
+                  __FUNCTION__, payload.GetData());
     return PacketResult::ErrorNoSequenceLock;
   }
 
@@ -2685,15 +2685,15 @@
   stream.PutHex32(file_permissions);
   stream.PutChar(',');
   stream.PutCStringAsRawHex8(path.c_str());
-  const char *packet = stream.GetData();
+  llvm::StringRef packet = stream.GetString();
   StringExtractorGDBRemote response;
 
   if (SendPacketAndWaitForResponse(packet, response, false) !=
       PacketResult::Success)
-    return Error("failed to send '%s' packet", packet);
+    return Error("failed to send '%s' packet", packet.str().c_str());
 
   if (response.GetChar() != 'F')
-    return Error("invalid response to '%s' packet", packet);
+    return Error("invalid response to '%s' packet", packet.str().c_str());
 
   return Error(response.GetU32(UINT32_MAX), eErrorTypePOSIX);
 }
@@ -2706,15 +2706,15 @@
   stream.PutHex32(file_permissions);
   stream.PutChar(',');
   stream.PutCStringAsRawHex8(path.c_str());
-  const char *packet = stream.GetData();
+  llvm::StringRef packet = stream.GetString();
   StringExtractorGDBRemote response;
 
   if (SendPacketAndWaitForResponse(packet, response, false) !=
       PacketResult::Success)
-    return Error("failed to send '%s' packet", packet);
+    return Error("failed to send '%s' packet", stream.GetData());
 
   if (response.GetChar() != 'F')
-    return Error("invalid response to '%s' packet", packet);
+    return Error("invalid response to '%s' packet", stream.GetData());
 
   return Error(response.GetU32(UINT32_MAX), eErrorTypePOSIX);
 }
@@ -2800,7 +2800,7 @@
       PacketResult::Success) {
     if (response.GetChar() != 'F') {
       error.SetErrorStringWithFormat("invalid response to '%s' packet",
-                                     stream.GetString().c_str());
+                                     stream.GetData());
     } else {
       const uint32_t mode = response.GetS32(-1);
       if (static_cast<int32_t>(mode) == -1) {
@@ -2818,7 +2818,7 @@
     }
   } else {
     error.SetErrorStringWithFormat("failed to send '%s' packet",
-                                   stream.GetString().c_str());
+                                   stream.GetData());
   }
   return error;
 }
@@ -3258,7 +3258,7 @@
   unescaped_payload.PutCString("jModulesInfo:");
   module_array_sp->Write(unescaped_payload);
   StreamGDBRemote payload;
-  payload.PutEscapedBytes(unescaped_payload.GetData(),
+  payload.PutEscapedBytes(unescaped_payload.GetString().data(),
                           unescaped_payload.GetSize());
 
   StringExtractorGDBRemote response;
@@ -3569,7 +3569,7 @@
       m_supported_async_json_packets_sp->Dump(stream);
       log->Printf("GDBRemoteCommunicationClient::%s(): supported async "
                   "JSON packets: %s",
-                  __FUNCTION__, stream.GetString().c_str());
+                  __FUNCTION__, stream.GetData());
     }
   }
 
@@ -3600,7 +3600,7 @@
     unescaped_stream.Flush();
 
     // Add it to the stream in escaped fashion.
-    stream.PutEscapedBytes(unescaped_stream.GetData(),
+    stream.PutEscapedBytes(unescaped_stream.GetString().data(),
                            unescaped_stream.GetSize());
   }
   stream.Flush();
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
index 28d94f8..e4e6810f6 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
@@ -1174,7 +1174,8 @@
   StreamString response;
   response_array_sp->Write(response);
   StreamGDBRemote escaped_response;
-  escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
+  escaped_response.PutEscapedBytes(response.GetString().data(),
+                                   response.GetSize());
   return SendPacketNoLock(escaped_response.GetString());
 }
 
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
index 33b60b5..8bf48e9 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
@@ -246,7 +246,8 @@
   server_list.Write(response);
 
   StreamGDBRemote escaped_response;
-  escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
+  escaped_response.PutEscapedBytes(response.GetString().data(),
+                                   response.GetSize());
   return SendPacketNoLock(escaped_response.GetString());
 }
 
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index c0fbccb..3761f06 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -1226,7 +1226,7 @@
 
       m_async_broadcaster.BroadcastEvent(
           eBroadcastBitAsyncContinue,
-          new EventDataBytes(packet.GetData(), packet.GetSize()));
+          new EventDataBytes(packet.GetString().data(), packet.GetSize()));
 
     } else
       SetExitStatus(-1, error.AsCString());
@@ -1330,7 +1330,7 @@
         }
 
         if (continue_packet_error)
-          continue_packet.GetString().clear();
+          continue_packet.Clear();
       }
     } else
       continue_packet_error = true;
@@ -1455,7 +1455,7 @@
 
       m_async_broadcaster.BroadcastEvent(
           eBroadcastBitAsyncContinue,
-          new EventDataBytes(continue_packet.GetData(),
+          new EventDataBytes(continue_packet.GetString().data(),
                              continue_packet.GetSize()));
 
       if (listener_sp->WaitForEvent(std::chrono::seconds(5), event_sp) ==
@@ -2812,10 +2812,10 @@
     else
       error.SetErrorStringWithFormat(
           "unexpected response to GDB server memory write packet '%s': '%s'",
-          packet.GetString().c_str(), response.GetStringRef().c_str());
+          packet.GetData(), response.GetStringRef().c_str());
   } else {
     error.SetErrorStringWithFormat("failed to send packet: '%s'",
-                                   packet.GetString().c_str());
+                                   packet.GetData());
   }
   return 0;
 }
@@ -4043,8 +4043,7 @@
     module_spec.Dump(stream);
     log->Printf("ProcessGDBRemote::%s - got module info for (%s:%s) : %s",
                 __FUNCTION__, module_file_spec.GetPath().c_str(),
-                arch.GetTriple().getTriple().c_str(),
-                stream.GetString().c_str());
+                arch.GetTriple().getTriple().c_str(), stream.GetData());
   }
 
   m_cached_module_specs[key] = module_spec;
@@ -4836,7 +4835,7 @@
       json_str.Flush();
       log->Printf("ProcessGDBRemote::%s() "
                   "received Async StructuredData packet: %s",
-                  __FUNCTION__, json_str.GetString().c_str());
+                  __FUNCTION__, json_str.GetData());
     } else {
       log->Printf("ProcessGDBRemote::%s"
                   "() received StructuredData packet:"
@@ -5083,7 +5082,7 @@
           packet.GetString(), response, send_async);
       result.SetStatus(eReturnStatusSuccessFinishResult);
       Stream &output_strm = result.GetOutputStream();
-      output_strm.Printf("  packet: %s\n", packet.GetString().c_str());
+      output_strm.Printf("  packet: %s\n", packet.GetData());
       const std::string &response_str = response.GetStringRef();
 
       if (response_str.empty())