[lldb] [Utility/UriParser] Replace port==-1 with llvm::None

Use llvm::Optional<uint16_t> instead of int for port number
in UriParser::Parse(), and use llvm::None to indicate missing port
instead of a magic value of -1.

Differential Revision: https://reviews.llvm.org/D112309
This commit is contained in:
Michał Górny
2021-10-22 14:00:07 +02:00
parent 43f8845dd3
commit ff569ed030
11 changed files with 31 additions and 32 deletions

View File

@@ -155,7 +155,7 @@ Status PlatformAndroid::ConnectRemote(Args &args) {
if (!m_remote_platform_sp)
m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
int port;
llvm::Optional<uint16_t> port;
llvm::StringRef scheme, host, path;
const char *url = args.GetArgumentAtIndex(0);
if (!url)

View File

@@ -109,7 +109,7 @@ Status PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) {
return Status(
"\"platform connect\" takes a single argument: <connect-url>");
int remote_port;
llvm::Optional<uint16_t> remote_port;
llvm::StringRef scheme, host, path;
const char *url = args.GetArgumentAtIndex(0);
if (!url)
@@ -126,9 +126,8 @@ Status PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) {
m_socket_namespace = AdbClient::UnixSocketNamespaceAbstract;
std::string connect_url;
auto error =
MakeConnectURL(g_remote_platform_pid, (remote_port < 0) ? 0 : remote_port,
path, connect_url);
auto error = MakeConnectURL(g_remote_platform_pid, remote_port.getValueOr(0),
path, connect_url);
if (error.Fail())
return error;
@@ -207,7 +206,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess(
// any other valid pid on android.
static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL;
int remote_port;
llvm::Optional<uint16_t> remote_port;
llvm::StringRef scheme, host, path;
if (!UriParser::Parse(connect_url, scheme, host, remote_port, path)) {
error.SetErrorStringWithFormat("Invalid URL: %s",
@@ -217,8 +216,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess(
std::string new_connect_url;
error = MakeConnectURL(s_remote_gdbserver_fake_pid--,
(remote_port < 0) ? 0 : remote_port, path,
new_connect_url);
remote_port.getValueOr(0), path, new_connect_url);
if (error.Fail())
return nullptr;

View File

@@ -305,7 +305,7 @@ Status PlatformRemoteGDBServer::ConnectRemote(Args &args) {
if (!url)
return Status("URL is null.");
int port;
llvm::Optional<uint16_t> port;
llvm::StringRef scheme, hostname, pathname;
if (!UriParser::Parse(url, scheme, hostname, port, pathname))
return Status("Invalid URL: %s", url);

View File

@@ -39,7 +39,6 @@
#include "lldb/Utility/State.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/UnimplementedError.h"
#include "lldb/Utility/UriParser.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/ScopedPrinter.h"

View File

@@ -199,7 +199,7 @@ Status GDBRemoteCommunicationServerPlatform::LaunchGDBServer(
if (m_socket_protocol == Socket::ProtocolTcp) {
llvm::StringRef platform_scheme;
llvm::StringRef platform_ip;
int platform_port;
llvm::Optional<uint16_t> platform_port;
llvm::StringRef platform_path;
std::string platform_uri = GetConnection()->GetURI();
bool ok = UriParser::Parse(platform_uri, platform_scheme, platform_ip,

View File

@@ -17,7 +17,7 @@ using namespace lldb_private;
// UriParser::Parse
bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
llvm::StringRef &hostname, int &port,
llvm::StringRef &hostname, llvm::Optional<uint16_t> &port,
llvm::StringRef &path) {
llvm::StringRef tmp_scheme, tmp_hostname, tmp_path;
@@ -61,7 +61,7 @@ bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
return false;
port = port_value;
} else
port = -1;
port = llvm::None;
scheme = tmp_scheme;
hostname = tmp_hostname;