Skip to main content

clang-format wrapper for SWIG .i files

Project description

clang-format-swig

clang-format for SWIG .i interface files.

Why

SWIG .i files are mostly C/C++ with a handful of SWIG-specific directives sprinkled in (lines that start with %):

%module mylib

%{
#include "mylib.h"
%}

%include "typemaps.i"

%typemap(in) MyStruct * {
    if (!convert($input, &$1)) return NULL;
}

Running clang-format directly on these files produces broken output because:

  • %{ and %} contain braces that shift clang-format's tracked brace depth, causing everything after them to be indented as if inside a block.
  • %module is parsed as a C++20 module declaration, altering scope state for all subsequent code.

The C/C++ content (the bulk of every .i file) ends up incorrectly indented or not formatted at all.

How it works

Before passing the file to clang-format, each %-prefixed line is swapped for a #pragma placeholder:

Original Placeholder
%module my_lib #pragma SWIG_3F9A12_0
%{ #pragma SWIG_3F9A12_1
%} #pragma SWIG_3F9A12_2

#pragma lines are preserved verbatim by clang-format and have no effect on brace depth or scope, so all surrounding C/C++ is formatted correctly. Afterwards the placeholders are replaced back with the original % lines.

Your .clang-format config is fully respected: clang-format-swig is a thin wrapper, not a reimplementation. It is written in Go and compiles to a single static binary, so it can ship through any package registry without requiring a runtime or build toolchain on the target system.

Installation

Pick whichever channel fits your stack, every install resolves to the same Go binary.
If there's no distribution available for your platform architecture, you can open a feature request.

clang-format must be installed separately and be on PATH. Install it however you'd normally get LLVM tooling on your platform (e.g. apt install clang-format, brew install clang-format, winget install LLVM.LLVM, uv tool install clang-format, npm install -g clang-format, ...).

Go

go install github.com/Avasam/clang-format-swig@latest

Requires Go (see go.mod for the minimum version).

pre-commit

Add to your .pre-commit-config.yaml:

# Requires having clang-format pre-installed and available on PATH
# Has go installation and build overhead
- repo: https://github.com/Avasam/clang-format-swig
  rev: vX.X.X
  hooks:
    - id: clang-format-swig

or

# Has Python installation overhead
- repo: local
  hooks:
    - id: clang-format-swig
      name: clang-format SWIG files
      language: python
      entry: clang-format-swig
      files: \.i$
      additional_dependencies:
        - clang-format-swig==X.X.X
        - clang-format==X.X.X

Python

uv tool install clang-format-swig

The platform binary is bundled in the wheel.

Node.js

npm install -g clang-format-swig

The platform binary is fetched as an optional dependency, so no install-time scripts run.

.NET

dotnet tool install -g clang-format-swig

See TargetFramework in clang-format-swig.csproj for the minimum .NET runtime. The platform binary is embedded in the package and extracted to your user cache on first run.

Usage

# Format files in-place
clang-format-swig src/mylibrary.i

# Format multiple files
clang-format-swig **/*.i

# Check without writing (useful in CI)
clang-format-swig --check **/*.i

# Print version
clang-format-swig --version

clang-format-swig exits 0 if no files were changed (or --check found nothing to change), and 1 if any file was reformatted (or would be reformatted under --check).

clang-format itself must be on PATH; the wrapper does not vendor it.

pre-commit usage

Once the hook is configured, it runs automatically on staged .i files:

pre-commit run clang-format-swig --all-files

LLM / Coding Agent disclaimer: This project was initially scaffolded with Claude (Sonnet 4.6 and Opus 4.7) from a Python proof of concept script. The initial project layout and Go script were machine-generated. Every change is reviewed by a human before being merged.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

clang_format_swig-0.0.1-py3-none-win_arm64.whl (729.4 kB view details)

Uploaded Python 3Windows ARM64

clang_format_swig-0.0.1-py3-none-win_amd64.whl (789.7 kB view details)

Uploaded Python 3Windows x86-64

clang_format_swig-0.0.1-py3-none-manylinux_2_17_x86_64.whl (738.4 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

clang_format_swig-0.0.1-py3-none-manylinux_2_17_aarch64.whl (685.4 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

clang_format_swig-0.0.1-py3-none-macosx_11_0_arm64.whl (756.8 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

clang_format_swig-0.0.1-py3-none-macosx_10_9_x86_64.whl (762.2 kB view details)

Uploaded Python 3macOS 10.9+ x86-64

File details

Details for the file clang_format_swig-0.0.1-py3-none-win_arm64.whl.

File metadata

  • Download URL: clang_format_swig-0.0.1-py3-none-win_arm64.whl
  • Upload date:
  • Size: 729.4 kB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for clang_format_swig-0.0.1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 1164f1a56d3546240fa6da47dd8be1499888ef2bc663a63b53735b63f259a6fc
MD5 bda6506b0811638a00de58a89ae9a6d0
BLAKE2b-256 a864e52189ccfe4dcc5a289c2cb7e4137349f7e2e5a87605e2ad4be59c19360e

See more details on using hashes here.

File details

Details for the file clang_format_swig-0.0.1-py3-none-win_amd64.whl.

File metadata

  • Download URL: clang_format_swig-0.0.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 789.7 kB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for clang_format_swig-0.0.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 7f610f8a6f1a2e6c671176fff32ef90609c08ef13b26cab61d77e0b95fdaf815
MD5 64a94bb95574d6676c33aeae7f4bc5ef
BLAKE2b-256 0619e47cc49d2a889568406b1c5ce8aa9ca77da18eaa63c60fbe87da3f60f5c1

See more details on using hashes here.

File details

Details for the file clang_format_swig-0.0.1-py3-none-manylinux_2_17_x86_64.whl.

File metadata

  • Download URL: clang_format_swig-0.0.1-py3-none-manylinux_2_17_x86_64.whl
  • Upload date:
  • Size: 738.4 kB
  • Tags: Python 3, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for clang_format_swig-0.0.1-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 417e52ebbfdb7b85b841b09c4b839abc039aab0955e7f0fa5b9bb430751f8a18
MD5 cb879ed2767191f2438f6ec1f34fb19d
BLAKE2b-256 5d927e6a08a1cf13cee0b7a887bb77c1041881bed91ce530f26a982dc1b825cd

See more details on using hashes here.

File details

Details for the file clang_format_swig-0.0.1-py3-none-manylinux_2_17_aarch64.whl.

File metadata

  • Download URL: clang_format_swig-0.0.1-py3-none-manylinux_2_17_aarch64.whl
  • Upload date:
  • Size: 685.4 kB
  • Tags: Python 3, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for clang_format_swig-0.0.1-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 55473db1605590c8fd36b1e45867634a7a5e718b5ba6a5a06fd60ca21c739ba9
MD5 7c22f3a3c82adf2f238aac46d81e765b
BLAKE2b-256 7890f6e9608da6d479d3806547cb61872e025d1b0d74d0edcee21ee1a5360e52

See more details on using hashes here.

File details

Details for the file clang_format_swig-0.0.1-py3-none-manylinux2014_x86_64.whl.

File metadata

  • Download URL: clang_format_swig-0.0.1-py3-none-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 738.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for clang_format_swig-0.0.1-py3-none-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7a41ef1c248a01760d37ad8a1606831e5a82ccc4d603b9a12e7316f71bac64cc
MD5 001bafaee7992f2e41bc11707284239a
BLAKE2b-256 5878e45eb2adf59b8d68b33d1a6ff6fa1ced4e381c73fdef13c911adb0eaa52a

See more details on using hashes here.

File details

Details for the file clang_format_swig-0.0.1-py3-none-manylinux2014_aarch64.whl.

File metadata

  • Download URL: clang_format_swig-0.0.1-py3-none-manylinux2014_aarch64.whl
  • Upload date:
  • Size: 685.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for clang_format_swig-0.0.1-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7a288c748ea6063f277d1ceb27b2dfe9e9834ab9f6bccc26fe93bec2f4e89e7e
MD5 63f25dcdd1ff77aa1591635ad0ae5698
BLAKE2b-256 004c2ced83c9709a3f54a0f280c7bce41172b82ab623f6aa2c9e74f9c53a13e8

See more details on using hashes here.

File details

Details for the file clang_format_swig-0.0.1-py3-none-macosx_11_0_arm64.whl.

File metadata

  • Download URL: clang_format_swig-0.0.1-py3-none-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 756.8 kB
  • Tags: Python 3, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for clang_format_swig-0.0.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8df11b77c252b581803c61fc75df2faff6eb22707d2e0e37cffe05cdc95bc5bb
MD5 d5e9f31f3d8d010543f7c1d8736f7b06
BLAKE2b-256 6655854c997102e0af009e5986a5f937e33b5193add525080da9c2668275e07b

See more details on using hashes here.

File details

Details for the file clang_format_swig-0.0.1-py3-none-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: clang_format_swig-0.0.1-py3-none-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 762.2 kB
  • Tags: Python 3, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for clang_format_swig-0.0.1-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fc43898e37c395951a6c58154958b99075de3430630528f4fbfd999ea8c8f4d4
MD5 5c762798c6b217fba535359559305b7d
BLAKE2b-256 06f58a56e175b697026adc6e4511291abcbd635776a1c682fc6cd70b622f9985

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