Skip to main content

pre-commit hooks for CMake based projects

Project description

pre-commit hooks

PyPI - Python Version PyPI version CI Build CodeQL pre-commit.ci status

This is a pre-commit hooks repo that integrates C/C++ linters/formatters to work with CMake-based projects.

clang-format, clang-tidy, cppcheck and iwyu

It is largely based on the work found here. The main difference with POCC's pre-commit hooks is that the ones from this repository will do a CMake configuration step prior to running any pre-commit hooks. This is done in order to have CMake generate the compilation database file that can then be used by the various hooks (using the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON CMake option).

This repository is only has Python-based pre-commit hooks.

Current known issues

Currently, the hooks that depend on having a compilation database generated by CMake (e.g. clang-tidy, cppcheck) are not working on Windows if you are not using the Ninja or Makefile generators.

Example usage

Assuming that you have the following directory structure for your projects

root
├── .pre-commit-config.yaml
├── CMakeLists.txt
└── src
    └── err.cpp

with the following file contents:

.pre-commit-config.yaml

repos:
  - repo: /Users/damien/code/pre-commit/cmake-pre-commit-hooks
    rev: 1.0.0
    hooks:
      - id: clang-format
      - id: clang-tidy
        args: [--checks=readability-magic-numbers,--warnings-as-errors=*]
      - id: cppcheck

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(LANGUAGE CXX)
add_library(mylib STATIC src/err.cpp)

src/err.cpp

#include <string>
int main() { int i; return 10; }

Runnning pre-commit on the above project will lead to an output similar to this one:

$ pre-commit run --all-files
clang-format.............................................................Failed
- hook id: clang-format
- exit code: 1

src/err.cpp
====================
<  int main() { int i; return 10; }
---
>  int main() {
>    int i;
>    return 10;
>  }

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

/tmp/temp/src/err.cpp:2:28: error: 10 is a magic number; consider replacing it with a named constant [readability-magic-numbers,-warnings-as-errors]
int main() { int i; return 10; }
                           ^

cppcheck.................................................................Failed
- hook id: cppcheck
- exit code: 1

/tmp/temp/src/err.cpp:2:18: style: Unused variable: i [unusedVariable]
int main() { int i; return 10; }
                 ^

Note that your mileage may vary depending on the version of the tools. The example above was generated using clang-format 12.0.0, clang-tidy 12.0.0 and cppcheck 2.4.1.

Using the Hooks

Python 3.6+ is required to use these hooks as all 3 invoking scripts are written in it. As this is also the minimum version of pre-commit, this should not be an issue.

Running multiple hooks in parallel is currently supported by using the fastener Python package. If the hooks are run in parallel, only one of the hooks will run the CMake configure step while the others will simply wait until the call to CMake ends to continue. In the case where the hooks are run serially, all the hooks will be running the CMake configure step. However, if nothing changed in your CMake configuration, this should not cost too much time.

Installation

For installing the various utilities, refer to your package manager documentation. Some guidance can also be found here.

Hook Info

Hook Info Type Languages
clang-format Formatter C, C++, ObjC
clang-tidy Static code analyzer C, C++, ObjC
cppcheck Static code analyzer C, C++

Hook Option Comparison

Hook Options Fix In Place Enable all Checks Set key/value
clang-format -i
clang-tidy --fix-errors [1] -checks=* -warnings-as-errors=* [2]
cppcheck -enable=all

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

[2]: Be careful with -checks=*. can have self-contradictory rules in newer versions of llvm (9+): modernize wants to use trailing return type but Fuchsia disallows it.

The '--' doubledash option

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

Standalone Hooks

If you want to have predictable return codes for your C linters outside of pre-commit, these hooks are available via PyPI. Install it with pip install CLinters. They are named as cmake-pc-$cmd-hook, so clang-format becomes cmake-pc-clang-format-hook.

If you want to run the tests below, you will need to install them from PyPI or locally with pip install ..

Project details


Download files

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

Source Distribution

cmake-pre-commit-hooks-1.1.0.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

cmake_pre_commit_hooks-1.1.0-py2.py3-none-any.whl (14.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cmake-pre-commit-hooks-1.1.0.tar.gz.

File metadata

  • Download URL: cmake-pre-commit-hooks-1.1.0.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for cmake-pre-commit-hooks-1.1.0.tar.gz
Algorithm Hash digest
SHA256 54dee1d92424b3448d0c1e0daeb1e225c1af5eee79a09bb959c52eede7226ac9
MD5 fb1496a842b9ac195bc279670591d68e
BLAKE2b-256 3ffe9a79e7813510a2494896aa7d47aff7f99071f3a944a3f5a362782645ea9b

See more details on using hashes here.

File details

Details for the file cmake_pre_commit_hooks-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cmake_pre_commit_hooks-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for cmake_pre_commit_hooks-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bd284f104b32de37175e9ae94be550095e74d52aa9623699c82dca5eb6f3196e
MD5 4e893df68dde2bc0d7a863c40a0a491a
BLAKE2b-256 a65b5689cf7689173ad47ad72b5add27d59066bccb530b07a61981fc3f153cb5

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