Skip to content

Commit 01e35b2

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 01e35b2

18 files changed

+368
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Usage: convert-lldb-header-to-rpc-header.py <path/to/input-header.h> <path/to/output-header.h>
4+
5+
This scripts takes common LLDB headers (such as lldb-defines.h) and replaces references to LLDB
6+
with those for RPC. This happens for:
7+
- namespace definitions
8+
- namespace usage
9+
- version string macros
10+
- ifdef/ifndef lines
11+
"""
12+
# Usage: convert-lldb-header-to-rpc-header.py <path/to/input-header.h> <path/to/output-header.h>
13+
14+
import argparse
15+
import os
16+
import re
17+
18+
19+
def main():
20+
parser = argparse.ArgumentParser()
21+
parser.add_argument("input")
22+
parser.add_argument("output")
23+
args = parser.parse_args()
24+
input_path = str(args.input)
25+
output_path = str(args.output)
26+
with open(input_path, "r") as input_file:
27+
lines = input_file.readlines()
28+
29+
with open(output_path, "w") as output_file:
30+
for line in lines:
31+
# NOTE: We do not use lldb-forward.h or lldb-versioning.h in RPC, so remove
32+
# all includes that are found for these files.
33+
if re.match(
34+
r'#include "lldb/lldb-forward|#include "lldb/lldb-versioning', line
35+
):
36+
continue
37+
# For lldb-rpc-defines.h, replace the ifndef LLDB_LLDB_ portion with LLDB_RPC_ as we're not
38+
# using LLDB private definitions in RPC.
39+
elif re.match(r".+LLDB_LLDB_", line):
40+
output_file.write(re.sub(r"LLDB_LLDB_", r"LLDB_RPC_", line))
41+
# Similarly to lldb-rpc-defines.h, replace the ifndef for LLDB_API in SBDefines.h to LLDB_RPC_API_ for the same reason.
42+
elif re.match(r".+LLDB_API_", line):
43+
output_file.write(re.sub(r"LLDB_API_", r"LLDB_RPC_API_", line))
44+
# Replace the references for the macros that define the versioning strings in
45+
# lldb-rpc-defines.h.
46+
# NOTE: Here we assume that the versioning info has already been uncommented and
47+
# populated from the original lldb-defines.h.
48+
elif re.match(r".+LLDB_VERSION", line):
49+
output_file.write(re.sub(r"LLDB_VERSION", r"LLDB_RPC_VERSION", line))
50+
elif re.match(r".+LLDB_REVISION", line):
51+
output_file.write(re.sub(r"LLDB_REVISION", r"LLDB_RPC_REVISION", line))
52+
elif re.match(r".+LLDB_VERSION_STRING", line):
53+
output_file.write(
54+
re.sub(r"LLDB_VERSION_STRING", r"LLDB_RPC_VERSION_STRING", line)
55+
)
56+
# For local #includes
57+
elif re.match(r'#include "lldb/lldb-', line):
58+
output_file.write(re.sub(r"lldb/lldb-", r"lldb-rpc-", line))
59+
# Rename the lldb namespace definition to lldb-rpc.
60+
elif re.match(r".*namespace lldb", line):
61+
output_file.write(re.sub(r"lldb", r"lldb_rpc", line))
62+
# Rename namespace references
63+
elif re.match(r".+lldb::", line):
64+
output_file.write(re.sub(r"lldb::", r"lldb_rpc::", line))
65+
else:
66+
# Write any line that doesn't need to be converted
67+
output_file.write(line)
68+
69+
70+
if __name__ == "__main__":
71+
main()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Usage: framework-header-include-fix.py <path/to/input-header.h> <path/to/output-header.h>
4+
5+
This script modifies all #include lines in all lldb-rpc headers
6+
from either filesystem or local includes to liblldbrpc includes.
7+
"""
8+
9+
import argparse
10+
import os
11+
import re
12+
13+
14+
def main():
15+
parser = argparse.ArgumentParser()
16+
parser.add_argument("input")
17+
parser.add_argument("output")
18+
args = parser.parse_args()
19+
input_path = str(args.input)
20+
output_path = str(args.output)
21+
with open(input_path, "r") as input_file:
22+
lines = input_file.readlines()
23+
24+
with open(output_path, "w") as output_file:
25+
for line in lines:
26+
# Replace includes from RPCCommon to liblldbrpc includes.
27+
# e.g. #include <lldb-rpc/common/RPCArgument.h> -> #include <LLDBRPC/RPCArgument.h>
28+
if re.match(r".+<lldb-rpc/common", line):
29+
output_file.write(re.sub(r"<lldb-rpc/common", r"<LLDBRPC", line))
30+
# Replace all local file includes to liblldbrpc includes.
31+
# e.g. #include "SBFoo.h" -> #include <LLDBRPC/SBFoo.h>
32+
elif include_filename := re.match(r'#include "(.*)"', line):
33+
output_file.write(
34+
re.sub(
35+
r'#include "(.*)"',
36+
r"#include <LLDBRPC/" + include_filename.group(1) + ">",
37+
line,
38+
)
39+
)
40+
else:
41+
# Write any line that doesn't need to be converted
42+
output_file.write(line)
43+
44+
45+
if __name__ == "__main__":
46+
main()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copy lldb-defines.h from source.
2+
RUN: mkdir -p %t/Outputs
3+
4+
# Run the convert script on it.
5+
RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/lldb-defines.h %t/Outputs/lldb-rpc-defines.h
6+
7+
# Check the output
8+
RUN: cat %t/output/lldb-rpc-defines.h | FileCheck %s
9+
10+
# The include guards must change from LLDB_LLDB_DEFINES_H to LLDB_RPC_DEFINES_H.
11+
CHECK: #ifndef LLDB_RPC_DEFINES_H
12+
CHECK: #define LLDB_RPC_DEFINES_H
13+
14+
# Includes of other lldb headers must begin with "lldb-rpc-".
15+
CHECK: #include "lldb-rpc-types.h"
16+
17+
# The version info must be changed from LLDB_VERSION to LLDB_RPC_VERSION
18+
CHECK: #define LLDB_RPC_VERSION
19+
CHECK: #define LLDB_RPC_REVISION
20+
CHECK: #define LLDB_RPC_VERSION_STRING
21+
22+
# The comment that closes the include guard should match the guard.
23+
CHECK: #endif // LLDB_RPC_DEFINES_H
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
# Run the convert script on it.
4+
RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/lldb-enumerations.h %t/Outputs/lldb-rpc-enumerations.h
5+
6+
# Check the output
7+
RUN: cat %t/Outputs/lldb-rpc-enumerations.h
8+
RUN: cat %t/Outputs/lldb-rpc-enumerations.h | FileCheck %s
9+
10+
# The include guards must change from LLDB_LLDB_ENUMERATIONS_H to LLDB_RPC_ENUMERATIONS_H.
11+
CHECK: #ifndef LLDB_RPC_ENUMERATIONS_H
12+
CHECK: #define LLDB_RPC_ENUMERATIONS_H
13+
14+
# Change the namespace to lldb_rpc.
15+
CHECK: namespace lldb_rpc
16+
17+
# The comment that closes the namespace should match the namespace.
18+
CHECK: // namespace lldb_rpc
19+
20+
# The comment that closes the include guard should match the guard.
21+
CHECK: #endif // LLDB_RPC_ENUMERATIONS_H
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
# Run the convert script on lldb-types.h
4+
RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/lldb-types.h %t/Outputs/lldb-rpc-types.h
5+
6+
# Check the output
7+
RUN: cat %t/Outputs/lldb-rpc-types.h
8+
RUN: cat %t/Outputs/lldb-rpc-types.h | FileCheck %s
9+
10+
# The include guards must change from LLDB_LLDB_TYPES_H to LLDB_RPC_TYPES_H.
11+
CHECK: #ifndef LLDB_RPC_TYPES_H
12+
CHECK: #define LLDB_RPC_TYPES_H
13+
14+
# Includes of other lldb headers must begin with "lldb-rpc-".
15+
# Also, the includes for lldb-forward.h should be removed.
16+
CHECK: #include "lldb-rpc-enumerations.h"
17+
18+
# Change the namespace to lldb_rpc.
19+
CHECK: namespace lldb_rpc
20+
21+
# The comment that closes the namespace should match the namespace.
22+
CHECK: // namespace lldb_rpc
23+
24+
# The comment that closes the include guard should match the guard.
25+
CHECK: #endif // LLDB_RPC_TYPES_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
# Run the convert script on it.
4+
RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/SBDefines.h %t/Outputs/SBDefines.h
5+
6+
# Check the output
7+
RUN: cat %t/Outputs/SBDefines.h
8+
RUN: cat %t/Outputs/SBDefines.h | FileCheck %s
9+
10+
# The include guards must change from LLDB_LLDB_API_SBDEFINES_H to LLDB_RPC_API_SBDEFINES_H.
11+
CHECK: #ifndef LLDB_RPC_API_SBDEFINES_H
12+
CHECK: #define LLDB_RPC_API_SBDEFINES_H
13+
14+
# Includes of other lldb headers must begin with "lldb-rpc-".
15+
# Also, the includes for lldb-forward.h and lldb-versioning.h should be removed.
16+
CHECK: #include "lldb-rpc-defines.h"
17+
CHECK-NOT: #include "lldb-rpc-forward.h"
18+
CHECK: #include "lldb-rpc-enumerations.h"
19+
CHECK: #include "lldb-rpc-types.h"
20+
CHECK-NOT: #include "lldb-rpc-versioning.h"
21+
22+
# The comment that closes the include guard should match the guard.
23+
CHECK: #endif // LLDB_RPC_API_SBDEFINES_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This is a truncated version of SBDefines.h used to test that the script
2+
// convert-lldb-header-to-rpc-header.py works correctly. The script changes LLDB references in
3+
// the original file to RPC references.
4+
5+
// The include guard should change from LLDB_LLDB to LLDB_RPC.
6+
// LLDB_API_SBDEFINES_H -> LLDB_RPC_SBDEFINES_H
7+
#ifndef LLDB_API_SBDEFINES_H
8+
#define LLDB_API_SBDEFINES_H
9+
10+
// Includes of public main LLDB headers should change to their RPC equivalents:
11+
// "lldb/lldb-defines.h" -> "lldb-rpc-defines.h"
12+
// Also, the includes for lldb-forward.h and lldb-versioning.h should be removed.
13+
#include "lldb/lldb-defines.h"
14+
#include "lldb/lldb-enumerations.h"
15+
#include "lldb/lldb-forward.h"
16+
#include "lldb/lldb-types.h"
17+
#include "lldb/lldb-versioning.h"
18+
19+
// The comment that closes the include guard must change in the same way
20+
// the original guard did.
21+
// #endif // LLDB_API_SBDEFINES_H -> #endif // LLDB_RPC_API_SBDEFINES_H
22+
#endif // LLDB_API_SBDEFINES_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This is a truncated version of lldb-defines.h used to test that the script
2+
// convert-lldb-header-to-rpc-header.py works correctly. The script changes LLDB references in
3+
// the original file to RPC references.
4+
5+
// The include guard should change from LLDB_LLDB to LLDB_RPC.
6+
// LLDB_LLDB_DEFINES_H -> LLDB_RPC_DEFINES_H
7+
#ifndef LLDB_LLDB_DEFINES_H
8+
#define LLDB_LLDB_DEFINES_H
9+
10+
// Includes of public main LLDB headers should change to their RPC equivalents:
11+
// "lldb/lldb-types.h" -> "lldb-rpc-types.h"
12+
#include "lldb/lldb-types.h"
13+
14+
// The LLDB version must change from LLDB to LLDB_RPC
15+
// LLDB_VERSION -> LLDB_RPC_VERSION
16+
#define LLDB_VERSION 21
17+
#define LLDB_REVISION 0
18+
#define LLDB_VERSION_STRING 0
19+
20+
// The comment that closes the include guard must change in the same way
21+
// the original guard did.
22+
// #endif // LLDB_LLDB_DEFINES_H -> #endif // LLDB_RPC_DEFINES_H
23+
#endif // LLDB_LLDB_DEFINES_H
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This is a truncated version of lldb-enumerations.h used to test that the script
2+
// convert-lldb-header-to-rpc-header.py works correctly. The script changes LLDB references in
3+
// the original file to RPC references.
4+
5+
// The include guard should change from LLDB_LLDB to LLDB_RPC.
6+
// LLDB_LLDB_ENUMERATIONS_H -> LLDB_RPC_ENUMERATIONS_H
7+
#ifndef LLDB_LLDB_ENUMERATIONS_H
8+
#define LLDB_LLDB_ENUMERATIONS_H
9+
10+
// The namespace definition should change to the lldb_rpc namespace, so should the comment that closes it:
11+
// namespace lldb -> namespace lldb_rpc
12+
namespace lldb {} // namespace lldb
13+
14+
// The comment that closes the include guard must change in the same way
15+
// the original guard did:
16+
// #endif // LLDB_LLDB_ENUMERATIONS_H -> #endif // LLDB_RPC_ENUMERATIONS_H
17+
#endif // LLDB_LLDB_ENUMERATIONS_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This is a truncated version of lldb-types.h used to test that the script
2+
// convert-lldb-header-to-rpc-header.py works correctly. The script changes LLDB references in
3+
// the original file to RPC references.
4+
5+
// The include guard should change from LLDB_LLDB to LLDB_RPC.
6+
// LLDB_LLDB_TYPES_H -> LLDB_RPC_TYPES_H
7+
#ifndef LLDB_LLDB_TYPES_H
8+
#define LLDB_LLDB_TYPES_H
9+
10+
// Includes of public main LLDB headers should change to their RPC equivalents:
11+
// "lldb/lldb-defines.h" -> "lldb-rpc-defines.h":
12+
// Also, the includes for lldb-forward.h should be removed.
13+
#include "lldb/lldb-enumerations.h"
14+
#include "lldb/lldb-forward.h"
15+
16+
// The namespace definition should change to the lldb_rpc namespace, so should the comment that closes it:
17+
// namespace lldb -> namespace lldb_rpc
18+
namespace lldb {} // namespace lldb
19+
20+
// The comment that closes the include guard must change in the same way
21+
// the original guard did:
22+
// #endif // LLDB_LLDB_TYPES_H -> #endif // LLDB_RPC_TYPES_H
23+
#endif // LLDB_LLDB_TYPES_H
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %p/Inputs/lldb-rpc-defines.h %t/Outputs/lldb-rpc-defines.h
4+
5+
# Check the output
6+
RUN: cat %t/Outputs/lldb-rpc-defines.h | FileCheck %s
7+
8+
# Local includes for LLDB RPC headers must be changed for the framework.
9+
CHECK: #include <LLDBRPC/lldb-rpc-types.h>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %p/Inputs/lldb-rpc-types.h %t/Outputs/lldb-rpc-types.h
4+
5+
# Check the output
6+
RUN: cat %t/Outputs/lldb-rpc-types.h | FileCheck %s
7+
8+
# Local includes for LLDB RPC headers must be changed for the framework.
9+
CHECK: #include <LLDBRPC/lldb-rpc-enumerations.h>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %p/Inputs/SBRPC-FrameworkFix.h %t/Outputs/SBRPC-FrameworkFix.h
4+
5+
# Check the output
6+
RUN: cat %t/Outputs/SBRPC-FrameworkFix.h
7+
RUN: cat %t/Outputs/SBRPC-FrameworkFix.h | FileCheck %s
8+
9+
CHECK: #include <LLDBRPC/LLDBRPC.h>
10+
CHECK: #include <LLDBRPC/SBDefines.h>
11+
CHECK: #include <LLDBRPC/RPCPublic.h>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
# Run the framework header fix script on SBDefines.h.
4+
RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %p/Inputs/SBDefines.h %t/Outputs/SBDefines.h
5+
6+
# Check the output.
7+
RUN: cat %t/Outputs/SBDefines.h | FileCheck %s
8+
9+
# Local includes for LLDB RPC headers must be changed for the framework.
10+
CHECK: #include <LLDBRPC/lldb-rpc-defines.h>
11+
CHECK: #include <LLDBRPC/lldb-rpc-enumerations.h>
12+
CHECK: #include <LLDBRPC/lldb-rpc-types.h>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This is a truncated version of SBDefines.h used to test that the script
2+
// framework-header-include-fix.py works correctly. The script fixes up header files
3+
// to be used in LLDBRPC.framework.
4+
5+
// Local includes of RPC main header files must be changed to framework includes:
6+
// #include "lldb-rpc-defines.h" -> #include <LLDBRPC/lldb-rpc-defines.h>
7+
#include "lldb-rpc-defines.h"
8+
#include "lldb-rpc-enumerations.h"
9+
#include "lldb-rpc-types.h"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This is a truncated version of a generated SB API header file used to test that the script
2+
// framework-header-include-fix.py works correctly. The script fixes up header files
3+
// to be used in LLDBRPC.framework.
4+
5+
// Local includes of RPC main header and includes of RPC common files must be changed to framework includes:
6+
// #include "SBDefines.h" -> #include <LLDBRPC/SBDefines.h>
7+
// #include <lldb-rpc/common/RPCPublic.h> -> #include <LLDBRPC/RPCPublic.h>
8+
#include "LLDBRPC.h"
9+
#include "SBDefines.h"
10+
#include <lldb-rpc/common/RPCPublic.h>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This is a truncated version of lldb-rpc-defines.h used to test that the script
2+
// framework-header-include-fix.py works correctly. The script fixes up header files
3+
// to be used in LLDBRPC.framework.
4+
5+
// Includes of public main LLDB headers should change to their RPC equivalents:
6+
// #include "lldb-rpc-types.h" -> #include <LLDBRPC/lldb-rpc-types.h>
7+
#include "lldb-rpc-types.h"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This is a truncated version of lldb-rpc-types.h used to test that the script
2+
// framework-header-include-fix.py works correctly. The script fixes up header files
3+
// to be used in LLDBRPC.framework.
4+
5+
// Local includes of RPC main header files must be changed to framework includes:
6+
// #include "lldb-rpc-enumerations.h" -> #include <LLDBRPC/lldb-rpc-enumerations.h>
7+
#include "lldb/lldb-rpc-enumerations.h"

0 commit comments

Comments
 (0)