|
| 1 | +# cpp-linter-hooks |
| 2 | + |
| 3 | +`cpp-linter-hooks` is a Python package that provides pre-commit hooks for C/C++ code formatting and linting using `clang-format` and `clang-tidy`. The hooks automatically install specific versions of these tools as Python wheel packages and format/lint C/C++ code. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Bootstrap and Setup |
| 10 | +- **Install dependencies**: |
| 11 | + - `python -m pip install --upgrade pip` -- takes ~5 seconds |
| 12 | + - `pip install .[dev]` -- takes ~10 seconds to install development dependencies |
| 13 | + - `pip install .[tools]` -- installs clang-format==21.1.0 and clang-tidy==21.1.0, takes ~2 seconds (may fail due to network timeouts in sandboxed environments) |
| 14 | + - **Alternative for clang tools**: `pip install clang-format==21.1.0 clang-tidy==21.1.0` -- more reliable installation method |
| 15 | + |
| 16 | +### Build and Test Process |
| 17 | +- **NEVER CANCEL builds or test commands**: The test suite takes 5.5 minutes. Set timeout to 10+ minutes. |
| 18 | +- **Run tests**: `coverage run --source=tests,cpp_linter_hooks -m pytest -vv` -- takes 5.5 minutes. NEVER CANCEL. |
| 19 | +- **Coverage report**: `coverage report` -- shows test coverage (typically 94%+) |
| 20 | +- **Python linting**: |
| 21 | + - `pip install ruff` (if not already installed) |
| 22 | + - `ruff check .` -- takes <1 second, must pass |
| 23 | + - `ruff format --check .` -- takes <1 second, checks code formatting |
| 24 | + |
| 25 | +### Testing the Hooks Manually |
| 26 | +- **Test clang-format hook directly**: |
| 27 | + - `clang-format-hook --version 21 --verbose /path/to/test.c` -- formats C/C++ files |
| 28 | + - Requires `--version` parameter to avoid version checking issues |
| 29 | +- **Test clang-tidy hook directly**: |
| 30 | + - `clang-tidy-hook --version 21 /path/to/test.c` -- analyzes C/C++ files |
| 31 | + - May show compilation database warnings (normal for standalone files) |
| 32 | + |
| 33 | +### Pre-commit Environment Issues |
| 34 | +- **Known Issue**: Pre-commit environment setup may fail with network timeouts in sandboxed environments when downloading clang tools |
| 35 | +- **Workaround**: Test hooks directly using the manual commands above |
| 36 | +- **CI Environment**: The GitHub Actions CI works correctly with longer timeouts |
| 37 | + |
| 38 | +## Validation |
| 39 | + |
| 40 | +### Required Validation Steps |
| 41 | +- **ALWAYS run the full test suite** after making changes: `coverage run --source=tests,cpp_linter_hooks -m pytest -vv` (5.5 minutes) |
| 42 | +- **ALWAYS run Python linting** before committing: `ruff check .` and `ruff format --check .` |
| 43 | +- **ALWAYS test both hooks manually** with sample C++ files using the direct hook commands |
| 44 | +- **Test end-to-end scenarios**: Create a malformed C++ file, run clang-format hook, verify it gets formatted correctly |
| 45 | + |
| 46 | +### Manual Testing Scenario |
| 47 | +```bash |
| 48 | +# Create test file with poor formatting |
| 49 | +echo '#include <stdio.h> |
| 50 | +int main() {for (;;) break; printf("Hello world!\n");return 0;}' > /tmp/test.c |
| 51 | + |
| 52 | +# Test clang-format (should reformat the file) |
| 53 | +clang-format-hook --version 21 --verbose /tmp/test.c |
| 54 | + |
| 55 | +# Verify file was formatted (should show properly indented code) |
| 56 | +cat /tmp/test.c |
| 57 | + |
| 58 | +# Test clang-tidy (should run without errors) |
| 59 | +clang-tidy-hook --version 21 /tmp/test.c |
| 60 | +``` |
| 61 | + |
| 62 | +## Project Structure |
| 63 | + |
| 64 | +### Key Directories and Files |
| 65 | +``` |
| 66 | +. |
| 67 | +├── cpp_linter_hooks/ # Main package source code |
| 68 | +│ ├── __init__.py |
| 69 | +│ ├── clang_format.py # clang-format hook implementation |
| 70 | +│ ├── clang_tidy.py # clang-tidy hook implementation |
| 71 | +│ └── util.py # Shared utilities for tool installation |
| 72 | +├── tests/ # Unit tests (73 tests total) |
| 73 | +│ ├── test_clang_format.py |
| 74 | +│ ├── test_clang_tidy.py |
| 75 | +│ └── test_util.py |
| 76 | +├── testing/ # Integration testing and examples |
| 77 | +│ ├── run.sh # Integration test script |
| 78 | +│ ├── main.c # Test C file (poorly formatted) |
| 79 | +│ ├── good.c # Well-formatted test file |
| 80 | +│ ├── .clang-format # Example clang-format config |
| 81 | +│ ├── .clang-tidy # Example clang-tidy config |
| 82 | +│ └── pre-commit-config*.yaml # Example pre-commit configurations |
| 83 | +├── .github/workflows/ # CI/CD pipelines |
| 84 | +│ ├── test.yml # Main test workflow |
| 85 | +│ ├── pre-commit.yml # Pre-commit validation |
| 86 | +│ └── publish.yml # Package publishing |
| 87 | +├── pyproject.toml # Package configuration and dependencies |
| 88 | +├── .pre-commit-hooks.yaml # Hook definitions for pre-commit |
| 89 | +└── .pre-commit-config.yaml # Repository's own pre-commit config |
| 90 | +``` |
| 91 | + |
| 92 | +### Entry Points |
| 93 | +- `clang-format-hook` → `cpp_linter_hooks.clang_format:main` |
| 94 | +- `clang-tidy-hook` → `cpp_linter_hooks.clang_tidy:main` |
| 95 | + |
| 96 | +## Common Commands Reference |
| 97 | + |
| 98 | +### Repository Root Contents |
| 99 | +```bash |
| 100 | +ls -la |
| 101 | +# .github/ - GitHub workflows and configurations |
| 102 | +# cpp_linter_hooks/ - Main Python package source |
| 103 | +# docs/ - Documentation (migration notes) |
| 104 | +# testing/ - Integration tests and examples |
| 105 | +# tests/ - Unit tests |
| 106 | +# pyproject.toml - Package configuration |
| 107 | +# .pre-commit-hooks.yaml - Hook definitions |
| 108 | +# README.md - Main documentation |
| 109 | +``` |
| 110 | + |
| 111 | +### Package Configuration (pyproject.toml) |
| 112 | +- **Python support**: 3.9-3.14 |
| 113 | +- **Main dependencies**: setuptools>=45.0.0, tomli (Python <3.11) |
| 114 | +- **Dev dependencies**: coverage, pre-commit, pytest, pytest-codspeed |
| 115 | +- **Optional clang tools**: clang-format==21.1.0, clang-tidy==21.1.0 |
| 116 | + |
| 117 | +### Common Workflow Times |
| 118 | +- **Dependency installation**: ~10-15 seconds |
| 119 | +- **Test suite execution**: 5.5 minutes (NEVER CANCEL - use 10+ minute timeouts) |
| 120 | +- **Python linting**: <1 second |
| 121 | +- **Hook execution**: <1 second per file |
| 122 | +- **Coverage reporting**: <1 second |
| 123 | + |
| 124 | +## Troubleshooting |
| 125 | + |
| 126 | +### Version Checking Issues |
| 127 | +- **Problem**: Hook fails with version checking TypeError |
| 128 | +- **Solution**: Always specify `--version 21` parameter when testing hooks directly |
| 129 | + |
| 130 | +### Network Timeout Issues |
| 131 | +- **Problem**: Pre-commit environment setup fails with pip timeout errors |
| 132 | +- **Cause**: Downloading large clang tool packages in sandboxed environments |
| 133 | +- **Workaround**: Test functionality using direct hook commands instead of pre-commit |
| 134 | + |
| 135 | +### Test Environment Setup |
| 136 | +- **Always install dev dependencies first**: `pip install .[dev]` |
| 137 | +- **Separately install clang tools**: `pip install clang-format==21.1.0 clang-tidy==21.1.0` |
| 138 | +- **Verify installation**: Check that `clang-format-hook --help` and `clang-tidy-hook --help` work |
| 139 | + |
| 140 | +## CI/CD Pipeline Notes |
| 141 | + |
| 142 | +- **GitHub Actions**: Uses Python 3.9-3.14 matrix testing |
| 143 | +- **Test timeout**: Set to allow 5.5+ minutes for full test suite |
| 144 | +- **Pre-commit CI**: May encounter timeout issues in certain environments |
| 145 | +- **Publishing**: Automated via GitHub Actions on releases |
0 commit comments