Skip to content

Commit 6a540d8

Browse files
authored
Merge pull request #17523 from github/redsun82/rust-break-up-schema
Codegen/Rust: allow breaking up schema file
2 parents fe1081e + a5e3fbf commit 6a540d8

File tree

8 files changed

+74
-64
lines changed

8 files changed

+74
-64
lines changed

misc/codegen/codegen.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def _parse_args() -> argparse.Namespace:
4343
"compute QL imports and in some comments and as root for relative paths provided as options. "
4444
"If not provided it defaults to the directory of the configuration file, if any")
4545
path_arguments = [
46-
p.add_argument("--schema", default="schema.py",
47-
help="input schema file (default %(default)s)"),
46+
p.add_argument("--schema",
47+
help="input schema file (default schema.py)"),
4848
p.add_argument("--dbscheme",
4949
help="output file for dbscheme generation, input file for trap generation"),
5050
p.add_argument("--ql-output",
@@ -87,6 +87,8 @@ def _parse_args() -> argparse.Namespace:
8787
setattr(opts, flag, getattr(defaults, flag))
8888
if opts.root_dir is None:
8989
opts.root_dir = opts.configuration_file.parent
90+
if opts.schema is None:
91+
opts.schema = "schema.py"
9092
if not opts.generate:
9193
p.error("Nothing to do, specify --generate")
9294
# absolutize all paths

misc/codegen/loaders/schemaloader.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" schema loader """
2+
import sys
23

34
import inflection
45
import typing
@@ -140,6 +141,8 @@ def load(m: types.ModuleType) -> schema.Schema:
140141
continue
141142
if name.startswith("__") or name == "_":
142143
continue
144+
if isinstance(data, types.ModuleType):
145+
continue
143146
cls = _get_class(data)
144147
if classes and not cls.bases:
145148
raise schema.Error(
@@ -160,7 +163,10 @@ def load(m: types.ModuleType) -> schema.Schema:
160163

161164

162165
def load_file(path: pathlib.Path) -> schema.Schema:
163-
spec = importlib.util.spec_from_file_location("schema", path)
164-
module = importlib.util.module_from_spec(spec)
165-
spec.loader.exec_module(module)
166+
assert path.suffix in ("", ".py")
167+
sys.path.insert(0, str(path.parent))
168+
try:
169+
module = importlib.import_module(path.with_suffix("").name)
170+
finally:
171+
sys.path.remove(str(path.parent))
166172
return load(module)

rust/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package(default_visibility = ["//rust:__subpackages__"])
99

1010
filegroup(
1111
name = "schema",
12-
srcs = ["schema.py"],
12+
srcs = glob(["schema/*.py"]),
1313
)
1414

1515
filegroup(

rust/codegen.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# configuration file for Rust code generation default options
2+
--schema=schema
23
--generate=dbscheme,rusttest,ql,rust
34
--dbscheme=ql/lib/rust.dbscheme
45
--ql-output=ql/lib/codeql/rust/elements/internal/generated

rust/ql/lib/rust.dbscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ locatable_locations(
117117
);
118118

119119

120-
// from schema.py
120+
// from schema
121121

122122
@element =
123123
@locatable

rust/schema/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Schema description
3+
4+
This file should be kept simple:
5+
* no flow control
6+
* no aliases
7+
* only class definitions with annotations and `include` calls
8+
9+
For how documentation of generated QL code works, please read `misc/codegen/schema_documentation.md`.
10+
"""
11+
12+
from .prelude import *
13+
from .ast import *
14+
15+
include("../shared/tree-sitter-extractor/src/generator/prefix.dbscheme")
16+
include("prefix.dbscheme")

rust/schema.py renamed to rust/schema/ast.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,4 @@
1-
"""
2-
Schema description
3-
4-
This file should be kept simple:
5-
* no flow control
6-
* no aliases
7-
* only class definitions with annotations and `include` calls
8-
9-
For how documentation of generated QL code works, please read `misc/codegen/schema_documentation.md`.
10-
"""
11-
12-
from misc.codegen.lib.schemadefs import *
13-
14-
include("../shared/tree-sitter-extractor/src/generator/prefix.dbscheme")
15-
include("prefix.dbscheme")
16-
17-
18-
@qltest.skip
19-
class Element:
20-
pass
21-
22-
23-
@qltest.skip
24-
class Locatable(Element):
25-
pass
26-
27-
28-
@qltest.skip
29-
class AstNode(Locatable):
30-
pass
31-
32-
33-
@qltest.skip
34-
class Unextracted(Element):
35-
"""
36-
The base class marking everything that was not properly extracted for some reason, such as:
37-
* syntax errors
38-
* insufficient context information
39-
* yet unimplemented parts of the extractor
40-
"""
41-
pass
42-
43-
44-
@qltest.skip
45-
class Missing(Unextracted):
46-
"""
47-
The base class marking errors during parsing or resolution.
48-
"""
49-
50-
51-
@qltest.skip
52-
class Unimplemented(Unextracted):
53-
"""
54-
The base class for unimplemented nodes. This is used to mark nodes that are not yet extracted.
55-
"""
56-
pass
57-
1+
from .prelude import *
582

593
class AssocItem(AstNode):
604
pass

rust/schema/prelude.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from misc.codegen.lib.schemadefs import *
2+
3+
@qltest.skip
4+
class Element:
5+
pass
6+
7+
8+
@qltest.skip
9+
class Locatable(Element):
10+
pass
11+
12+
13+
@qltest.skip
14+
class AstNode(Locatable):
15+
pass
16+
17+
18+
@qltest.skip
19+
class Unextracted(Element):
20+
"""
21+
The base class marking everything that was not properly extracted for some reason, such as:
22+
* syntax errors
23+
* insufficient context information
24+
* yet unimplemented parts of the extractor
25+
"""
26+
pass
27+
28+
29+
@qltest.skip
30+
class Missing(Unextracted):
31+
"""
32+
The base class marking errors during parsing or resolution.
33+
"""
34+
35+
36+
@qltest.skip
37+
class Unimplemented(Unextracted):
38+
"""
39+
The base class for unimplemented nodes. This is used to mark nodes that are not yet extracted.
40+
"""
41+
pass

0 commit comments

Comments
 (0)