The invention discloses a
software row-level defect prediction method based on a hierarchical attention mechanism, and the method comprises the steps: converting each row of statements in a
source code file into vector representation through employing a CodeBERT pre-training model, so as to obtain the
semantic information of a code row; extracting a program
dependency graph of the
source code, recording a mapping relation between each node in the program
dependency graph and a
source code line, and calculating a
network measurement index corresponding to each node; fusing the semantic vectors of the code lines and the
network measurement indexes to generate fused node features so as to construct a program
dependency graph after the node features are expanded; constructing a HAGLineDP model, wherein the HAGLineDP model comprises a graph
feature extraction network and a dual-task classification network; the graph
feature extraction network designs three-level feature evolution paths from local to global and from structure to semantic, and sequentially comprises a
local structure aggregation layer, a structure generalization enhancement layer and a semantic importance weighting layer; the dual-task classification network comprises a node classification path and a
graph classification path; and training the HAGLineDP model by using a joint
loss function and an AdamW optimizer, outputting the defect probability of each
code line for a to-be-predicted
source code file through a node classification path, and sorting according to the defect probability to locate a high-risk
code line. According to the method, fine positioning of code defects can be effectively completed, and efficient distribution and utilization of
software testing resources are promoted.