From f1c8ea471bd6286cc1413db49f6df63e3e031ff9 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 12 Jul 2022 22:06:56 +0800 Subject: [PATCH 1/5] debug clang-tidy --- cpp_linter_hooks/clang_tidy.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp_linter_hooks/clang_tidy.py b/cpp_linter_hooks/clang_tidy.py index f803578..1de870c 100644 --- a/cpp_linter_hooks/clang_tidy.py +++ b/cpp_linter_hooks/clang_tidy.py @@ -14,6 +14,7 @@ def run_clang_tidy(args) -> int: continue command.append(arg) + print(command) retval = 0 output = "" try: From 90f80320bd32d1cc6e018a8c62ffb537373af33b Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 12 Jul 2022 22:09:57 +0800 Subject: [PATCH 2/5] fix clang-format dry-run --- cpp_linter_hooks/clang_format.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp_linter_hooks/clang_format.py b/cpp_linter_hooks/clang_format.py index 01b10d7..c635af8 100644 --- a/cpp_linter_hooks/clang_format.py +++ b/cpp_linter_hooks/clang_format.py @@ -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 From 9465704ea606324bee9ce2aadea8aad2c4421404 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 12 Jul 2022 22:29:46 +0800 Subject: [PATCH 3/5] update README.md --- README.md | 74 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index d4f7a48..5188e91 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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 +-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 @@ -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 --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 From ec921320aff3351dfd9494eccfc9470d6e509a4d Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 12 Jul 2022 22:44:23 +0800 Subject: [PATCH 4/5] print clang-tidy warning and error --- cpp_linter_hooks/clang_tidy.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp_linter_hooks/clang_tidy.py b/cpp_linter_hooks/clang_tidy.py index 1de870c..c352bff 100644 --- a/cpp_linter_hooks/clang_tidy.py +++ b/cpp_linter_hooks/clang_tidy.py @@ -21,6 +21,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: + retval = 1 return retval, output except FileNotFoundError as e: retval = 1 From b975910ae13302ea75244327a0e0a7f38c78a967 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 12 Jul 2022 22:49:33 +0800 Subject: [PATCH 5/5] remove debug print from clang-tidy --- cpp_linter_hooks/clang_tidy.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cpp_linter_hooks/clang_tidy.py b/cpp_linter_hooks/clang_tidy.py index c352bff..2ce4f7c 100644 --- a/cpp_linter_hooks/clang_tidy.py +++ b/cpp_linter_hooks/clang_tidy.py @@ -14,14 +14,13 @@ def run_clang_tidy(args) -> int: continue command.append(arg) - print(command) retval = 0 output = "" try: sp = subprocess.run(command, stdout=subprocess.PIPE) retval = sp.returncode output = sp.stdout.decode("utf-8") - if "warning:" in output: + if "warning:" in output or "error:" in output: retval = 1 return retval, output except FileNotFoundError as e: