Skip to content
clang hooks to integrate with pre-commit
Python Shell Other
Branch: master
Clone or download
pocc Merge branch 'bash-only'
Minor cleanup
Fixes #3
Latest commit d43e9c1 Jul 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
hooks Fixes #3 Jul 22, 2019
tests Fixes #3 Jul 22, 2019
.gitignore Fixes #3 Jul 22, 2019
.pre-commit-config.yaml Added --version hook arg Apr 25, 2019
.pre-commit-hooks.yaml Added image of usage Apr 25, 2019
LICENSE Initial commit Apr 20, 2019
README.md Fixes #3 Jul 22, 2019
requirements.txt Added requirements.txt Apr 22, 2019

README.md

pre-commit hooks

This is a pre-commit hooks repo that integrates C/C++ linters clang-format, clang-tidy, and oclint.

Example Usage

With int main() { int i; return 10; } in a file, all three linters should fail on commit:

The above uses this .pre-commit-config.yaml:

---
fail_fast: false
repos:
  - repo: https://github.com/pocc/pre-commit-hooks
    sha: master
    hooks:
      - id: clang-format
        args: [--style=Google]
      - id: clang-tidy
        args: [-checks=*, -warnings-as-errors=*]
      - id: oclint
        args: [-enable-clang-static-analyzer, -enable-global-analysis]

Note that for your config yaml, you can supply your own args or remove the args line entirely, depending on your use case.

Using the Hooks

Prerequisites

You will need to install these utilities in order to use them. Your package manager may already have them.

  • brew install llvm oclint
  • apt install clang-format clang-tidy oclint

Bash is required to use these hooks as all 3 invoking scripts are written in it.

Hook Info

What it does Fix Inplace
clang-format Formats C/C++ code according to a style -i
clang-tidy clang-based C/C++ linter -fix, --fix-errors [1]
oclint static code analysis tool for C, C++ and Objective-C code N/A

[1]: -fix will fail if there are compiler errors. -fix-errors will -fix and fix compiler errors if it can, like missing semicolons.

Enforcing linter version with --version=

Some linters change behavior between versions. To enforce a linter version 8.0.0, for example, add --version=8.0.0 to args: for that linter. Note that this is a pre-commit hook arg and passing it to the linter will cause an error.

Compilation Database

clang-tidy and oclint both expect a compilation database. Both of the hooks for them will ignore the error for not having one.

You can generate with one cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ... if you have a cmake-based project.

The -- option

Options after -- like -std=c++11 will be interpreted correctly for clang-tidy and oclint. Make sure they sequentially follow the -- argument in the hook's args list.

Testing

To run the tests and verify clang-format, clang-tidy, and oclint are working as expected on your system, use pytest tests/hooks_test.py --runslow.

Additional Resources

clang-format

clang-tidy

oclint

License

Apache 2.0

You can’t perform that action at this time.