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

Shihan Dou,Yueming Wu,Haoxiang Jia,Yuhao Zhou,Yan Liu,Yang Liu
2024-05-01
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.
Software Engineering
What problem does this paper attempt to address?
This paper attempts to solve two main problems in code clone detection: 1. **How to preserve program semantics when only analyzing source - code tokens?** 2. **How to distinguish code based on its function rather than its structure?** ### Problem Background With the development of the open - source community, code is often copied, spread, and evolved in multiple software systems, which brings uncertainty and risks to software systems (e.g., error propagation and copyright infringement). Therefore, it is very important to conduct code clone detection to find similar code pairs. Although many existing code clone detection methods can handle large - scale code (such as token - based methods), they cannot handle more complex semantic code clones due to the lack of program details. ### Solution Overview To solve the above problems, the author proposes CC2Vec, a novel code encoding method that combines contrastive learning and self - attention mechanism. The main contributions of CC2Vec are as follows: - **Challenge 1: Preserving Program Semantics** CC2Vec preserves program semantics by parsing the source code into different types of tokens (i.e., typed tokens) and encoding them using a self - attention mechanism layer. Specifically, CC2Vec first divides the tokens into 15 categories and then applies two - layer self - attention mechanism layers to encode these tokens into vector representations. This method not only explains the detection results but also preserves the potential relationships between tokens, which may represent the program details of certain methods. - **Challenge 2: Distinguishing Code Based on Function Rather than Structure** To meet this challenge, CC2Vec introduces contrastive learning to train the program encoder. The goal of contrastive learning is to maximize the representational similarity between a code fragment and its clone code (i.e., positive sample), while minimizing the representational similarity between a code fragment and its non - clone code (i.e., negative sample). For semantic code clones, although their code structures may vary greatly, the implemented functions remain unchanged. Therefore, contrastive learning can reduce the distance between semantic code pairs while increasing the differences between different code pairs. After contrastive learning, CC2Vec can identify code clones that are semantically similar but syntactically different. ### Experimental Verification To verify the effectiveness of CC2Vec, the author evaluated it on two widely used datasets (BigCloneBench and Google Code Jam). The experimental results show that CC2Vec not only performs well in detecting simple code clones but also can effectively detect more complex semantic code clones, and is significantly more efficient than existing methods. ### Formula Representation In terms of formula representation, the objective function of contrastive learning can be expressed as: \[ \mathcal{L}=-\log \frac{\exp(\text{sim}(f(x_i), f(x_j^+)) / \tau)}{\sum_{k = 1}^{N}\exp(\text{sim}(f(x_i), f(x_k^-)) / \tau)} \] where: - \( f(x_i) \) is the encoded representation of code fragment \( x_i \), - \( x_j^+ \) is the positive sample of \( x_i \) (i.e., clone code), - \( x_k^- \) is the negative sample of \( x_i \) (i.e., non - clone code), - \( \text{sim} \) is a similarity function (e.g., cosine similarity), - \( \tau \) is a temperature parameter. In conclusion, by combining contrastive learning and self - attention mechanism, CC2Vec effectively solves the key challenges in code clone detection, especially making significant progress in semantic code clone detection.