一句话:提出 CodeGrid,将代码 token 嵌入保留布局的网格,为下游任务提供空间信号。
现有代码表示多依赖通用 NLP 或抽象语法树(AST),但 AST 忽略了控制流与数据流等代码结构,而纯图像表示性能接近 AST 表示,暗示空间信号尚未被充分利用。本文提出 CodeGrid,一种将代码 token 嵌入网格并保留代码布局的表示,且与下游任务无关,可同时服务生成与分类任务,并为 CNN、PixelCNN 等空间感知模型提供空间信号。作者在代码分类、克隆检测、漏洞检测和代码补全等任务上展开实验,并通过改变网格保留程度验证“代码是空间的”假设。结果显示 CodeGrid 能增强现有模型,在克隆检测中将 ASTNN 的 F1 提升 3.3%。
Code representation is a key step in the application of AI in software engineering. Generic NLP representations are effective but do not exploit all the rich structure inherent to code. Recent work has focused on extracting abstract syntax trees (AST) and integrating their structural information into code representations.These AST-enhanced representations advanced the state of the art and accelerated new applications of AI to software engineering. ASTs, however, neglect important aspects of code structure, notably control and data flow, leaving some potentially relevant code signal unexploited. For example, purely image-based representations perform nearly as well as AST-based representations, despite the fact that they must learn to even recognize tokens, let alone their semantics. This result, from prior work, is strong evidence that these new code representations can still be improved; it also raises the question of just what signal image-based approaches are exploiting. We answer this question. We show that code is spatial and exploit this fact to propose , a new representation that embeds tokens into a grid that preserves code layout. Unlike some of the existing state of the art, is agnostic to the downstream task: whether that task is generation or classification, can complement the learning algorithm with spatial signal. For example, we show that CNNs, which are inherently spatially-aware models, can exploit outputs to effectively tackle fundamental software engineering tasks, such as code classification, code clone detection and vulnerability detection. PixelCNN leverages ’s grid representations to achieve code completion. Through extensive experiments, we validate our spatial code hypothesis, quantifying model performance as we vary the degree to which the representation preserves the grid. To demonstrate its generality, we show that augments models, improving their performance on a range of tasks, On clone detection, improves ASTNN’s performance by 3.3% F1 score.