@@ -43,7 +43,16 @@ fn populate_empty_file(writer: &mut trap::Writer) -> trap::Label {
43
43
44
44
pub fn populate_empty_location ( writer : & mut trap:: Writer ) {
45
45
let file_label = populate_empty_file ( writer) ;
46
- location ( writer, file_label, 0 , 0 , 0 , 0 ) ;
46
+ global_location (
47
+ writer,
48
+ file_label,
49
+ trap:: Location {
50
+ start_line : 0 ,
51
+ start_column : 0 ,
52
+ end_line : 0 ,
53
+ end_column : 0 ,
54
+ } ,
55
+ ) ;
47
56
}
48
57
49
58
pub fn populate_parent_folders (
@@ -85,28 +94,54 @@ pub fn populate_parent_folders(
85
94
}
86
95
}
87
96
88
- fn location (
97
+ /** Get the label for the given location, defining it a global ID if it doesn't exist yet. */
98
+ fn global_location (
89
99
writer : & mut trap:: Writer ,
90
100
file_label : trap:: Label ,
91
- start_line : usize ,
92
- start_column : usize ,
93
- end_line : usize ,
94
- end_column : usize ,
101
+ location : trap:: Location ,
95
102
) -> trap:: Label {
96
103
let ( loc_label, fresh) = writer. global_id ( & format ! (
97
104
"loc,{{{}}},{},{},{},{}" ,
98
- file_label, start_line, start_column, end_line, end_column
105
+ file_label,
106
+ location. start_line,
107
+ location. start_column,
108
+ location. end_line,
109
+ location. end_column
99
110
) ) ;
100
111
if fresh {
101
112
writer. add_tuple (
102
113
"locations_default" ,
103
114
vec ! [
104
115
trap:: Arg :: Label ( loc_label) ,
105
116
trap:: Arg :: Label ( file_label) ,
106
- trap:: Arg :: Int ( start_line) ,
107
- trap:: Arg :: Int ( start_column) ,
108
- trap:: Arg :: Int ( end_line) ,
109
- trap:: Arg :: Int ( end_column) ,
117
+ trap:: Arg :: Int ( location. start_line) ,
118
+ trap:: Arg :: Int ( location. start_column) ,
119
+ trap:: Arg :: Int ( location. end_line) ,
120
+ trap:: Arg :: Int ( location. end_column) ,
121
+ ] ,
122
+ ) ;
123
+ }
124
+ loc_label
125
+ }
126
+
127
+ /** Get the label for the given location, creating it as a fresh ID if we haven't seen the location
128
+ * yet for this file. */
129
+ fn location_label (
130
+ writer : & mut trap:: Writer ,
131
+ file_label : trap:: Label ,
132
+ location : trap:: Location ,
133
+ ) -> trap:: Label {
134
+ let ( loc_label, fresh) = writer. location_label ( location) ;
135
+ if fresh {
136
+ writer. add_tuple (
137
+ "locations_default" ,
138
+ vec ! [
139
+ trap:: Arg :: Label ( loc_label) ,
140
+ trap:: Arg :: Label ( file_label) ,
141
+ trap:: Arg :: Int ( location. start_line) ,
142
+ trap:: Arg :: Int ( location. start_column) ,
143
+ trap:: Arg :: Int ( location. end_line) ,
144
+ trap:: Arg :: Int ( location. end_column) ,
110
145
] ,
111
146
) ;
112
147
}
@@ -245,26 +280,25 @@ impl<'a> Visitor<'a> {
245
280
node : Node ,
246
281
status_page : bool ,
247
282
) {
248
- let ( start_line, start_column, end_line, end_column) = location_for ( self , node) ;
249
- let loc = location (
250
- self . trap_writer ,
251
- self . file_label ,
252
- start_line,
253
- start_column,
254
- end_line,
255
- end_column,
256
- ) ;
283
+ let loc = location_for ( self , node) ;
284
+ let loc_label = location_label ( self . trap_writer , self . file_label , loc) ;
257
285
let mut mesg = self . diagnostics_writer . new_entry (
258
286
"parse-error" ,
259
287
"Could not process some files due to syntax errors" ,
260
288
) ;
261
289
mesg. severity ( diagnostics:: Severity :: Warning )
262
- . location ( self . path , start_line, start_column, end_line, end_column)
290
+ . location (
291
+ self . path ,
292
+ loc. start_line ,
293
+ loc. start_column ,
294
+ loc. end_line ,
295
+ loc. end_column ,
296
+ )
263
297
. message ( message, args) ;
264
298
if status_page {
265
299
mesg. status_page ( ) ;
266
300
}
267
- self . record_parse_error ( loc , & mesg) ;
301
+ self . record_parse_error ( loc_label , & mesg) ;
268
302
}
269
303
270
304
fn enter_node ( & mut self , node : Node ) -> bool {
@@ -298,15 +332,8 @@ impl<'a> Visitor<'a> {
298
332
return ;
299
333
}
300
334
let ( id, _, child_nodes) = self . stack . pop ( ) . expect ( "Vistor: empty stack" ) ;
301
- let ( start_line, start_column, end_line, end_column) = location_for ( self , node) ;
302
- let loc = location (
303
- self . trap_writer ,
304
- self . file_label ,
305
- start_line,
306
- start_column,
307
- end_line,
308
- end_column,
309
- ) ;
335
+ let loc = location_for ( self , node) ;
336
+ let loc_label = location_label ( self . trap_writer , self . file_label , loc) ;
310
337
let table = self
311
338
. schema
312
339
. get ( & TypeName {
@@ -333,7 +360,7 @@ impl<'a> Visitor<'a> {
333
360
trap:: Arg :: Label ( id) ,
334
361
trap:: Arg :: Label ( parent_id) ,
335
362
trap:: Arg :: Int ( parent_index) ,
336
- trap:: Arg :: Label ( loc ) ,
363
+ trap:: Arg :: Label ( loc_label ) ,
337
364
] ,
338
365
) ;
339
366
self . trap_writer . add_tuple (
@@ -356,7 +383,7 @@ impl<'a> Visitor<'a> {
356
383
trap:: Arg :: Label ( id) ,
357
384
trap:: Arg :: Label ( parent_id) ,
358
385
trap:: Arg :: Int ( parent_index) ,
359
- trap:: Arg :: Label ( loc ) ,
386
+ trap:: Arg :: Label ( loc_label ) ,
360
387
] ,
361
388
) ;
362
389
let mut all_args = vec ! [ trap:: Arg :: Label ( id) ] ;
@@ -366,14 +393,20 @@ impl<'a> Visitor<'a> {
366
393
}
367
394
_ => {
368
395
self . record_parse_error (
369
- loc ,
396
+ loc_label ,
370
397
self . diagnostics_writer
371
398
. new_entry (
372
399
"parse-error" ,
373
400
"Could not process some files due to syntax errors" ,
374
401
)
375
402
. severity ( diagnostics:: Severity :: Warning )
376
- . location ( self . path , start_line, start_column, end_line, end_column)
403
+ . location (
404
+ self . path ,
405
+ loc. start_line ,
406
+ loc. start_column ,
407
+ loc. end_line ,
408
+ loc. end_column ,
409
+ )
377
410
. message (
378
411
"Unknown table type: {}" ,
379
412
& [ diagnostics:: MessageArg :: Code ( node. kind ( ) ) ] ,
@@ -555,7 +588,7 @@ fn sliced_source_arg(source: &[u8], n: Node) -> trap::Arg {
555
588
// Emit a pair of `TrapEntry`s for the provided node, appropriately calibrated.
556
589
// The first is the location and label definition, and the second is the
557
590
// 'Located' entry.
558
- fn location_for ( visitor : & mut Visitor , n : Node ) -> ( usize , usize , usize , usize ) {
591
+ fn location_for ( visitor : & mut Visitor , n : Node ) -> trap :: Location {
559
592
// Tree-sitter row, column values are 0-based while CodeQL starts
560
593
// counting at 1. In addition Tree-sitter's row and column for the
561
594
// end position are exclusive while CodeQL's end positions are inclusive.
@@ -565,16 +598,16 @@ fn location_for(visitor: &mut Visitor, n: Node) -> (usize, usize, usize, usize)
565
598
// the end column is 0 (start of a line). In such cases the end position must be
566
599
// set to the end of the previous line.
567
600
let start_line = n. start_position ( ) . row + 1 ;
568
- let start_col = n. start_position ( ) . column + 1 ;
601
+ let start_column = n. start_position ( ) . column + 1 ;
569
602
let mut end_line = n. end_position ( ) . row + 1 ;
570
- let mut end_col = n. end_position ( ) . column ;
571
- if start_line > end_line || start_line == end_line && start_col > end_col {
603
+ let mut end_column = n. end_position ( ) . column ;
604
+ if start_line > end_line || start_line == end_line && start_column > end_column {
572
605
// the range is empty, clip it to sensible values
573
606
end_line = start_line;
574
- end_col = start_col - 1 ;
575
- } else if end_col == 0 {
607
+ end_column = start_column - 1 ;
608
+ } else if end_column == 0 {
576
609
let source = visitor. source ;
577
- // end_col = 0 means that we are at the start of a line
610
+ // end_column = 0 means that we are at the start of a line
578
611
// unfortunately 0 is invalid as column number, therefore
579
612
// we should update the end location to be the end of the
580
613
// previous line
@@ -591,10 +624,10 @@ fn location_for(visitor: &mut Visitor, n: Node) -> (usize, usize, usize, usize)
591
624
) ;
592
625
}
593
626
end_line -= 1 ;
594
- end_col = 1 ;
627
+ end_column = 1 ;
595
628
while index > 0 && source[ index - 1 ] != b'\n' {
596
629
index -= 1 ;
597
- end_col += 1 ;
630
+ end_column += 1 ;
598
631
}
599
632
} else {
600
633
visitor. diagnostics_writer . write (
@@ -612,7 +645,12 @@ fn location_for(visitor: &mut Visitor, n: Node) -> (usize, usize, usize, usize)
612
645
) ;
613
646
}
614
647
}
615
- ( start_line, start_col, end_line, end_col)
648
+ trap:: Location {
649
+ start_line,
650
+ start_column,
651
+ end_line,
652
+ end_column,
653
+ }
616
654
}
617
655
618
656
fn traverse ( tree : & Tree , visitor : & mut Visitor ) {
0 commit comments