Skip to content

Commit 37b405f

Browse files
committed
Swift: add generated docs for predicates
1 parent 22bd101 commit 37b405f

File tree

16 files changed

+76
-29
lines changed

16 files changed

+76
-29
lines changed

swift/codegen/generators/qlgen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def _get_doc(cls: schema.Class, prop: schema.Property, plural=None):
9696

9797
prop_name = _humanize(prop.name)
9898
class_name = _humanize(inflection.underscore(cls.name))
99+
if prop.is_predicate:
100+
return f"this {class_name} {prop_name}"
99101
if plural is not None:
100102
prop_name = inflection.pluralize(prop_name) if plural else inflection.singularize(prop_name)
101103
return f"{prop_name} of this {class_name}"
@@ -138,6 +140,7 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, prev_child: str =
138140
singular=inflection.camelize(prop.name, uppercase_first_letter=False),
139141
tablename=inflection.underscore(f"{cls.name}_{prop.name}"),
140142
tableparams=["this"],
143+
doc=_get_doc(cls, prop),
141144
)
142145
else:
143146
raise ValueError(f"unknown property kind for {prop.name} from {cls.name}")

swift/codegen/lib/schema/defs.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class _DocnameModifier(_schema.PropertyModifier):
1616
doc: str
1717

1818
def modify(self, prop: _schema.Property):
19-
if prop.is_predicate:
20-
raise _schema.Error("Predicates cannot have a doc name")
2119
prop.doc = self.doc
2220

2321

swift/codegen/templates/ql_class.mustache

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ module Generated {
8686

8787
{{/type_is_class}}
8888
{{^type_is_class}}
89-
{{^is_predicate}}
9089
/**
9190
* {{>ql_property_doc}} *
9291
{{#has_description}}
@@ -95,16 +94,6 @@ module Generated {
9594
{{/description}}
9695
{{/has_description}}
9796
*/
98-
{{/is_predicate}}
99-
{{#is_predicate}}
100-
{{#has_doc}}
101-
/**
102-
{{#doc}}
103-
* {{.}}
104-
{{/doc}}
105-
*/
106-
{{/has_doc}}
107-
{{/is_predicate}}
10897
{{type}} {{getter}}({{#is_repeated}}int index{{/is_repeated}}) {
10998
{{^ipa}}
11099
{{^is_predicate}}result = {{/is_predicate}}Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_repeated}}index{{/is_repeated}})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
{{^is_predicate}}
12
Gets the {{#is_repeated}}`index`th {{/is_repeated}}{{doc}}{{#is_repeated}} (0-based){{/is_repeated}}{{#is_optional}}, if it exists{{/is_optional}}.
3+
{{/is_predicate}}
4+
{{#is_predicate}}
5+
Holds if {{doc}}.
6+
{{/is_predicate}}

swift/codegen/test/test_qlgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_predicate_property(generate_classes):
328328
"MyObject.qll": (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
329329
ql.Class(name="MyObject", final=True, properties=[
330330
ql.Property(singular="isFoo", type="predicate", tablename="my_object_is_foo",
331-
tableparams=["this"], is_predicate=True),
331+
tableparams=["this"], is_predicate=True, doc="this my object is foo"),
332332
])),
333333
}
334334

swift/codegen/test/test_schema.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,5 @@ class A:
554554
}
555555

556556

557-
def test_predicate_cannot_have_doc_override():
558-
with pytest.raises(schema.Error):
559-
@schema.load
560-
class data:
561-
class A:
562-
x: defs.predicate | defs.doc("y")
563-
564-
565557
if __name__ == '__main__':
566558
sys.exit(pytest.main([__file__] + sys.argv[1:]))

swift/ql/lib/codeql/swift/generated/Element.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ module Generated {
4141
result = getResolveStep().resolve()
4242
}
4343

44+
/**
45+
* Holds if this element is unknown.
46+
*/
4447
predicate isUnknown() { Synth::convertElementToRaw(this).isUnknown() }
4548
}
4649
}

swift/ql/lib/codeql/swift/generated/decl/AccessorDecl.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ module Generated {
77
class AccessorDecl extends Synth::TAccessorDecl, FuncDecl {
88
override string getAPrimaryQlClass() { result = "AccessorDecl" }
99

10+
/**
11+
* Holds if this accessor is a getter.
12+
*/
1013
predicate isGetter() { Synth::convertAccessorDeclToRaw(this).(Raw::AccessorDecl).isGetter() }
1114

15+
/**
16+
* Holds if this accessor is a setter.
17+
*/
1218
predicate isSetter() { Synth::convertAccessorDeclToRaw(this).(Raw::AccessorDecl).isSetter() }
1319

20+
/**
21+
* Holds if this accessor is a `willSet`, called before the property is set.
22+
*/
1423
predicate isWillSet() { Synth::convertAccessorDeclToRaw(this).(Raw::AccessorDecl).isWillSet() }
1524

25+
/**
26+
* Holds if this accessor is a `didSet`, called after the property is set.
27+
*/
1628
predicate isDidSet() { Synth::convertAccessorDeclToRaw(this).(Raw::AccessorDecl).isDidSet() }
1729
}
1830
}

swift/ql/lib/codeql/swift/generated/decl/ImportDecl.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module Generated {
99
class ImportDecl extends Synth::TImportDecl, Decl {
1010
override string getAPrimaryQlClass() { result = "ImportDecl" }
1111

12+
/**
13+
* Holds if this import declaration is exported.
14+
*/
1215
predicate isExported() { Synth::convertImportDeclToRaw(this).(Raw::ImportDecl).isExported() }
1316

1417
/**

swift/ql/lib/codeql/swift/generated/decl/ModuleDecl.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ module Generated {
77
class ModuleDecl extends Synth::TModuleDecl, TypeDecl {
88
override string getAPrimaryQlClass() { result = "ModuleDecl" }
99

10+
/**
11+
* Holds if this module is the built-in one.
12+
*/
1013
predicate isBuiltinModule() {
1114
Synth::convertModuleDeclToRaw(this).(Raw::ModuleDecl).isBuiltinModule()
1215
}
1316

17+
/**
18+
* Holds if this module is a system one.
19+
*/
1420
predicate isSystemModule() {
1521
Synth::convertModuleDeclToRaw(this).(Raw::ModuleDecl).isSystemModule()
1622
}

0 commit comments

Comments
 (0)