Previous studies have demonstrated that neural code comprehension models are vulnerable to identifier naming. By renaming as few as one identifier in the source code, the models would output completely irrelevant results, indicating that identifiers can be misleading for model prediction. However, identifiers are not completely detrimental to code comprehension, since the semantics of identifier names can be related to the program semantics. Well exploiting the two opposite impacts of identifiers is essential for enhancing the robustness and accuracy of neural code comprehension, and still remains under-explored. In this work, we propose to model the impact of identifiers from a novel causal perspective, and propose a counterfactual reasoning-based framework named CREAM. CREAM explicitly captures the misleading information of identifiers through multi-task learning in the training stage, and reduces the misleading impact by counterfactual inference in the inference stage. We evaluate CREAM on three popular neural code comprehension tasks, including function naming, defect detection and code classification. Experiment results show that CREAM not only significantly outperforms baselines in terms of robustness (e.g., +37.9% on the function naming task at F1 score), but also achieve improved results on the original datasets (e.g., +0.5% on the function naming task at F1 score).
翻译:先前研究表明,神经网络代码理解模型易受标识符命名的影响。通过重命名源代码中哪怕一个标识符,模型就会输出完全不相关的结果,这表明标识符可能误导模型预测。然而,标识符并非完全有害于代码理解,因为标识符名称的语义可能与程序语义相关。妥善利用标识符这两种相反的影响对于增强神经网络代码理解的鲁棒性和准确性至关重要,但该问题仍未被充分探索。本文从新的因果视角对标识符的影响进行建模,并提出一个名为CREAM的基于反事实推理的框架。CREAM在训练阶段通过多任务学习显式捕获标识符的误导信息,并在推理阶段通过反事实推理减少误导性影响。我们在三个流行的神经网络代码理解任务(包括函数命名、缺陷检测和代码分类)上评估CREAM。实验结果表明,CREAM不仅在鲁棒性上显著优于基线(例如,在函数命名任务上F1分数提升37.9%),而且在原始数据集上也取得了改进结果(例如,在函数命名任务上F1分数提升0.5%)。