Skip to main content

A python library for extracting C++ function implementations from source code

Project description

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.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpp_source_extractor-0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cpp_source_extractor-0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file cpp_source_extractor-0.1.tar.gz.

File metadata

  • Download URL: cpp_source_extractor-0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for cpp_source_extractor-0.1.tar.gz
Algorithm Hash digest
SHA256 3e1066a50f3384feca90136a23542cead6304328697d1a219a1380f1a512ee57
MD5 f7b3ff2633b2d4ce0235810d8e197023
BLAKE2b-256 422f60b9fe5b9978a156862ba62447531d807de1f8c7e3cfcb0a13d4729647d2

See more details on using hashes here.

File details

Details for the file cpp_source_extractor-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cpp_source_extractor-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 709cd9aa12c949084cf17f6cec7d6dba66772badeac8d4f97d04cd2696b817b7
MD5 28d2e08f675d55c04e9b476871798e5e
BLAKE2b-256 7e49ca45deee68941a963b70bfb3b2bfa3390cdf4f5ac2348fda5b8d5a018d94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page