一句话:用数据流感知预训练的 Transformer 自动改写粘贴代码中的变量标识符,实现代码适配。
开发者在复制粘贴代码片段后,通常需要手动修改变量名以匹配周围已有上下文,这一「代码适配」任务(程序修复的变体)此前缺乏有效方法。本文提出 AdaptivePaste,一种基于 Transformer 的源代码适配学习方法,并设计了一个数据流感知的去混淆预训练任务,以学习变量使用模式的有意义表示。在 Python 代码片段上,该方法取得 67.8% 的精确匹配准确率;通过并行解码器 Transformer 并结合置信度阈值的选择性适配设置,模型精度可进一步提升至 85.9%。作者还针对真实复制粘贴实例对 Python 开发者开展用户研究,结果表明 AdaptivePaste 将停留时间缩短至手动移植代码的近一半,并有助于避免缺陷,同时依据参与者反馈指出了改进方向。
In software development, it is common for programmers to copy-paste or port code snippets and then adapt them to their use case. This scenario motivates the code adaptation task – a variant of program repair which aims to adapt variable identifiers in a pasted snippet of code to the surrounding, preexisting context. However, no existing approach has been shown to effectively address this task. In this paper, we introduce AdaptivePaste, a learning-based approach to source code adaptation, based on transformers and a dedicated dataflow-aware deobfuscation pre-training task to learn meaningful representations of variable usage patterns. We demonstrate that AdaptivePaste can learn to adapt Python source code snippets with 67.8% exact match accuracy. We study the impact of confidence thresholds on the model predictions, showing the model precision can be further improved to 85.9% with our parallel-decoder transformer model in a selective code adaptation setting. To assess the practical use of AdaptivePaste we perform a user study among Python software developers on real-world copy-paste instances. The results show that AdaptivePaste reduces dwell time to nearly half the time it takes to port code manually, and helps to avoid bugs. In addition, we utilize the participant feedback to identify potential avenues for improvement.