# cpp-linter-hooks
[](https://pypi.org/project/cpp-linter-hooks/)
[](https://pypi.org/project/cpp-linter-hooks/)
[](https://codecov.io/gh/cpp-linter/cpp-linter-hooks)
[](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/test.yml)
[](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/codeql.yml)
A pre-commit hook that automatically formats and lints your C/C++ code using `clang-format` and `clang-tidy`.
## Table of Contents
- [Quick Start](#quick-start)
- [Custom Configuration Files](#custom-configuration-files)
- [Custom Clang Tool Version](#custom-clang-tool-version)
- [Output](#output)
- [clang-format Output](#clang-format-output)
- [clang-tidy Output](#clang-tidy-output)
- [Troubleshooting](#troubleshooting)
- [Performance Optimization](#performance-optimization)
- [Verbose Output](#verbose-output)
- [FAQ](#faq)
- [What's the difference between `cpp-linter-hooks` and `mirrors-clang-format`?](#whats-the-difference-between-cpp-linter-hooks-and-mirrors-clang-format)
- [Contributing](#contributing)
- [License](#license)
## Quick Start
Add this configuration to your `.pre-commit-config.yaml` file:
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.3 # Use the tag or commit you want
hooks:
- id: clang-format
args: [--style=Google] # Other coding style: LLVM, GNU, Chromium, Microsoft, Mozilla, WebKit.
- id: clang-tidy
args: [--checks='boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-*,cppcoreguidelines-*']
```
### Custom Configuration Files
To use custom configurations like `.clang-format` and `.clang-tidy`:
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.3
hooks:
- id: clang-format
args: [--style=file] # Loads style from .clang-format file
- id: clang-tidy
args: [--checks=.clang-tidy] # Loads checks from .clang-tidy file
```
> [!TIP]
> By default, the latest version of [`clang-format`](https://pypi.org/project/clang-format/#history) and [`clang-tidy`](https://pypi.org/project/clang-tidy/#history) will be installed if not specified. You can specify the version using the `--version` argument in the `args` list as shown below.
### Custom Clang Tool Version
To use specific versions of clang-format and clang-tidy (using Python wheel packages):
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.3
hooks:
- id: clang-format
args: [--style=file, --version=21] # Specifies version
- id: clang-tidy
args: [--checks=.clang-tidy, --version=21] # Specifies version
```
## Output
### clang-format Output
```bash
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
```
Here’s a sample diff showing the formatting applied:
```diff
--- a/testing/main.c
+++ b/testing/main.c
@@ -1,3 +1,6 @@
#include <stdio.h>
-int main() {for (;;) break; printf("Hello world!\n");return 0;}
-
+int main() {
+ for (;;) break;
+ printf("Hello world!\n");
+ return 0;
+}
```
> [!NOTE]
> Use `--dry-run` in `args` of `clang-format` to print instead of changing the format, e.g.:
```bash
clang-format.............................................................Failed
- hook id: clang-format
- exit code: 255
main.c:2:11: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {for (;;) break; printf("Hello world!\n");return 0;}
^
main.c:2:13: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {for (;;) break; printf("Hello world!\n");return 0;}
^
main.c:2:21: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {for (;;) break; printf("Hello world!\n");return 0;}
^
main.c:2:28: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {for (;;) break; printf("Hello world!\n");return 0;}
^
main.c:2:54: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {for (;;) break; printf("Hello world!\n");return 0;}
^
main.c:2:63: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {for (;;) break; printf("Hello world!\n");return 0;}
^
```
### clang-tidy Output
```bash
clang-tidy...............................................................Failed
- hook id: clang-tidy
- exit code: 1
522 warnings generated.
Suppressed 521 warnings (521 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
/home/runner/work/cpp-linter-hooks/cpp-linter-hooks/testing/main.c:4:13: warning: statement should be inside braces [readability-braces-around-statements]
for (;;)
^
{
```
## Troubleshooting
### Performance Optimization
> [!TIP]
> For large codebases, if your `pre-commit` runs longer than expected, it is highly recommended to add `files` in `.pre-commit-config.yaml` to limit the scope of the hook. This helps improve performance by reducing the number of files being checked and avoids unnecessary processing. Here's an example configuration:
```yaml
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.3
hooks:
- id: clang-format
args: [--style=file, --version=21]
files: ^(src|include)/.*\.(cpp|cc|cxx|h|hpp)$ # Limits to specific dirs and file types
- id: clang-tidy
args: [--checks=.clang-tidy, --version=21]
files: ^(src|include)/.*\.(cpp|cc|cxx|h|hpp)$
```
Alternatively, if you want to run the hooks manually on only the changed files, you can use the following command:
```bash
pre-commit run --files $(git diff --name-only)
```
This approach ensures that only modified files are checked, further speeding up the linting process during development.
### Verbose Output
> [!NOTE]
> Use `-v` or `--verbose` in `args` of `clang-format` to show the list of processed files e.g.:
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.3
hooks:
- id: clang-format
args: [--style=file, --version=21, --verbose] # Add -v or --verbose for detailed output
```
## FAQ
### What's the difference between [`cpp-linter-hooks`](https://github.com/cpp-linter/cpp-linter-hooks) and [`mirrors-clang-format`](https://github.com/pre-commit/mirrors-clang-format)?
| Feature | `cpp-linter-hooks` | `mirrors-clang-format` |
|----------------------------------|-------------------------------------------|----------------------------------------|
| Supports `clang-format` and `clang-tidy` | ✅ (`clang-format` & `clang-tidy`) | ✅ (`clang-format` only) |
| Loads style configuration | ✅ via `--version` | ✅ (default behavior) |
| Specify `clang-format` version | ✅ via `--version` | ✅ via `rev` |
| Supports passing code string | ✅ via `--style` | ❌ |
| Verbose output | ✅ via `--verbose` | ❌ |
<!-- > [!TIP]
> In most cases, there is no significant performance difference between `cpp-linter-hooks` and `mirrors-clang-format`. See the [benchmark results](testing/benchmark.md) for details. -->
## Contributing
We welcome contributions! Whether it's fixing issues, suggesting improvements, or submitting pull requests, your support is greatly appreciated.
## License
This project is licensed under the [MIT License](LICENSE).
Raw data
{
"_id": null,
"home_page": null,
"name": "cpp-linter-hooks",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "clang, clang-format, clang-tidy, pre-commit, pre-commit-hooks",
"author": null,
"author_email": "Xianpeng Shen <xianpeng.shen@gmail.com>",
"download_url": null,
"platform": null,
"description": "# cpp-linter-hooks\n\n[](https://pypi.org/project/cpp-linter-hooks/)\n[](https://pypi.org/project/cpp-linter-hooks/)\n[](https://codecov.io/gh/cpp-linter/cpp-linter-hooks)\n[](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/test.yml)\n[](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/codeql.yml)\n\nA pre-commit hook that automatically formats and lints your C/C++ code using `clang-format` and `clang-tidy`.\n\n## Table of Contents\n\n- [Quick Start](#quick-start)\n - [Custom Configuration Files](#custom-configuration-files)\n - [Custom Clang Tool Version](#custom-clang-tool-version)\n- [Output](#output)\n - [clang-format Output](#clang-format-output)\n - [clang-tidy Output](#clang-tidy-output)\n- [Troubleshooting](#troubleshooting)\n - [Performance Optimization](#performance-optimization)\n - [Verbose Output](#verbose-output)\n- [FAQ](#faq)\n - [What's the difference between `cpp-linter-hooks` and `mirrors-clang-format`?](#whats-the-difference-between-cpp-linter-hooks-and-mirrors-clang-format)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Quick Start\n\nAdd this configuration to your `.pre-commit-config.yaml` file:\n\n```yaml\nrepos:\n - repo: https://github.com/cpp-linter/cpp-linter-hooks\n rev: v1.1.3 # Use the tag or commit you want\n hooks:\n - id: clang-format\n args: [--style=Google] # Other coding style: LLVM, GNU, Chromium, Microsoft, Mozilla, WebKit.\n - id: clang-tidy\n args: [--checks='boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-*,cppcoreguidelines-*']\n```\n\n### Custom Configuration Files\n\nTo use custom configurations like `.clang-format` and `.clang-tidy`:\n\n```yaml\nrepos:\n - repo: https://github.com/cpp-linter/cpp-linter-hooks\n rev: v1.1.3\n hooks:\n - id: clang-format\n args: [--style=file] # Loads style from .clang-format file\n - id: clang-tidy\n args: [--checks=.clang-tidy] # Loads checks from .clang-tidy file\n```\n\n> [!TIP]\n> By default, the latest version of [`clang-format`](https://pypi.org/project/clang-format/#history) and [`clang-tidy`](https://pypi.org/project/clang-tidy/#history) will be installed if not specified. You can specify the version using the `--version` argument in the `args` list as shown below.\n\n### Custom Clang Tool Version\n\nTo use specific versions of clang-format and clang-tidy (using Python wheel packages):\n\n```yaml\nrepos:\n - repo: https://github.com/cpp-linter/cpp-linter-hooks\n rev: v1.1.3\n hooks:\n - id: clang-format\n args: [--style=file, --version=21] # Specifies version\n - id: clang-tidy\n args: [--checks=.clang-tidy, --version=21] # Specifies version\n```\n\n## Output\n\n### clang-format Output\n\n```bash\nclang-format.............................................................Failed\n- hook id: clang-format\n- files were modified by this hook\n```\n\nHere\u2019s a sample diff showing the formatting applied:\n\n```diff\n--- a/testing/main.c\n+++ b/testing/main.c\n@@ -1,3 +1,6 @@\n #include <stdio.h>\n-int main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n-\n+int main() {\n+ for (;;) break;\n+ printf(\"Hello world!\\n\");\n+ return 0;\n+}\n```\n> [!NOTE]\n> Use `--dry-run` in `args` of `clang-format` to print instead of changing the format, e.g.:\n\n```bash\nclang-format.............................................................Failed\n- hook id: clang-format\n- exit code: 255\n\nmain.c:2:11: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n ^\nmain.c:2:13: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n ^\nmain.c:2:21: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n ^\nmain.c:2:28: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n ^\nmain.c:2:54: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n ^\nmain.c:2:63: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n ^\n```\n\n### clang-tidy Output\n\n```bash\nclang-tidy...............................................................Failed\n- hook id: clang-tidy\n- exit code: 1\n\n522 warnings generated.\nSuppressed 521 warnings (521 in non-user code).\nUse -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.\n/home/runner/work/cpp-linter-hooks/cpp-linter-hooks/testing/main.c:4:13: warning: statement should be inside braces [readability-braces-around-statements]\n for (;;)\n ^\n {\n\n```\n\n## Troubleshooting\n\n### Performance Optimization\n\n> [!TIP]\n> For large codebases, if your `pre-commit` runs longer than expected, it is highly recommended to add `files` in `.pre-commit-config.yaml` to limit the scope of the hook. This helps improve performance by reducing the number of files being checked and avoids unnecessary processing. Here's an example configuration:\n\n```yaml\n- repo: https://github.com/cpp-linter/cpp-linter-hooks\n rev: v1.1.3\n hooks:\n - id: clang-format\n args: [--style=file, --version=21]\n files: ^(src|include)/.*\\.(cpp|cc|cxx|h|hpp)$ # Limits to specific dirs and file types\n - id: clang-tidy\n args: [--checks=.clang-tidy, --version=21]\n files: ^(src|include)/.*\\.(cpp|cc|cxx|h|hpp)$\n```\n\nAlternatively, if you want to run the hooks manually on only the changed files, you can use the following command:\n\n```bash\npre-commit run --files $(git diff --name-only)\n```\n\nThis approach ensures that only modified files are checked, further speeding up the linting process during development.\n\n### Verbose Output\n\n> [!NOTE]\n> Use `-v` or `--verbose` in `args` of `clang-format` to show the list of processed files e.g.:\n\n```yaml\nrepos:\n - repo: https://github.com/cpp-linter/cpp-linter-hooks\n rev: v1.1.3\n hooks:\n - id: clang-format\n args: [--style=file, --version=21, --verbose] # Add -v or --verbose for detailed output\n```\n\n## FAQ\n\n### What's the difference between [`cpp-linter-hooks`](https://github.com/cpp-linter/cpp-linter-hooks) and [`mirrors-clang-format`](https://github.com/pre-commit/mirrors-clang-format)?\n\n| Feature | `cpp-linter-hooks` | `mirrors-clang-format` |\n|----------------------------------|-------------------------------------------|----------------------------------------|\n| Supports `clang-format` and `clang-tidy` | \u2705 (`clang-format` & `clang-tidy`) | \u2705 (`clang-format` only) |\n| Loads style configuration | \u2705 via `--version` | \u2705 (default behavior) |\n| Specify `clang-format` version | \u2705 via `--version` | \u2705 via `rev` |\n| Supports passing code string | \u2705 via `--style` | \u274c |\n| Verbose output | \u2705 via `--verbose` | \u274c |\n\n<!-- > [!TIP]\n> In most cases, there is no significant performance difference between `cpp-linter-hooks` and `mirrors-clang-format`. See the [benchmark results](testing/benchmark.md) for details. -->\n\n## Contributing\n\nWe welcome contributions! Whether it's fixing issues, suggesting improvements, or submitting pull requests, your support is greatly appreciated.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n",
"bugtrack_url": null,
"license": null,
"summary": "Automatically formats and lints C/C++ code using clang-format and clang-tidy",
"version": "1.1.7",
"project_urls": {
"source": "https://github.com/cpp-linter/cpp-linter-hooks",
"tracker": "https://github.com/cpp-linter/cpp-linter-hooks/issues"
},
"split_keywords": [
"clang",
" clang-format",
" clang-tidy",
" pre-commit",
" pre-commit-hooks"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "6dca4feb4542d84bc94ac2205c13d2bf0f1b5eb0c67c317643e4cad3a4fbe95b",
"md5": "b60b4bf1fc18c0fb8fb08a5d1b215a29",
"sha256": "80f047799861abda85c76bee964262e5a4b9f0a7f72b56c661b08e88fedf7ae6"
},
"downloads": -1,
"filename": "cpp_linter_hooks-1.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b60b4bf1fc18c0fb8fb08a5d1b215a29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 10135,
"upload_time": "2025-11-01T00:30:20",
"upload_time_iso_8601": "2025-11-01T00:30:20.697920Z",
"url": "https://files.pythonhosted.org/packages/6d/ca/4feb4542d84bc94ac2205c13d2bf0f1b5eb0c67c317643e4cad3a4fbe95b/cpp_linter_hooks-1.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-11-01 00:30:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cpp-linter",
"github_project": "cpp-linter-hooks",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "cpp-linter-hooks"
}