Detect duplicated code in Swift and Objective-C/C codebases.
Swift CPD is a Clone & Pattern Detector built on SwiftSyntax. It finds exact copies, parameterized clones, structural similarities, and semantically equivalent code across your project.
- Detects Type-1/Type-2 clones (exact and parameterized copies)
- Detects Type-3 clones (structural similarity with gaps via Greedy String Tiling)
- Detects Type-4 clones (semantic equivalence via AST-based behavior analysis)
- Supports cross-language detection between Swift and Objective-C/C
- Provides inline suppression with
// swiftcpd:ignore - Tracks known duplications with baseline workflow
- Produces deterministic, reproducible output
The recommended way to install Swift Clone & Pattern Detector is via Homebrew:
brew tap ericodx/homebrew-tools
brew install swift-cpdView Complete Installation Guide
- Swift Package Manager plugin
- Manual build from source
- Direct download of pre-compiled binaries
# Analyze Sources directory
swift-cpd Sources/
# JSON output with custom thresholds
swift-cpd --format json --min-tokens 30 --min-lines 3 Sources/
# Fail if duplication exceeds 5%
swift-cpd --max-duplication 5 Sources/
# HTML report to file
swift-cpd --format html --output report.html Sources/
# Xcode-compatible warnings
swift-cpd --format xcode Sources/
# Exclude generated files
swift-cpd --exclude "*.generated.swift" --exclude "**/Generated/**" Sources/
# Ignore same-file clones (cross-file only)
swift-cpd --ignore-same-file Sources/
# Ignore structural clones (Type-3/Type-4)
swift-cpd --ignore-structural Sources/See CLI Usage Reference for the complete list of options and flags.
// Package.swift
dependencies: [
.package(url: "https://github.com/ericodx/swift-cpd.git", from: "1.0.0"),
]
targets: [
.target(
name: "MyApp",
plugins: [
.plugin(name: "SwiftCPDPlugin", package: "SwiftCPD")
]
),
]Xcode Projects:
- Add
swift-cpdas a package dependency - Select your target > Build Phases
- Add SwiftCPDPlugin to "Run Build Tool Plug-ins"
See Xcode Plugin Installation for complete setup.
- name: Check duplication
run: swift-cpd --max-duplication 5 --format xcode Sources/- name: Check for new duplications
run: swift-cpd --baseline .swiftcpd-baseline.json --format xcode Sources/Swift CPD uses .swift-cpd.yml for configuration.
# Initialize configuration file
swift-cpd initSee CLI Usage Reference for configuration options and precedence rules.
// swiftcpd:ignore
func knownDuplicate() {
// This block is excluded from detection
}# Generate initial baseline
swift-cpd --baseline-generate Sources/
# Compare against baseline (fails only on new clones)
swift-cpd --baseline .swiftcpd-baseline.json Sources/
# Update baseline after accepting new clones
swift-cpd --baseline-update Sources/| Code | Meaning |
|---|---|
0 |
No clones detected (or within threshold) |
1 |
Clones detected (or above threshold) |
2 |
Configuration error |
3 |
Analysis error |
| Document | Description |
|---|---|
| Architecture | System design and pipeline pattern |
| Codebase | Module reference and implementation details |
| CLI Usage | Commands, flags, and configuration |
| Installation | Homebrew, SPM, and manual build |
| Xcode Plugin | Build tool plugin setup |
MIT