@@ -165,7 +165,7 @@ pub fn extract(
165
165
schema : & NodeTypeMap ,
166
166
trap_writer : & mut TrapWriter ,
167
167
path : & Path ,
168
- source : & Vec < u8 > ,
168
+ source : & [ u8 ] ,
169
169
ranges : & [ Range ] ,
170
170
) -> std:: io:: Result < ( ) > {
171
171
let span = span ! (
@@ -180,17 +180,16 @@ pub fn extract(
180
180
181
181
let mut parser = Parser :: new ( ) ;
182
182
parser. set_language ( language) . unwrap ( ) ;
183
- parser. set_included_ranges ( & ranges) . unwrap ( ) ;
183
+ parser. set_included_ranges ( ranges) . unwrap ( ) ;
184
184
let tree = parser. parse ( & source, None ) . expect ( "Failed to parse file" ) ;
185
185
trap_writer. comment ( format ! ( "Auto-generated TRAP file for {}" , path. display( ) ) ) ;
186
186
let file_label = & trap_writer. populate_file ( path) ;
187
187
let mut visitor = Visitor {
188
- source : & source ,
189
- trap_writer : trap_writer ,
188
+ source,
189
+ trap_writer,
190
190
// TODO: should we handle path strings that are not valid UTF8 better?
191
191
path : format ! ( "{}" , path. display( ) ) ,
192
192
file_label : * file_label,
193
- token_counter : 0 ,
194
193
toplevel_child_counter : 0 ,
195
194
stack : Vec :: new ( ) ,
196
195
language_prefix,
@@ -206,15 +205,7 @@ pub fn extract(
206
205
/// HTML entities.
207
206
fn escape_key < ' a , S : Into < Cow < ' a , str > > > ( key : S ) -> Cow < ' a , str > {
208
207
fn needs_escaping ( c : char ) -> bool {
209
- match c {
210
- '&' => true ,
211
- '{' => true ,
212
- '}' => true ,
213
- '"' => true ,
214
- '@' => true ,
215
- '#' => true ,
216
- _ => false ,
217
- }
208
+ matches ! ( c, '&' | '{' | '}' | '"' | '@' | '#' )
218
209
}
219
210
220
211
let key = key. into ( ) ;
@@ -297,11 +288,9 @@ struct Visitor<'a> {
297
288
/// source file.
298
289
file_label : Label ,
299
290
/// The source code as a UTF-8 byte array
300
- source : & ' a Vec < u8 > ,
291
+ source : & ' a [ u8 ] ,
301
292
/// A TrapWriter to accumulate trap entries
302
293
trap_writer : & ' a mut TrapWriter ,
303
- /// A counter for tokens
304
- token_counter : usize ,
305
294
/// A counter for top-level child nodes
306
295
toplevel_child_counter : usize ,
307
296
/// Language prefix
@@ -345,7 +334,7 @@ impl Visitor<'_> {
345
334
full_error_message : String ,
346
335
node : Node ,
347
336
) {
348
- let ( start_line, start_column, end_line, end_column) = location_for ( & self . source , node) ;
337
+ let ( start_line, start_column, end_line, end_column) = location_for ( self . source , node) ;
349
338
let loc = self . trap_writer . location (
350
339
self . file_label ,
351
340
start_line,
@@ -376,15 +365,15 @@ impl Visitor<'_> {
376
365
let id = self . trap_writer . fresh_id ( ) ;
377
366
378
367
self . stack . push ( ( id, 0 , Vec :: new ( ) ) ) ;
379
- return true ;
368
+ true
380
369
}
381
370
382
371
fn leave_node ( & mut self , field_name : Option < & ' static str > , node : Node ) {
383
372
if node. is_error ( ) || node. is_missing ( ) {
384
373
return ;
385
374
}
386
375
let ( id, _, child_nodes) = self . stack . pop ( ) . expect ( "Vistor: empty stack" ) ;
387
- let ( start_line, start_column, end_line, end_column) = location_for ( & self . source , node) ;
376
+ let ( start_line, start_column, end_line, end_column) = location_for ( self . source , node) ;
388
377
let loc = self . trap_writer . location (
389
378
self . file_label ,
390
379
start_line,
@@ -425,13 +414,10 @@ impl Visitor<'_> {
425
414
vec ! [
426
415
Arg :: Label ( id) ,
427
416
Arg :: Int ( * kind_id) ,
428
- Arg :: Label ( self . file_label) ,
429
- Arg :: Int ( self . token_counter) ,
430
417
sliced_source_arg( self . source, node) ,
431
418
Arg :: Label ( loc) ,
432
419
] ,
433
420
) ;
434
- self . token_counter += 1 ;
435
421
}
436
422
EntryKind :: Table {
437
423
fields,
@@ -446,11 +432,10 @@ impl Visitor<'_> {
446
432
Arg :: Int ( parent_index) ,
447
433
] ,
448
434
) ;
449
- let mut all_args = Vec :: new ( ) ;
450
- all_args. push ( Arg :: Label ( id) ) ;
435
+ let mut all_args = vec ! [ Arg :: Label ( id) ] ;
451
436
all_args. extend ( args) ;
452
437
all_args. push ( Arg :: Label ( loc) ) ;
453
- self . trap_writer . add_tuple ( & table_name, all_args) ;
438
+ self . trap_writer . add_tuple ( table_name, all_args) ;
454
439
}
455
440
}
456
441
_ => {
@@ -485,8 +470,8 @@ impl Visitor<'_> {
485
470
fn complex_node (
486
471
& mut self ,
487
472
node : & Node ,
488
- fields : & Vec < Field > ,
489
- child_nodes : & Vec < ChildNode > ,
473
+ fields : & [ Field ] ,
474
+ child_nodes : & [ ChildNode ] ,
490
475
parent_id : Label ,
491
476
) -> Option < Vec < Arg > > {
492
477
let mut map: Map < & Option < String > , ( & Field , Vec < Arg > ) > = Map :: new ( ) ;
@@ -523,22 +508,20 @@ impl Visitor<'_> {
523
508
) ;
524
509
self . record_parse_error_for_node ( error_message, full_error_message, * node) ;
525
510
}
526
- } else {
527
- if child_node. field_name . is_some ( ) || child_node. type_name . named {
528
- let error_message = format ! (
529
- "value for unknown field: {}::{} and type {:?}" ,
530
- node. kind( ) ,
531
- & child_node. field_name. unwrap_or( "child" ) ,
532
- & child_node. type_name
533
- ) ;
534
- let full_error_message = format ! (
535
- "{}:{}: {}" ,
536
- & self . path,
537
- node. start_position( ) . row + 1 ,
538
- error_message
539
- ) ;
540
- self . record_parse_error_for_node ( error_message, full_error_message, * node) ;
541
- }
511
+ } else if child_node. field_name . is_some ( ) || child_node. type_name . named {
512
+ let error_message = format ! (
513
+ "value for unknown field: {}::{} and type {:?}" ,
514
+ node. kind( ) ,
515
+ & child_node. field_name. unwrap_or( "child" ) ,
516
+ & child_node. type_name
517
+ ) ;
518
+ let full_error_message = format ! (
519
+ "{}:{}: {}" ,
520
+ & self . path,
521
+ node. start_position( ) . row + 1 ,
522
+ error_message
523
+ ) ;
524
+ self . record_parse_error_for_node ( error_message, full_error_message, * node) ;
542
525
}
543
526
}
544
527
let mut args = Vec :: new ( ) ;
@@ -586,13 +569,12 @@ impl Visitor<'_> {
586
569
) ;
587
570
break ;
588
571
}
589
- let mut args = Vec :: new ( ) ;
590
- args. push ( Arg :: Label ( parent_id) ) ;
572
+ let mut args = vec ! [ Arg :: Label ( parent_id) ] ;
591
573
if * has_index {
592
574
args. push ( Arg :: Int ( index) )
593
575
}
594
576
args. push ( child_value. clone ( ) ) ;
595
- self . trap_writer . add_tuple ( & table_name, args) ;
577
+ self . trap_writer . add_tuple ( table_name, args) ;
596
578
}
597
579
}
598
580
}
@@ -610,13 +592,10 @@ impl Visitor<'_> {
610
592
if tp == single_type {
611
593
return true ;
612
594
}
613
- match & self . schema . get ( single_type) . unwrap ( ) . kind {
614
- EntryKind :: Union { members } => {
615
- if self . type_matches_set ( tp, members) {
616
- return true ;
617
- }
595
+ if let EntryKind :: Union { members } = & self . schema . get ( single_type) . unwrap ( ) . kind {
596
+ if self . type_matches_set ( tp, members) {
597
+ return true ;
618
598
}
619
- _ => { }
620
599
}
621
600
}
622
601
node_types:: FieldTypeInfo :: Multiple { types, .. } => {
@@ -646,15 +625,15 @@ impl Visitor<'_> {
646
625
}
647
626
648
627
// Emit a slice of a source file as an Arg.
649
- fn sliced_source_arg ( source : & Vec < u8 > , n : Node ) -> Arg {
628
+ fn sliced_source_arg ( source : & [ u8 ] , n : Node ) -> Arg {
650
629
let range = n. byte_range ( ) ;
651
630
Arg :: String ( String :: from_utf8_lossy ( & source[ range. start ..range. end ] ) . into_owned ( ) )
652
631
}
653
632
654
633
// Emit a pair of `TrapEntry`s for the provided node, appropriately calibrated.
655
634
// The first is the location and label definition, and the second is the
656
635
// 'Located' entry.
657
- fn location_for < ' a > ( source : & Vec < u8 > , n : Node ) -> ( usize , usize , usize , usize ) {
636
+ fn location_for ( source : & [ u8 ] , n : Node ) -> ( usize , usize , usize , usize ) {
658
637
// Tree-sitter row, column values are 0-based while CodeQL starts
659
638
// counting at 1. In addition Tree-sitter's row and column for the
660
639
// end position are exclusive while CodeQL's end positions are inclusive.
@@ -812,18 +791,18 @@ impl fmt::Display for Arg {
812
791
/// the string is sliced at the provided limit. If there is a multi-byte character
813
792
/// at the limit then the returned slice will be slightly shorter than the limit to
814
793
/// avoid splitting that multi-byte character.
815
- fn limit_string ( string : & String , max_size : usize ) -> & str {
794
+ fn limit_string ( string : & str , max_size : usize ) -> & str {
816
795
if string. len ( ) <= max_size {
817
796
return string;
818
797
}
819
- let p = string. as_ptr ( ) ;
798
+ let p = string. as_bytes ( ) ;
820
799
let mut index = max_size;
821
800
// We want to clip the string at [max_size]; however, the character at that position
822
801
// may span several bytes. We need to find the first byte of the character. In UTF-8
823
802
// encoded data any byte that matches the bit pattern 10XXXXXX is not a start byte.
824
803
// Therefore we decrement the index as long as there are bytes matching this pattern.
825
804
// This ensures we cut the string at the border between one character and another.
826
- while index > 0 && unsafe { ( * p . offset ( index as isize ) & 0b11000000 ) == 0b10000000 } {
805
+ while index > 0 && ( p [ index] & 0b11000000 ) == 0b10000000 {
827
806
index -= 1 ;
828
807
}
829
808
& string[ 0 ..index]
0 commit comments