Skip to content

Fixed some issues of clang-format and clang-tidy #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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