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凭借其语言能力在克隆检测方面具有潜力,为开发基于LLMs的鲁棒方法以增强软件工程提供了见解。