https://github.com/cpp-linter/cpp-linter-hooks
C/C++ code linter hooks for pre-commit
https://github.com/cpp-linter/cpp-linter-hooks
clang clang-format clang-tidy pre-commit pre-commit-hook pre-commit-hooks
Last synced: 7 days ago
JSON representation
C/C++ code linter hooks for pre-commit
- Host: GitHub
- URL: https://github.com/cpp-linter/cpp-linter-hooks
- Owner: cpp-linter
- License: mit
- Created: 2022-06-13T08:00:00.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-27T10:09:30.000Z (about 1 month ago)
- Last Synced: 2025-04-16T08:58:41.984Z (13 days ago)
- Topics: clang, clang-format, clang-tidy, pre-commit, pre-commit-hook, pre-commit-hooks
- Language: Python
- Homepage:
- Size: 106 KB
- Stars: 21
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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)cpp-linter-hooks is a [pre-commit](https://pre-commit.com/) hook that uses `clang-format` and `clang-tidy` to format C/C++ code.
> [!NOTE]
> This hook automatically downloads specific versions of `clang-format` or `clang-tidy` [binaries](https://github.com/cpp-linter/clang-tools-static-binaries) and installs them on your system.## Usage
To use cpp-linter-hooks, add the following configuration to your `.pre-commit-config.yaml`:
### Basic Configuration
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v0.6.1 # Use the ref you want to point at
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
To use custom configurations like `.clang-format` and `.clang-tidy`:
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v0.6.1
hooks:
- id: clang-format
args: [--style=file] # to load .clang-format
- id: clang-tidy
args: [--checks=.clang-tidy] # path/to/.clang-tidy
```To use specific versions of [clang-tools](https://github.com/cpp-linter/clang-tools-pip?tab=readme-ov-file#supported-versions):
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v0.6.1
hooks:
- id: clang-format
args: [--style=file, --version=16] # Specifies version
- id: clang-tidy
args: [--checks=.clang-tidy, --version=16] # Specifies version
```## Output
### clang-format Example
```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
-int main() {for (;;) break; printf("Hello world!\n");return 0;}
-
+int main() {
+ for (;;) break;
+ printf("Hello world!\n");
+ return 0;
+}
```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: 255main.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 Example
```bash
clang-tidy...............................................................Failed
- hook id: clang-tidy
- exit code: 1522 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 (;;)
^
{```
## Contributing
We welcome contributions! Whether it's fixing issues, suggesting improvements, or submitting pull requests, your support is greatly appreciated.
## License
cpp-linter-hooks is licensed under the [MIT License](LICENSE)