Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here.
cpp-linter-hooks is a Python package that provides pre-commit hooks for automatically formatting and linting C/C++ code using clang-format and clang-tidy. The package installs specific versions of these tools and integrates them into pre-commit workflows.
python -m pip install --upgrade pippip install .[dev]-- installs the package in development mode with all dev dependencies- Time expectation: 2-3 minutes for initial install
pytest -vv -k "not (test_get_version_from_dependency_success or test_get_version_from_dependency_missing_dependency or test_get_version_from_dependency_malformed_toml or test_resolve_install_tool_already_installed_correct_version or test_default_versions)"-- runs working tests, excluding 5 known failing tests related to installation path detection- NEVER CANCEL: Test suite takes 6-8 minutes. Set timeout to 15+ minutes.
coverage run --source=tests,cpp_linter_hooks -m pytest -vv -k "not (test_get_version_from_dependency_success or test_get_version_from_dependency_missing_dependency or test_get_version_from_dependency_malformed_toml or test_resolve_install_tool_already_installed_correct_version or test_default_versions)"-- runs tests with coverage- NEVER CANCEL: Coverage test takes 8-10 minutes. Set timeout to 20+ minutes.
coverage report-- shows coverage after running coverage testscoverage xml-- generates XML coverage report
python -m cpp_linter_hooks.clang_format --help-- shows clang-format hook helppython -m cpp_linter_hooks.clang_tidy --help-- shows clang-tidy hook helppython -m cpp_linter_hooks.clang_format --version=21 file.c-- formats a C file using clang-format version 21python -m cpp_linter_hooks.clang_tidy --version=21 --checks=readability-* file.c-- lints a C file using clang-tidy- Time expectation: Each hook takes 15-20 seconds on first run (tool installation), then 1-2 seconds
WARNING: Network timeouts may affect pre-commit setup in CI environments. For testing, use local system hooks:
Create test scenario:
mkdir /tmp/test_precommit && cd /tmp/test_precommit
git init && git config user.email "test@example.com" && git config user.name "Test User"
echo '#include <stdio.h>
int main(){printf("test");return 0;}' > test.cCreate .pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: clang-format
name: clang-format
entry: clang-format-hook
language: system
types_or: [c++, c]
args: [--version=21]Run: git add . && git commit -m "initial" && pre-commit run --all-files
- Time expectation: 15-20 seconds
NOTE: The main repository testing script sh testing/run.sh may fail due to network timeouts when installing clang tools from PyPI. This is a known limitation in sandboxed environments.
WARNING: Standard pre-commit setup may fail due to network issues. The repository uses:
pre-commit run --all-files-- may timeout in CI environments- The project has a
.pre-commit-config.yamlwith ruff for Python formatting - Time expectation: When working, takes 2-3 minutes
-
Basic Hook Functionality:
echo '#include <stdio.h> int main(){printf("test");return 0;}' > /tmp/test.c python -m cpp_linter_hooks.clang_format --version=21 /tmp/test.c cat /tmp/test.c # Verify formatting applied
-
Clang-tidy Linting:
echo '[ { "directory": "/tmp", "command": "gcc -o test test.c", "file": "test.c" } ]' > /tmp/compile_commands.json cd /tmp && python -m cpp_linter_hooks.clang_tidy --version=21 --checks=readability-* test.c
-
Test Suite Validation:
- Always run the working test subset (see command above)
- 55 tests should pass, 5 known failures are acceptable
- Tests validate multiple clang-format/clang-tidy versions (16-21)
.
├── .github/ # GitHub workflows and configs
├── .gitignore
├── .pre-commit-config.yaml
├── .pre-commit-hooks.yaml # Hook definitions for users
├── LICENSE
├── README.md
├── cpp_linter_hooks/ # Main package source
│ ├── __init__.py
│ ├── clang_format.py
│ ├── clang_tidy.py
│ └── util.py
├── docs/
├── pyproject.toml # Package configuration
├── testing/ # Integration tests and examples
│ ├── main.c # Poorly formatted test file
│ ├── good.c # Well formatted test file
│ ├── run.sh # Integration test script
│ └── ...
└── tests/ # Unit tests
├── test_clang_format.py
├── test_clang_tidy.py
└── test_util.py
- Always check
cpp_linter_hooks/util.pyafter modifying version handling - Always test both
clang_format.pyandclang_tidy.pyafter utility changes - Always verify
pyproject.tomldependencies match expected tool versions
- Some tests fail due to installation path detection issues (5 tests expected to fail)
- Pre-commit setup may timeout in CI environments due to network restrictions
- The
testing/run.shscript requires network access to PyPI for clang tool installation - Tool installation on first use takes 15-20 seconds per version
- Package installation: 2-3 minutes
- Test suite (working tests): 6-8 minutes
- Coverage tests: 8-10 minutes
- Individual hook execution: 15-20 seconds (first use), 1-2 seconds (subsequent)
- Pre-commit setup: 15-20 seconds (when network allows)
- Use
--verboseflag with clang-format hook for detailed output - Check
/home/runner/.cache/pre-commit/for pre-commit environment issues - Use local system hooks in
.pre-commit-config.yamlto avoid network timeouts - Test hooks directly with
python -m cpp_linter_hooks.{tool}before using via pre-commit
Always validate that your changes don't break the core functionality by running the hook validation scenarios above.