Skip to content

Commit 74aa47a

Browse files
authored
Merge pull request #18094 from github/redsun82/rust-rename
Codegen/Rust: allow renaming in QL
2 parents 8fd581d + 269ea75 commit 74aa47a

File tree

15 files changed

+62
-59
lines changed

15 files changed

+62
-59
lines changed

misc/codegen/generators/qlgen.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,33 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
118118
type_is_hideable="ql_hideable" in lookup[prop.type].pragmas if prop.type in lookup else False,
119119
internal="ql_internal" in prop.pragmas,
120120
)
121+
ql_name = prop.pragmas.get("ql_name", prop.name)
121122
if prop.is_single:
122123
args.update(
123-
singular=inflection.camelize(prop.name),
124+
singular=inflection.camelize(ql_name),
124125
tablename=inflection.tableize(cls.name),
125126
tableparams=["this"] + ["result" if p is prop else "_" for p in cls.properties if p.is_single],
126127
doc=_get_doc(cls, prop),
127128
)
128129
elif prop.is_repeated:
129130
args.update(
130-
singular=inflection.singularize(inflection.camelize(prop.name)),
131-
plural=inflection.pluralize(inflection.camelize(prop.name)),
131+
singular=inflection.singularize(inflection.camelize(ql_name)),
132+
plural=inflection.pluralize(inflection.camelize(ql_name)),
132133
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
133134
tableparams=["this", "index", "result"] if not prop.is_unordered else ["this", "result"],
134135
doc=_get_doc(cls, prop, plural=False),
135136
doc_plural=_get_doc(cls, prop, plural=True),
136137
)
137138
elif prop.is_optional:
138139
args.update(
139-
singular=inflection.camelize(prop.name),
140+
singular=inflection.camelize(ql_name),
140141
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
141142
tableparams=["this", "result"],
142143
doc=_get_doc(cls, prop),
143144
)
144145
elif prop.is_predicate:
145146
args.update(
146-
singular=inflection.camelize(prop.name, uppercase_first_letter=False),
147+
singular=inflection.camelize(ql_name, uppercase_first_letter=False),
147148
tablename=inflection.underscore(f"{cls.name}_{prop.name}"),
148149
tableparams=["this"],
149150
doc=_get_doc(cls, prop),
@@ -154,6 +155,8 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
154155

155156

156157
def get_ql_class(cls: schema.Class, lookup: typing.Dict[str, schema.Class]) -> ql.Class:
158+
if "ql_name" in cls.pragmas:
159+
raise Error("ql_name is not supported yet for classes, only for properties")
157160
prev_child = ""
158161
properties = []
159162
for p in cls.properties:

misc/codegen/lib/schemadefs.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ def include(source: str):
7272
@_dataclass
7373
class _Namespace:
7474
""" simple namespacing mechanism """
75-
name: str
75+
_name: str
7676

7777
def add(self, pragma: "_PragmaBase", key: str | None = None):
7878
self.__dict__[pragma.pragma] = pragma
79-
pragma.pragma = key or f"{self.name}_{pragma.pragma}"
79+
pragma.pragma = key or f"{self._name}_{pragma.pragma}"
8080

8181

8282
@_dataclass
@@ -87,7 +87,7 @@ def modify(self, prop: _schema.Property):
8787
prop.synth = self.synth
8888

8989
def negate(self) -> _schema.PropertyModifier:
90-
return _SynthModifier(self.name, False)
90+
return _SynthModifier(self._name, False)
9191

9292

9393
qltest = _Namespace("qltest")
@@ -239,6 +239,7 @@ def __getitem__(self, item):
239239
ql.add(_ParametrizedClassPragma("default_doc_name", factory=lambda doc: doc))
240240
ql.add(_ClassPragma("hideable", inherited=True))
241241
ql.add(_Pragma("internal"))
242+
ql.add(_ParametrizedPragma("name", factory=lambda name: name))
242243

243244
cpp.add(_Pragma("skip"))
244245

@@ -256,38 +257,32 @@ def __getitem__(self, item):
256257
_schema.SynthInfo(on_arguments={k: _schema.get_type_name(t) for k, t in kwargs.items()})), key="synth")
257258

258259

260+
@_dataclass(frozen=True)
259261
class _PropertyModifierList(_schema.PropertyModifier):
260-
def __init__(self):
261-
self._mods = []
262+
_mods: tuple[_schema.PropertyModifier, ...]
262263

263264
def __or__(self, other: _schema.PropertyModifier):
264-
self._mods.append(other)
265-
return self
265+
return _PropertyModifierList(self._mods + (other,))
266266

267267
def modify(self, prop: Property):
268268
for m in self._mods:
269269
m.modify(prop)
270270

271271

272-
class _PropertyAnnotation:
273-
def __or__(self, other: _schema.PropertyModifier):
274-
return _PropertyModifierList() | other
275-
276-
277-
_ = _PropertyAnnotation()
272+
_ = _PropertyModifierList(())
278273

279274
drop = object()
280275

281276

282-
def annotate(annotated_cls: type, add_bases: _Iterable[type] | None = None, replace_bases: _Dict[type, type] | None = None, cfg: bool = False) -> _Callable[[type], _PropertyAnnotation]:
277+
def annotate(annotated_cls: type, add_bases: _Iterable[type] | None = None, replace_bases: _Dict[type, type] | None = None, cfg: bool = False) -> _Callable[[type], _PropertyModifierList]:
283278
"""
284279
Add or modify schema annotations after a class has been defined previously.
285280
286281
The name of the class used for annotation must be `_`.
287282
288283
`replace_bases` can be used to replace bases on the annotated class.
289284
"""
290-
def decorator(cls: type) -> _PropertyAnnotation:
285+
def decorator(cls: type) -> _PropertyModifierList:
291286
if cls.__name__ != "_":
292287
raise _schema.Error("Annotation classes must be named _")
293288
if cls.__doc__ is not None:
@@ -307,7 +302,7 @@ def decorator(cls: type) -> _PropertyAnnotation:
307302
del annotated_cls.__annotations__[p]
308303
elif p in annotated_cls.__annotations__:
309304
annotated_cls.__annotations__[p] |= a
310-
elif isinstance(a, (_PropertyAnnotation, _PropertyModifierList)):
305+
elif isinstance(a, (_PropertyModifierList, _PropertyModifierList)):
311306
raise _schema.Error(f"annotated property {p} not present in annotated class "
312307
f"{annotated_cls.__name__}")
313308
else:

rust/ql/.generated.list

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,14 @@ module ExprTrees {
514514

515515
class MatchExprTree extends PostOrderTree instanceof MatchExpr {
516516
override predicate propagatesAbnormal(AstNode child) {
517-
child = [super.getExpr(), super.getAnArm().getExpr()]
517+
child = [super.getScrutinee(), super.getAnArm().getExpr()]
518518
}
519519

520-
override predicate first(AstNode node) { first(super.getExpr(), node) }
520+
override predicate first(AstNode node) { first(super.getScrutinee(), node) }
521521

522522
override predicate succ(AstNode pred, AstNode succ, Completion c) {
523523
// Edge from the scrutinee to the first arm or to the match expression if no arms.
524-
last(super.getExpr(), pred, c) and
524+
last(super.getScrutinee(), pred, c) and
525525
(
526526
first(super.getArm(0).getPat(), succ)
527527
or

rust/ql/lib/codeql/rust/controlflow/internal/generated/CfgNodes.qll

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/MatchExprImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module Impl {
2929
*/
3030
class MatchExpr extends Generated::MatchExpr {
3131
override string toString() {
32-
result = "match " + this.getExpr().toAbbreviatedString() + " { ... }"
32+
result = "match " + this.getScrutinee().toAbbreviatedString() + " { ... }"
3333
}
3434

3535
/**

rust/ql/lib/codeql/rust/elements/internal/generated/MatchExpr.qll

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/generated/ParentChild.qll

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/generated/Raw.qll

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)