Files
cpp-linter-template/README.md

2.7 KiB

C++ Linter Template

這是一個展示如何在 C++ 專案中整合 linter 工具的模板專案。

目的

  • 展示 clang-tidy 在舊 C++ 代碼中的應用
  • 提供漸進式導入 linter 的範例
  • Azure DevOps CI/CD 整合範例

快速開始

1. 本地開發環境設定

# 安裝相依套件 (Ubuntu/Debian)
sudo apt install clang-tidy clang-format cmake build-essential

# 安裝相依套件 (macOS)
brew install llvm cmake

# 克隆專案
git clone <your-repo-url>
cd cpp-linter-template

2. 執行 Linter

# 使用提供的腳本
./scripts/run-linter.sh

# 或手動執行
clang-tidy src/*.cpp src/*.h tests/*.cpp --config-file=.clang-tidy

3. 建構專案

mkdir build && cd build
cmake ..
make
./main

Linter 配置說明

當前採用階段一設定:

  • 只檢查最關鍵的錯誤
  • 不會因 linter 警告而中斷 CI
  • 適合舊代碼逐步改進

後續階段計劃

  • 階段二:增加記憶體和資源管理檢查
  • 階段三:加入可讀性和現代化建議
  • 階段四:強制執行,警告視為錯誤

在 Azure DevOps 中使用

  1. 將此專案推送到 Azure DevOps
  2. 設定 Pipeline 使用 azure-pipelines.yml
  3. 建立 Pull Request 時會自動執行 linter

自定義配置

編輯 .clang-tidy 檔案來調整檢查規則:

# 增加更多檢查
Checks: '-*,clang-analyzer-*,bugprone-*,readability-*'

檔案說明

.clang-tidy: Linter 配置檔 .clang-format: 程式碼格式化配置 azure-pipelines.yml: Azure DevOps CI/CD 配置 scripts/run-linter.sh: 本地執行 linter 腳本

注意事項

  • 此模板使用 C++98 標準,適合舊代碼
  • Linter 設定較為寬鬆,適合逐步導入
  • 可以根據團隊需求調整檢查規則

專案結構

cpp-linter-template/ ├── .clang-tidy # linter 配置檔 ├── .clang-format # 格式化配置檔
├── azure-pipelines.yml # Azure DevOps CI/CD 配置 ├── CMakeLists.txt # 建構配置 ├── README.md # 專案說明 ├── docs/ # 文件目錄 │ ├── linter-setup.md # Linter 設定指南 │ └── coding-standards.md # 編碼標準 ├── src/ # 原始碼 │ ├── main.cpp # 主程式(含各種測試案例) │ ├── utils.cpp # 工具函數 │ └── utils.h # 標頭檔 ├── tests/ # 測試程式碼 │ └── test_main.cpp # 簡單測試 └── scripts/ # 輔助腳本 ├── run-linter.sh # 本地執行 linter └── setup-hooks.sh # 設定 git hooks