Skip to content

Commit

Permalink
Fixed some issues of clang-format and clang-tidy (#2)
Browse files Browse the repository at this point in the history
* debug clang-tidy

* fix clang-format dry-run

* update README.md

* print clang-tidy warning and error

* remove debug print from clang-tidy
  • Loading branch information
shenxianpeng authored Jul 12, 2022
1 parent d7446c9 commit 5594389
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
74 changes: 56 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repos:
- id: clang-format
args: [--style=file] # to load .clang-format
- id: clang-tidy
args: [--config=.clang-tidy] # path/to/.clang-tidy
args: [--checks=.clang-tidy] # path/to/.clang-tidy
```

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

## Output

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

```
```bash
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
```

Here is the diff between the modified file.

```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;
+}
```

Pass `--dry-run` to the `args` of `clang-format`(can also pass other arg which clang-format supports)

This just prints 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;}
^
```

### chang-tidy

```bash
clang-tidy...............................................................Failed
- hook id: clang-tidy
- exit code: 1
Expand All @@ -74,21 +124,9 @@ Found compiler error(s).
^~~~~~~~~~
```

The diff of the modified file.
## Contributing

```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;
+}
```
Any contribution is very welcome, including submitting issues, PRs, etc.

## License

Expand Down
3 changes: 2 additions & 1 deletion cpp_linter_hooks/clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def run_clang_format(args) -> int:
sp = subprocess.run(command, stdout=subprocess.PIPE)
retval = -1 # Not a fail just identify it's a dry-run.
output = sp.stdout.decode("utf-8")
retval = subprocess.run(command, stdout=subprocess.PIPE).returncode
else:
retval = subprocess.run(command, stdout=subprocess.PIPE).returncode
return retval, output
except FileNotFoundError as e:
retval = 1
Expand Down
2 changes: 2 additions & 0 deletions cpp_linter_hooks/clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def run_clang_tidy(args) -> int:
sp = subprocess.run(command, stdout=subprocess.PIPE)
retval = sp.returncode
output = sp.stdout.decode("utf-8")
if "warning:" in output or "error:" in output:
retval = 1
return retval, output
except FileNotFoundError as e:
retval = 1
Expand Down

0 comments on commit 5594389

Please sign in to comment.