Manuscript Offprint

arXiv:2608.22383

Learning Spectral Representations of Code through Latent Graph Learning for Generalizable Cross-Language Code Clone Detection

Mohsen Hesamolhokama, Ali Sadeghi, Kousha Moeini, Behnam Rohani, Mohammadamin Fazli, Jafar Habibi

Abstract

Current code clone detection (CCD) methods rely on fixed, language-specific graph representations like abstract syntax trees (ASTs) or program dependency graphs (PDGs). Because functionally identical code fragments can yield wildly different structures, these rigid graphs produce non-discriminative spectra that perform close to chance. To address this, we propose SPECTRA-SIAM, a Siamese latent graph learning network that learns a latent space such that the graph's spectrum serves as a discriminative signature of code functionality by optimizing downstream CCD performance. Given a fragment's AST and data-dependencies, SPECTRA-SIAM induces a fixed-size weighted latent graph through soft slot assignment and multi-head attention, and extracts a multi-scale spectral representation from its normalized Laplacian. Mapping all fragments into this shared space yields comparable spectra across programming languages. Experiments on BigCloneBench, AtCoder, and a four-language CodeNet benchmark (Java, Python, C++, C#) support this design choice. Using the same downstream classifier, moving from fixed to learned latent graphs spectra jumps F1 from 0.37 to 0.67 on BigCloneBench and accuracy from 0.60 to 0.71 on AtCoder. On CodeNet, the full model reaches 0.69 accuracy in four epochs and 0.79 after thirty epochs. In bridge-assisted language transfer across 60 unseen paths, SPECTRA-SIAM's performance degrades by only 0.058, versus 0.112–0.228 for baselines, showing that learned graph spectra provide a highly generalizable representation for cross-language clone detection.

Keywords: Cross-Language Code Clone Detection, Source Code Representation, Graph Representation Learning, Latent Graph Learning, Spectral Graph Analysis,

1. Introduction

Nearly every automated software engineering task begins with source code, and its effectiveness depends on how the underlying program is represented (Allamanis et al., 2018b). A useful representation should preserve the structural and semantic information required for automated reasoning about programs. This requirement is central to tasks such as code search (Gu et al., 2018), plagiarism detection (Schleimer et al., 2003), and vulnerability analysis (Zhou et al., 2019). Among these tasks, Code Clone Detection (CCD) aims to identify fragments that are identical, syntactically similar, or functionally equivalent. Such clones are prevalent in real-world software systems and increase maintenance costs because modifications to one fragment may need to be consistently propagated to its counterparts (Juergens et al., 2009; Roy and Cordy, 2007).

The effectiveness of CCD depends strongly on whether a representation captures meaningful similarities beyond surface-level patterns. Early approaches represented code primarily as text or token sequences (Schleimer et al., 2003). Later methods incorporated abstract syntax trees (ASTs) using tree-based neural networks (Mou et al., 2016), while another line learned distributed representations from code tokens and their contexts (Mikolov et al., 2013; Alon et al., 2019). More recently, the Transformer architecture (Vaswani et al., 2017) and pre-trained models such as BERT (Devlin et al., 2019) have been adapted to source code, resulting in models such as CodeBERT (Feng et al., 2020) and GraphCodeBERT (Guo et al., 2021). All these approaches share the goal of mapping semantically related

∗^{*}Corresponding author.

Email addresses: hokama@ce.sharif.edu (Mohsen Hesamolhokama), ali.sadeghi@sharif.edu (Ali Sadeghi), Kousha.m83@sharif.edu (Kousha Moeini), behnam.rohani058@sharif.edu (Behnam Rohani), fazli@sharif.edu (Mohammadamin Fazli), jhabibi@sharif.edu (Jafar Habibi)

Preprint submitted to Elsevier

August 25, 2026 fragments to similar representations. Contrastive learning further strengthens this objective by encouraging such alignment through semantics-preserving transformations (Chen et al., 2020a; Jain et al., 2021; Bui et al., 2021b) or self-supervised objectives derived from AST structure (Bui et al., 2021a).

Despite these advances, capturing program semantics remains a major challenge for CCD. Clones are commonly categorized into four types (Roy and Cordy, 2007); while the first three involve varying degrees of syntactic similarity, Type-IV clones represent functionally equivalent fragments and are difficult to detect (Al-Omari et al., 2020). Existing CCD approaches have explored learned representations through deep feature models (White et al., 2016; Wei and Li, 2017), AST-based encoders (Zhang et al., 2019), and graph-based representations that incorporate richer program structures (Zhao and Huang, 2018; Wang et al., 2020; Wu et al., 2020). However, semantic clone detectors often generalize poorly to projects outside their training data, limiting their ability to handle unseen code (Sonnekalb et al., 2022). Graph-based methods incorporate richer program information through ASTs, CFGs, DDGs, or CPGs (Yamaguchi et al., 2014; Zhao and Huang, 2018; Wang et al., 2020; Wu et al., 2020), but these structures are fixed before training as the model learns how to encode a given graph. This limitation becomes significant in cross-language clone detection, where equivalent functionality may be expressed through different syntactic structures and language constructs (Perez and Chiba, 2019; Zhang et al., 2024; Alam et al., 2023).

Graph spectra offer an alternative by summarizing graph structure through the eigenvalues of adjacency or Laplacian matrices. These eigenvalues are invariant to node relabeling (Chung, 1997; Mohar, 1991), capture global connectivity properties (Fiedler, 1973), and can be truncated to a bounded number of leading components (Defferrard et al., 2016a). Although spectral descriptors have been widely used as structural signatures in geometry processing (Sun et al., 2009a; Zhang et al., 2010) and graph mining (Belkin and Niyogi, 2003; Narayanan et al., 2017), their use in software engineering has largely focused on software architecture analysis, such as partitioning module dependency graphs (Mitchell and Mancoridis, 2006). To our knowledge, the graph spectrum has not been explored as a representation of individual program fragments for source code representation learning or clone detection.

This motivates our central design choice: we do not commit to any predefined program graph. We learn a graph whose spectral properties are optimized for distinguishing functional equivalence. This perspective follows latent graph learning, where the graph structure itself is treated as a learnable variable jointly optimized with the prediction task (Kipf et al., 2018; Franceschi et al., 2019; Chen et al., 2020b; Zhu et al., 2021; Saha et al., 2023). We propose SPECTRA-SIAM, a Siamese model that transforms the AST and projected data-dependence information of each code fragment into a language-independent latent graph. The learned graph provides a compact and comparable structure across fragments of different sizes, while its spectral properties serve as the basis for the final representation. Unlike approaches that commit to a fixed AST, CFG, DDG, or CPG structure (Yamaguchi et al., 2014), SPECTRA-SIAM lets clone supervision determine the topology that best captures functional similarity. A shared representation space across parsers further enables cross-language clone detection.

References (100)

Couldn't load all references. Reload the page to try again.