[Lldb-commits] [lldb] r287190 - Convert UriParser to use StringRef.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 16 17:38:02 PST 2016


Author: zturner
Date: Wed Nov 16 19:38:02 2016
New Revision: 287190

URL: http://llvm.org/viewvc/llvm-project?rev=287190&view=rev
Log:
Convert UriParser to use StringRef.

Modified:
    lldb/trunk/include/lldb/Core/UUID.h
    lldb/trunk/source/Core/UUID.cpp
    lldb/trunk/source/Interpreter/OptionValueUUID.cpp
    lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp
    lldb/trunk/source/Plugins/Platform/Android/AdbClient.h
    lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp
    lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp
    lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h
    lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
    lldb/trunk/source/Utility/UriParser.cpp
    lldb/trunk/source/Utility/UriParser.h
    lldb/trunk/unittests/Utility/UriParserTest.cpp

Modified: lldb/trunk/include/lldb/Core/UUID.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/UUID.h?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/UUID.h (original)
+++ lldb/trunk/include/lldb/Core/UUID.h Wed Nov 16 19:38:02 2016
@@ -73,6 +73,7 @@ public:
   //------------------------------------------------------------------
   static llvm::StringRef
   DecodeUUIDBytesFromString(llvm::StringRef str, ValueType &uuid_bytes,
+                            uint32_t &bytes_decoded,
                             uint32_t num_uuid_bytes = 16);
 
 protected:

Modified: lldb/trunk/source/Core/UUID.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/UUID.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Core/UUID.cpp (original)
+++ lldb/trunk/source/Core/UUID.cpp Wed Nov 16 19:38:02 2016
@@ -128,6 +128,7 @@ static inline int xdigit_to_int(char ch)
 
 llvm::StringRef UUID::DecodeUUIDBytesFromString(llvm::StringRef p,
                                                 ValueType &uuid_bytes,
+                                                uint32_t &bytes_decoded,
                                                 uint32_t num_uuid_bytes) {
   ::memset(uuid_bytes, 0, sizeof(uuid_bytes));
   size_t uuid_byte_idx = 0;
@@ -157,6 +158,7 @@ llvm::StringRef UUID::DecodeUUIDBytesFro
   // Clear trailing bytes to 0.
   for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++)
     uuid_bytes[i] = 0;
+  bytes_decoded = uuid_byte_idx;
   return p;
 }
 size_t UUID::SetFromCString(const char *cstr, uint32_t num_uuid_bytes) {
@@ -169,9 +171,9 @@ size_t UUID::SetFromCString(const char *
   // Skip leading whitespace characters
   p = p.ltrim();
 
+  uint32_t bytes_decoded = 0;
   llvm::StringRef rest =
-      UUID::DecodeUUIDBytesFromString(p, m_uuid, num_uuid_bytes);
-  size_t bytes_decoded = p.size() - rest.size();
+      UUID::DecodeUUIDBytesFromString(p, m_uuid, bytes_decoded, num_uuid_bytes);
 
   // If we successfully decoded a UUID, return the amount of characters that
   // were consumed

Modified: lldb/trunk/source/Interpreter/OptionValueUUID.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/OptionValueUUID.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/OptionValueUUID.cpp (original)
+++ lldb/trunk/source/Interpreter/OptionValueUUID.cpp Wed Nov 16 19:38:02 2016
@@ -79,8 +79,9 @@ size_t OptionValueUUID::AutoComplete(Com
     const size_t num_modules = target->GetImages().GetSize();
     if (num_modules > 0) {
       UUID::ValueType uuid_bytes;
-      llvm::StringRef rest = UUID::DecodeUUIDBytesFromString(s, uuid_bytes);
-      const size_t num_bytes_decoded = s.size() - rest.size();
+      size_t num_bytes_decoded = 0;
+      llvm::StringRef rest =
+          UUID::DecodeUUIDBytesFromString(s, uuid_bytes, num_bytes_decoded);
       for (size_t i = 0; i < num_modules; ++i) {
         ModuleSP module_sp(target->GetImages().GetModuleAtIndex(i));
         if (module_sp) {

Modified: lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp Wed Nov 16 19:38:02 2016
@@ -185,7 +185,7 @@ Error AdbClient::SetPortForwarding(const
 }
 
 Error AdbClient::SetPortForwarding(const uint16_t local_port,
-                                   const char *remote_socket_name,
+                                   llvm::StringRef remote_socket_name,
                                    const UnixSocketNamespace socket_namespace) {
   char message[PATH_MAX];
   const char *sock_namespace_str =
@@ -193,7 +193,7 @@ Error AdbClient::SetPortForwarding(const
           ? kSocketNamespaceAbstract
           : kSocketNamespaceFileSystem;
   snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port,
-           sock_namespace_str, remote_socket_name);
+           sock_namespace_str, remote_socket_name.str().c_str());
 
   const auto error = SendDeviceMessage(message);
   if (error.Fail())

Modified: lldb/trunk/source/Plugins/Platform/Android/AdbClient.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/AdbClient.h?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/AdbClient.h (original)
+++ lldb/trunk/source/Plugins/Platform/Android/AdbClient.h Wed Nov 16 19:38:02 2016
@@ -96,7 +96,7 @@ public:
                           const uint16_t remote_port);
 
   Error SetPortForwarding(const uint16_t local_port,
-                          const char *remote_socket_name,
+                          llvm::StringRef remote_socket_name,
                           const UnixSocketNamespace socket_namespace);
 
   Error DeletePortForwarding(const uint16_t local_port);

Modified: lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp Wed Nov 16 19:38:02 2016
@@ -168,7 +168,7 @@ Error PlatformAndroid::ConnectRemote(Arg
     m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
 
   int port;
-  std::string scheme, host, path;
+  llvm::StringRef scheme, host, path;
   const char *url = args.GetArgumentAtIndex(0);
   if (!url)
     return Error("URL is null.");

Modified: lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp Wed Nov 16 19:38:02 2016
@@ -27,7 +27,7 @@ static const lldb::pid_t g_remote_platfo
 
 static Error ForwardPortWithAdb(
     const uint16_t local_port, const uint16_t remote_port,
-    const char *remote_socket_name,
+    llvm::StringRef remote_socket_name,
     const llvm::Optional<AdbClient::UnixSocketNamespace> &socket_namespace,
     std::string &device_id) {
   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
@@ -50,7 +50,7 @@ static Error ForwardPortWithAdb(
 
   if (log)
     log->Printf("Forwarding remote socket \"%s\" to local TCP port %d",
-                remote_socket_name, local_port);
+                remote_socket_name.str().c_str(), local_port);
 
   if (!socket_namespace)
     return Error("Invalid socket namespace");
@@ -114,7 +114,7 @@ Error PlatformAndroidRemoteGDBServer::Co
     return Error("\"platform connect\" takes a single argument: <connect-url>");
 
   int remote_port;
-  std::string scheme, host, path;
+  llvm::StringRef scheme, host, path;
   const char *url = args.GetArgumentAtIndex(0);
   if (!url)
     return Error("URL is null.");
@@ -132,7 +132,7 @@ Error PlatformAndroidRemoteGDBServer::Co
   std::string connect_url;
   auto error =
       MakeConnectURL(g_remote_platform_pid, (remote_port < 0) ? 0 : remote_port,
-                     path.c_str(), connect_url);
+                     path, connect_url);
 
   if (error.Fail())
     return error;
@@ -175,7 +175,7 @@ void PlatformAndroidRemoteGDBServer::Del
 
 Error PlatformAndroidRemoteGDBServer::MakeConnectURL(
     const lldb::pid_t pid, const uint16_t remote_port,
-    const char *remote_socket_name, std::string &connect_url) {
+    llvm::StringRef remote_socket_name, std::string &connect_url) {
   static const int kAttempsNum = 5;
 
   Error error;
@@ -214,7 +214,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDB
   static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL;
 
   int remote_port;
-  std::string scheme, host, path;
+  llvm::StringRef scheme, host, path;
   if (!UriParser::Parse(connect_url, scheme, host, remote_port, path)) {
     error.SetErrorStringWithFormat("Invalid URL: %s", connect_url);
     return nullptr;
@@ -222,7 +222,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDB
 
   std::string new_connect_url;
   error = MakeConnectURL(s_remote_gdbserver_fake_pid--,
-                         (remote_port < 0) ? 0 : remote_port, path.c_str(),
+                         (remote_port < 0) ? 0 : remote_port, path,
                          new_connect_url);
   if (error.Fail())
     return nullptr;

Modified: lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h (original)
+++ lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h Wed Nov 16 19:38:02 2016
@@ -55,7 +55,7 @@ protected:
   void DeleteForwardPort(lldb::pid_t pid);
 
   Error MakeConnectURL(const lldb::pid_t pid, const uint16_t remote_port,
-                       const char *remote_socket_name,
+                       llvm::StringRef remote_socket_name,
                        std::string &connect_url);
 
 private:

Modified: lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp Wed Nov 16 19:38:02 2016
@@ -309,9 +309,12 @@ Error PlatformRemoteGDBServer::ConnectRe
       const char *url = args.GetArgumentAtIndex(0);
       if (!url)
         return Error("URL is null.");
-      if (!UriParser::Parse(url, m_platform_scheme, m_platform_hostname, port,
-                            path))
+      llvm::StringRef scheme, hostname, pathname;
+      if (!UriParser::Parse(url, scheme, hostname, port, pathname))
         return Error("Invalid URL: %s", url);
+      m_platform_scheme = scheme;
+      m_platform_hostname = hostname;
+      path = pathname;
 
       const ConnectionStatus status = m_gdb_client.Connect(url, &error);
       if (status == eConnectionStatusSuccess) {

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp Wed Nov 16 19:38:02 2016
@@ -124,10 +124,10 @@ Error GDBRemoteCommunicationServerPlatfo
                 this, std::placeholders::_1),
       false);
 
-  std::string platform_scheme;
-  std::string platform_ip;
+  llvm::StringRef platform_scheme;
+  llvm::StringRef platform_ip;
   int platform_port;
-  std::string platform_path;
+  llvm::StringRef platform_path;
   bool ok = UriParser::Parse(GetConnection()->GetURI(), platform_scheme,
                              platform_ip, platform_port, platform_path);
   UNUSED_IF_ASSERT_DISABLED(ok);
@@ -140,7 +140,7 @@ Error GDBRemoteCommunicationServerPlatfo
 #endif
   uint16_t *port_ptr = &port;
   if (m_socket_protocol == Socket::ProtocolTcp)
-    url << platform_ip << ":" << port;
+    url << platform_ip.str() << ":" << port;
   else {
     socket_name = GetDomainSocketPath("gdbserver").GetPath();
     url << socket_name;

Modified: lldb/trunk/source/Utility/UriParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/UriParser.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Utility/UriParser.cpp (original)
+++ lldb/trunk/source/Utility/UriParser.cpp Wed Nov 16 19:38:02 2016
@@ -23,18 +23,19 @@ using namespace lldb_private;
 //----------------------------------------------------------------------
 // UriParser::Parse
 //----------------------------------------------------------------------
-bool UriParser::Parse(const std::string &uri, std::string &scheme,
-                      std::string &hostname, int &port, std::string &path) {
-  std::string tmp_scheme, tmp_hostname, tmp_port, tmp_path;
+bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
+                      llvm::StringRef &hostname, int &port,
+                      llvm::StringRef &path) {
+  llvm::StringRef tmp_scheme, tmp_hostname, tmp_port, tmp_path;
 
-  static const char *kSchemeSep = "://";
+  const llvm::StringRef kSchemeSep("://");
   auto pos = uri.find(kSchemeSep);
   if (pos == std::string::npos)
     return false;
 
   // Extract path.
   tmp_scheme = uri.substr(0, pos);
-  auto host_pos = pos + strlen(kSchemeSep);
+  auto host_pos = pos + kSchemeSep.size();
   auto path_pos = uri.find('/', host_pos);
   if (path_pos != std::string::npos)
     tmp_path = uri.substr(path_pos);
@@ -53,28 +54,19 @@ bool UriParser::Parse(const std::string
       return false;
 
     tmp_hostname = host_port.substr(1, pos - 1);
-    host_port.erase(0, pos + 1);
+    host_port = host_port.drop_front(pos + 1);
+    if (!host_port.empty() && !host_port.consume_front(":"))
+      return false;
   } else {
-    pos = host_port.find(':');
-    tmp_hostname = host_port.substr(
-        0, (pos != std::string::npos) ? pos : host_port.size());
-    host_port.erase(0, (pos != std::string::npos) ? pos : host_port.size());
+    std::tie(tmp_hostname, host_port) = host_port.split(':');
   }
 
   // Extract port
-  tmp_port = host_port;
-  if (!tmp_port.empty()) {
-    if (tmp_port[0] != ':')
-      return false;
-    tmp_port = tmp_port.substr(1);
-    bool success = false;
-    auto port_tmp =
-        StringConvert::ToUInt32(tmp_port.c_str(), UINT32_MAX, 10, &success);
-    if (!success || port_tmp > 65535) {
-      // there are invalid characters in port_buf
+  if (!host_port.empty()) {
+    uint16_t port_value = 0;
+    if (host_port.getAsInteger(0, port_value))
       return false;
-    }
-    port = port_tmp;
+    port = port_value;
   } else
     port = -1;
 

Modified: lldb/trunk/source/Utility/UriParser.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/UriParser.h?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/source/Utility/UriParser.h (original)
+++ lldb/trunk/source/Utility/UriParser.h Wed Nov 16 19:38:02 2016
@@ -12,9 +12,10 @@
 
 // C Includes
 // C++ Includes
-#include <string>
 
 // Other libraries and framework includes
+#include "llvm/ADT/StringRef.h"
+
 // Project includes
 
 class UriParser {
@@ -27,8 +28,9 @@ public:
   //
   //   if the url is invalid, function returns false and
   //   output parameters remain unchanged
-  static bool Parse(const std::string &uri, std::string &scheme,
-                    std::string &hostname, int &port, std::string &path);
+  static bool Parse(llvm::StringRef uri, llvm::StringRef &scheme,
+                    llvm::StringRef &hostname, int &port,
+                    llvm::StringRef &path);
 };
 
 #endif // utility_UriParser_h_

Modified: lldb/trunk/unittests/Utility/UriParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/UriParserTest.cpp?rev=287190&r1=287189&r2=287190&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/UriParserTest.cpp (original)
+++ lldb/trunk/unittests/Utility/UriParserTest.cpp Wed Nov 16 19:38:02 2016
@@ -30,16 +30,16 @@ public:
 };
 
 #define VALIDATE                                                               \
-  std::string scheme(kAsdf);                                                   \
-  std::string hostname(kAsdf);                                                 \
+  llvm::StringRef scheme(kAsdf);                                               \
+  llvm::StringRef hostname(kAsdf);                                             \
   int port(1138);                                                              \
-  std::string path(kAsdf);                                                     \
+  llvm::StringRef path(kAsdf);                                                 \
   EXPECT_EQ(testCase.m_result,                                                 \
             UriParser::Parse(testCase.m_uri, scheme, hostname, port, path));   \
-  EXPECT_STREQ(testCase.m_scheme, scheme.c_str());                             \
-  EXPECT_STREQ(testCase.m_hostname, hostname.c_str());                         \
+  EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());                       \
+  EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());                   \
   EXPECT_EQ(testCase.m_port, port);                                            \
-  EXPECT_STREQ(testCase.m_path, path.c_str());
+  EXPECT_STREQ(testCase.m_path, path.str().c_str());
 
 TEST_F(UriParserTest, Minimal) {
   const UriTestCase testCase("x://y", "x", "y", -1, "/");
@@ -48,7 +48,17 @@ TEST_F(UriParserTest, Minimal) {
 
 TEST_F(UriParserTest, MinimalPort) {
   const UriTestCase testCase("x://y:1", "x", "y", 1, "/");
-  VALIDATE
+  llvm::StringRef scheme(kAsdf);
+  llvm::StringRef hostname(kAsdf);
+  int port(1138);
+  llvm::StringRef path(kAsdf);
+  bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
+  EXPECT_EQ(testCase.m_result, result);
+
+  EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
+  EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
+  EXPECT_EQ(testCase.m_port, port);
+  EXPECT_STREQ(testCase.m_path, path.str().c_str());
 }
 
 TEST_F(UriParserTest, MinimalPath) {
@@ -69,13 +79,23 @@ TEST_F(UriParserTest, LongPath) {
 TEST_F(UriParserTest, TypicalPortPath) {
   const UriTestCase testCase("connect://192.168.100.132:5432/", "connect",
                              "192.168.100.132", 5432, "/");
-  VALIDATE
+  VALIDATE;
 }
 
 TEST_F(UriParserTest, BracketedHostnamePort) {
   const UriTestCase testCase("connect://[192.168.100.132]:5432/", "connect",
                              "192.168.100.132", 5432, "/");
-  VALIDATE
+  llvm::StringRef scheme(kAsdf);
+  llvm::StringRef hostname(kAsdf);
+  int port(1138);
+  llvm::StringRef path(kAsdf);
+  bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
+  EXPECT_EQ(testCase.m_result, result);
+
+  EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
+  EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
+  EXPECT_EQ(testCase.m_port, port);
+  EXPECT_STREQ(testCase.m_path, path.str().c_str());
 }
 
 TEST_F(UriParserTest, BracketedHostname) {




More information about the lldb-commits mailing list