Skip to content

Commit 7e1290a

Browse files
author
Paolo Tranquilli
committed
Rust: reuse shared rust trap library
1 parent 4f0fe1c commit 7e1290a

19 files changed

Lines changed: 666 additions & 205 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ repos:
4545

4646
- id: sync-files
4747
name: Fix files required to be identical
48-
files: \.(qll?|qhelp|swift)$|^config/identical-files\.json$
48+
files: \.(qll?|qhelp|swift|toml)$|^config/identical-files\.json$
4949
language: system
5050
entry: python3 config/sync-files.py --latest
5151
pass_filenames: false

config/identical-files.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@
358358
],
359359
"shared tree-sitter extractor cargo.toml": [
360360
"shared/tree-sitter-extractor/Cargo.toml",
361-
"ruby/extractor/codeql-extractor-fake-crate/Cargo.toml"
361+
"ruby/extractor/codeql-extractor-fake-crate/Cargo.toml",
362+
"rust/extractor/codeql-extractor-fake-crate/Cargo.toml"
362363
]
363364
}

misc/codegen/generators/rustgen.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313

1414
def _get_type(t: str) -> str:
1515
match t:
16-
case None | "boolean": # None means a predicate
16+
case None: # None means a predicate
1717
return "bool"
1818
case "string":
1919
return "String"
2020
case "int":
21-
return "i32"
21+
return "usize"
2222
case _ if t[0].isupper():
23-
return "TrapLabel"
23+
return "trap::Label"
24+
case "boolean":
25+
assert False, "boolean unsupported"
2426
case _:
2527
return t
2628

misc/codegen/lib/rust.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@
5858
}
5959

6060
_field_overrides = [
61-
(
62-
re.compile(r"(start|end)_(line|column)|(.*_)?index|width|num_.*"),
63-
{"base_type": "u32"},
64-
),
6561
(re.compile(r"(.*)_"), lambda m: {"field_name": m[1]}),
6662
]
6763

@@ -98,20 +94,13 @@ def type(self) -> str:
9894
type = f"Vec<{type}>"
9995
return type
10096

101-
# using @property breaks pystache internals here
102-
def emitter(self):
103-
if self.type == "String":
104-
return lambda x: f"quoted(&{x})"
105-
else:
106-
return lambda x: x
107-
10897
@property
10998
def is_single(self):
11099
return not (self.is_optional or self.is_repeated or self.is_predicate)
111100

112101
@property
113102
def is_label(self):
114-
return self.base_type == "TrapLabel"
103+
return self.base_type == "trap::Label"
115104

116105

117106
@dataclasses.dataclass
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// generated by {{generator}}
22

3-
use crate::trap::{TrapLabel, TrapId, TrapEntry, quoted};
4-
use std::io::Write;
3+
use crate::trap::{TrapId, TrapEntry};
4+
use codeql_extractor::trap;
55
{{#classes}}
66

77
#[derive(Debug)]
@@ -17,37 +17,36 @@ impl TrapEntry for {{name}} {
1717
std::mem::replace(&mut self.id, TrapId::Star)
1818
}
1919

20-
fn emit<W: Write>(self, id: TrapLabel, out: &mut W) -> std::io::Result<()> {
20+
fn emit(self, id: trap::Label, out: &mut trap::Writer) {
2121
{{#single_field_entries}}
22-
write!(out, "{{table_name}}({id}{{#fields}}, {}{{/fields}})\n"{{#fields}}, {{#emitter}}self.{{field_name}}{{/emitter}}{{/fields}})?;
22+
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{#fields}}, self.{{field_name}}.into(){{/fields}}]);
2323
{{/single_field_entries}}
2424
{{#fields}}
2525
{{#is_predicate}}
2626
if self.{{field_name}} {
27-
write!(out, "{{table_name}}({id})\n")?;
27+
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id)]);
2828
}
2929
{{/is_predicate}}
3030
{{#is_optional}}
3131
{{^is_repeated}}
32-
if let Some(ref v) = &self.{{field_name}} {
33-
write!(out, "{{table_name}}({id}, {})\n", {{#emitter}}v{{/emitter}})?;
32+
if let Some(v) = self.{{field_name}} {
33+
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id), v.into()]);
3434
}
3535
{{/is_repeated}}
3636
{{/is_optional}}
3737
{{#is_repeated}}
38-
for (i, &ref v) in self.{{field_name}}.iter().enumerate() {
38+
for (i, &v) in self.{{field_name}}.iter().enumerate() {
3939
{{^is_optional}}
40-
write!(out, "{{table_name}}({id}, {{^is_unordered}}{}, {{/is_unordered}}{})\n", {{^is_unordered}}i, {{/is_unordered}}{{#emitter}}v{{/emitter}})?;
40+
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]);
4141
{{/is_optional}}
4242
{{#is_optional}}
43-
if let Some(ref vv) = &v {
44-
write!(out, "{{table_name}}({id}, {{^is_unordered}}{}, {{/is_unordered}}{})\n", {{^is_unordered}}i, {{/is_unordered}}{{#emitter}}vv{{/emitter}})?;
43+
if let Some(vv) = v {
44+
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]);
4545
}
4646
{{/is_optional}}
4747
}
4848
{{/is_repeated}}
4949
{{/fields}}
50-
Ok(())
5150
}
5251
}
5352
{{/classes}}

rust/extractor/.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
paths = ["../../shared/tree-sitter-extractor"]

rust/extractor/BUILD.bazel

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ codeql_rust_binary(
1111
visibility = ["//rust:__subpackages__"],
1212
deps = all_crate_deps(
1313
normal = True,
14-
),
14+
) + [
15+
"//shared/tree-sitter-extractor:codeql-extractor",
16+
],
1517
)

0 commit comments

Comments
 (0)