Skip to content

Commit ff569ed

Browse files
committed
[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
1 parent 43f8845 commit ff569ed

File tree

11 files changed

+31
-32
lines changed

11 files changed

+31
-32
lines changed

lldb/include/lldb/Utility/UriParser.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_UTILITY_URIPARSER_H
1010
#define LLDB_UTILITY_URIPARSER_H
1111

12+
#include "llvm/ADT/Optional.h"
1213
#include "llvm/ADT/StringRef.h"
1314

1415
namespace lldb_private {
@@ -18,12 +19,12 @@ class UriParser {
1819
// RETURN VALUE
1920
// if url is valid, function returns true and
2021
// scheme/hostname/port/path are set to the parsed values
21-
// port it set to -1 if it is not included in the URL
22+
// port it set to llvm::None if it is not included in the URL
2223
//
2324
// if the url is invalid, function returns false and
2425
// output parameters remain unchanged
2526
static bool Parse(llvm::StringRef uri, llvm::StringRef &scheme,
26-
llvm::StringRef &hostname, int &port,
27+
llvm::StringRef &hostname, llvm::Optional<uint16_t> &port,
2728
llvm::StringRef &path);
2829
};
2930
}

lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Status PlatformAndroid::ConnectRemote(Args &args) {
155155
if (!m_remote_platform_sp)
156156
m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
157157

158-
int port;
158+
llvm::Optional<uint16_t> port;
159159
llvm::StringRef scheme, host, path;
160160
const char *url = args.GetArgumentAtIndex(0);
161161
if (!url)

lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Status PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) {
109109
return Status(
110110
"\"platform connect\" takes a single argument: <connect-url>");
111111

112-
int remote_port;
112+
llvm::Optional<uint16_t> remote_port;
113113
llvm::StringRef scheme, host, path;
114114
const char *url = args.GetArgumentAtIndex(0);
115115
if (!url)
@@ -126,9 +126,8 @@ Status PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) {
126126
m_socket_namespace = AdbClient::UnixSocketNamespaceAbstract;
127127

128128
std::string connect_url;
129-
auto error =
130-
MakeConnectURL(g_remote_platform_pid, (remote_port < 0) ? 0 : remote_port,
131-
path, connect_url);
129+
auto error = MakeConnectURL(g_remote_platform_pid, remote_port.getValueOr(0),
130+
path, connect_url);
132131

133132
if (error.Fail())
134133
return error;
@@ -207,7 +206,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess(
207206
// any other valid pid on android.
208207
static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL;
209208

210-
int remote_port;
209+
llvm::Optional<uint16_t> remote_port;
211210
llvm::StringRef scheme, host, path;
212211
if (!UriParser::Parse(connect_url, scheme, host, remote_port, path)) {
213212
error.SetErrorStringWithFormat("Invalid URL: %s",
@@ -217,8 +216,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess(
217216

218217
std::string new_connect_url;
219218
error = MakeConnectURL(s_remote_gdbserver_fake_pid--,
220-
(remote_port < 0) ? 0 : remote_port, path,
221-
new_connect_url);
219+
remote_port.getValueOr(0), path, new_connect_url);
222220
if (error.Fail())
223221
return nullptr;
224222

lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Status PlatformRemoteGDBServer::ConnectRemote(Args &args) {
305305
if (!url)
306306
return Status("URL is null.");
307307

308-
int port;
308+
llvm::Optional<uint16_t> port;
309309
llvm::StringRef scheme, hostname, pathname;
310310
if (!UriParser::Parse(url, scheme, hostname, port, pathname))
311311
return Status("Invalid URL: %s", url);

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include "lldb/Utility/State.h"
4040
#include "lldb/Utility/StreamString.h"
4141
#include "lldb/Utility/UnimplementedError.h"
42-
#include "lldb/Utility/UriParser.h"
4342
#include "llvm/ADT/Triple.h"
4443
#include "llvm/Support/JSON.h"
4544
#include "llvm/Support/ScopedPrinter.h"

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Status GDBRemoteCommunicationServerPlatform::LaunchGDBServer(
199199
if (m_socket_protocol == Socket::ProtocolTcp) {
200200
llvm::StringRef platform_scheme;
201201
llvm::StringRef platform_ip;
202-
int platform_port;
202+
llvm::Optional<uint16_t> platform_port;
203203
llvm::StringRef platform_path;
204204
std::string platform_uri = GetConnection()->GetURI();
205205
bool ok = UriParser::Parse(platform_uri, platform_scheme, platform_ip,

lldb/source/Utility/UriParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using namespace lldb_private;
1717

1818
// UriParser::Parse
1919
bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
20-
llvm::StringRef &hostname, int &port,
20+
llvm::StringRef &hostname, llvm::Optional<uint16_t> &port,
2121
llvm::StringRef &path) {
2222
llvm::StringRef tmp_scheme, tmp_hostname, tmp_path;
2323

@@ -61,7 +61,7 @@ bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
6161
return false;
6262
port = port_value;
6363
} else
64-
port = -1;
64+
port = llvm::None;
6565

6666
scheme = tmp_scheme;
6767
hostname = tmp_hostname;

lldb/tools/lldb-server/Acceptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ std::unique_ptr<Acceptor> Acceptor::Create(StringRef name,
8484
error.Clear();
8585

8686
Socket::SocketProtocol socket_protocol = Socket::ProtocolUnixDomain;
87-
int port;
87+
llvm::Optional<uint16_t> port;
8888
StringRef scheme, host, path;
8989
// Try to match socket name as URL - e.g., tcp://localhost:5555
9090
if (UriParser::Parse(name, scheme, host, port, path)) {

lldb/unittests/Host/ConnectionFileDescriptorTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class ConnectionFileDescriptorTest : public testing::Test {
2828

2929
llvm::StringRef scheme;
3030
llvm::StringRef hostname;
31-
int port;
31+
llvm::Optional<uint16_t> port;
3232
llvm::StringRef path;
3333
std::string uri(connection_file_descriptor.GetURI());
3434
EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));
3535
EXPECT_EQ(ip, hostname);
36-
EXPECT_EQ(socket->GetRemotePortNumber(), port);
36+
EXPECT_EQ(socket->GetRemotePortNumber(), port.getValue());
3737
}
3838
};
3939

lldb/unittests/Host/SocketTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ TEST_P(SocketTest, TCPGetConnectURI) {
173173

174174
llvm::StringRef scheme;
175175
llvm::StringRef hostname;
176-
int port;
176+
llvm::Optional<uint16_t> port;
177177
llvm::StringRef path;
178178
std::string uri(socket_a_up->GetRemoteConnectionURI());
179179
EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));
@@ -191,7 +191,7 @@ TEST_P(SocketTest, UDPGetConnectURI) {
191191

192192
llvm::StringRef scheme;
193193
llvm::StringRef hostname;
194-
int port;
194+
llvm::Optional<uint16_t> port;
195195
llvm::StringRef path;
196196
std::string uri = socket.get()->GetRemoteConnectionURI();
197197
EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));
@@ -216,7 +216,7 @@ TEST_P(SocketTest, DomainGetConnectURI) {
216216

217217
llvm::StringRef scheme;
218218
llvm::StringRef hostname;
219-
int port;
219+
llvm::Optional<uint16_t> port;
220220
llvm::StringRef path;
221221
std::string uri(socket_a_up->GetRemoteConnectionURI());
222222
EXPECT_TRUE(UriParser::Parse(uri, scheme, hostname, port, path));

0 commit comments

Comments
 (0)