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.
翻译:代码克隆,即代码片段的重复现象,在软件开发中普遍存在。虽然部分重复有助于提高效率,但过度克隆会损害可维护性并引入缺陷。因此,自动化代码克隆检测至关重要。与此同时,大型语言模型具备丰富的代码相关知识,使其能够灵活应对各类软件工程挑战。然而,大型语言模型在代码克隆检测中的具体表现尚不明确,亟需深入研究以进行准确评估。本文首次对大型语言模型在克隆检测中的能力进行了全面评估,涵盖不同类型克隆、编程语言及提示策略。研究发现,先进的大型语言模型在检测复杂语义克隆方面表现出色,超越了现有方法。通过思维链提示引入中间推理步骤可显著提升性能。此外,将代码表示为向量嵌入(尤其是借助文本编码器)能有效辅助克隆检测。最后,大型语言模型检测代码克隆的能力因编程语言而异。本研究表明,凭借其语言处理能力,大型语言模型在代码克隆检测领域具有应用潜力,为开发基于大型语言模型的稳健方法以优化软件工程提供了重要启示。