- 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
129 lines
3.8 KiB
YAML
129 lines
3.8 KiB
YAML
# Azure DevOps Pipeline for C++ Linter Template
|
|
trigger:
|
|
branches:
|
|
include:
|
|
- main
|
|
- develop
|
|
|
|
pr:
|
|
branches:
|
|
include:
|
|
- main
|
|
- develop
|
|
|
|
pool:
|
|
vmImage: 'ubuntu-latest'
|
|
|
|
variables:
|
|
buildConfiguration: 'Debug'
|
|
|
|
stages:
|
|
- stage: LintAndBuild
|
|
displayName: 'Lint and Build'
|
|
jobs:
|
|
- job: StaticAnalysis
|
|
displayName: 'Static Analysis with clang-tidy'
|
|
steps:
|
|
- task: Bash@3
|
|
displayName: 'Install Dependencies'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y clang-tidy clang-format cmake build-essential
|
|
clang-tidy --version
|
|
cmake --version
|
|
|
|
- task: Bash@3
|
|
displayName: 'Check Code Format'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
echo "Checking code format..."
|
|
find src tests -name "*.cpp" -o -name "*.h" | xargs clang-format --dry-run --Werror
|
|
|
|
- task: Bash@3
|
|
displayName: 'Configure build (export compile_commands)'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
echo "Configuring CMake to export compile_commands.json..."
|
|
mkdir -p build
|
|
cd build
|
|
cmake .. -DCMAKE_BUILD_TYPE=${{ variables.buildConfiguration }} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
|
|
ls -la
|
|
|
|
- task: Bash@3
|
|
displayName: 'Run clang-tidy on all files (advisory)'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
echo "Running clang-tidy (Phase 1: advisory only)..."
|
|
FILES=$(find src tests -type f \( -name "*.cpp" -o -name "*.cxx" -o -name "*.cc" -o -name "*.c++" -o -name "*.h" -o -name "*.hpp" -o -name "*.hxx" \))
|
|
if [ -n "$FILES" ]; then
|
|
echo "$FILES" | xargs -r clang-tidy -p build --config-file=.clang-tidy || true
|
|
fi
|
|
echo "Linter completed. Review warnings above."
|
|
|
|
- task: Bash@3
|
|
displayName: 'Build Project'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
cd build
|
|
cmake .. -DCMAKE_BUILD_TYPE=${{ variables.buildConfiguration }}
|
|
make -j$(nproc)
|
|
|
|
- task: Bash@3
|
|
displayName: 'Run Tests'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
cd build
|
|
./tests
|
|
./main
|
|
|
|
- job: PRAnalysis
|
|
displayName: 'PR Changed Files Analysis'
|
|
condition: eq(variables['Build.Reason'], 'PullRequest')
|
|
steps:
|
|
- task: Bash@3
|
|
displayName: 'Install Dependencies'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y clang-tidy cmake build-essential
|
|
cmake --version
|
|
|
|
- task: Bash@3
|
|
displayName: 'Configure build (export compile_commands)'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
echo "Configuring CMake to export compile_commands.json for PR analysis..."
|
|
mkdir -p build
|
|
cd build
|
|
cmake .. -DCMAKE_BUILD_TYPE=${{ variables.buildConfiguration }} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
|
|
- task: Bash@3
|
|
displayName: 'Analyze Changed Files Only'
|
|
inputs:
|
|
targetType: 'inline'
|
|
script: |
|
|
echo "Analyzing only changed files in PR..."
|
|
|
|
# 取得變更的檔案
|
|
git fetch origin $(System.PullRequest.TargetBranch)
|
|
CHANGED_FILES=$(git diff origin/$(System.PullRequest.TargetBranch)...HEAD --name-only | grep -E '\.(cpp|cxx|cc|c\+\+|h|hpp|hxx)$' || true)
|
|
|
|
if [ -z "$CHANGED_FILES" ]; then
|
|
echo "No C++ files changed in this PR"
|
|
exit 0
|
|
fi
|
|
|
|
echo "Changed C++ files:"
|
|
echo "$CHANGED_FILES"
|
|
|
|
echo "Running clang-tidy on changed files (advisory)..."
|
|
echo "$CHANGED_FILES" | xargs -r clang-tidy -p build --config-file=.clang-tidy || true
|