[论文解读] Learning to Represent Programs with Graphs
本文提出将源代码表示为结构化图,整合语法、数据流和类型层次结构,以提升程序理解能力。采用门控图神经网络(GGNNs)学习语义表示,在VarNaming任务中达到32.9%的准确率,在VarMisuse任务中达到85.5%的准确率,成功检测到RavenDB和Roslyn等开源项目中的真实缺陷。
Learning tasks on source code (i.e., formal languages) have been considered recently, but most work has tried to transfer natural language methods and does not capitalize on the unique opportunities offered by code's known syntax. For example, long-range dependencies induced by using the same variable or function in distant locations are often not considered. We propose to use graphs to represent both the syntactic and semantic structure of code and use graph-based deep learning methods to learn to reason over program structures. In this work, we present how to construct graphs from source code and how to scale Gated Graph Neural Networks training to such large graphs. We evaluate our method on two tasks: VarNaming, in which a network attempts to predict the name of a variable given its usage, and VarMisuse, in which the network learns to reason about selecting the correct variable that should be used at a given program location. Our comparison to methods that use less structured program representations shows the advantages of modeling known structure, and suggests that our models learn to infer meaningful names and to solve the VarMisuse task in many cases. Additionally, our testing showed that VarMisuse identifies a number of bugs in mature open-source projects.
研究动机与目标
- 解决现有代码表示方法忽略深层语义结构(如数据流和类型层次结构)的局限性。
- 提出一项新的机器学习任务——变量误用预测(VarMisuse),该任务需要对程序语义进行推理。
- 证明基于图的模型能够学习具有泛化能力的有意义程序表示,适用于多样化的代码库。
- 通过在RavenDB和Roslyn等成熟开源项目中检测真实缺陷,验证该方法的实际应用价值。
提出的方法
- 通过编码语法关系(如标记顺序)和语义关系(如数据流、参数绑定)构建程序图。
- 将数据流和类型层次结构信息整合到图结构中,以增强语义信号。
- 在大规模程序图上训练门控图神经网络(GGNNs),以学习代码元素的分布式表示。
- 利用学习到的表示预测变量名称(VarNaming)并从代码槽位中选择正确变量(VarMisuse)。
- 将GGNN训练扩展至可处理源自290万行真实世界源代码的大规模图结构。
- 在学习到的表示上应用余弦相似度,分析代码片段之间的语义相似性。
实验结果
研究问题
- RQ1基于图的神经网络能否有效建模源代码中的语法和语义结构,从而改善程序表示?
- RQ2在程序图上进行训练的模型,能否根据使用上下文准确预测变量名称(VarNaming)?
- RQ3在程序图上进行训练的模型,能否检测出语义上错误但语法上正确的变量误用错误?
- RQ4学习到的表示在多大程度上捕捉了代码片段之间的有意义语义相似性?
- RQ5VarMisuse模型能否识别出生产级软件中此前未被发现的真实缺陷?
主要发现
- 所提出的基于图的模型在VarNaming任务中达到32.9%的准确率,显著优于依赖结构化程度较低表示的基线模型。
- 该模型在VarMisuse任务中达到85.5%的准确率,展现出在跨代码上下文推理变量语义方面的强大能力。
- 该模型成功检测到RavenDB中3个此前未知的缺陷,以及Roslyn中3个此前未知的缺陷,这些缺陷已由开发人员私下报告并修复。
- 可视化结果表明,模型学习到了语义上有意义的表示,能将不同代码段中相似的变量使用模式(如空值检查)聚类在一起。
- 该方法识别出RavenDB中一处内存效率问题:缓冲区被不必要地设置为'length'大小,而非'size',这是一个细微但可度量的问题。
- 该方法补充了传统静态分析,能够识别出编译器或语法检查无法捕获的缺陷,例如错误的错误信息或复制粘贴错误。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。