Table of Contents
Fetching ...

CC2Vec: Combining Typed Tokens with Contrastive Learning for Effective Code Clone Detection

Shihan Dou, Yueming Wu, Haoxiang Jia, Yuhao Zhou, Yan Liu, Yang Liu

TL;DR

Code clone detection is challenged by semantic clones that differ syntactically yet preserve functionality. CC2Vec introduces a typed-token encoding with two layers of self-attention plus contrastive learning to preserve program details while remaining robust to semantic variation. It achieves competitive or superior results compared with pretrained and deep-learning baselines on BigCloneBench and Google Code Jam, while offering faster detection for large-scale code. The approach provides interpretable attention-based insights into which token categories drive clone decisions and demonstrates scalable performance suitable for real-world code bases.

Abstract

With the development of the open source community, the code is often copied, spread, and evolved in multiple software systems, which brings uncertainty and risk to the software system (e.g., bug propagation and copyright infringement). Therefore, it is important to conduct code clone detection to discover similar code pairs. Many approaches have been proposed to detect code clones where token-based tools can scale to big code. However, due to the lack of program details, they cannot handle more complicated code clones, i.e., semantic code clones. In this paper, we introduce CC2Vec, a novel code encoding method designed to swiftly identify simple code clones while also enhancing the capability for semantic code clone detection. To retain the program details between tokens, CC2Vec divides them into different categories (i.e., typed tokens) according to the syntactic types and then applies two self-attention mechanism layers to encode them. To resist changes in the code structure of semantic code clones, CC2Vec performs contrastive learning to reduce the differences introduced by different code implementations. We evaluate CC2Vec on two widely used datasets (i.e., BigCloneBench and Google Code Jam) and the results report that our method can effectively detect simple code clones. In addition, CC2Vec not only attains comparable performance to widely used semantic code clone detection systems such as ASTNN, SCDetector, and FCCA by simply fine-tuning, but also significantly surpasses these methods in both detection efficiency.

CC2Vec: Combining Typed Tokens with Contrastive Learning for Effective Code Clone Detection

TL;DR

Code clone detection is challenged by semantic clones that differ syntactically yet preserve functionality. CC2Vec introduces a typed-token encoding with two layers of self-attention plus contrastive learning to preserve program details while remaining robust to semantic variation. It achieves competitive or superior results compared with pretrained and deep-learning baselines on BigCloneBench and Google Code Jam, while offering faster detection for large-scale code. The approach provides interpretable attention-based insights into which token categories drive clone decisions and demonstrates scalable performance suitable for real-world code bases.

Abstract

With the development of the open source community, the code is often copied, spread, and evolved in multiple software systems, which brings uncertainty and risk to the software system (e.g., bug propagation and copyright infringement). Therefore, it is important to conduct code clone detection to discover similar code pairs. Many approaches have been proposed to detect code clones where token-based tools can scale to big code. However, due to the lack of program details, they cannot handle more complicated code clones, i.e., semantic code clones. In this paper, we introduce CC2Vec, a novel code encoding method designed to swiftly identify simple code clones while also enhancing the capability for semantic code clone detection. To retain the program details between tokens, CC2Vec divides them into different categories (i.e., typed tokens) according to the syntactic types and then applies two self-attention mechanism layers to encode them. To resist changes in the code structure of semantic code clones, CC2Vec performs contrastive learning to reduce the differences introduced by different code implementations. We evaluate CC2Vec on two widely used datasets (i.e., BigCloneBench and Google Code Jam) and the results report that our method can effectively detect simple code clones. In addition, CC2Vec not only attains comparable performance to widely used semantic code clone detection systems such as ASTNN, SCDetector, and FCCA by simply fine-tuning, but also significantly surpasses these methods in both detection efficiency.
Paper Structure (31 sections, 1 equation, 7 figures, 7 tables)

This paper contains 31 sections, 1 equation, 7 figures, 7 tables.

Figures (7)

  • Figure 1: Examples of four code clone types
  • Figure 2: Source code and corresponding tokens of gcd$_1$.java and gcd$_2$.java after lexical analysis
  • Figure 3: System of CC2Vec
  • Figure 4: Token categorization of CC2Vec
  • Figure 5: Program encoding of CC2Vec
  • ...and 2 more figures