Sort C++ forward declarations after clang-format
Project description
cpp-fwd-sorter
A tiny helper that alphabetically sorts C++ forward declarations in your headers/sources — and plays nicely with clang-format. Think of it as a breezy little cleanup crew for your class Foo; and struct Bar; lines.
The project ships with:
- Sorter: reads from stdin, writes to stdout; sorts contiguous forward-declaration blocks.
- Wrapper: runs
clang-formatfirst, then the sorter (perfect for editor integration).
Supports
class,struct, andenum classdeclarations, keeps end-of-line comments with their lines, and preserves indentation. Selection formatting is supported via--offset/--length(likeclang-format).
Install
pip install cpp-fwd-sorter
Pip will install platform-specific launchers for both tools so you can call them directly from your shell and from VSCode.
Quick start
Sort a file (after clang-format)
clang-format -style=file my_header.hpp | cpp-fwd-sorter > my_header.hpp.sorted
Sort in place (using a temp file)
cpp-fwd-sorter < my_header.hpp > /tmp/sorted && mv /tmp/sorted my_header.hpp
Example
Before:
#include "Foo.h"
#include "Bar.h"
class Zeta;
class Alpha;
struct Beta;
enum class Gamma;
class Widget { };
After:
#include "Foo.h"
#include "Bar.h"
class Alpha;
class Zeta;
struct Beta;
enum class Gamma;
class Widget { };
(Classes first, then structs, then enum classes — each group alphabetized.)
VSCode: use the bundled wrapper
If you want VSCode’s Format Document / Format Selection to include sorting:
-
Install the package:
pip install cpp-fwd-sorter
-
In VSCode settings, point the clang-format path to the wrapper launcher (pip created it for you). For the Microsoft C/C++ extension:
// settings.json { "C_Cpp.clang_format_path": "cpp-fwd-format", // the wrapper launcher "editor.defaultFormatter": "ms-vscode.cpptools", "editor.formatOnSave": true }If your system needs a full path, use
which cpp-fwd-format(Unix) or find it under your Python “Scripts” directory on Windows.
That’s it. VSCode will call the wrapper, which runs clang-format and then the sorter on the result.
What gets sorted (and how)
-
Detects contiguous blocks of simple forward declarations:
class Name;struct ns::Name;enum class Name;
-
Sorts by kind (class → struct → enum class), then alphabetically (case-insensitive).
-
Keeps comments on the same line as the declaration.
-
Leaves everything else untouched.
-
When VSCode calls selection formatting, we honor
--offsetand--length, expanding to full lines to keep things tidy.
Tips & extras
- Works great in pre-commit hooks: run your normal
clang-format, then pipe tocpp-fwd-sorter. - Not trying to be a parser — it’s a pragmatic formatter for common forward-decl patterns. If your codebase uses exotic declarations, open an issue and we can extend it.
Enjoy the alphabetical zen ✨
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cpp_fwd_sorter-1.0.0.tar.gz.
File metadata
- Download URL: cpp_fwd_sorter-1.0.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4424fc96c24960bed2a97aa82f34ab163fb12f5a01b14c03d431c996f114ec18
|
|
| MD5 |
a2ec9afce85b758f9632760cd49d2e71
|
|
| BLAKE2b-256 |
178c8ddc53a1e009e52f20c266b3d180160c876c72695ad6bcad6c8a6779de87
|
Provenance
The following attestation bundles were made for cpp_fwd_sorter-1.0.0.tar.gz:
Publisher:
publish.yml on FakeTruth/cpp-fwd-sorter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cpp_fwd_sorter-1.0.0.tar.gz -
Subject digest:
4424fc96c24960bed2a97aa82f34ab163fb12f5a01b14c03d431c996f114ec18 - Sigstore transparency entry: 561959985
- Sigstore integration time:
-
Permalink:
FakeTruth/cpp-fwd-sorter@9f95d741208e2eaebf887e1d1d827d78213bd5d9 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/FakeTruth
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9f95d741208e2eaebf887e1d1d827d78213bd5d9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file cpp_fwd_sorter-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cpp_fwd_sorter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1d783e377693c2992b6ce0bcb12ed1b79b2761e67e3e975f3b27b6ce7e9a29b
|
|
| MD5 |
5ffc94eb6e0c1a0c45f8c8f33db1b749
|
|
| BLAKE2b-256 |
f35e2027e3126809b376c04f0f3ab2c7f1154f0214f956ce567be2d2ccbbeb66
|
Provenance
The following attestation bundles were made for cpp_fwd_sorter-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on FakeTruth/cpp-fwd-sorter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cpp_fwd_sorter-1.0.0-py3-none-any.whl -
Subject digest:
c1d783e377693c2992b6ce0bcb12ed1b79b2761e67e3e975f3b27b6ce7e9a29b - Sigstore transparency entry: 561959991
- Sigstore integration time:
-
Permalink:
FakeTruth/cpp-fwd-sorter@9f95d741208e2eaebf887e1d1d827d78213bd5d9 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/FakeTruth
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9f95d741208e2eaebf887e1d1d827d78213bd5d9 -
Trigger Event:
push
-
Statement type: