Skip to content

Commit a00c76d

Browse files
committed
[lldb][RPC] Upstream Python scripts
As part of upstreaming LLDB RPC, this commit adds python scripts that are used by LLDB RPC to modify the public lldb header files for use with RPC. https://discourse.llvm.org/t/rfc-upstreaming-lldb-rpc/85804
1 parent b85f37a commit a00c76d

13 files changed

+360
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
# Usage: convert-lldb-header-to-rpc-header.py <path/to/input-header.h> <path/to/output-header.h>
3+
# This scripts takes common LLDB headers (such as lldb-defines.h) and replaces references to LLDB
4+
# with those for RPC. This happens for:
5+
# - namespace definitions
6+
# - namespace usage
7+
# - version string macros
8+
# - ifdef/ifndef lines
9+
10+
import argparse
11+
import os
12+
import re
13+
14+
15+
def main():
16+
parser = argparse.ArgumentParser()
17+
parser.add_argument("input")
18+
parser.add_argument("output")
19+
args = parser.parse_args()
20+
input_path = str(args.input)
21+
output_path = str(args.output)
22+
with open(input_path, "r") as input_file:
23+
lines = input_file.readlines()
24+
25+
with open(output_path, "w") as output_file:
26+
for line in lines:
27+
# NOTE: We do not use lldb-forward.h or lldb-versioning.h in RPC, so remove
28+
# all includes that are found for these files.
29+
if re.match(
30+
r'#include "lldb/lldb-forward|#include "lldb/lldb-versioning', line
31+
):
32+
continue
33+
# For lldb-rpc-defines.h, replace the ifndef LLDB_LLDB_ portion with LLDB_RPC_ as we're not
34+
# using LLDB private definitions in RPC.
35+
elif re.match(r".+LLDB_LLDB_", line):
36+
output_file.write(re.sub(r"LLDB_LLDB_", r"LLDB_RPC_", line))
37+
# Similarly to lldb-rpc-defines.h, replace the ifndef for LLDB_API in SBDefines.h to LLDB_RPC_API_ for the same reason.
38+
elif re.match(r".+LLDB_API_", line):
39+
output_file.write(re.sub(r"LLDB_API_", r"LLDB_RPC_API_", line))
40+
# Replace the references for the macros that define the versioning strings in
41+
# lldb-rpc-defines.h.
42+
elif re.match(r".+LLDB_VERSION", line):
43+
output_file.write(re.sub(r"LLDB_VERSION", r"LLDB_RPC_VERSION", line))
44+
elif re.match(r".+LLDB_REVISION", line):
45+
output_file.write(re.sub(r"LLDB_REVISION", r"LLDB_RPC_REVISION", line))
46+
elif re.match(r".+LLDB_VERSION_STRING", line):
47+
output_file.write(
48+
re.sub(r"LLDB_VERSION_STRING", r"LLDB_RPC_VERSION_STRING", line)
49+
)
50+
# For local #includes
51+
elif re.match(r'#include "lldb/lldb-', line):
52+
output_file.write(re.sub(r"lldb/lldb-", r"lldb-rpc-", line))
53+
# Rename the lldb namespace definition to lldb-rpc.
54+
elif re.match(r".*namespace lldb", line):
55+
output_file.write(re.sub(r"lldb", r"lldb_rpc", line))
56+
# Rename namespace references
57+
elif re.match(r".+lldb::", line):
58+
output_file.write(re.sub(r"lldb::", r"lldb_rpc::", line))
59+
else:
60+
# Write any line that doesn't need to be converted
61+
output_file.write(line)
62+
63+
64+
if __name__ == "__main__":
65+
main()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# Usage: framework-header-include-fix.py <path/to/input-header.h> <path/to/output-header.h>
3+
# This script modifies all #include lines in all lldb-rpc headers
4+
# from either filesystem or local includes to liblldbrpc includes.
5+
6+
import argparse
7+
import os
8+
import re
9+
10+
11+
def main():
12+
parser = argparse.ArgumentParser()
13+
parser.add_argument("input")
14+
parser.add_argument("output")
15+
args = parser.parse_args()
16+
input_path = str(args.input)
17+
output_path = str(args.output)
18+
with open(input_path, "r") as input_file:
19+
lines = input_file.readlines()
20+
21+
with open(output_path, "w") as output_file:
22+
for line in lines:
23+
# Replace includes from RPCCommon to liblldbrpc includes.
24+
# e.g. #include <lldb-rpc/common/RPCArgument.h> -> #include <LLDBRPC/RPCArgument.h>
25+
if re.match(r".+<lldb-rpc/common", line):
26+
output_file.write(re.sub(r"<lldb-rpc/common", r"<LLDBRPC", line))
27+
# Replace all local file includes to liblldbrpc includes.
28+
# e.g. #include "SBFoo.h" -> #include <LLDBRPC/SBFoo.h>
29+
elif include_filename := re.match(r'#include "(.*)"', line):
30+
output_file.write(
31+
re.sub(
32+
r'#include "(.*)"',
33+
r"#include <LLDBRPC/" + include_filename.group(1) + ">",
34+
line,
35+
)
36+
)
37+
else:
38+
# Write any line that doesn't need to be converted
39+
output_file.write(line)
40+
41+
42+
if __name__ == "__main__":
43+
main()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
# Usage: framework-header-version-fix.py <path/to/input-header.h> <path/to/output-header.h> MAJOR MINOR PATCH
3+
# This script modifies lldb-rpc-defines.h to uncomment the macro defines used for the LLDB
4+
# major, minor and patch values as well as populating their definitions.
5+
6+
import argparse
7+
import os
8+
import re
9+
10+
11+
def main():
12+
parser = argparse.ArgumentParser()
13+
parser.add_argument("input")
14+
parser.add_argument("output")
15+
parser.add_argument("lldb_version_major")
16+
parser.add_argument("lldb_version_minor")
17+
parser.add_argument("lldb_version_patch")
18+
args = parser.parse_args()
19+
input_path = str(args.input)
20+
output_path = str(args.output)
21+
lldb_version_major = args.lldb_version_major
22+
lldb_version_minor = args.lldb_version_minor
23+
lldb_version_patch = args.lldb_version_patch
24+
25+
with open(input_path, "r") as input_file:
26+
lines = input_file.readlines()
27+
28+
with open(output_path, "w") as output_file:
29+
for line in lines:
30+
# Uncomment the line that defines the LLDB major version and populate its value.
31+
if re.match(r"//#define LLDB_RPC_VERSION$", line):
32+
output_file.write(
33+
re.sub(
34+
r"//#define LLDB_RPC_VERSION",
35+
r"#define LLDB_RPC_VERSION " + lldb_version_major,
36+
line,
37+
)
38+
)
39+
# Uncomment the line that defines the LLDB minor version and populate its value.
40+
elif re.match(r"//#define LLDB_RPC_REVISION$", line):
41+
output_file.write(
42+
re.sub(
43+
r"//#define LLDB_RPC_REVISION",
44+
r"#define LLDB_RPC_REVISION " + lldb_version_minor,
45+
line,
46+
)
47+
)
48+
# Uncomment the line that defines the complete LLDB version string and populate its value.
49+
elif re.match(r"//#define LLDB_RPC_VERSION_STRING$", line):
50+
output_file.write(
51+
re.sub(
52+
r"//#define LLDB_RPC_VERSION_STRING",
53+
r'#define LLDB_RPC_VERSION_STRING "{0}.{1}.{2}"'.format(
54+
lldb_version_major, lldb_version_minor, lldb_version_patch
55+
),
56+
line,
57+
)
58+
)
59+
else:
60+
# Write any line that doesn't need to be converted
61+
output_file.write(line)
62+
63+
64+
if __name__ == "__main__":
65+
main()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copy lldb-defines.h from source.
2+
# RUN: mkdir -p %t/input
3+
# RUN: mkdir -p %t/output
4+
# RUN: cp %p/../../../../../include/lldb/lldb-defines.h %t/input
5+
6+
// Run the convert script on it.
7+
# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-defines.h %t/output/lldb-rpc-defines.h
8+
9+
// Check the output
10+
# RUN: cat %t/output/lldb-rpc-defines.h | FileCheck %s
11+
12+
// The include guards must change from LLDB_LLDB_DEFINES_H to LLDB_RPC_DEFINES_H.
13+
# CHECK: #ifndef LLDB_RPC_DEFINES_H
14+
# CHECK: #define LLDB_RPC_DEFINES_H
15+
16+
// Includes of other lldb headers must begin with "lldb-rpc-".
17+
# CHECK: #include "lldb-rpc-types.h"
18+
19+
// The comment that closes the include guard should match the guard.
20+
# CHECK: #endif // LLDB_RPC_DEFINES_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copy lldb-enumerations.h from source.
2+
# RUN: mkdir -p %t/input
3+
# RUN: mkdir -p %t/output
4+
# RUN: cp %p/../../../../../include/lldb/lldb-enumerations.h %t/input
5+
6+
// Run the convert script on it.
7+
# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-enumerations.h %t/output/lldb-rpc-enumerations.h
8+
9+
// Check the output
10+
# RUN: cat %t/output/lldb-rpc-enumerations.h | FileCheck %s
11+
12+
// The include guards must change from LLDB_LLDB_ENUMERATIONS_H to LLDB_RPC_ENUMERATIONS_H.
13+
# CHECK: #ifndef LLDB_RPC_ENUMERATIONS_H
14+
# CHECK: #define LLDB_RPC_ENUMERATIONS_H
15+
16+
// Change the namespace to lldb_rpc.
17+
# CHECK: namespace lldb_rpc
18+
19+
// The comment that closes the namespace should match the namespace.
20+
# CHECK: // namespace lldb_rpc
21+
22+
// The comment that closes the include guard should match the guard.
23+
# CHECK: #endif // LLDB_RPC_ENUMERATIONS_H
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copy lldb-types.h from source.
2+
# RUN: mkdir -p %t/input
3+
# RUN: mkdir -p %t/output
4+
# RUN: cp %p/../../../../../include/lldb/lldb-types.h %t/input
5+
6+
// Run the convert script on it.
7+
# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-types.h %t/output/lldb-rpc-types.h
8+
9+
// Check the output
10+
# RUN: cat %t/output/lldb-rpc-types.h | FileCheck %s
11+
12+
// The include guards must change from LLDB_LLDB_ENUMERATIONS_H to LLDB_RPC_ENUMERATIONS_H.
13+
# CHECK: #ifndef LLDB_RPC_TYPES_H
14+
# CHECK: #define LLDB_RPC_TYPES_H
15+
16+
// Includes of other lldb headers must begin with "lldb-rpc-".
17+
# CHECK: #include "lldb-rpc-enumerations.h"
18+
19+
// Change the namespace to lldb_rpc.
20+
# CHECK: namespace lldb_rpc
21+
22+
// The comment that closes the namespace should match the namespace.
23+
# CHECK: // namespace lldb_rpc
24+
25+
// The comment that closes the include guard should match the guard.
26+
# CHECK: #endif // LLDB_RPC_TYPES_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copy SBDefines.h from source.
2+
# RUN: mkdir -p %t/input
3+
# RUN: mkdir -p %t/output
4+
# RUN: cp %p/../../../../../include/lldb/API/SBDefines.h %t/input
5+
6+
// Run the convert script on it.
7+
# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/SBDefines.h %t/output/SBDefines.h
8+
9+
// Check the output
10+
# RUN: cat %t/output/SBDefines.h | FileCheck %s
11+
12+
// The include guards must change from LLDB_LLDB_API_SBDEFINES_H to LLDB_RPC_API_SBDEFINES_H.
13+
# CHECK: #ifndef LLDB_RPC_API_SBDEFINES_H
14+
# CHECK: #define LLDB_RPC_API_SBDEFINES_H
15+
16+
// Includes of other lldb headers must begin with "lldb-rpc-".
17+
# CHECK: #include "lldb-rpc-defines.h"
18+
# CHECK: #include "lldb-rpc-enumerations.h"
19+
# CHECK: #include "lldb-rpc-types.h"
20+
21+
// The comment that closes the include guard should match the guard.
22+
# CHECK: #endif // LLDB_RPC_API_SBDEFINES_H
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copy lldb-rpc-defines.h from source.
2+
# RUN: mkdir -p %t/input
3+
# RUN: mkdir -p %t/output
4+
# RUN: cp %p/../../../../../include/lldb/lldb-defines.h %t/input
5+
6+
// Run the convert script on it, then run the framework include fix on it. The framework include fix script
7+
// expects that all lldb references have been renamed to lldb-rpc in order for it to modify the includes
8+
// to go into the framework.
9+
# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-defines.h %t/output/lldb-rpc-defines.h
10+
# RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %t/output/lldb-rpc-defines.h %t/output/lldb-rpc-defines.h
11+
12+
// Check the output
13+
# RUN: cat %t/output/lldb-rpc-defines.h | FileCheck %s
14+
15+
// Local includes for LLDB RPC headers must be changed for the framework.
16+
# CHECK: #include <LLDBRPC/lldb-rpc-types.h>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copy lldb-rpc-types.h from source.
2+
# RUN: mkdir -p %t/input
3+
# RUN: mkdir -p %t/output
4+
# RUN: cp %p/../../../../../include/lldb/lldb-types.h %t/input
5+
6+
// Run the convert script on it, then run the framework include fix on it. The framework include fix script
7+
// expects that all lldb references have been renamed to lldb-rpc in order for it to modify the includes
8+
// to go into the framework.
9+
# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-types.h %t/output/lldb-rpc-types.h
10+
# RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %t/output/lldb-rpc-types.h %t/output/lldb-rpc-types.h
11+
12+
// Check the output
13+
# RUN: cat %t/output/lldb-rpc-types.h | FileCheck %s
14+
15+
// Local includes for LLDB RPC headers must be changed for the framework.
16+
# CHECK: #include <LLDBRPC/lldb-rpc-enumerations.h>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Disabling until the lldb-rpc-gen tool itself lands.
2+
# XFAIL: *
3+
// Generate a dummy SB API file using lldb-rpc-gen.
4+
# RUN: mkdir -p %t/server
5+
# RUN: mkdir -p %t/lib
6+
# RUN: %lldb-rpc-gen --output-dir=%t %S/Inputs/SBRPC-FrameworkFix.h
7+
8+
# RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %t/lib/SBRPC-FrameworkFix.h %t/lib/SBRPC-FrameworkFix.h
9+
10+
// Check the output
11+
# RUN: cat %t/lib/SBRPC-FrameworkFix.h | FileCheck %s
12+
13+
# CHECK: #include <LLDBRPC/RPCPublic.h>
14+
# CHECK: #include <LLDBRPC/SBDefines.h>
15+
# CHECK: #include <LLDBRPC/LLDBRPC.h>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copy SBDefines.h from source.
2+
# RUN: mkdir -p %t/input
3+
# RUN: mkdir -p %t/output
4+
# RUN: cp %p/../../../../../include/lldb/API/SBDefines.h %t/input
5+
6+
// Run the convert script on it, then run the framework include fix on it. The framework include fix script
7+
// expects that all lldb references have been renamed to lldb-rpc in order for it to modify the includes
8+
// to go into the framework.
9+
# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/SBDefines.h %t/output/SBDefines.h
10+
# RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %t/output/SBDefines.h %t/output/SBDefines.h
11+
12+
// Check the output
13+
# RUN: cat %t/output/SBDefines.h | FileCheck %s
14+
15+
// Local includes for LLDB RPC headers must be changed for the framework.
16+
# CHECK: #include <LLDBRPC/lldb-rpc-defines.h>
17+
# CHECK: #include <LLDBRPC/lldb-rpc-enumerations.h>
18+
# CHECK: #include <LLDBRPC/lldb-rpc-types.h>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef LLDB_API_SBRPC_FRAMEWORKINCLUDEFIX_H
2+
#define LLDB_API_SBRPC_FRAMEWORKINCLUDEFIX_H
3+
4+
// The includes for local SB API files and RPC common files
5+
// must be changed to become framework includes.
6+
// They're commented out so that the tool doesn't actually
7+
// try and locate these files.
8+
9+
// #include <lldb-rpc/common/RPCPublic.h>
10+
// #include "SBDefines.h"
11+
// #include "LLDBRPC.h"
12+
13+
#endif // LLDB_API_SBRPC_FRAMEWORKINCLUDEFIX_H
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copy lldb-rpc-defines.h from source.
2+
# RUN: mkdir -p %t/input
3+
# RUN: mkdir -p %t/output
4+
# RUN: cp %p/../../../../../include/lldb/lldb-defines.h %t/input
5+
6+
// Run the convert script on it, then run the framework include fix on it. The framework version fix script
7+
// expects that all lldb references have been renamed to lldb-rpc in order for it to modify the includes
8+
// to go into the framework.
9+
# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-defines.h %t/output/lldb-rpc-defines.h
10+
# RUN: %python %p/../../../../../scripts/framework-header-version-fix.py %t/output/lldb-rpc-defines.h %t/output/lldb-rpc-defines.h 17 0 0
11+
12+
// Check the output
13+
# RUN: cat %t/output/lldb-rpc-defines.h | FileCheck %s
14+
15+
// The LLDB version defines must be uncommented and filled in with the values passed into the script.
16+
# CHECK: {{^}}#define LLDB_RPC_VERSION 17
17+
# CHECK: {{^}}#define LLDB_RPC_REVISION 0
18+
# CHECK: {{^}}#define LLDB_RPC_VERSION_STRING "17.0.0"

0 commit comments

Comments
 (0)