Skip to main content

Automatically check c/c++ with clang-format and clang-tidy

Project description

cpp-linter-hooks

PyPI codecov Test CodeQL pre-commit

cpp-linter-hooks integrates clang-format and clang-tidy hooks with pre-commit to lint your C/C++ code efficiently.

[!NOTE] This hook automatically downloads a specific version of clang-format or clang-tidy binaries and installs it on the system.

Usage

Add the following configuration to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/cpp-linter/cpp-linter-hooks
    rev: v0.5.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-*']

To use custom configurations like .clang-format and .clang-tidy:

repos:
  - repo: https://github.com/cpp-linter/cpp-linter-hooks
    rev: v0.5.1
    hooks:
      - id: clang-format
        args: [--style=file]  # to load .clang-format
      - id: clang-tidy
        args: [--checks=.clang-tidy] # path/to/.clang-tidy

To use specific versions of clang-tools:

repos:
  - repo: https://github.com/cpp-linter/cpp-linter-hooks
    rev: v0.5.1
    hooks:
      - id: clang-format
        args: [--style=file, --version=16] # Specifies version
      - id: clang-tidy
        args: [--checks=.clang-tidy, --version=16]  # Specifies version

Output

clang-format output

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

The diff between the modified file is as follows:

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

Use --dry-run in args of clang-format to 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;}
                                                              ^

clang-tidy output

clang-tidy...............................................................Failed
- hook id: clang-tidy
- exit code: 1

522 warnings generated.
Suppressed 521 warnings (521 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.
/home/runner/work/cpp-linter-hooks/cpp-linter-hooks/testing/main.c:4:13: warning: statement should be inside braces [readability-braces-around-statements]
    for (;;)
            ^
             {

Contributing

Contributions are very welcome, including submitting issues, PRs, etc.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

cpp_linter_hooks-0.5.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file cpp_linter_hooks-0.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cpp_linter_hooks-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 de5da00a62029c50e844f694807e25fff23aff0589938666d39692cd478b43ff
MD5 504e62d0ac5d3c835d922e46dd0a734f
BLAKE2b-256 4832c26f3cc19bc8462a44d670e0c693c177c214e3dbf2c36f5467b6c7597815

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page