# Linter Setup (Phase 1) 本文件說明如何在本地與 CI 環境執行 Phase 1 的 linter 自動化。 ## 需求 - clang-tidy, clang-format - CMake 3.10+ ## 本地流程 1) 安裝工具 - Ubuntu/Debian: `sudo apt install clang-tidy clang-format cmake` - macOS: `brew install llvm cmake` 2) 產出 compile_commands.json(建議,提高 tidy 準確度) - `mkdir -p build && cd build` - `cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON` - `cd -` 3) 執行 linter - `./scripts/run-linter.sh` - 若偵測到 `build/compile_commands.json`,腳本會自動以 `-p build` 方式執行 clang-tidy。 4) 安裝 Git hooks(建議) - `./scripts/setup-hooks.sh` - pre-commit 會: - 對 staged C/C++ 檔執行 clang-format 並重新加入索引;若有改動會中止一次提交,請重新檢視並再次提交。 - 以建議模式執行 clang-tidy(不阻擋提交)。 ## CI 流程(Azure Pipelines) - 安裝工具 → `clang-format --dry-run --Werror` → CMake 匯出 `compile_commands.json` → 以 `-p build` 跑 clang-tidy(建議模式)→ Build → Run tests。 - Pull Request Job 只分析變更檔案,並以建議模式跑 clang-tidy。 ## Phase 1 政策 - 格式:必須符合 `.clang-format`,否則 CI 失敗、pre-commit 會自動修正。 - Tidy:使用 `.clang-tidy` 的安全規則集合,僅回報警告,不使 CI 失敗。 ## 後續升級(概述) - Phase 2:對關鍵規則(記憶體/資源/未定義行為)提升為失敗門檻。 - Phase 3:擴大規則(readability/modernize),對 PR 變更檔強制。