- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[lldb][RPC] Upstream LLDB to RPC converstion Python script #138028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            chelcassanova
  merged 1 commit into
  llvm:main
from
chelcassanova:upstream-lldb-rpc/add-python-scripts-for-headers
  
      
      
   
  Jun 11, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #!/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 | ||
| 
     | 
||
| 
     | 
||
| INCLUDES_TO_REMOVE_REGEX = re.compile( | ||
| r'#include "lldb/lldb-forward.h"|#include "lldb/lldb-versioning.h"' | ||
| ) | ||
| LLDB_GUARD_REGEX = re.compile(r"(?P<guard_type>#.+)LLDB_LLDB_\s*", re.M) | ||
| LLDB_API_GUARD_REGEX = re.compile(r"(?P<guard_type>#.+)LLDB_API_\s*", re.M) | ||
| LLDB_VERSION_REGEX = re.compile(r"#define LLDB_VERSION", re.M) | ||
| LLDB_REVISION_REGEX = re.compile(r"#define LLDB_REVISION", re.M) | ||
| LLDB_VERSION_STRING_REGEX = re.compile(r"#define LLDB_VERSION_STRING", re.M) | ||
| LLDB_LOCAL_INCLUDE_REGEX = re.compile(r'#include "lldb/lldb-\s*', re.M) | ||
| LLDB_NAMESPACE_DEFINITION_REGEX = re.compile( | ||
| r"(?P<comment_marker>//\s*){,1}namespace lldb\s{1}", re.M | ||
| ) | ||
| LLDB_NAMESPACE_REGEX = re.compile(r"\s*.+lldb::\s*", re.M) | ||
| 
     | 
||
| 
     | 
||
| 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() | ||
| file_buffer = "".join(lines) | ||
| 
     | 
||
| with open(output_path, "w") as output_file: | ||
| # NOTE: We do not use lldb-forward.h or lldb-versioning.h in RPC, so remove | ||
| # all includes that are found for these files. | ||
| file_buffer = re.sub(INCLUDES_TO_REMOVE_REGEX, r"", file_buffer) | ||
| 
     | 
||
| # For lldb-rpc-defines.h, replace the ifndef LLDB_LLDB_ portion with LLDB_RPC_ as we're not | ||
| # using LLDB private definitions in RPC. | ||
| lldb_guard_matches = LLDB_GUARD_REGEX.finditer(file_buffer) | ||
| for match in lldb_guard_matches: | ||
| file_buffer = re.sub( | ||
| match.group(), | ||
| r"{0}LLDB_RPC_".format(match.group("guard_type")), | ||
| file_buffer, | ||
| ) | ||
| 
     | 
||
| # Similarly to lldb-rpc-defines.h, replace the ifndef for LLDB_API in SBDefines.h to LLDB_RPC_API_ for the same reason. | ||
| lldb_api_guard_matches = LLDB_API_GUARD_REGEX.finditer(file_buffer) | ||
| for match in lldb_api_guard_matches: | ||
| file_buffer = re.sub( | ||
| match.group(), | ||
| r"{0}LLDB_RPC_API_".format(match.group("guard_type")), | ||
| file_buffer, | ||
| ) | ||
| 
     | 
||
| # Replace the references for the macros that define the versioning strings in | ||
| # lldb-rpc-defines.h. | ||
| # NOTE: Here we assume that the versioning info has already been uncommented and | ||
| # populated from the original lldb-defines.h. | ||
| file_buffer = re.sub( | ||
| LLDB_VERSION_REGEX, r"#define LLDB_RPC_VERSION", file_buffer | ||
| ) | ||
| file_buffer = re.sub( | ||
| LLDB_REVISION_REGEX, r"#define LLDB_RPC_REVISION", file_buffer | ||
| ) | ||
| file_buffer = re.sub( | ||
| LLDB_VERSION_STRING_REGEX, r"#define LLDB_RPC_VERSION_STRING", file_buffer | ||
| ) | ||
| 
     | 
||
| # For local #includes | ||
| file_buffer = re.sub( | ||
| LLDB_LOCAL_INCLUDE_REGEX, r'#include "lldb-rpc-', file_buffer | ||
| ) | ||
| 
     | 
||
| # Rename the lldb namespace definition to lldb-rpc. | ||
| lldb_rpc_namespace_definition_matches = ( | ||
| LLDB_NAMESPACE_DEFINITION_REGEX.finditer(file_buffer) | ||
| ) | ||
| for match in lldb_rpc_namespace_definition_matches: | ||
| comment_marker = ( | ||
| match.group("comment_marker") if match.group("comment_marker") else "" | ||
| ) | ||
| file_buffer = re.sub( | ||
| match.group(), | ||
| r"{0}namespace lldb_rpc ".format(comment_marker), | ||
| file_buffer, | ||
| ) | ||
| 
     | 
||
| # Rename the lldb namespace definition to lldb-rpc. | ||
| file_buffer = re.sub(LLDB_NAMESPACE_REGEX, r"lldb_rpc::", file_buffer) | ||
| 
     | 
||
| output_file.write(file_buffer) | ||
| 
     | 
||
| 
     | 
||
| if __name__ == "__main__": | ||
| main() | ||
        
          
  
    
      
          
            22 changes: 22 additions & 0 deletions
          
          22 
        
  lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBDefines.test
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| RUN: mkdir -p %t/Outputs | ||
| 
     | 
||
| # Run the convert script on lldb-defines.h. | ||
| RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/lldb-defines.h %t/Outputs/lldb-rpc-defines.h | ||
| 
     | 
||
| # Check the output | ||
| RUN: cat %t/Outputs/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 version info must be changed from LLDB_VERSION to LLDB_RPC_VERSION | ||
| CHECK: #define LLDB_RPC_VERSION 21 | ||
| CHECK: #define LLDB_RPC_REVISION 12 | ||
| CHECK: #define LLDB_RPC_VERSION_STRING "21.0.12" | ||
| 
     | 
||
| # The comment that closes the include guard should match the guard. | ||
| CHECK: #endif // LLDB_RPC_DEFINES_H | 
        
          
  
    
      
          
            17 changes: 17 additions & 0 deletions
          
          17 
        
  lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBEnumerations.test
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| RUN: mkdir -p %t/Outputs | ||
| 
     | 
||
| # Run the convert script on lldb-enumerations.h. | ||
| RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/lldb-enumerations.h %t/Outputs/lldb-rpc-enumerations.h | ||
| 
     | 
||
| # Check the output | ||
| RUN: cat %t/Outputs/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. Also, the comment that closes the namespace should match the namespace. | ||
| CHECK: namespace lldb_rpc {} // namespace lldb_rpc | ||
| 
     | 
||
| # The comment that closes the include guard should match the guard. | ||
| CHECK: #endif // LLDB_RPC_ENUMERATIONS_H | 
        
          
  
    
      
          
            24 changes: 24 additions & 0 deletions
          
          24 
        
  lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckLLDBTypes.test
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| RUN: mkdir -p %t/Outputs | ||
| 
     | 
||
| # Run the convert script on lldb-types.h. | ||
| RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/lldb-types.h %t/Outputs/lldb-rpc-types.h | ||
| 
     | 
||
| # Check the output | ||
| RUN: cat %t/Outputs/lldb-rpc-types.h | FileCheck %s | ||
| 
     | 
||
| # The include guards must change from LLDB_LLDB_TYPES_H to LLDB_RPC_TYPES_H. | ||
| CHECK: #ifndef LLDB_RPC_TYPES_H | ||
| CHECK: #define LLDB_RPC_TYPES_H | ||
| 
     | 
||
| # Includes of other lldb headers must begin with "lldb-rpc-". | ||
| # Also, the includes for lldb-forward.h should be removed. | ||
| 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 | 
        
          
  
    
      
          
            22 changes: 22 additions & 0 deletions
          
          22 
        
  lldb/test/Shell/RPC/Scripts/TestConvertScript/CheckSBDefines.test
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| RUN: mkdir -p %t/Outputs | ||
| 
     | 
||
| # Run the convert script on SBDefines.h. | ||
| RUN: %python %p/../../../../../scripts/convert-lldb-header-to-rpc-header.py %p/Inputs/SBDefines.h %t/Outputs/SBDefines.h | ||
| 
     | 
||
| # Check the output | ||
| RUN: cat %t/Outputs/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-". | ||
| # Also, the includes for lldb-forward.h and lldb-versioning.h should be removed. | ||
| CHECK: #include "lldb-rpc-defines.h" | ||
| CHECK-NOT: #include "lldb-rpc-forward.h" | ||
| CHECK: #include "lldb-rpc-enumerations.h" | ||
| CHECK: #include "lldb-rpc-types.h" | ||
| CHECK-NOT: #include "lldb-rpc-versioning.h" | ||
| 
     | 
||
| # The comment that closes the include guard should match the guard. | ||
| CHECK: #endif // LLDB_RPC_API_SBDEFINES_H | 
        
          
  
    
      
          
            22 changes: 22 additions & 0 deletions
          
          22 
        
  lldb/test/Shell/RPC/Scripts/TestConvertScript/Inputs/SBDefines.h
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // This is a truncated version of SBDefines.h used to test that the script | ||
| // convert-lldb-header-to-rpc-header.py works correctly. The script changes LLDB references in | ||
| // the original file to RPC references. | ||
| 
     | 
||
| // The include guard should change from LLDB_LLDB to LLDB_RPC. | ||
| // LLDB_API_SBDEFINES_H -> LLDB_RPC_SBDEFINES_H | ||
| #ifndef LLDB_API_SBDEFINES_H | ||
| #define LLDB_API_SBDEFINES_H | ||
| 
     | 
||
| // Includes of public main LLDB headers should change to their RPC equivalents: | ||
| // "lldb/lldb-defines.h" -> "lldb-rpc-defines.h" | ||
| // Also, the includes for lldb-forward.h and lldb-versioning.h should be removed. | ||
| #include "lldb/lldb-defines.h" | ||
| #include "lldb/lldb-enumerations.h" | ||
| #include "lldb/lldb-forward.h" | ||
| #include "lldb/lldb-types.h" | ||
| #include "lldb/lldb-versioning.h" | ||
| 
     | 
||
| // The comment that closes the include guard must change in the same way | ||
| // the original guard did. | ||
| // #endif // LLDB_API_SBDEFINES_H -> #endif // LLDB_RPC_API_SBDEFINES_H | ||
| #endif // LLDB_API_SBDEFINES_H | 
        
          
  
    
      
          
            23 changes: 23 additions & 0 deletions
          
          23 
        
  lldb/test/Shell/RPC/Scripts/TestConvertScript/Inputs/lldb-defines.h
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // This is a truncated version of lldb-defines.h used to test that the script | ||
| // convert-lldb-header-to-rpc-header.py works correctly. The script changes LLDB references in | ||
| // the original file to RPC references. | ||
| 
     | 
||
| // The include guard should change from LLDB_LLDB to LLDB_RPC. | ||
| // LLDB_LLDB_DEFINES_H -> LLDB_RPC_DEFINES_H | ||
| #ifndef LLDB_LLDB_DEFINES_H | ||
| #define LLDB_LLDB_DEFINES_H | ||
| 
     | 
||
| // Includes of public main LLDB headers should change to their RPC equivalents: | ||
| // "lldb/lldb-types.h" -> "lldb-rpc-types.h" | ||
| #include "lldb/lldb-types.h" | ||
| 
     | 
||
| // The LLDB version must change from LLDB to LLDB_RPC | ||
| // LLDB_VERSION -> LLDB_RPC_VERSION | ||
| #define LLDB_VERSION 21 | ||
| #define LLDB_REVISION 12 | ||
| #define LLDB_VERSION_STRING "21.0.12" | ||
| 
     | 
||
| // The comment that closes the include guard must change in the same way | ||
| // the original guard did. | ||
| // #endif // LLDB_LLDB_DEFINES_H -> #endif // LLDB_RPC_DEFINES_H | ||
| #endif // LLDB_LLDB_DEFINES_H | 
        
          
  
    
      
          
            17 changes: 17 additions & 0 deletions
          
          17 
        
  lldb/test/Shell/RPC/Scripts/TestConvertScript/Inputs/lldb-enumerations.h
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // This is a truncated version of lldb-enumerations.h used to test that the script | ||
| // convert-lldb-header-to-rpc-header.py works correctly. The script changes LLDB references in | ||
| // the original file to RPC references. | ||
| 
     | 
||
| // The include guard should change from LLDB_LLDB to LLDB_RPC. | ||
| // LLDB_LLDB_ENUMERATIONS_H -> LLDB_RPC_ENUMERATIONS_H | ||
| #ifndef LLDB_LLDB_ENUMERATIONS_H | ||
| #define LLDB_LLDB_ENUMERATIONS_H | ||
| 
     | 
||
| // The namespace definition should change to the lldb_rpc namespace, so should the comment that closes it: | ||
| // namespace lldb -> namespace lldb_rpc | ||
| namespace lldb {} // namespace lldb | ||
| 
     | 
||
| // The comment that closes the include guard must change in the same way | ||
| // the original guard did: | ||
| // #endif // LLDB_LLDB_ENUMERATIONS_H -> #endif // LLDB_RPC_ENUMERATIONS_H | ||
| #endif // LLDB_LLDB_ENUMERATIONS_H | 
        
          
  
    
      
          
            23 changes: 23 additions & 0 deletions
          
          23 
        
  lldb/test/Shell/RPC/Scripts/TestConvertScript/Inputs/lldb-types.h
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // This is a truncated version of lldb-types.h used to test that the script | ||
| // convert-lldb-header-to-rpc-header.py works correctly. The script changes LLDB references in | ||
| // the original file to RPC references. | ||
| 
     | 
||
| // The include guard should change from LLDB_LLDB to LLDB_RPC. | ||
| // LLDB_LLDB_TYPES_H -> LLDB_RPC_TYPES_H | ||
| #ifndef LLDB_LLDB_TYPES_H | ||
| #define LLDB_LLDB_TYPES_H | ||
| 
     | 
||
| // Includes of public main LLDB headers should change to their RPC equivalents: | ||
| // "lldb/lldb-defines.h" -> "lldb-rpc-defines.h": | ||
| // Also, the includes for lldb-forward.h should be removed. | ||
| #include "lldb/lldb-enumerations.h" | ||
| #include "lldb/lldb-forward.h" | ||
| 
     | 
||
| // The namespace definition should change to the lldb_rpc namespace, so should the comment that closes it: | ||
| // namespace lldb -> namespace lldb_rpc | ||
| namespace lldb {} // namespace lldb | ||
| 
     | 
||
| // The comment that closes the include guard must change in the same way | ||
| // the original guard did: | ||
| // #endif // LLDB_LLDB_TYPES_H -> #endif // LLDB_RPC_TYPES_H | ||
| #endif // LLDB_LLDB_TYPES_H | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.