Skip to main content

Automatic extraction of C APIs from header files using libclang

Project description

Automatic extraction of C APIs from header files using Python and clang. Exports struct, union, enum, typedefs, static variables and function definitions to a JSON file content.

Installing

c_api_extract is available on PyPI and may be installed using pip:

$ pip install c-api-extract

Usage

Using the command line interface:

$ c_api_extract <input> [-i <include_pattern>...] [options] [-- <clang_args>...]

Check out the available options with:

$ c_api_extract -h

Or using Python:

import c_api_extract

# `definitions` follow the same format as output JSON
definitions = c_api_extract.definitions_from_header('header_name.h', ['-Dsome_clang_args', ...])

c_api_extract.py works on a single header file for simplicity. If you need more than one header processed, create a new one and #include them.

Output format

Output is a list of definitions, each kind with its format:

# variable definitions
{
  'kind': 'var',
  'name': '<name>',         # variable name
  'type': {<type object>},  # type object
  # only present if you pass `--source` to c_api_extract
  'source': '<verbatim definition source code>',
}

# enum definitions
{
  'kind': 'enum',
  'name': '<name>',         # enum name, generated for anonymous enums
  'type': {<type object>},  # enum underlying type, normally `{"base": "unsigned int"}`
  'values': [               # list of declared names and values
    ['<name>', <integer value>]
    # ...
  ],
  # only present if you pass `--source` to c_api_extract
  'source': '<verbatim definition source code>',
}

# struct|union definitions
{
  'kind': 'struct' | 'union',
  'name': '<name>',          # struct|union name, generated for anonymous struct|unions
  'spelling': '<spelling>',  # Spelling that be used directly in C to refer to type
  'fields': [                # list of declared fields, empty for opaque struct|unions
    [{<type object>}, '<name>'],  # name may be "" for nested anonymous structs|unions
    ...
  ],
  # only present if you pass `--source` to c_api_extract
  'source': '<verbatim definition source code>',
}

# typedef definitions
{
  'kind': 'typedef',
  'name': '<name>',         # name of the typedef
  'type': {<type object>},  # underlying type
  # only present if you pass `--source` to c_api_extract
  'source': '<verbatim definition source code>',
}

# function definitions
{
  'kind': 'function',
  'name': '<name>',                # name of the function
  'return_type': {<type object>},  # return type
  'arguments': [                   # list of arguments
    [{<type object>}, '<name>'],
    ...
  ],
  'variadic': true,  # only present if function is variadic
  # only present if you pass `--source` to c_api_extract
  'source': '<verbatim definition source code>',
}

#########################################################
# All type objects use the following structure:
{
  'base': '<unqualified base type spelling>',
  # only present if type is a pointer type
  'pointer': ['*', ...],
  # only present if type is an array type
  'array': [<integer size>, '<"*" if incomplete array or pointer type>', ...],
  # only present if type is a function pointer type
  'function': {<type object>},
  # only present if type is a function type
  'return_type': {<type object>},
  # only present if type is a function type
  # notice that function types don't carry argument names
  'arguments': [{<type object>}, ...],
  # only present if type is a function type and function is variadic
  'variadic': true,
  # only present if base type is const qualified
  'const': true,
  # only present if base type is volatile qualified
  'volatile': true,
  # only present if base type is restrict qualified
  'restrict': true,
  # only present if you pass `--size` to c_api_extract
  'size': <integer sizeof, may be negative for "void" and incomplete arrays>,
}

TODO

  • Add support for constants defined using #define

  • Add docstrings

Project details


Download files

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

Source Distribution

c_api_extract-0.5.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

c_api_extract-0.5.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file c_api_extract-0.5.0.tar.gz.

File metadata

  • Download URL: c_api_extract-0.5.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.2 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for c_api_extract-0.5.0.tar.gz
Algorithm Hash digest
SHA256 2a4df4c3514a61a8e6ae5eea275466d01cb6609e97a5a7b8589c8c07f5968541
MD5 5d7f02b902431a0c4f52e9ca33848474
BLAKE2b-256 e4fc09e8526b820535ebed5899aacdfbf5fe7a8bbb23824d275d2a3ea14c6fca

See more details on using hashes here.

File details

Details for the file c_api_extract-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: c_api_extract-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.2 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for c_api_extract-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d28f2f4ad43db849bdf92a26d873ee54860d787a00374716573ffbefbd4067fa
MD5 e2717085d80dc1367c5fab4d0b84969c
BLAKE2b-256 fc893e66c868bec8341baab199d58de827c4e2244f635a54e6d19ce8da36cf04

See more details on using hashes here.

Supported by

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