feat(linter): implement Phase 1 linter automation and docs

- Add .clang-tidy (analyzer + selected bugprone) and .clang-format (LLVM, 4-space, 100 cols)
- Enhance scripts/run-linter.sh to use compile_commands.json when available
- Add scripts/setup-hooks.sh pre-commit (format enforcement; advisory tidy)
- Update azure-pipelines.yml to export compile_commands and run clang-tidy -p build
- Fill docs/linter-setup.md and docs/coding-standards.md for Phase 1
- Add minimal tests in tests/test_main.cpp to ensure CI executes
- Rewrite README with Phase 1 workflow
This commit is contained in:
2025-09-09 22:52:34 +08:00
parent d5350c35a8
commit f55d10ee07
9 changed files with 296 additions and 81 deletions

View File

@@ -0,0 +1,22 @@
# Coding Standards (Phase 1)
## 語言與標準
- C++98 為主要標準(舊代碼相容)。
- 禁止使用危險 API偏好安全封裝必要時以註解說明風險與保護措施。
## 格式(由 `.clang-format` 強制)
- BasedOnStyle: LLVMIndentWidth: 4ColumnLimit: 100。
- 以 pre-commit 與 CI 確保格式一致性。
## 一般規範
- 生命週期new/delete 成對,避免裸指標長期持有;儘量早釋放資源。
- 控制流:避免過深巢狀;優先早回傳以簡化邏輯。
- 註解:描述「為何」而非「做什麼」。
- 介面:標頭檔最小公開;避免不必要的 include採前置宣告以降低耦合。
## Tidy 規則Phase 1
- 啟用:`clang-analyzer-*` 與精選 `bugprone-*`,聚焦重大缺陷(洩漏、未定義行為)。
- 停用:`modernize-*``readability-*`(後續階段再逐步導入)。
## 例外處理
- 舊代碼可逐步改善若需暫時抑制特定告警請附上註解與追蹤項TODO/issue

View File

@@ -0,0 +1,39 @@
# 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 變更檔強制。