Detect and eliminate duplicated logic in Swift and Objective-C/C codebases to improve maintainability and code quality.
swift-cpd performs structural analysis to detect duplication patterns across Swift, Objective-C, and C codebases, going beyond simple text-based detection.
Code duplication leads to:
- inconsistent behavior across features
- fragile refactors and hidden regressions
- increased maintenance cost and cognitive load
swift-cpd helps you detect and address duplication early, supporting long-term code health and developer productivity.
- Structural duplication detection (AST-based)
- Works with Swift, Objective-C, and C codebases
- Enforces duplication rules in CI pipelines
- Supports code quality and governance practices
brew tap ericodx/homebrew-tools
brew install swift-cpdOther installation methods — pre-built binary, build from source, pre-commit hook, Xcode plugin — are covered in the Installation Guide.
# Generate a config file (auto-detects your source directories)
swift-cpd init
# Run
swift-cpdExample output:
Clone detected — Type 2 | 15 lines | 120 tokens | 100.0% similarity
Sources/App/Services/UserService.swift : 34 – 48
Sources/App/Services/ProductService.swift : 71 – 85
1 clone(s) found in 32 file(s) — 2.1% duplication — 0.8s
Drop a .swift-cpd.yml in the project root to control paths, thresholds, excluded files, and output format:
paths:
- Sources/
minimumTokenCount: 50
minimumLineCount: 5
enabledCloneTypes: [1, 2, 3, 4]
ignoreSameFile: true
exclude:
- "**/*Tests*"
- "**/*.generated.swift"Full reference in the Usage & Configuration Guide.
| Document | Description |
|---|---|
| Installation | Homebrew, binary, source, pre-commit, Xcode plugin |
| Usage & Configuration | CLI options, YAML config, output formats, CI integration |
| Xcode Plugin | Step-by-step Xcode and SPM plugin setup |
| Architecture | System design, pipeline, detection algorithms |
| CodeBase Reference | Every type, protocol, and algorithm documented |