109 lines · python
1#!/usr/bin/env python32"""3Usage: convert-lldb-header-to-rpc-header.py <path/to/input-header.h> <path/to/output-header.h>4 5This scripts takes common LLDB headers (such as lldb-defines.h) and replaces references to LLDB6with those for RPC. This happens for:7- namespace definitions8- namespace usage9- version string macros10- ifdef/ifndef lines11"""12 13import argparse14import os15import re16 17 18INCLUDES_TO_REMOVE_REGEX = re.compile(19 r'#include "lldb/lldb-forward.h"|#include "lldb/lldb-versioning.h"'20)21LLDB_GUARD_REGEX = re.compile(r"(?P<guard_type>#.+)LLDB_LLDB_\s*", re.M)22LLDB_API_GUARD_REGEX = re.compile(r"(?P<guard_type>#.+)LLDB_API_\s*", re.M)23LLDB_VERSION_REGEX = re.compile(r"#define LLDB_VERSION", re.M)24LLDB_REVISION_REGEX = re.compile(r"#define LLDB_REVISION", re.M)25LLDB_VERSION_STRING_REGEX = re.compile(r"#define LLDB_VERSION_STRING", re.M)26LLDB_LOCAL_INCLUDE_REGEX = re.compile(r'#include "lldb/lldb-\s*', re.M)27LLDB_NAMESPACE_DEFINITION_REGEX = re.compile(28 r"(?P<comment_marker>//\s*){,1}namespace lldb\s{1}", re.M29)30LLDB_NAMESPACE_REGEX = re.compile(r"lldb::\s*", re.M)31 32 33def 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 remove46 # 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 not50 # 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 in69 # lldb-rpc-defines.h.70 # NOTE: Here we assume that the versioning info has already been uncommented and71 # populated from the original lldb-defines.h.72 file_buffer = re.sub(73 LLDB_VERSION_REGEX, r"#define LLDB_RPC_VERSION", file_buffer74 )75 file_buffer = re.sub(76 LLDB_REVISION_REGEX, r"#define LLDB_RPC_REVISION", file_buffer77 )78 file_buffer = re.sub(79 LLDB_VERSION_STRING_REGEX, r"#define LLDB_RPC_VERSION_STRING", file_buffer80 )81 82 # For local #includes83 file_buffer = re.sub(84 LLDB_LOCAL_INCLUDE_REGEX, r'#include "lldb-rpc-', file_buffer85 )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 107if __name__ == "__main__":108 main()109