Automatically check c/c++ with clang-format and clang-tidy
Project description
cpp-linter-hooks
Using clang-format
and clang-tidy
hooks with pre-commit to lint your C/C++ code.
Highlight✨: No need to manually download and install clang-format
or clang-tidy
on your system.
Usage
Add this to your .pre-commit-config.yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v0.2.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-*']
The example of using custom config: .clang-format
and .clang-tidy
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v0.2.1
hooks:
- id: clang-format
args: [--style=file] # to load .clang-format
- id: clang-tidy
args: [--checks=.clang-tidy] # path/to/.clang-tidy
The example of using any version of clang-tools.
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v0.2.1
hooks:
- id: clang-format
args: [--style=file, --version=13]
- id: clang-tidy
args: [--checks=.clang-tidy, --version=12]
Output
clang-format output
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
Here is the diff between the modified file.
--- 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;
+}
Pass --dry-run
to the args
of clang-format
(can also pass other arg which clang-format supports)
Then it will just print instead of changing the format. E.g:
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;}
^
chang-tidy output
clang-tidy...............................................................Failed
- hook id: clang-tidy
- exit code: 1
418 warnings and 1 error generated.
Error while processing /home/ubuntu/cpp-linter-hooks/testing/main.c.
Suppressed 417 warnings (417 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.
Found compiler error(s).
/home/ubuntu/cpp-linter-hooks/testing/main.c:3:11: warning: statement should be inside braces [readability-braces-around-statements]
for (;;) break;
^
{
/usr/include/stdio.h:33:10: error: 'stddef.h' file not found [clang-diagnostic-error]
#include <stddef.h>
^~~~~~~~~~
Contributing
Any contribution is very welcome, including submitting issues, PRs, etc.
License
This project is licensed under the terms of the MIT license.
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 Distributions
Built Distribution
Hashes for cpp_linter_hooks-0.2.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e50f980c8116b0a432c43ee35e30bf0328ea9d55df3850315b8468cecf2e7888 |
|
MD5 | 97f1fc9ab227b25d8121778c78da652f |
|
BLAKE2b-256 | 6ecd351af2013f6b1a18fecd875ffa2310f52d03eb988e1181b878d17f650d53 |