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.

Usage

Calling from console:

$ c_api_extract.py <input> [-p <pattern>...] [-c] [-- <clang_args>...]

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', ['-Dlist_of_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 it’s format:

# variable definitions
{
  'kind': 'var',
  'name': '<name>',   # variable name
  'type': '<type>'    # type name as written in source code
  'source': '<code>'  # source C code from read header file
}

# enum definitions
{
  'kind': 'enum',
  'name': '<name>',        # enum name, empty for anonymous enums
  'typedef': '<typedef>',  # typedef name, may be empty
  'type': '<C type>',      # enum underlying C type name
  'values': [              # list of declared names and values
    ['<name>', <integer value>]
    # ...
  ],
  'source': '<code>'       # source C code from read header file
}

# struct|union definitions
{
  'kind': 'struct' | 'union',
  'name': '<name>',        # struct|union name, empty for anonymous struct|unions
  'typedef': '<typedef>',  # typedef name, may be empty
  'fields': [              # list of declared fields, empty for opaque struct|unions
    ['<type>', '<name>']
    # ...
  ],
  'source': '<code>'       # source C code from read header file
}

# typedef definitions
{
  'kind': 'typedef',
  'name': '<name>',   # name of the typedef
  'type': '<type>',   # name of the underlying type
  'source': '<code>'  # source C code from read header file
}

# function definitions
{
  'kind': 'function',
  'name': '<name>',          # name of the function
  'return_type': '<type>',   # return type name
  'arguments': [             # list of arguments
    ['<type>', '<name>']
    # ...
  ],
  'variadic': true | false,  # true if function is variadic
  'source': '<code>'         # source C code from read header file
}

TODO

  • Add support for constants defined using #define

  • Add support for nested anonymous struct|unions

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.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

c_api_extract-0.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: c_api_extract-0.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for c_api_extract-0.2.tar.gz
Algorithm Hash digest
SHA256 6a8024893e5366861e210a631734f831a76543cb0e5d792b8e32388bc49eadbc
MD5 d1503df678c50efdec1beec2a7976c53
BLAKE2b-256 04210ca5043dfbf711768895bfd5b277d19541cf3ea01ceef47971a03c43bfa1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: c_api_extract-0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for c_api_extract-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 add5ae196e4c2858194013cfcc1a80aa277a0e5d8edf7d1861da8fd419098a2b
MD5 d9b422f0a173ced8b4d339f4df75551d
BLAKE2b-256 e093c0cd0e0a13c70f8209f638b22ae29f0b2fc09ac0d886c3a2fe8110da601c

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