作者投稿和查稿 主编审稿 专家审稿 编委审稿 远程编辑

计算机工程 ›› 2026, Vol. 52 ›› Issue (9): 333-347. doi: 10.19678/j.issn.1000-3428.0252041

• 高性能计算与大数据 • 上一篇    下一篇

TGMM: 结合解析树与GPU的大规模多语言多粒度代码克隆检测

叶宇航1, 任潇宁1, 吴月明2,*()   

  1. 1. 中国科学技术大学计算机科学与技术学院, 安徽 合肥 230026
    2. 华中科技大学计算机科学与技术学院, 湖北 武汉 430074
  • 收稿日期:2025-01-14 修回日期:2025-03-21 出版日期:2026-09-15 发布日期:2025-05-09
  • 通讯作者: 吴月明
  • 作者简介:

    叶宇航, 男, 硕士研究生, 主研方向为代码克隆检测

    任潇宁, 博士研究生

    吴月明(通信作者), 教授

  • 基金资助:
    国家自然科学基金面上项目(61972373)

TGMM: Large-scale Multilingual and Multi-Granularity Code Clone Detection Combining with Parse Tree and GPU

YE Yuhang1, REN Xiaoning1, WU Yueming2,*()   

  1. 1. School of Computer Science and Technology, University of Science and Technology of China, Hefei 230026, Anhui, China
    2. School of Computer Science and Technology, Huazhong University of Science and Technology, Wuhan 430074, Hubei, China
  • Received:2025-01-14 Revised:2025-03-21 Online:2026-09-15 Published:2025-05-09
  • Contact: WU Yueming

摘要:

针对现有代码克隆检测工具在多语言适配与大规模克隆分析方面存在的不足, 提出一种基于解析树和图形处理器(GPU)加速的大规模代码克隆检测方法——TGMM。该方法采用三级处理架构进行克隆分析: 首先, 根据各编程语言的词法与语法规则生成标准化解析树, 并从中提取满足特定粒度要求的子树; 其次, 通过对子树进行剪枝和语义等价转换, 实现子树的简化与非功能性差异的消除; 最后, 利用GPU并行构建全局后缀数组, 实现大规模代码块相似度的快速计算。实验环节从克隆检测效能和语言扩展性两个维度对TGMM进行了测试: 在公开的基准数据集BigCloneBench上, TGMM以97%的精确率显著优于对比的7种主流工具, 其执行时间较次优工具缩短50%以上, 同时保证召回率在各类克隆类型上与对比工具相当; 在语言拓展性测试中, TGMM成功解析22种主流编程语言中的19种。此外, 通过应用TGMM对GitHub排名前45的项目(涵盖9种编程语言)执行多粒度克隆分析, 揭示了不同语言在克隆分布密度上的显著差异, 并详细分析了其背后成因, 从而为软件维护提供了切实有效的参考依据。

关键词: 克隆检测, 多语言, 大规模, 图形处理器加速, 解析树

Abstract:

This study proposes TGMM, a large-scale code clone detection method based on parse trees and Graphics Processing Unit (GPU) acceleration, to address the limitations of existing tools in multilingual adaptation and large-scale analysis. The method employs a three-stage architecture for clone detection. First, it generates standardized parse trees based on lexical and syntactic rules of each programming language and then extracts subtrees that meet specific granularity requirements. Second, it simplifies these subtrees by pruning and eliminates nonfunctional differences through semantic equivalence transformations. Finally, it leverages GPUs to construct a global suffix array in parallel, enabling rapid similarity calculation of large-scale code blocks. The proposed method is evaluated in terms of clone detection efficiency and language scalability. On the public benchmark dataset BigCloneBench, TGMM achieves a precision of 97%, significantly outperforming seven mainstream tools compared, while reducing execution time by over 50% compared to the second-best tool and maintaining a recall rate similar to the compared tools across various clone types. In the language scalability test, TGMM successfully parses 19 out of 22 mainstream programming languages. Furthermore, by applying TGMM to conduct a multi-granularity clone analysis of the top 45 GitHub projects (covering nine programming languages), the study reveals significant differences in clone density across different languages, provides an in-depth analysis of the underlying causes, and thus offers practical and effective references for software maintenance.

Key words: clone detection, multilingual, large-scale, Graphics Processing Unit (GPU) acceleration, parse tree