Skip to content

Commit 5798138

Browse files
committed
Swift: extract ProtocolComposition- and BuiltinType
1 parent b41cbae commit 5798138

File tree

70 files changed

+303
-262
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+303
-262
lines changed

swift/codegen/schema.yml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ _directories:
1414

1515
Element:
1616
is_unknown: predicate
17-
_pragma: skip_qltest
17+
_pragma: qltest_skip
1818

1919
File:
2020
name: string
2121

2222
Locatable:
2323
location: Location?
24-
_pragma: skip_qltest
24+
_pragma: qltest_skip
2525

2626
Location:
2727
file: File
2828
start_line: int
2929
start_column: int
3030
end_line: int
3131
end_column: int
32-
_pragma: skip_qltest
32+
_pragma: qltest_skip
3333

3434
Type:
3535
diagnostics_name: string
@@ -85,6 +85,7 @@ AnyMetatypeType:
8585

8686
BuiltinType:
8787
_extends: Type
88+
_pragma: qltest_collapse_hierarchy
8889

8990
DependentMemberType:
9091
_extends: Type
@@ -114,6 +115,7 @@ PlaceholderType:
114115

115116
ProtocolCompositionType:
116117
_extends: Type
118+
members: Type*
117119

118120
ExistentialType:
119121
_extends: Type
@@ -393,7 +395,7 @@ EnumIsCaseExpr:
393395

394396
ErrorExpr:
395397
_extends: Expr
396-
_pragma: skip_qltest # unexpected emission
398+
_pragma: qltest_skip # unexpected emission
397399

398400
ExplicitCastExpr:
399401
_extends: Expr
@@ -468,7 +470,7 @@ ObjCSelectorExpr:
468470
_children:
469471
sub_expr: Expr
470472
method: AbstractFunctionDecl
471-
_pragma: skip_qltest # to be tested in integration tests
473+
_pragma: qltest_skip # to be tested in integration tests
472474

473475
OneWayExpr:
474476
_extends: Expr
@@ -510,7 +512,7 @@ SequenceExpr:
510512
_extends: Expr
511513
_children:
512514
elements: Expr*
513-
_pragma: skip_qltest # we should really never extract these, as these should be resolved to trees of operations
515+
_pragma: qltest_skip # we should really never extract these, as these should be resolved to trees of operations
514516

515517
SuperRefExpr:
516518
_extends: Expr
@@ -542,7 +544,7 @@ TypeExpr:
542544
UnresolvedDeclRefExpr:
543545
_extends: Expr
544546
name: string?
545-
_pragma: skip_qltest # we should really never extract these
547+
_pragma: qltest_skip # we should really never extract these
546548

547549
UnresolvedDotExpr:
548550
_extends: Expr
@@ -553,15 +555,15 @@ UnresolvedDotExpr:
553555
UnresolvedMemberExpr:
554556
_extends: Expr
555557
name: string
556-
_pragma: skip_qltest # we should really never extract these
558+
_pragma: qltest_skip # we should really never extract these
557559

558560
UnresolvedPatternExpr:
559561
_extends: Expr
560-
_pragma: skip_qltest # we should really never extract these
562+
_pragma: qltest_skip # we should really never extract these
561563

562564
UnresolvedSpecializeExpr:
563565
_extends: Expr
564-
_pragma: skip_qltest # we should really never extract these
566+
_pragma: qltest_skip # we should really never extract these
565567

566568
VarargExpansionExpr:
567569
_extends: Expr
@@ -698,6 +700,7 @@ BuiltinIntegerLiteralType:
698700

699701
BuiltinIntegerType:
700702
_extends: AnyBuiltinIntegerType
703+
_pragma: qltest_uncollapse_hierarchy
701704
width: int?
702705

703706
NestedArchetypeType:
@@ -830,11 +833,11 @@ ArrayToPointerExpr:
830833

831834
BridgeFromObjCExpr:
832835
_extends: ImplicitConversionExpr
833-
_pragma: skip_qltest # to be tested in integration tests
836+
_pragma: qltest_skip # to be tested in integration tests
834837

835838
BridgeToObjCExpr:
836839
_extends: ImplicitConversionExpr
837-
_pragma: skip_qltest # to be tested in integration tests
840+
_pragma: qltest_skip # to be tested in integration tests
838841

839842
ClassMetatypeToObjectExpr:
840843
_extends: ImplicitConversionExpr
@@ -844,7 +847,7 @@ CollectionUpcastConversionExpr:
844847

845848
ConditionalBridgeFromObjCExpr:
846849
_extends: ImplicitConversionExpr
847-
_pragma: skip_qltest # to be tested in integration tests
850+
_pragma: qltest_skip # to be tested in integration tests
848851

849852
CovariantFunctionConversionExpr:
850853
_extends: ImplicitConversionExpr

swift/extractor/visitors/TypeVisitor.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,74 @@ void TypeVisitor::fillReferenceStorageType(const swift::ReferenceStorageType& ty
304304
fillType(type, entry);
305305
}
306306

307+
codeql::ProtocolCompositionType TypeVisitor::translateProtocolCompositionType(
308+
const swift::ProtocolCompositionType& type) {
309+
auto entry = createEntry(type);
310+
entry.members = dispatcher_.fetchRepeatedLabels(type.getMembers());
311+
return entry;
312+
}
313+
314+
codeql::BuiltinIntegerLiteralType TypeVisitor::translateBuiltinIntegerLiteralType(
315+
const swift::BuiltinIntegerLiteralType& type) {
316+
return createEntry(type);
317+
}
318+
319+
codeql::BuiltinIntegerType TypeVisitor::translateBuiltinIntegerType(
320+
const swift::BuiltinIntegerType& type) {
321+
auto entry = createEntry(type);
322+
if (type.isFixedWidth()) {
323+
entry.width = type.getFixedWidth();
324+
}
325+
return entry;
326+
}
327+
328+
codeql::BuiltinBridgeObjectType TypeVisitor::translateBuiltinBridgeObjectType(
329+
const swift::BuiltinBridgeObjectType& type) {
330+
return createEntry(type);
331+
}
332+
333+
codeql::BuiltinDefaultActorStorageType TypeVisitor::translateBuiltinDefaultActorStorageType(
334+
const swift::BuiltinDefaultActorStorageType& type) {
335+
return createEntry(type);
336+
}
337+
338+
codeql::BuiltinExecutorType TypeVisitor::translateBuiltinExecutorType(
339+
const swift::BuiltinExecutorType& type) {
340+
return createEntry(type);
341+
}
342+
343+
codeql::BuiltinFloatType TypeVisitor::translateBuiltinFloatType(
344+
const swift::BuiltinFloatType& type) {
345+
return createEntry(type);
346+
}
347+
348+
codeql::BuiltinJobType TypeVisitor::translateBuiltinJobType(const swift::BuiltinJobType& type) {
349+
return createEntry(type);
350+
}
351+
352+
codeql::BuiltinNativeObjectType TypeVisitor::translateBuiltinNativeObjectType(
353+
const swift::BuiltinNativeObjectType& type) {
354+
return createEntry(type);
355+
}
356+
357+
codeql::BuiltinRawPointerType TypeVisitor::translateBuiltinRawPointerType(
358+
const swift::BuiltinRawPointerType& type) {
359+
return createEntry(type);
360+
}
361+
362+
codeql::BuiltinRawUnsafeContinuationType TypeVisitor::translateBuiltinRawUnsafeContinuationType(
363+
const swift::BuiltinRawUnsafeContinuationType& type) {
364+
return createEntry(type);
365+
}
366+
367+
codeql::BuiltinUnsafeValueBufferType TypeVisitor::translateBuiltinUnsafeValueBufferType(
368+
const swift::BuiltinUnsafeValueBufferType& type) {
369+
return createEntry(type);
370+
}
371+
372+
codeql::BuiltinVectorType TypeVisitor::translateBuiltinVectorType(
373+
const swift::BuiltinVectorType& type) {
374+
return createEntry(type);
375+
}
376+
307377
} // namespace codeql

swift/extractor/visitors/TypeVisitor.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
4747
const swift::UnmanagedStorageType& type);
4848
codeql::WeakStorageType translateWeakStorageType(const swift::WeakStorageType& type);
4949
codeql::UnownedStorageType translateUnownedStorageType(const swift::UnownedStorageType& type);
50+
codeql::ProtocolCompositionType translateProtocolCompositionType(
51+
const swift::ProtocolCompositionType& type);
52+
codeql::BuiltinIntegerLiteralType translateBuiltinIntegerLiteralType(
53+
const swift::BuiltinIntegerLiteralType& type);
54+
codeql::BuiltinIntegerType translateBuiltinIntegerType(const swift::BuiltinIntegerType& type);
55+
codeql::BuiltinBridgeObjectType translateBuiltinBridgeObjectType(
56+
const swift::BuiltinBridgeObjectType& type);
57+
codeql::BuiltinDefaultActorStorageType translateBuiltinDefaultActorStorageType(
58+
const swift::BuiltinDefaultActorStorageType& type);
59+
codeql::BuiltinExecutorType translateBuiltinExecutorType(const swift::BuiltinExecutorType& type);
60+
codeql::BuiltinFloatType translateBuiltinFloatType(const swift::BuiltinFloatType& type);
61+
codeql::BuiltinJobType translateBuiltinJobType(const swift::BuiltinJobType& type);
62+
codeql::BuiltinNativeObjectType translateBuiltinNativeObjectType(
63+
const swift::BuiltinNativeObjectType& type);
64+
codeql::BuiltinRawPointerType translateBuiltinRawPointerType(
65+
const swift::BuiltinRawPointerType& type);
66+
codeql::BuiltinRawUnsafeContinuationType translateBuiltinRawUnsafeContinuationType(
67+
const swift::BuiltinRawUnsafeContinuationType& type);
68+
codeql::BuiltinUnsafeValueBufferType translateBuiltinUnsafeValueBufferType(
69+
const swift::BuiltinUnsafeValueBufferType& type);
70+
codeql::BuiltinVectorType translateBuiltinVectorType(const swift::BuiltinVectorType& type);
5071

5172
private:
5273
void fillType(const swift::TypeBase& type, codeql::Type& entry);
@@ -58,6 +79,13 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
5879
void emitAnyFunctionType(const swift::AnyFunctionType* type, TrapLabel<AnyFunctionTypeTag> label);
5980
void emitBoundGenericType(swift::BoundGenericType* type, TrapLabel<BoundGenericTypeTag> label);
6081
void emitAnyGenericType(swift::AnyGenericType* type, TrapLabel<AnyGenericTypeTag> label);
82+
83+
template <typename T>
84+
auto createEntry(const T& type) {
85+
TrapClassOf<T> entry{dispatcher_.assignNewLabel(type)};
86+
fillType(type, entry);
87+
return entry;
88+
}
6189
};
6290

6391
} // namespace codeql
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1+
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
12
private import codeql.swift.generated.type.ExistentialType
2-
private import codeql.swift.elements.type.ProtocolType
33

4-
class ExistentialType extends ExistentialTypeBase {
5-
override ProtocolType getConstraint() { result = super.getConstraint() }
6-
}
4+
class ExistentialType extends ExistentialTypeBase { }

swift/ql/lib/codeql/swift/generated/type/ProtocolCompositionType.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,15 @@ import codeql.swift.elements.type.Type
33

44
class ProtocolCompositionTypeBase extends @protocol_composition_type, Type {
55
override string getAPrimaryQlClass() { result = "ProtocolCompositionType" }
6+
7+
Type getMember(int index) {
8+
exists(Type x |
9+
protocol_composition_type_members(this, index, x) and
10+
result = x.resolve()
11+
)
12+
}
13+
14+
Type getAMember() { result = getMember(_) }
15+
16+
int getNumberOfMembers() { result = count(getAMember()) }
617
}

swift/ql/lib/swift.dbscheme

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ protocol_composition_types( //dir=type
274274
unique int id: @protocol_composition_type
275275
);
276276

277+
#keyset[id, index]
278+
protocol_composition_type_members( //dir=type
279+
int id: @protocol_composition_type ref,
280+
int index: int ref,
281+
int member: @type ref
282+
);
283+
277284
existential_types( //dir=type
278285
unique int id: @existential_type,
279286
int constraint: @type ref

swift/ql/test/TestUtils.qll

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ predicate toBeTested(Element e) {
99
(
1010
e = loc
1111
or
12-
e = loc.(ValueDecl).getInterfaceType()
13-
or
14-
e = loc.(NominalTypeDecl).getType()
15-
or
16-
e = loc.(VarDecl).getType()
17-
or
18-
e = loc.(Expr).getType()
12+
exists(Type t |
13+
(e = t or e = t.(ExistentialType).getConstraint() or e = t.getCanonicalType()) and
14+
(
15+
t = loc.(ValueDecl).getInterfaceType()
16+
or
17+
t = loc.(NominalTypeDecl).getType()
18+
or
19+
t = loc.(VarDecl).getType()
20+
or
21+
t = loc.(Expr).getType()
22+
)
23+
)
1924
)
2025
)
2126
}

swift/ql/test/extractor-tests/generated/File/File.ql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import codeql.swift.elements
33
import TestUtils
44

5-
from File x, string isUnknown, string getName
5+
from File x, string getName
66
where
77
toBeTested(x) and
88
not x.isUnknown() and
9-
(if x.isUnknown() then isUnknown = "yes" else isUnknown = "no") and
109
getName = x.getName()
11-
select x, "isUnknown:", isUnknown, "getName:", getName
10+
select x, "getName:", getName

swift/ql/test/extractor-tests/generated/Location/MISSING_SOURCE.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

swift/ql/test/extractor-tests/generated/decl/AccessorDecl/AccessorDecl.ql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import codeql.swift.elements
33
import TestUtils
44

55
from
6-
AccessorDecl x, string isUnknown, Type getInterfaceType, string getName, string isGetter,
7-
string isSetter, string isWillSet, string isDidSet
6+
AccessorDecl x, Type getInterfaceType, string getName, string isGetter, string isSetter,
7+
string isWillSet, string isDidSet
88
where
99
toBeTested(x) and
1010
not x.isUnknown() and
11-
(if x.isUnknown() then isUnknown = "yes" else isUnknown = "no") and
1211
getInterfaceType = x.getInterfaceType() and
1312
getName = x.getName() and
1413
(if x.isGetter() then isGetter = "yes" else isGetter = "no") and
1514
(if x.isSetter() then isSetter = "yes" else isSetter = "no") and
1615
(if x.isWillSet() then isWillSet = "yes" else isWillSet = "no") and
1716
if x.isDidSet() then isDidSet = "yes" else isDidSet = "no"
18-
select x, "isUnknown:", isUnknown, "getInterfaceType:", getInterfaceType, "getName:", getName,
19-
"isGetter:", isGetter, "isSetter:", isSetter, "isWillSet:", isWillSet, "isDidSet:", isDidSet
17+
select x, "getInterfaceType:", getInterfaceType, "getName:", getName, "isGetter:", isGetter,
18+
"isSetter:", isSetter, "isWillSet:", isWillSet, "isDidSet:", isDidSet

0 commit comments

Comments
 (0)