cpp-linter-hooks


Namecpp-linter-hooks JSON
Version 1.0.1 PyPI version JSON
download
home_pageNone
SummaryAutomatically formats and lints C/C++ code using clang-format and clang-tidy
upload_time2025-07-16 19:07:29
maintainerNone
docs_urlNone
authorNone
requires_pythonNone
licenseNone
keywords clang clang-format clang-tidy pre-commit pre-commit-hooks
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # cpp-linter-hooks

[![PyPI](https://img.shields.io/pypi/v/cpp-linter-hooks?color=blue)](https://pypi.org/project/cpp-linter-hooks/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/cpp-linter-hooks)](https://pypi.org/project/cpp-linter-hooks/)
[![codecov](https://codecov.io/gh/cpp-linter/cpp-linter-hooks/branch/main/graph/badge.svg?token=L74Z3HZ4Y5)](https://codecov.io/gh/cpp-linter/cpp-linter-hooks)
[![Test](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/test.yml/badge.svg)](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/test.yml)
[![CodeQL](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/codeql.yml/badge.svg)](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/codeql.yml)

A powerful [pre-commit](https://pre-commit.com/) hook for auto-formatting and linting C/C++ code with `clang-format` and `clang-tidy`.

## Table of Contents

- [Quick Start](#quick-start)
  - [Custom Configuration Files](#custom-configuration-files)
  - [Custom Clang Tool Version](#custom-clang-tool-version)
- [Output](#output)
  - [clang-format Output](#clang-format-output)
  - [clang-tidy Output](#clang-tidy-output)
- [Troubleshooting](#troubleshooting)
  - [Performance Optimization](#performance-optimization)
  - [Verbose Output](#verbose-output)
- [Contributing](#contributing)
- [License](#license)

## Quick Start

Add this configuration to your `.pre-commit-config.yaml` file:

```yaml
repos:
  - repo: https://github.com/cpp-linter/cpp-linter-hooks
    rev: v1.0.0  # Use the tag or commit you want
    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-*']
```

### Custom Configuration Files

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

```yaml
repos:
  - repo: https://github.com/cpp-linter/cpp-linter-hooks
    rev: v1.0.0
    hooks:
      - id: clang-format
        args: [--style=file]  # Loads style from .clang-format file
      - id: clang-tidy
        args: [--checks=.clang-tidy] # Loads checks from .clang-tidy file
```

### Custom Clang Tool Version

To use specific versions of clang-format and clang-tidy (using Python wheel packages):

```yaml
repos:
  - repo: https://github.com/cpp-linter/cpp-linter-hooks
    rev: v1.0.0
    hooks:
      - id: clang-format
        args: [--style=file, --version=18] # Specifies version
      - id: clang-tidy
        args: [--checks=.clang-tidy, --version=18] # Specifies version
```

> [!NOTE]
> Starting from version v1.0.0, this package uses Python wheel packages ([clang-format](https://pypi.org/project/clang-format/) and [clang-tidy](https://pypi.org/project/clang-tidy/)) instead of the previous clang-tools binaries. The wheel packages provide better cross-platform compatibility and easier installation. For more details, see the [Migration Guide](MIGRATION.md).

## Output

### clang-format Output

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

Here’s a sample diff showing the formatting applied:

```diff
--- 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;
+}
```
> [!NOTE]
> Use `--dry-run` in `args` of `clang-format` to print 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;}
                                                              ^
```

### clang-tidy Output

```bash
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 (;;)
            ^
             {

```

## Troubleshooting

### Performance Optimization

> [!TIP]
> If your `pre-commit` runs longer than expected, it is highly recommended to add `files` in `.pre-commit-config.yaml` to limit the scope of the hook. This helps improve performance by reducing the number of files being checked and avoids unnecessary processing. Here's an example configuration:

```yaml
- repo: https://github.com/cpp-linter/cpp-linter-hooks
  rev: v1.0.0
  hooks:
    - id: clang-format
      args: [--style=file, --version=18]
      files: ^(src|include)/.*\.(cpp|cc|cxx|h|hpp)$ # Limits to specific dirs and file types
    - id: clang-tidy
      args: [--checks=.clang-tidy, --version=18]
      files: ^(src|include)/.*\.(cpp|cc|cxx|h|hpp)$
```

Alternatively, if you want to run the hooks manually on only the changed files, you can use the following command:

```bash
pre-commit run --files $(git diff --name-only)
```

This approach ensures that only modified files are checked, further speeding up the linting process during development.

### Verbose Output

> [!NOTE]
> Use `-v` or `--verbose` in `args` of `clang-format` to show the list of processed files e.g.:

```yaml
repos:
  - repo: https://github.com/cpp-linter/cpp-linter-hooks
    rev: v1.0.0
    hooks:
      - id: clang-format
        args: [--style=file, --version=18, --verbose]   # Add -v or --verbose for detailed output
```

## Contributing

We welcome contributions! Whether it's fixing issues, suggesting improvements, or submitting pull requests, your support is greatly appreciated.

## License

This project is licensed under the [MIT License](LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "cpp-linter-hooks",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "clang, clang-format, clang-tidy, pre-commit, pre-commit-hooks",
    "author": null,
    "author_email": "Xianpeng Shen <xianpeng.shen@gmail.com>",
    "download_url": null,
    "platform": null,
    "description": "# cpp-linter-hooks\n\n[![PyPI](https://img.shields.io/pypi/v/cpp-linter-hooks?color=blue)](https://pypi.org/project/cpp-linter-hooks/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/cpp-linter-hooks)](https://pypi.org/project/cpp-linter-hooks/)\n[![codecov](https://codecov.io/gh/cpp-linter/cpp-linter-hooks/branch/main/graph/badge.svg?token=L74Z3HZ4Y5)](https://codecov.io/gh/cpp-linter/cpp-linter-hooks)\n[![Test](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/test.yml/badge.svg)](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/test.yml)\n[![CodeQL](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/codeql.yml/badge.svg)](https://github.com/cpp-linter/cpp-linter-hooks/actions/workflows/codeql.yml)\n\nA powerful [pre-commit](https://pre-commit.com/) hook for auto-formatting and linting C/C++ code with `clang-format` and `clang-tidy`.\n\n## Table of Contents\n\n- [Quick Start](#quick-start)\n  - [Custom Configuration Files](#custom-configuration-files)\n  - [Custom Clang Tool Version](#custom-clang-tool-version)\n- [Output](#output)\n  - [clang-format Output](#clang-format-output)\n  - [clang-tidy Output](#clang-tidy-output)\n- [Troubleshooting](#troubleshooting)\n  - [Performance Optimization](#performance-optimization)\n  - [Verbose Output](#verbose-output)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Quick Start\n\nAdd this configuration to your `.pre-commit-config.yaml` file:\n\n```yaml\nrepos:\n  - repo: https://github.com/cpp-linter/cpp-linter-hooks\n    rev: v1.0.0  # Use the tag or commit you want\n    hooks:\n      - id: clang-format\n        args: [--style=Google] # Other coding style: LLVM, GNU, Chromium, Microsoft, Mozilla, WebKit.\n      - id: clang-tidy\n        args: [--checks='boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-*,cppcoreguidelines-*']\n```\n\n### Custom Configuration Files\n\nTo use custom configurations like `.clang-format` and `.clang-tidy`:\n\n```yaml\nrepos:\n  - repo: https://github.com/cpp-linter/cpp-linter-hooks\n    rev: v1.0.0\n    hooks:\n      - id: clang-format\n        args: [--style=file]  # Loads style from .clang-format file\n      - id: clang-tidy\n        args: [--checks=.clang-tidy] # Loads checks from .clang-tidy file\n```\n\n### Custom Clang Tool Version\n\nTo use specific versions of clang-format and clang-tidy (using Python wheel packages):\n\n```yaml\nrepos:\n  - repo: https://github.com/cpp-linter/cpp-linter-hooks\n    rev: v1.0.0\n    hooks:\n      - id: clang-format\n        args: [--style=file, --version=18] # Specifies version\n      - id: clang-tidy\n        args: [--checks=.clang-tidy, --version=18] # Specifies version\n```\n\n> [!NOTE]\n> Starting from version v1.0.0, this package uses Python wheel packages ([clang-format](https://pypi.org/project/clang-format/) and [clang-tidy](https://pypi.org/project/clang-tidy/)) instead of the previous clang-tools binaries. The wheel packages provide better cross-platform compatibility and easier installation. For more details, see the [Migration Guide](MIGRATION.md).\n\n## Output\n\n### clang-format Output\n\n```bash\nclang-format.............................................................Failed\n- hook id: clang-format\n- files were modified by this hook\n```\n\nHere\u2019s a sample diff showing the formatting applied:\n\n```diff\n--- a/testing/main.c\n+++ b/testing/main.c\n@@ -1,3 +1,6 @@\n #include <stdio.h>\n-int main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n-\n+int main() {\n+  for (;;) break;\n+  printf(\"Hello world!\\n\");\n+  return 0;\n+}\n```\n> [!NOTE]\n> Use `--dry-run` in `args` of `clang-format` to print instead of changing the format, e.g.:\n\n```bash\nclang-format.............................................................Failed\n- hook id: clang-format\n- exit code: 255\n\nmain.c:2:11: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n          ^\nmain.c:2:13: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n            ^\nmain.c:2:21: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n                    ^\nmain.c:2:28: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n                           ^\nmain.c:2:54: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n                                                     ^\nmain.c:2:63: warning: code should be clang-formatted [-Wclang-format-violations]\nint main() {for (;;) break; printf(\"Hello world!\\n\");return 0;}\n                                                              ^\n```\n\n### clang-tidy Output\n\n```bash\nclang-tidy...............................................................Failed\n- hook id: clang-tidy\n- exit code: 1\n\n522 warnings generated.\nSuppressed 521 warnings (521 in non-user code).\nUse -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.\n/home/runner/work/cpp-linter-hooks/cpp-linter-hooks/testing/main.c:4:13: warning: statement should be inside braces [readability-braces-around-statements]\n    for (;;)\n            ^\n             {\n\n```\n\n## Troubleshooting\n\n### Performance Optimization\n\n> [!TIP]\n> If your `pre-commit` runs longer than expected, it is highly recommended to add `files` in `.pre-commit-config.yaml` to limit the scope of the hook. This helps improve performance by reducing the number of files being checked and avoids unnecessary processing. Here's an example configuration:\n\n```yaml\n- repo: https://github.com/cpp-linter/cpp-linter-hooks\n  rev: v1.0.0\n  hooks:\n    - id: clang-format\n      args: [--style=file, --version=18]\n      files: ^(src|include)/.*\\.(cpp|cc|cxx|h|hpp)$ # Limits to specific dirs and file types\n    - id: clang-tidy\n      args: [--checks=.clang-tidy, --version=18]\n      files: ^(src|include)/.*\\.(cpp|cc|cxx|h|hpp)$\n```\n\nAlternatively, if you want to run the hooks manually on only the changed files, you can use the following command:\n\n```bash\npre-commit run --files $(git diff --name-only)\n```\n\nThis approach ensures that only modified files are checked, further speeding up the linting process during development.\n\n### Verbose Output\n\n> [!NOTE]\n> Use `-v` or `--verbose` in `args` of `clang-format` to show the list of processed files e.g.:\n\n```yaml\nrepos:\n  - repo: https://github.com/cpp-linter/cpp-linter-hooks\n    rev: v1.0.0\n    hooks:\n      - id: clang-format\n        args: [--style=file, --version=18, --verbose]   # Add -v or --verbose for detailed output\n```\n\n## Contributing\n\nWe welcome contributions! Whether it's fixing issues, suggesting improvements, or submitting pull requests, your support is greatly appreciated.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Automatically formats and lints C/C++ code using clang-format and clang-tidy",
    "version": "1.0.1",
    "project_urls": {
        "source": "https://github.com/cpp-linter/cpp-linter-hooks",
        "tracker": "https://github.com/cpp-linter/cpp-linter-hooks/issues"
    },
    "split_keywords": [
        "clang",
        " clang-format",
        " clang-tidy",
        " pre-commit",
        " pre-commit-hooks"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "e2eb932e0b8affe45220fb75da95255ea6e7e18a1438455118006701189813ce",
                "md5": "7a4b5ac0a12c5a6ab8f09ba489d4b414",
                "sha256": "1498916101bc55a09369d4d965284c486168c4abb6a9a66e2afc9825ca84f680"
            },
            "downloads": -1,
            "filename": "cpp_linter_hooks-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7a4b5ac0a12c5a6ab8f09ba489d4b414",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8693,
            "upload_time": "2025-07-16T19:07:29",
            "upload_time_iso_8601": "2025-07-16T19:07:29.069085Z",
            "url": "https://files.pythonhosted.org/packages/e2/eb/932e0b8affe45220fb75da95255ea6e7e18a1438455118006701189813ce/cpp_linter_hooks-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-16 19:07:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cpp-linter",
    "github_project": "cpp-linter-hooks",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "cpp-linter-hooks"
}
        
Elapsed time: 1.41851s