Skip to content

Commit 2dcd7e1

Browse files
authored
Merge pull request #9353 from MathiasVP/swift-extract-throwing-and-async
Swift: Extract `isThrowing` and `isAsync`
2 parents 5d4473b + 6815e73 commit 2dcd7e1

File tree

8 files changed

+41
-0
lines changed

8 files changed

+41
-0
lines changed

swift/codegen/schema.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ AnyFunctionType:
6363
param_types: Type*
6464
param_labels: string*
6565
is_throwing: predicate
66+
is_async: predicate
6667

6768
AnyGenericType:
6869
_extends: Type

swift/extractor/visitors/TypeVisitor.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
211211
}
212212
++i;
213213
}
214+
215+
if (type->isThrowing()) {
216+
dispatcher_.emit(AnyFunctionTypeIsThrowingTrap{label});
217+
}
218+
219+
if (type->isAsync()) {
220+
dispatcher_.emit(AnyFunctionTypeIsAsyncTrap{label});
221+
}
214222
}
215223

216224
void emitBoundGenericType(swift::BoundGenericType* type, TrapLabel<BoundGenericTypeTag> label) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ class AnyFunctionTypeBase extends @any_function_type, Type {
2727
int getNumberOfParamLabels() { result = count(getAParamLabel()) }
2828

2929
predicate isThrowing() { any_function_type_is_throwing(this) }
30+
31+
predicate isAsync() { any_function_type_is_async(this) }
3032
}

swift/ql/lib/swift.dbscheme

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ any_function_type_is_throwing(
176176
int id: @any_function_type ref
177177
);
178178

179+
#keyset[id]
180+
any_function_type_is_async(
181+
int id: @any_function_type ref
182+
);
183+
179184
@any_generic_type =
180185
@nominal_or_bound_generic_nominal_type
181186
| @unbound_generic_type
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| types.swift:24:1:26:1 | throwingFunc | () throws -> Int | throws |
2+
| types.swift:28:1:28:36 | asyncFunction | (Int) async -> () | async |
3+
| types.swift:30:1:30:54 | throwingAndAsyncFunction | (Int) async throws -> () | async |
4+
| types.swift:30:1:30:54 | throwingAndAsyncFunction | (Int) async throws -> () | throws |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import codeql.swift.elements
2+
3+
from FuncDecl f, AnyFunctionType t, string s
4+
where
5+
f.getInterfaceType() = t and
6+
f.getLocation().getFile().getName().matches("%swift/ql/test%") and
7+
(
8+
t.isAsync() and s = "async"
9+
or
10+
t.isThrowing() and s = "throws"
11+
)
12+
select f, t, s

swift/ql/test/extractor-tests/types/Types.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@
3838
| types.swift:21:10:21:10 | f | (Int) -> Int |
3939
| types.swift:21:10:21:13 | call to ... | Int |
4040
| types.swift:21:12:21:12 | x | Int |
41+
| types.swift:25:10:25:10 | 42 | Int |

swift/ql/test/extractor-tests/types/types.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ func f(x: Int, y: Int) -> Int {
2020
func g(_ f: (Int) -> Int, _ x: Int) -> Int {
2121
return f(x)
2222
}
23+
24+
func throwingFunc() throws -> Int {
25+
return 42
26+
}
27+
28+
func asyncFunction(x: Int) async { }
29+
30+
func throwingAndAsyncFunction(x: Int) async throws { }

0 commit comments

Comments
 (0)