Skip to content

Commit f1cd921

Browse files
authored
Merge pull request github#17407 from github/redsun82/rust-extractor-generalize-location-cache
Tree-sitter: allow multiple sources per trap file
2 parents d1cca13 + 2c472dd commit f1cd921

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

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

Lines changed: 12 additions & 19 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,
@@ -127,14 +127,10 @@ pub fn populate_parent_folders(
127127
}
128128

129129
/** Get the label for the given location, defining it a global ID if it doesn't exist yet. */
130-
fn global_location(
131-
writer: &mut trap::Writer,
132-
file_label: trap::Label,
133-
location: trap::Location,
134-
) -> trap::Label {
130+
fn global_location(writer: &mut trap::Writer, location: trap::Location) -> trap::Label {
135131
let (loc_label, fresh) = writer.global_id(&format!(
136132
"loc,{{{}}},{},{},{},{}",
137-
file_label,
133+
location.file_label,
138134
location.start_line,
139135
location.start_column,
140136
location.end_line,
@@ -145,7 +141,7 @@ fn global_location(
145141
"locations_default",
146142
vec![
147143
trap::Arg::Label(loc_label),
148-
trap::Arg::Label(file_label),
144+
trap::Arg::Label(location.file_label),
149145
trap::Arg::Int(location.start_line),
150146
trap::Arg::Int(location.start_column),
151147
trap::Arg::Int(location.end_line),
@@ -158,18 +154,14 @@ fn global_location(
158154

159155
/** Get the label for the given location, creating it as a fresh ID if we haven't seen the location
160156
* yet for this file. */
161-
fn location_label(
162-
writer: &mut trap::Writer,
163-
file_label: trap::Label,
164-
location: trap::Location,
165-
) -> trap::Label {
157+
fn location_label(writer: &mut trap::Writer, location: trap::Location) -> trap::Label {
166158
let (loc_label, fresh) = writer.location_label(location);
167159
if fresh {
168160
writer.add_tuple(
169161
"locations_default",
170162
vec![
171163
trap::Arg::Label(loc_label),
172-
trap::Arg::Label(file_label),
164+
trap::Arg::Label(location.file_label),
173165
trap::Arg::Int(location.start_line),
174166
trap::Arg::Int(location.start_column),
175167
trap::Arg::Int(location.end_line),
@@ -312,8 +304,8 @@ impl<'a> Visitor<'a> {
312304
node: Node,
313305
status_page: bool,
314306
) {
315-
let loc = location_for(self, node);
316-
let loc_label = location_label(self.trap_writer, self.file_label, loc);
307+
let loc = location_for(self, self.file_label, node);
308+
let loc_label = location_label(self.trap_writer, loc);
317309
let mut mesg = self.diagnostics_writer.new_entry(
318310
"parse-error",
319311
"Could not process some files due to syntax errors",
@@ -364,8 +356,8 @@ impl<'a> Visitor<'a> {
364356
return;
365357
}
366358
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);
359+
let loc = location_for(self, self.file_label, node);
360+
let loc_label = location_label(self.trap_writer, loc);
369361
let table = self
370362
.schema
371363
.get(&TypeName {
@@ -627,7 +619,7 @@ fn sliced_source_arg(source: &[u8], n: Node) -> trap::Arg {
627619
// Emit a pair of `TrapEntry`s for the provided node, appropriately calibrated.
628620
// The first is the location and label definition, and the second is the
629621
// 'Located' entry.
630-
fn location_for(visitor: &mut Visitor, n: Node) -> trap::Location {
622+
fn location_for(visitor: &mut Visitor, file_label: trap::Label, n: Node) -> trap::Location {
631623
// Tree-sitter row, column values are 0-based while CodeQL starts
632624
// counting at 1. In addition Tree-sitter's row and column for the
633625
// end position are exclusive while CodeQL's end positions are inclusive.
@@ -685,6 +677,7 @@ fn location_for(visitor: &mut Visitor, n: Node) -> trap::Location {
685677
}
686678
}
687679
trap::Location {
680+
file_label,
688681
start_line,
689682
start_column,
690683
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)