Adapter for clang-format for use as a formatting plugin in dprint. It formats C, C++, and Objective-C.
Then in your project's directory with a dprint.json file, run:
dprint add clang-formatNote: You do not need clang-format installed globally as dprint will run clang-format from the .wasm file in a sandboxed environment.
The plugin formats files with these extensions: .c, .cc, .cpp, .cxx,
.h, .hh, .hpp, .hxx, .m, .mm.
To add configuration, specify a "clangFormat" key in your dprint.json. It
takes
clang-format style options
with their clang-format names, represented as JSON:
dprint global options are also mapped when the matching clang-format option is not set explicitly:
| dprint option | clang-format option |
|---|---|
lineWidth |
ColumnLimit |
indentWidth |
IndentWidth |
useTabs |
UseTab |
clangFormat wins over the global option when both are present.
Note: The plugin does not understand clang-format's .clang-format files
because it runs sandboxed in a Wasm runtime—it has no access to the file system
in order to read them. For the same reason, BasedOnStyle: "file" and
BasedOnStyle: "InheritParentConfig" are rejected. Put the style in your dprint
configuration instead.
Files whose nested #if/#ifdef/#elif branches produce more than 16 branch
combinations may format differently from native clang-format in inactive or
platform-specific branches.
The plugin links Clang LibFormat into a dprint plugin ABI v4 WebAssembly module.
It is built for wasm32-unknown-unknown with no Wasm imports.
We patch LLVM to disable code paths that do not make sense in a sandboxed dprint plugin, such as real filesystem probing, process/environment helpers, crash/signal handling, and other cold-path host facilities.
The plugin appears to be about 30% faster than native clang-format. Try it
yourself with mise run bench.
Benchmarking stdin formatting over 250 LLVM/Clang source files.
Set BENCH_FILE_LIMIT=all for the full LLVM tree, or set BENCH_RUNS/BENCH_WARMUP to adjust rounds.
Benchmark 1: native clang-format
Time (mean ± σ): 6.469 s ± 0.095 s [User: 4.691 s, System: 1.502 s]
Range (min … max): 6.385 s … 6.573 s 3 runs
Benchmark 2: dprint wasm plugin
Time (mean ± σ): 4.969 s ± 0.094 s [User: 2.986 s, System: 1.674 s]
Range (min … max): 4.913 s … 5.078 s 3 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
Summary
dprint wasm plugin ran
1.30 ± 0.03 times faster than native clang-formatTested on MacBook Pro 14" with an M5 Pro.
The build uses mise for the host tools and pixi for the conda-forge clang, lld, and clang-format. It runs on Linux and macOS.
mise installinstalls the pinned host tools.mise run builddownloads and patches the pinned LLVM source, then buildsbuild/plugin.wasm.mise run testbuilds, then runs the Bats tests intests/, which compare the plugin's output with native clang-format.
- JS Formatter - Browser/Deno and Node
- npm package
This repo automatically upgrades to the latest version of clang-format (LLVM)
once a day. You can check which version of clang-format is being used by looking
at llvm_version in the scripts/fetch_llvm.sh file in this repo:
https://github.com/dprint/dprint-plugin-clang-format/blob/main/scripts/fetch_llvm.sh
The version of this plugin does not reflect the version of clang-format.
{ "clangFormat": { "BasedOnStyle": "LLVM", "ColumnLimit": 100, "IndentWidth": 2, "SpaceBeforeParens": "Custom", "SpaceBeforeParensOptions": { "AfterControlStatements": false, "AfterFunctionDeclarationName": true, "AfterFunctionDefinitionName": true, }, }, "plugins": [ // ...etc... ], }