文档 chevron_right 研究背景 — 代码克隆检测算法

研究背景

Deslop 组合了成熟的克隆检测技术:归一化语法树、Merkle 指纹、兄弟窗口、MinHash LSH 与可选的嵌入搜索。下表中的实现指针标明了每项已交付技术所在的位置。

为什么 AI 改变了克隆问题

在 AI 编码助手出现之前,代码克隆就已经是一种维护风险。既有的担忧并非每一处重复都自动算错;而是被复制的逻辑必须在各类修复、安全补丁和功能变更中保持一致。AI 辅助开发改变了经济账:再生成一个相似实现的成本下降了,因此重复逻辑可能在日常功能开发中进入仓库,而不再只是通过刻意的复制粘贴。

近期研究支持这种风险模型:

克隆分类法

Type I 是除布局和注释外的精确副本;Type II 允许重命名和字面量变化;Type III 包含语句编辑;Type IV 描述不同实现中的等价行为。Deslop 当前标签及更严格的文本一致性规则见工作原理。仅形状相同的结果不属于克隆分类,也不计入重复率。

实现

  • tree-sitter 解析和归一化寻找结构候选:crates/deslop-core/src/lang/。
  • 子树和连续语句指纹定位重复代码:fingerprint.rs 和 sibling.rs。
  • MinHash 与 LSH 检索近似候选:lsh.rs、lsh/banding.rs 和 tokens.rs。
  • 可选嵌入分析补充语义候选:embedding/。
  • 配对准入同时检查内容和结构:pair/content_gate.rs。明确的比较展示配对证据;簇不继承某一对的置信度。
  • 报告按重复质量排列克隆:report_weight.rs。百分比计算位于 report_metrics.rs。

这些是检测技术,不是安全重构的证明。独立检查见准确性文档。

参考文献

  • Baxter, Yahin, Moura, Sant'Anna, and Bier, Clone Detection Using Abstract Syntax Trees, 1998. PDF
  • Chilowicz, Duris, and Roussel, Syntax Tree Fingerprinting for Source Code Similarity Detection, 2009. PDF
  • Sajnani, Saini, Svajlenko, Roy, and Lopes, SourcererCC: Scaling Code Clone Detection to Big Code, 2016. arXiv
  • Roy and Cordy, NICAD: Accurate Detection of Near-Miss Intentional Clones Using Flexible Pretty-Printing and Code Normalization, 2008. ResearchGate entry
  • Shrivastava and Li, Densifying One Permutation Hashing via Rotation for Fast Near Neighbor Search, 2014. PDF
  • Roy, Alam, Al-omari, Roy, Roy, and Schneider, Unveiling the potential of large language models in generating semantic and cross-language clones, 2023. arXiv
  • Roy et al., GPTCloneBench: A comprehensive benchmark of semantic clones and cross-language clones using GPT-3 model and SemanticCloneBench, 2023. arXiv
  • Eagal, Stolee, and Ore, Analyzing the dependability of Large Language Models for code clone generation, 2025. Journal of Systems and Software
  • Liu et al., Code Copycat Conundrum: Demystifying Repetition in LLM-based Code Generation, 2025. arXiv
  • Alam et al., Are Classical Clone Detectors Good Enough For the AI Era?, 2025. arXiv
  • Wu et al., An Empirical Study of Code Clones from Commercial AI Code Generators, 2025. FSE 2025 entry
  • Liu, Widyasari, Zhao, Irsan, and Lo, Debt Behind the AI Boom: A Large-Scale Empirical Study of AI-Generated Code in the Wild, 2026. arXiv