Code cloning, the duplication of code fragments, is common in software development. While some reuse aids productivity, excessive cloning hurts maintainability and introduces bugs. Hence, automatic code clone detection is vital. Meanwhile, large language models (LLMs) possess diverse code-related knowledge, making them versatile for various software engineering challenges. However, LLMs' performance in code clone detection is unclear and needs more study for accurate assessment.In this paper, we provide the first comprehensive evaluation of LLMs for clone detection, covering different clone types, languages, and prompts. We find advanced LLMs excel in detecting complex semantic clones, surpassing existing methods. Adding intermediate reasoning steps via chain-of-thought prompts noticeably enhances performance. Additionally, representing code as vector embeddings, especially with text encoders, effectively aids clone detection.Lastly, the ability of LLMs to detect code clones differs among various programming languages. Our study suggests that LLMs have potential for clone detection due to their language capabilities, offering insights for developing robust LLM-based methods to enhance software engineering.
翻译:代码克隆,即代码片段的复制,在软件开发中十分常见。虽然某些重用有助于提高生产力,但过度克隆会损害可维护性并引入缺陷。因此,自动代码克隆检测至关重要。与此同时,大型语言模型(LLMs)具备多样化的代码相关知识,使其能灵活应对各种软件工程挑战。然而,LLMs在代码克隆检测中的性能尚不明确,需要进一步研究以进行准确评估。本文首次对LLMs在克隆检测中的表现进行了全面评估,涵盖不同类型的克隆、编程语言和提示方式。我们发现,先进的LLMs在检测复杂语义克隆方面表现优异,超越了现有方法。通过链式思维提示引入中间推理步骤,能显著提升性能。此外,将代码表示为向量嵌入(尤其是使用文本编码器)能有效辅助克隆检测。最后,LLMs检测代码克隆的能力因编程语言而异。我们的研究表明,由于具备语言能力,LLMs在克隆检测方面具有潜力,这为开发基于LLM的稳健方法以改进软件工程提供了见解。