Skip to main content

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-format first, then the sorter (perfect for editor integration).

Supports class, struct, and enum class declarations, keeps end-of-line comments with their lines, and preserves indentation. Selection formatting is supported via --offset / --length (like clang-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:

  1. Install the package:

    pip install cpp-fwd-sorter
    
  2. 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 --offset and --length, expanding to full lines to keep things tidy.


Tips & extras

  • Works great in pre-commit hooks: run your normal clang-format, then pipe to cpp-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


Download files

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

Source Distribution

cpp_fwd_sorter-1.0.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

cpp_fwd_sorter-1.0.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

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

Hashes for cpp_fwd_sorter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4424fc96c24960bed2a97aa82f34ab163fb12f5a01b14c03d431c996f114ec18
MD5 a2ec9afce85b758f9632760cd49d2e71
BLAKE2b-256 178c8ddc53a1e009e52f20c266b3d180160c876c72695ad6bcad6c8a6779de87

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_fwd_sorter-1.0.0.tar.gz:

Publisher: publish.yml on FakeTruth/cpp-fwd-sorter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for cpp_fwd_sorter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1d783e377693c2992b6ce0bcb12ed1b79b2761e67e3e975f3b27b6ce7e9a29b
MD5 5ffc94eb6e0c1a0c45f8c8f33db1b749
BLAKE2b-256 f35e2027e3126809b376c04f0f3ab2c7f1154f0214f956ce567be2d2ccbbeb66

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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