Skip to content

Commit 5594389

Browse files
authored
Fixed some issues of clang-format and clang-tidy (#2)
* debug clang-tidy * fix clang-format dry-run * update README.md * print clang-tidy warning and error * remove debug print from clang-tidy
1 parent d7446c9 commit 5594389

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

README.md

+56-18
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ repos:
3232
- id: clang-format
3333
args: [--style=file] # to load .clang-format
3434
- id: clang-tidy
35-
args: [--config=.clang-tidy] # path/to/.clang-tidy
35+
args: [--checks=.clang-tidy] # path/to/.clang-tidy
3636
```
3737

3838
The example of using any version of [clang-tools](https://github.com/shenxianpeng/clang-tools-pip).
@@ -45,17 +45,67 @@ repos:
4545
- id: clang-format
4646
args: [--style=file, --version=13]
4747
- id: clang-tidy
48-
args: [--config=.clang-tidy, --version=12]
48+
args: [--checks=.clang-tidy, --version=12]
4949
```
5050

5151
## Output
5252

53-
The output when catching unformatted and error code.
53+
### clang-format
5454

55-
```
55+
```bash
5656
clang-format.............................................................Failed
5757
- hook id: clang-format
5858
- files were modified by this hook
59+
```
60+
61+
Here is the diff between the modified file.
62+
63+
```diff
64+
--- a/testing/main.c
65+
+++ b/testing/main.c
66+
@@ -1,3 +1,6 @@
67+
#include <stdio.h>
68+
-int main() {for (;;) break; printf("Hello world!\n");return 0;}
69+
-
70+
+int main() {
71+
+ for (;;) break;
72+
+ printf("Hello world!\n");
73+
+ return 0;
74+
+}
75+
```
76+
77+
Pass `--dry-run` to the `args` of `clang-format`(can also pass other arg which clang-format supports)
78+
79+
This just prints instead of changing the format. E.g:
80+
81+
```bash
82+
clang-format.............................................................Failed
83+
- hook id: clang-format
84+
- exit code: 255
85+
86+
main.c:2:11: warning: code should be clang-formatted [-Wclang-format-violations]
87+
int main() {for (;;) break; printf("Hello world!\n");return 0;}
88+
^
89+
main.c:2:13: warning: code should be clang-formatted [-Wclang-format-violations]
90+
int main() {for (;;) break; printf("Hello world!\n");return 0;}
91+
^
92+
main.c:2:21: warning: code should be clang-formatted [-Wclang-format-violations]
93+
int main() {for (;;) break; printf("Hello world!\n");return 0;}
94+
^
95+
main.c:2:28: warning: code should be clang-formatted [-Wclang-format-violations]
96+
int main() {for (;;) break; printf("Hello world!\n");return 0;}
97+
^
98+
main.c:2:54: warning: code should be clang-formatted [-Wclang-format-violations]
99+
int main() {for (;;) break; printf("Hello world!\n");return 0;}
100+
^
101+
main.c:2:63: warning: code should be clang-formatted [-Wclang-format-violations]
102+
int main() {for (;;) break; printf("Hello world!\n");return 0;}
103+
^
104+
```
105+
106+
### chang-tidy
107+
108+
```bash
59109
clang-tidy...............................................................Failed
60110
- hook id: clang-tidy
61111
- exit code: 1
@@ -74,21 +124,9 @@ Found compiler error(s).
74124
^~~~~~~~~~
75125
```
76126

77-
The diff of the modified file.
127+
## Contributing
78128

79-
```diff
80-
--- a/testing/main.c
81-
+++ b/testing/main.c
82-
@@ -1,3 +1,6 @@
83-
#include <stdio.h>
84-
-int main() {for (;;) break; printf("Hello world!\n");return 0;}
85-
-
86-
+int main() {
87-
+ for (;;) break;
88-
+ printf("Hello world!\n");
89-
+ return 0;
90-
+}
91-
```
129+
Any contribution is very welcome, including submitting issues, PRs, etc.
92130

93131
## License
94132

cpp_linter_hooks/clang_format.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def run_clang_format(args) -> int:
2121
sp = subprocess.run(command, stdout=subprocess.PIPE)
2222
retval = -1 # Not a fail just identify it's a dry-run.
2323
output = sp.stdout.decode("utf-8")
24-
retval = subprocess.run(command, stdout=subprocess.PIPE).returncode
24+
else:
25+
retval = subprocess.run(command, stdout=subprocess.PIPE).returncode
2526
return retval, output
2627
except FileNotFoundError as e:
2728
retval = 1

cpp_linter_hooks/clang_tidy.py

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def run_clang_tidy(args) -> int:
2020
sp = subprocess.run(command, stdout=subprocess.PIPE)
2121
retval = sp.returncode
2222
output = sp.stdout.decode("utf-8")
23+
if "warning:" in output or "error:" in output:
24+
retval = 1
2325
return retval, output
2426
except FileNotFoundError as e:
2527
retval = 1

0 commit comments

Comments
 (0)