ISSTA2023

CodeGrid: A Grid Representation of Code

Abdoul Kader Kaboré, Earl T. Barr, Jacques Klein, Tegawendé F. Bissyandé

4 citations

Abstract

Code representation is a key step in the application of AI in software engineering. Generic NLP representations are e ective 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 ow, 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 CodeGrid, a new representation that embeds tokens into a grid that preserves code layout. Unlike some of the existing state of the art, CodeGrid is agnostic to the downstream task: whether that task is generation or classi cation, CodeGrid can complement the learning algorithm with spatial signal. For example, we show that CNNs, which are inherently spatially-aware models, can exploit CodeGrid outputs to e ectively tackle fundamental software engineering tasks, such as code classi cation, code clone detection and vulnerability detection. PixelCNN leverages CodeGrid'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 CodeGrid augments models, improving their performance on a range of tasks. On clone detection, CodeGrid improves ASTNN's performance by 3.3% F1 score. * Some work carried out while a visiting scholar at Google DeepMind.