Skip to content

Commit a2d2941

Browse files
[lldb][RPC] Upstream LLDB to RPC converstion Python script (#138028)
As part of upstreaming LLDB RPC, this commit adds a python script that is 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 52583b3 commit a2d2941

File tree

9 files changed

+278
-0
lines changed

9 files changed

+278
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
13+
import argparse
14+
import os
15+
import re
16+
17+
18+
INCLUDES_TO_REMOVE_REGEX = re.compile(
19+
r'#include "lldb/lldb-forward.h"|#include "lldb/lldb-versioning.h"'
20+
)
21+
LLDB_GUARD_REGEX = re.compile(r"(?P<guard_type>#.+)LLDB_LLDB_\s*", re.M)
22+
LLDB_API_GUARD_REGEX = re.compile(r"(?P<guard_type>#.+)LLDB_API_\s*", re.M)
23+
LLDB_VERSION_REGEX = re.compile(r"#define LLDB_VERSION", re.M)
24+
LLDB_REVISION_REGEX = re.compile(r"#define LLDB_REVISION", re.M)
25+
LLDB_VERSION_STRING_REGEX = re.compile(r"#define LLDB_VERSION_STRING", re.M)
26+
LLDB_LOCAL_INCLUDE_REGEX = re.compile(r'#include "lldb/lldb-\s*', re.M)
27+
LLDB_NAMESPACE_DEFINITION_REGEX = re.compile(
28+
r"(?P<comment_marker>//\s*){,1}namespace lldb\s{1}", re.M
29+
)
30+
LLDB_NAMESPACE_REGEX = re.compile(r"\s*.+lldb::\s*", re.M)
31+
32+
33+
def main():
34+
parser = argparse.ArgumentParser()
35+
parser.add_argument("input")
36+
parser.add_argument("output")
37+
args = parser.parse_args()
38+
input_path = str(args.input)
39+
output_path = str(args.output)
40+
with open(input_path, "r") as input_file:
41+
lines = input_file.readlines()
42+
file_buffer = "".join(lines)
43+
44+
with open(output_path, "w") as output_file:
45+
# NOTE: We do not use lldb-forward.h or lldb-versioning.h in RPC, so remove
46+
# all includes that are found for these files.
47+
file_buffer = re.sub(INCLUDES_TO_REMOVE_REGEX, r"", file_buffer)
48+
49+
# For lldb-rpc-defines.h, replace the ifndef LLDB_LLDB_ portion with LLDB_RPC_ as we're not
50+
# using LLDB private definitions in RPC.
51+
lldb_guard_matches = LLDB_GUARD_REGEX.finditer(file_buffer)
52+
for match in lldb_guard_matches:
53+
file_buffer = re.sub(
54+
match.group(),
55+
r"{0}LLDB_RPC_".format(match.group("guard_type")),
56+
file_buffer,
57+
)
58+
59+
# Similarly to lldb-rpc-defines.h, replace the ifndef for LLDB_API in SBDefines.h to LLDB_RPC_API_ for the same reason.
60+
lldb_api_guard_matches = LLDB_API_GUARD_REGEX.finditer(file_buffer)
61+
for match in lldb_api_guard_matches:
62+
file_buffer = re.sub(
63+
match.group(),
64+
r"{0}LLDB_RPC_API_".format(match.group("guard_type")),
65+
file_buffer,
66+
)
67+
68+
# Replace the references for the macros that define the versioning strings in
69+
# lldb-rpc-defines.h.
70+
# NOTE: Here we assume that the versioning info has already been uncommented and
71+
# populated from the original lldb-defines.h.
72+
file_buffer = re.sub(
73+
LLDB_VERSION_REGEX, r"#define LLDB_RPC_VERSION", file_buffer
74+
)
75+
file_buffer = re.sub(
76+
LLDB_REVISION_REGEX, r"#define LLDB_RPC_REVISION", file_buffer
77+
)
78+
file_buffer = re.sub(
79+
LLDB_VERSION_STRING_REGEX, r"#define LLDB_RPC_VERSION_STRING", file_buffer
80+
)
81+
82+
# For local #includes
83+
file_buffer = re.sub(
84+
LLDB_LOCAL_INCLUDE_REGEX, r'#include "lldb-rpc-', file_buffer
85+
)
86+
87+
# Rename the lldb namespace definition to lldb-rpc.
88+
lldb_rpc_namespace_definition_matches = (
89+
LLDB_NAMESPACE_DEFINITION_REGEX.finditer(file_buffer)
90+
)
91+
for match in lldb_rpc_namespace_definition_matches:
92+
comment_marker = (
93+
match.group("comment_marker") if match.group("comment_marker") else ""
94+
)
95+
file_buffer = re.sub(
96+
match.group(),
97+
r"{0}namespace lldb_rpc ".format(comment_marker),
98+
file_buffer,
99+
)
100+
101+
# Rename the lldb namespace definition to lldb-rpc.
102+
file_buffer = re.sub(LLDB_NAMESPACE_REGEX, r"lldb_rpc::", file_buffer)
103+
104+
output_file.write(file_buffer)
105+
106+
107+
if __name__ == "__main__":
108+
main()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
# Run the convert script on lldb-defines.h.
4+
RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/lldb-defines.h %t/Outputs/lldb-rpc-defines.h
5+
6+
# Check the output
7+
RUN: cat %t/Outputs/lldb-rpc-defines.h | FileCheck %s
8+
9+
# The include guards must change from LLDB_LLDB_DEFINES_H to LLDB_RPC_DEFINES_H.
10+
CHECK: #ifndef LLDB_RPC_DEFINES_H
11+
CHECK: #define LLDB_RPC_DEFINES_H
12+
13+
# Includes of other lldb headers must begin with "lldb-rpc-".
14+
CHECK: #include "lldb-rpc-types.h"
15+
16+
# The version info must be changed from LLDB_VERSION to LLDB_RPC_VERSION
17+
CHECK: #define LLDB_RPC_VERSION 21
18+
CHECK: #define LLDB_RPC_REVISION 12
19+
CHECK: #define LLDB_RPC_VERSION_STRING "21.0.12"
20+
21+
# The comment that closes the include guard should match the guard.
22+
CHECK: #endif // LLDB_RPC_DEFINES_H
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
RUN: mkdir -p %t/Outputs
2+
3+
# Run the convert script on lldb-enumerations.h.
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 | FileCheck %s
8+
9+
# The include guards must change from LLDB_LLDB_ENUMERATIONS_H to LLDB_RPC_ENUMERATIONS_H.
10+
CHECK: #ifndef LLDB_RPC_ENUMERATIONS_H
11+
CHECK: #define LLDB_RPC_ENUMERATIONS_H
12+
13+
# Change the namespace to lldb_rpc. Also, the comment that closes the namespace should match the namespace.
14+
CHECK: namespace lldb_rpc {} // namespace lldb_rpc
15+
16+
# The comment that closes the include guard should match the guard.
17+
CHECK: #endif // LLDB_RPC_ENUMERATIONS_H
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 | FileCheck %s
8+
9+
# The include guards must change from LLDB_LLDB_TYPES_H to LLDB_RPC_TYPES_H.
10+
CHECK: #ifndef LLDB_RPC_TYPES_H
11+
CHECK: #define LLDB_RPC_TYPES_H
12+
13+
# Includes of other lldb headers must begin with "lldb-rpc-".
14+
# Also, the includes for lldb-forward.h should be removed.
15+
CHECK: #include "lldb-rpc-enumerations.h"
16+
17+
# Change the namespace to lldb_rpc.
18+
CHECK: namespace lldb_rpc
19+
20+
# The comment that closes the namespace should match the namespace.
21+
CHECK: // namespace lldb_rpc
22+
23+
# The comment that closes the include guard should match the guard.
24+
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+
RUN: mkdir -p %t/Outputs
2+
3+
# Run the convert script on SBDefines.h.
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 | FileCheck %s
8+
9+
# The include guards must change from LLDB_LLDB_API_SBDEFINES_H to LLDB_RPC_API_SBDEFINES_H.
10+
CHECK: #ifndef LLDB_RPC_API_SBDEFINES_H
11+
CHECK: #define LLDB_RPC_API_SBDEFINES_H
12+
13+
# Includes of other lldb headers must begin with "lldb-rpc-".
14+
# Also, the includes for lldb-forward.h and lldb-versioning.h should be removed.
15+
CHECK: #include "lldb-rpc-defines.h"
16+
CHECK-NOT: #include "lldb-rpc-forward.h"
17+
CHECK: #include "lldb-rpc-enumerations.h"
18+
CHECK: #include "lldb-rpc-types.h"
19+
CHECK-NOT: #include "lldb-rpc-versioning.h"
20+
21+
# The comment that closes the include guard should match the guard.
22+
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 12
18+
#define LLDB_VERSION_STRING "21.0.12"
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

0 commit comments

Comments
 (0)