Skip to content

Commit 4520d08

Browse files
committed
[lldb][RPC] Upstream LLDB to RPC converstion Python script
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 cc6f446 commit 4520d08

File tree

9 files changed

+243
-0
lines changed

9 files changed

+243
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
def main():
19+
parser = argparse.ArgumentParser()
20+
parser.add_argument("input")
21+
parser.add_argument("output")
22+
args = parser.parse_args()
23+
input_path = str(args.input)
24+
output_path = str(args.output)
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+
# NOTE: We do not use lldb-forward.h or lldb-versioning.h in RPC, so remove
31+
# all includes that are found for these files.
32+
if re.match(
33+
r'#include "lldb/lldb-forward|#include "lldb/lldb-versioning', line
34+
):
35+
continue
36+
# For lldb-rpc-defines.h, replace the ifndef LLDB_LLDB_ portion with LLDB_RPC_ as we're not
37+
# using LLDB private definitions in RPC.
38+
elif re.match(r".+LLDB_LLDB_", line):
39+
output_file.write(re.sub(r"LLDB_LLDB_", r"LLDB_RPC_", line))
40+
# Similarly to lldb-rpc-defines.h, replace the ifndef for LLDB_API in SBDefines.h to LLDB_RPC_API_ for the same reason.
41+
elif re.match(r".+LLDB_API_", line):
42+
output_file.write(re.sub(r"LLDB_API_", r"LLDB_RPC_API_", line))
43+
# Replace the references for the macros that define the versioning strings in
44+
# lldb-rpc-defines.h.
45+
# NOTE: Here we assume that the versioning info has already been uncommented and
46+
# populated from the original lldb-defines.h.
47+
elif re.match(r".+LLDB_VERSION", line):
48+
output_file.write(re.sub(r"LLDB_VERSION", r"LLDB_RPC_VERSION", line))
49+
elif re.match(r".+LLDB_REVISION", line):
50+
output_file.write(re.sub(r"LLDB_REVISION", r"LLDB_RPC_REVISION", line))
51+
elif re.match(r".+LLDB_VERSION_STRING", line):
52+
output_file.write(
53+
re.sub(r"LLDB_VERSION_STRING", r"LLDB_RPC_VERSION_STRING", line)
54+
)
55+
# For local #includes
56+
elif re.match(r'#include "lldb/lldb-', line):
57+
output_file.write(re.sub(r"lldb/lldb-", r"lldb-rpc-", line))
58+
# Rename the lldb namespace definition to lldb-rpc.
59+
elif re.match(r".*namespace lldb", line):
60+
output_file.write(re.sub(r"lldb", r"lldb_rpc", line))
61+
# Rename namespace references
62+
elif re.match(r".+lldb::", line):
63+
output_file.write(re.sub(r"lldb::", r"lldb_rpc::", line))
64+
else:
65+
# Write any line that doesn't need to be converted
66+
output_file.write(line)
67+
68+
69+
if __name__ == "__main__":
70+
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
18+
CHECK: #define LLDB_RPC_REVISION
19+
CHECK: #define LLDB_RPC_VERSION_STRING
20+
21+
# The comment that closes the include guard should match the guard.
22+
CHECK: #endif // LLDB_RPC_DEFINES_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.
14+
CHECK: namespace lldb_rpc
15+
16+
# The comment that closes the namespace should match the namespace.
17+
CHECK: // namespace lldb_rpc
18+
19+
# The comment that closes the include guard should match the guard.
20+
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 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

0 commit comments

Comments
 (0)