Skip to content

Commit 4454566

Browse files
author
Paolo Tranquilli
committed
Tree-sitter: allow multiple sources per trap file
This generalizes the location cache to allow multiple sources to be extracted in the same trap file, by adding `file_label` to `Location`, and therefore to location cache keys. This will be used by the Rust extractor.
1 parent e165fc7 commit 4454566

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

shared/tree-sitter-extractor/src/extractor/mod.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ pub fn populate_empty_location(writer: &mut trap::Writer) {
7676
let file_label = populate_empty_file(writer);
7777
let loc_label = global_location(
7878
writer,
79-
file_label,
8079
trap::Location {
80+
file_label,
8181
start_line: 0,
8282
start_column: 0,
8383
end_line: 0,
@@ -129,12 +129,11 @@ pub fn populate_parent_folders(
129129
/** Get the label for the given location, defining it a global ID if it doesn't exist yet. */
130130
fn global_location(
131131
writer: &mut trap::Writer,
132-
file_label: trap::Label,
133132
location: trap::Location,
134133
) -> trap::Label {
135134
let (loc_label, fresh) = writer.global_id(&format!(
136135
"loc,{{{}}},{},{},{},{}",
137-
file_label,
136+
location.file_label,
138137
location.start_line,
139138
location.start_column,
140139
location.end_line,
@@ -145,7 +144,7 @@ fn global_location(
145144
"locations_default",
146145
vec![
147146
trap::Arg::Label(loc_label),
148-
trap::Arg::Label(file_label),
147+
trap::Arg::Label(location.file_label),
149148
trap::Arg::Int(location.start_line),
150149
trap::Arg::Int(location.start_column),
151150
trap::Arg::Int(location.end_line),
@@ -160,7 +159,6 @@ fn global_location(
160159
* yet for this file. */
161160
fn location_label(
162161
writer: &mut trap::Writer,
163-
file_label: trap::Label,
164162
location: trap::Location,
165163
) -> trap::Label {
166164
let (loc_label, fresh) = writer.location_label(location);
@@ -169,7 +167,7 @@ fn location_label(
169167
"locations_default",
170168
vec![
171169
trap::Arg::Label(loc_label),
172-
trap::Arg::Label(file_label),
170+
trap::Arg::Label(location.file_label),
173171
trap::Arg::Int(location.start_line),
174172
trap::Arg::Int(location.start_column),
175173
trap::Arg::Int(location.end_line),
@@ -312,8 +310,8 @@ impl<'a> Visitor<'a> {
312310
node: Node,
313311
status_page: bool,
314312
) {
315-
let loc = location_for(self, node);
316-
let loc_label = location_label(self.trap_writer, self.file_label, loc);
313+
let loc = location_for(self, self.file_label, node);
314+
let loc_label = location_label(self.trap_writer, loc);
317315
let mut mesg = self.diagnostics_writer.new_entry(
318316
"parse-error",
319317
"Could not process some files due to syntax errors",
@@ -364,8 +362,8 @@ impl<'a> Visitor<'a> {
364362
return;
365363
}
366364
let (id, _, child_nodes) = self.stack.pop().expect("Vistor: empty stack");
367-
let loc = location_for(self, node);
368-
let loc_label = location_label(self.trap_writer, self.file_label, loc);
365+
let loc = location_for(self, self.file_label, node);
366+
let loc_label = location_label(self.trap_writer, loc);
369367
let table = self
370368
.schema
371369
.get(&TypeName {
@@ -627,7 +625,7 @@ fn sliced_source_arg(source: &[u8], n: Node) -> trap::Arg {
627625
// Emit a pair of `TrapEntry`s for the provided node, appropriately calibrated.
628626
// The first is the location and label definition, and the second is the
629627
// 'Located' entry.
630-
fn location_for(visitor: &mut Visitor, n: Node) -> trap::Location {
628+
fn location_for(visitor: &mut Visitor, file_label: trap::Label, n: Node) -> trap::Location {
631629
// Tree-sitter row, column values are 0-based while CodeQL starts
632630
// counting at 1. In addition Tree-sitter's row and column for the
633631
// end position are exclusive while CodeQL's end positions are inclusive.
@@ -685,6 +683,7 @@ fn location_for(visitor: &mut Visitor, n: Node) -> trap::Location {
685683
}
686684
}
687685
trap::Location {
686+
file_label,
688687
start_line,
689688
start_column,
690689
end_line,

shared/tree-sitter-extractor/src/trap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use flate2::write::GzEncoder;
77

88
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
99
pub struct Location {
10+
pub file_label: Label,
1011
pub start_line: usize,
1112
pub start_column: usize,
1213
pub end_line: usize,
@@ -136,7 +137,7 @@ impl fmt::Display for Entry {
136137
}
137138
}
138139

139-
#[derive(Debug, Copy, Clone)]
140+
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
140141
// Identifiers of the form #0, #1...
141142
pub struct Label(u32);
142143

0 commit comments

Comments
 (0)