RainerKuemmerle/hdrnorm

Format C++ includes according to Google style

★ 0Forks 0RustGitHub ↗Compare

README

hdrnorm

hdrnorm banner

hdrnorm is a small Rust command-line utility that rewrites C/C++ #include directives to the preferred delimiter style.

What it does

  • Converts local includes like #include "vector" to #include <vector> when the header is a known standard header
  • Converts system-style includes like #include <foo.h> to #include "foo.h" for project or third-party headers
  • Preserves non-include lines and trailing newlines
  • Supports // hdrnorm: keep annotations to preserve a specific include exactly

Installation

Build the tool from the repository:

cargo build --release

The binary will be available at target/release/hdrnorm.

Usage

Check a file without modifying it:

cargo run -- --check src/example.cpp

Rewrite a file in place:

cargo run -- --fix src/example.cpp

Read from stdin and write formatted output to stdout:

cat src/example.cpp | cargo run -- --stdin

Example

Run hdrnorm with --fix to rewrite includes in place:

cargo run -- --fix src/example.cpp

Before

#include "vector"
#include <foo.h>
#include <third_party/foo.h> // hdrnorm: keep

After

#include <vector>
#include "foo.h"
#include <third_party/foo.h> // hdrnorm: keep

pre-commit integration

This repository provides a pre-commit hook manifest at .pre-commit-hooks.yaml. The hook is configured for language: rust, so pre-commit builds and runs the hdrnorm binary from this repository.

To use the hook from GitHub:

repos:
- repo: https://github.com/RainerKuemmerle/hdrnorm
  rev: v0.1.1
  hooks:
    - id: hdrnorm

If you want automatic fixing instead of just checking, use the hdrnorm-fix hook:

repos:
- repo: https://github.com/RainerKuemmerle/hdrnorm
  rev: v0.1.1
  hooks:
    - id: hdrnorm-fix

Use pre-commit install in the consuming repository after adding the hook configuration.

Annotation support

If a header should remain exactly as written, add // hdrnorm: keep to the include line:

#include <third_party/foo.h> // hdrnorm: keep

This annotation tells hdrnorm not to change the delimiter style for that include.

Rust project setup

This repository includes common Rust project setup files:

  • Cargo.toml and Cargo.lock for dependency management
  • .gitignore for target, editor swap files, and generated artifacts
  • rustfmt.toml for formatting configuration
  • Makefile for common commands: make build, make test, make fmt, make clippy, and make clean
  • .github/workflows/rust.yml for CI build and test on GitHub Actions
  • release.py for version bumping, git tagging, and pushing release metadata

Run the development workflow locally with:

cargo build
cargo test
cargo fmt --all
cargo clippy --all-targets --all-features -- -D warnings

Notes

  • hdrnorm does not sort includes; use clang-format or a separate tool for ordering
  • hdrnorm relies on a built-in list of common C++ standard headers and common C-style headers
  • This repository was built with assistance from GitHub Copilot.

Contributors

RainerKuemmerle

Issues