https://github.com/chelcassanova updated https://github.com/llvm/llvm-project/pull/138028
>From c51a312cd3901a1e51d9e210b3efdc89389f0e10 Mon Sep 17 00:00:00 2001 From: Chelsea Cassanova <chelsea_cassan...@apple.com> Date: Wed, 30 Apr 2025 13:37:15 -0700 Subject: [PATCH] [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 --- .../convert-lldb-header-to-rpc-header.py | 65 +++++++++++++++++++ lldb/scripts/framework-header-include-fix.py | 44 +++++++++++++ lldb/scripts/framework-header-version-fix.py | 65 +++++++++++++++++++ .../TestConvertScript/CheckLLDBDefines.test | 20 ++++++ .../CheckLLDBEnumerations.test | 23 +++++++ .../TestConvertScript/CheckLLDBTypes.test | 26 ++++++++ .../TestConvertScript/CheckSBDefines.test | 22 +++++++ .../CheckLLDBDefines.test | 16 +++++ .../CheckLLDBTypes.test | 16 +++++ .../CheckSBClass.test | 13 ++++ .../CheckSBDefines.test | 18 +++++ .../Inputs/SBRPC-FrameworkFix.h | 13 ++++ .../CheckLLDBDefines.test | 18 +++++ 13 files changed, 359 insertions(+) create mode 100755 lldb/scripts/convert-lldb-header-to-rpc-header.py create mode 100755 lldb/scripts/framework-header-include-fix.py create mode 100755 lldb/scripts/framework-header-version-fix.py create mode 100644 lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBDefines.test create mode 100644 lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBEnumerations.test create mode 100644 lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBTypes.test create mode 100644 lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckSBDefines.test create mode 100644 lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckLLDBDefines.test create mode 100644 lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckLLDBTypes.test create mode 100644 lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckSBClass.test create mode 100644 lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckSBDefines.test create mode 100644 lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/Inputs/SBRPC-FrameworkFix.h create mode 100644 lldb/test/Shell/RPC/Scripts/TestVersionFixScript/CheckLLDBDefines.test diff --git a/lldb/scripts/convert-lldb-header-to-rpc-header.py b/lldb/scripts/convert-lldb-header-to-rpc-header.py new file mode 100755 index 0000000000000..fe23ef029dcf1 --- /dev/null +++ b/lldb/scripts/convert-lldb-header-to-rpc-header.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +# Usage: convert-lldb-header-to-rpc-header.py <path/to/input-header.h> <path/to/output-header.h> +# This scripts takes common LLDB headers (such as lldb-defines.h) and replaces references to LLDB +# with those for RPC. This happens for: +# - namespace definitions +# - namespace usage +# - version string macros +# - ifdef/ifndef lines + +import argparse +import os +import re + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("input") + parser.add_argument("output") + args = parser.parse_args() + input_path = str(args.input) + output_path = str(args.output) + with open(input_path, "r") as input_file: + lines = input_file.readlines() + + with open(output_path, "w") as output_file: + for line in lines: + # NOTE: We do not use lldb-forward.h or lldb-versioning.h in RPC, so remove + # all includes that are found for these files. + if re.match( + r'#include "lldb/lldb-forward|#include "lldb/lldb-versioning', line + ): + continue + # For lldb-rpc-defines.h, replace the ifndef LLDB_LLDB_ portion with LLDB_RPC_ as we're not + # using LLDB private definitions in RPC. + elif re.match(r".+LLDB_LLDB_", line): + output_file.write(re.sub(r"LLDB_LLDB_", r"LLDB_RPC_", line)) + # Similarly to lldb-rpc-defines.h, replace the ifndef for LLDB_API in SBDefines.h to LLDB_RPC_API_ for the same reason. + elif re.match(r".+LLDB_API_", line): + output_file.write(re.sub(r"LLDB_API_", r"LLDB_RPC_API_", line)) + # Replace the references for the macros that define the versioning strings in + # lldb-rpc-defines.h. + elif re.match(r".+LLDB_VERSION", line): + output_file.write(re.sub(r"LLDB_VERSION", r"LLDB_RPC_VERSION", line)) + elif re.match(r".+LLDB_REVISION", line): + output_file.write(re.sub(r"LLDB_REVISION", r"LLDB_RPC_REVISION", line)) + elif re.match(r".+LLDB_VERSION_STRING", line): + output_file.write( + re.sub(r"LLDB_VERSION_STRING", r"LLDB_RPC_VERSION_STRING", line) + ) + # For local #includes + elif re.match(r'#include "lldb/lldb-', line): + output_file.write(re.sub(r"lldb/lldb-", r"lldb-rpc-", line)) + # Rename the lldb namespace definition to lldb-rpc. + elif re.match(r".*namespace lldb", line): + output_file.write(re.sub(r"lldb", r"lldb_rpc", line)) + # Rename namespace references + elif re.match(r".+lldb::", line): + output_file.write(re.sub(r"lldb::", r"lldb_rpc::", line)) + else: + # Write any line that doesn't need to be converted + output_file.write(line) + + +if __name__ == "__main__": + main() diff --git a/lldb/scripts/framework-header-include-fix.py b/lldb/scripts/framework-header-include-fix.py new file mode 100755 index 0000000000000..e214ce005923f --- /dev/null +++ b/lldb/scripts/framework-header-include-fix.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +# Usage: framework-header-include-fix.py <path/to/input-header.h> <path/to/output-header.h> +# This script modifies all #include lines in all lldb-rpc headers +# from either filesystem or local includes to liblldbrpc includes. + +import argparse +import os +import re + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("input") + parser.add_argument("output") + args = parser.parse_args() + input_path = str(args.input) + output_path = str(args.output) + with open(input_path, "r+") as input_file: + lines = input_file.readlines() + + with open(output_path, "w+") as output_file: + for line in lines: + # Replace includes from RPCCommon to liblldbrpc includes. + # e.g. #include <lldb-rpc/common/RPCArgument.h> -> #include <LLDBRPC/RPCArgument.h> + if re.match(r".+<lldb-rpc/common", line): + output_file.write(re.sub(r"<lldb-rpc/common", r"<LLDBRPC", line)) + # Replace all local file includes to liblldbrpc includes. + # e.g. #include "SBFoo.h" -> #include <LLDBRPC/SBFoo.h> + elif re.match(r'#include "(.*)"', line): + include_filename = re.search(r'#include "(.*)"', line).groups()[0] + output_file.write( + re.sub( + r'#include "(.*)"', + r"#include <LLDBRPC/" + include_filename + ">", + line, + ) + ) + else: + # Write any line that doesn't need to be converted + output_file.write(line) + + +if __name__ == "__main__": + main() diff --git a/lldb/scripts/framework-header-version-fix.py b/lldb/scripts/framework-header-version-fix.py new file mode 100755 index 0000000000000..72185f8e820ce --- /dev/null +++ b/lldb/scripts/framework-header-version-fix.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +# Usage: framework-header-version-fix.py <path/to/input-header.h> <path/to/output-header.h> MAJOR MINOR PATCH +# This script modifies lldb-rpc-defines.h to uncomment the macro defines used for the LLDB +# major, minor and patch values as well as populating their definitions. + +import argparse +import os +import re + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("input") + parser.add_argument("output") + parser.add_argument("lldb_version_major") + parser.add_argument("lldb_version_minor") + parser.add_argument("lldb_version_patch") + args = parser.parse_args() + input_path = str(args.input) + output_path = str(args.output) + lldb_version_major = args.lldb_version_major + lldb_version_minor = args.lldb_version_minor + lldb_version_patch = args.lldb_version_patch + + with open(input_path, "r") as input_file: + lines = input_file.readlines() + + with open(output_path, "w") as output_file: + for line in lines: + # Uncomment the line that defines the LLDB major version and populate its value. + if re.match(r"//#define LLDB_RPC_VERSION$", line): + output_file.write( + re.sub( + r"//#define LLDB_RPC_VERSION", + r"#define LLDB_RPC_VERSION " + lldb_version_major, + line, + ) + ) + # Uncomment the line that defines the LLDB minor version and populate its value. + elif re.match(r"//#define LLDB_RPC_REVISION$", line): + output_file.write( + re.sub( + r"//#define LLDB_RPC_REVISION", + r"#define LLDB_RPC_REVISION " + lldb_version_minor, + line, + ) + ) + # Uncomment the line that defines the complete LLDB version string and populate its value. + elif re.match(r"//#define LLDB_RPC_VERSION_STRING$", line): + output_file.write( + re.sub( + r"//#define LLDB_RPC_VERSION_STRING", + r'#define LLDB_RPC_VERSION_STRING "{0}.{1}.{2}"'.format( + lldb_version_major, lldb_version_minor, lldb_version_patch + ), + line, + ) + ) + else: + # Write any line that doesn't need to be converted + output_file.write(line) + + +if __name__ == "__main__": + main() diff --git a/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBDefines.test b/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBDefines.test new file mode 100644 index 0000000000000..bd85eaa823230 --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBDefines.test @@ -0,0 +1,20 @@ +// Copy lldb-defines.h from source. +# RUN: mkdir -p %t/input +# RUN: mkdir -p %t/output +# RUN: cp %p/../../../../../include/lldb/lldb-defines.h %t/input + +// Run the convert script on it. +# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-defines.h %t/output/lldb-rpc-defines.h + +// Check the output +# RUN: cat %t/output/lldb-rpc-defines.h | FileCheck %s + +// The include guards must change from LLDB_LLDB_DEFINES_H to LLDB_RPC_DEFINES_H. +# CHECK: #ifndef LLDB_RPC_DEFINES_H +# CHECK: #define LLDB_RPC_DEFINES_H + +// Includes of other lldb headers must begin with "lldb-rpc-". +# CHECK: #include "lldb-rpc-types.h" + +// The comment that closes the include guard should match the guard. +# CHECK: #endif // LLDB_RPC_DEFINES_H diff --git a/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBEnumerations.test b/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBEnumerations.test new file mode 100644 index 0000000000000..285072a7673f9 --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBEnumerations.test @@ -0,0 +1,23 @@ +// Copy lldb-enumerations.h from source. +# RUN: mkdir -p %t/input +# RUN: mkdir -p %t/output +# RUN: cp %p/../../../../../include/lldb/lldb-enumerations.h %t/input + +// Run the convert script on it. +# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-enumerations.h %t/output/lldb-rpc-enumerations.h + +// Check the output +# RUN: cat %t/output/lldb-rpc-enumerations.h | FileCheck %s + +// The include guards must change from LLDB_LLDB_ENUMERATIONS_H to LLDB_RPC_ENUMERATIONS_H. +# CHECK: #ifndef LLDB_RPC_ENUMERATIONS_H +# CHECK: #define LLDB_RPC_ENUMERATIONS_H + +// Change the namespace to lldb_rpc. +# CHECK: namespace lldb_rpc + +// The comment that closes the namespace should match the namespace. +# CHECK: // namespace lldb_rpc + +// The comment that closes the include guard should match the guard. +# CHECK: #endif // LLDB_RPC_ENUMERATIONS_H diff --git a/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBTypes.test b/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBTypes.test new file mode 100644 index 0000000000000..3641195babeab --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBTypes.test @@ -0,0 +1,26 @@ +// Copy lldb-types.h from source. +# RUN: mkdir -p %t/input +# RUN: mkdir -p %t/output +# RUN: cp %p/../../../../../include/lldb/lldb-types.h %t/input + +// Run the convert script on it. +# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-types.h %t/output/lldb-rpc-types.h + +// Check the output +# RUN: cat %t/output/lldb-rpc-types.h | FileCheck %s + +// The include guards must change from LLDB_LLDB_ENUMERATIONS_H to LLDB_RPC_ENUMERATIONS_H. +# CHECK: #ifndef LLDB_RPC_TYPES_H +# CHECK: #define LLDB_RPC_TYPES_H + +// Includes of other lldb headers must begin with "lldb-rpc-". +# CHECK: #include "lldb-rpc-enumerations.h" + +// Change the namespace to lldb_rpc. +# CHECK: namespace lldb_rpc + +// The comment that closes the namespace should match the namespace. +# CHECK: // namespace lldb_rpc + +// The comment that closes the include guard should match the guard. +# CHECK: #endif // LLDB_RPC_TYPES_H diff --git a/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckSBDefines.test b/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckSBDefines.test new file mode 100644 index 0000000000000..39cd7a954c554 --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckSBDefines.test @@ -0,0 +1,22 @@ +// Copy SBDefines.h from source. +# RUN: mkdir -p %t/input +# RUN: mkdir -p %t/output +# RUN: cp %p/../../../../../include/lldb/API/SBDefines.h %t/input + +// Run the convert script on it. +# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/SBDefines.h %t/output/SBDefines.h + +// Check the output +# RUN: cat %t/output/SBDefines.h | FileCheck %s + +// The include guards must change from LLDB_LLDB_API_SBDEFINES_H to LLDB_RPC_API_SBDEFINES_H. +# CHECK: #ifndef LLDB_RPC_API_SBDEFINES_H +# CHECK: #define LLDB_RPC_API_SBDEFINES_H + +// Includes of other lldb headers must begin with "lldb-rpc-". +# CHECK: #include "lldb-rpc-defines.h" +# CHECK: #include "lldb-rpc-enumerations.h" +# CHECK: #include "lldb-rpc-types.h" + +// The comment that closes the include guard should match the guard. +# CHECK: #endif // LLDB_RPC_API_SBDEFINES_H diff --git a/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckLLDBDefines.test b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckLLDBDefines.test new file mode 100644 index 0000000000000..b2e25f7a70eae --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckLLDBDefines.test @@ -0,0 +1,16 @@ +// Copy lldb-rpc-defines.h from source. +# RUN: mkdir -p %t/input +# RUN: mkdir -p %t/output +# RUN: cp %p/../../../../../include/lldb/lldb-defines.h %t/input + +// Run the convert script on it, then run the framework include fix on it. The framework include fix script +// expects that all lldb references have been renamed to lldb-rpc in order for it to modify the includes +// to go into the framework. +# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-defines.h %t/output/lldb-rpc-defines.h +# RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %t/output/lldb-rpc-defines.h %t/output/lldb-rpc-defines.h + +// Check the output +# RUN: cat %t/output/lldb-rpc-defines.h | FileCheck %s + +// Local includes for LLDB RPC headers must be changed for the framework. +# CHECK: #include <LLDBRPC/lldb-rpc-types.h> diff --git a/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckLLDBTypes.test b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckLLDBTypes.test new file mode 100644 index 0000000000000..7da7c6ad80ac5 --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckLLDBTypes.test @@ -0,0 +1,16 @@ +// Copy lldb-rpc-types.h from source. +# RUN: mkdir -p %t/input +# RUN: mkdir -p %t/output +# RUN: cp %p/../../../../../include/lldb/lldb-types.h %t/input + +// Run the convert script on it, then run the framework include fix on it. The framework include fix script +// expects that all lldb references have been renamed to lldb-rpc in order for it to modify the includes +// to go into the framework. +# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-types.h %t/output/lldb-rpc-types.h +# RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %t/output/lldb-rpc-types.h %t/output/lldb-rpc-types.h + +// Check the output +# RUN: cat %t/output/lldb-rpc-types.h | FileCheck %s + +// Local includes for LLDB RPC headers must be changed for the framework. +# CHECK: #include <LLDBRPC/lldb-rpc-enumerations.h> diff --git a/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckSBClass.test b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckSBClass.test new file mode 100644 index 0000000000000..fb7dd8b93a132 --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckSBClass.test @@ -0,0 +1,13 @@ +// Generate a dummy SB API file using lldb-rpc-gen. +# RUN: mkdir -p %t/server +# RUN: mkdir -p %t/lib +# RUN: %lldb-rpc-gen --output-dir=%t %S/Inputs/SBRPC-FrameworkFix.h + +# RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %t/lib/SBRPC-FrameworkFix.h %t/lib/SBRPC-FrameworkFix.h + +// Check the output +# RUN: cat %t/lib/SBRPC-FrameworkFix.h | FileCheck %s + +# CHECK: #include <LLDBRPC/RPCPublic.h> +# CHECK: #include <LLDBRPC/SBDefines.h> +# CHECK: #include <LLDBRPC/LLDBRPC.h> diff --git a/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckSBDefines.test b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckSBDefines.test new file mode 100644 index 0000000000000..6dee17232aee7 --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/CheckSBDefines.test @@ -0,0 +1,18 @@ +// Copy SBDefines.h from source. +# RUN: mkdir -p %t/input +# RUN: mkdir -p %t/output +# RUN: cp %p/../../../../../include/lldb/API/SBDefines.h %t/input + +// Run the convert script on it, then run the framework include fix on it. The framework include fix script +// expects that all lldb references have been renamed to lldb-rpc in order for it to modify the includes +// to go into the framework. +# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/SBDefines.h %t/output/SBDefines.h +# RUN: %python %p/../../../../../scripts/framework-header-include-fix.py %t/output/SBDefines.h %t/output/SBDefines.h + +// Check the output +# RUN: cat %t/output/SBDefines.h | FileCheck %s + +// Local includes for LLDB RPC headers must be changed for the framework. +# CHECK: #include <LLDBRPC/lldb-rpc-defines.h> +# CHECK: #include <LLDBRPC/lldb-rpc-enumerations.h> +# CHECK: #include <LLDBRPC/lldb-rpc-types.h> diff --git a/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/Inputs/SBRPC-FrameworkFix.h b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/Inputs/SBRPC-FrameworkFix.h new file mode 100644 index 0000000000000..39cf490737293 --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestFrameworkIncludeFixScript/Inputs/SBRPC-FrameworkFix.h @@ -0,0 +1,13 @@ +#ifndef LLDB_API_SBRPC_FRAMEWORKINCLUDEFIX_H +#define LLDB_API_SBRPC_FRAMEWORKINCLUDEFIX_H + +// The includes for local SB API files and RPC common files +// must be changed to become framework includes. +// They're commented out so that the tool doesn't actually +// try and locate these files. + +// #include <lldb-rpc/common/RPCPublic.h> +// #include "SBDefines.h" +// #include "LLDBRPC.h" + +#endif // LLDB_API_SBRPC_FRAMEWORKINCLUDEFIX_H diff --git a/lldb/test/Shell/RPC/Scripts/TestVersionFixScript/CheckLLDBDefines.test b/lldb/test/Shell/RPC/Scripts/TestVersionFixScript/CheckLLDBDefines.test new file mode 100644 index 0000000000000..45bd62aff37ca --- /dev/null +++ b/lldb/test/Shell/RPC/Scripts/TestVersionFixScript/CheckLLDBDefines.test @@ -0,0 +1,18 @@ +// Copy lldb-rpc-defines.h from source. +# RUN: mkdir -p %t/input +# RUN: mkdir -p %t/output +# RUN: cp %p/../../../../../include/lldb/lldb-defines.h %t/input + +// Run the convert script on it, then run the framework include fix on it. The framework version fix script +// expects that all lldb references have been renamed to lldb-rpc in order for it to modify the includes +// to go into the framework. +# RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %t/input/lldb-defines.h %t/output/lldb-rpc-defines.h +# RUN: %python %p/../../../../../scripts/framework-header-version-fix.py %t/output/lldb-rpc-defines.h %t/output/lldb-rpc-defines.h 17 0 0 + +// Check the output +# RUN: cat %t/output/lldb-rpc-defines.h | FileCheck %s + +// The LLDB version defines must be uncommented and filled in with the values passed into the script. +# CHECK: #define LLDB_RPC_VERSION 17 +# CHECK: #define LLDB_RPC_REVISION 0 +# CHECK: #define LLDB_RPC_VERSION_STRING "17.0.0" _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits