Code Monkey home page Code Monkey logo

cpp-linter-hooks's Introduction

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.4.0  # 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.4.0
    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.4.0
    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

cpp-linter-hooks's People

Contributors

2bndy5 avatar dependabot[bot] avatar maxnoe avatar shenxianpeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

maxnoe

cpp-linter-hooks's Issues

fix skip tests

The tests are failing on github actions now, because clang tools are already installed there, so the tests that assumed that when starting the tests, we don't have the tools fail.

Will think about how to actually test that the installation works.

Originally posted in #29 (comment)

Support passing clang version

Because clang-tools already support passing clang version, this hook should also support it.

  1. I need to test first.
  2. Also add test to testing forlder

Create cpp-linter-hooks for pre-commit

Add following config to .pre-commit-config.yaml

repos:
  - repo: https://github.com/shenxianpeng/cpp-linter-hooks
    rev: v0.1.0
    hooks:
      - id: clang-format
        args: [--style=.clang-format]
      - id: clang-tidy
        args: [--checks=.clang-tidy]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.