Skip to main content

C++ Source Extractor

C++ Source Extractor is a Python library designed to extract C++ function implementations from source code files using the Clang cindex module.

Features

  • Extract functions by name
  • Extract functions using regular expressions
  • Extract functions by line number

Installation

To use this library, ensure you have Python installed along with the clang library. You can install clang using pip:

pip install clang

Usage

Extract a Function by Name

You can extract a typical C-style function by specifying its name.

from cpp_source_extractor import extract_function_by_name

code = extract_function_by_name("main", "main.cpp")
print(code)

Extract a Function by Line Number

If you have the line number (from a debugger or stack trace) where the function is located, you can extract it using this method. This is recommended as it avoids issues like name mangling.

Note: The line number can be within the body of the function, not necessarily the line where the function is defined.

from cpp_source_extractor import extract_function_by_line_number

code = extract_function_by_line_number(10, "main.cpp")
print(code)

Add semantic information

Extracting functions wrapped in preprocessor directives can be challenging, as shown in the example below:

#ifdef __os_linux__
void foo() {
    // ...
}
#elif __os_macos__
void foo() {
    // ...
}
#endif

Here, the function foo is defined differently based on the operating system configuration. To ensure you extract the correct version of the function, you have two options:

  1. Use Compile Arguments: Specify the relevant macro definitions as compile arguments (e.g., -D__os_linux__ or -D__os_macos__) when extracting functions:
code = extract_function_by_name("foo", "main.cpp", extra_args=["-D__os_linux__"])
print(code)
  1. Use a Compilation Database: Provide the path to a JSON Compilation Database, which contains the necessary build information:
code = extract_function_by_name("foo", "main.cpp", database="/path/to/compile_commands.json")
print(code)

You can generate a compilation database using CMake:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .

Or, for Make-based projects, you can use bear to generate the compilation database.

Contributing

Contributions are welcome! If you have suggestions or improvements, feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for cpp-source-extractor 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cpp-source-extractor 0.1
File Size Uploaded
cpp_source_extractor-0.1.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cpp-source-extractor 0.1
File Interpreter ABI Platform
cpp_source_extractor-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / cpp_source_extractor-0.1.tar.gz

Download URL cpp_source_extractor-0.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3e1066a50f3384feca90136a23542cead6304328697d1a219a1380f1a512ee57
BLAKE2b-256 checksum
How to use checksums
422f60b9fe5b9978a156862ba62447531d807de1f8c7e3cfcb0a13d4729647d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / cpp_source_extractor-0.1-py3-none-any.whl

Download URL cpp_source_extractor-0.1-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
709cd9aa12c949084cf17f6cec7d6dba66772badeac8d4f97d04cd2696b817b7
BLAKE2b-256 checksum
How to use checksums
7e49ca45deee68941a963b70bfb3b2bfa3390cdf4f5ac2348fda5b8d5a018d94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page