Skip to content

ericodx/swift-cpd

Swift Clone & Pattern Detector

Platform Swift CI Quality Gate Coverage

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.


What Swift CPD Does

  • 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

Installation

The recommended way to install Swift Clone & Pattern Detector is via Homebrew:

brew tap ericodx/homebrew-tools
brew install swift-cpd

Other Installation Methods

View Complete Installation Guide

  • Swift Package Manager plugin
  • Manual build from source
  • Direct download of pre-compiled binaries

Usage

# 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.


Xcode Integration

Build Tool Plugin (Recommended)

// 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:

  1. Add swift-cpd as a package dependency
  2. Select your target > Build Phases
  3. Add SwiftCPDPlugin to "Run Build Tool Plug-ins"

See Xcode Plugin Installation for complete setup.


CI Integration

GitHub Actions

- name: Check duplication
  run: swift-cpd --max-duplication 5 --format xcode Sources/

With Baseline

- name: Check for new duplications
  run: swift-cpd --baseline .swiftcpd-baseline.json --format xcode Sources/

Configuration

Swift CPD uses .swift-cpd.yml for configuration.

# Initialize configuration file
swift-cpd init

See CLI Usage Reference for configuration options and precedence rules.

Inline Suppression

// swiftcpd:ignore
func knownDuplicate() {
    // This block is excluded from detection
}

Baseline Workflow

# 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/

Exit Codes

Code Meaning
0 No clones detected (or within threshold)
1 Clones detected (or above threshold)
2 Configuration error
3 Analysis error

Documentation

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

License

MIT

About

Clone & Pattern Detector tool for Swift and Objective-C/C source code.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages