Skip to content

Commit 25d1098

Browse files
authored
Merge pull request #10511 from github/redsun82/swift-ifconfigdecl-ql
Swift: fix `IfConfigDecl` in QL libraries
2 parents 46a23e1 + 935d5d9 commit 25d1098

File tree

9 files changed

+147
-8
lines changed

9 files changed

+147
-8
lines changed

swift/codegen/schema.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ EnumCaseDecl:
303303

304304
IfConfigDecl:
305305
_extends: Decl
306-
_children:
307-
active_elements: AstNode*
306+
active_elements: AstNode*
308307

309308
ImportDecl:
310309
_extends: Decl

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,15 @@ module Decls {
10021002
i = ast.getNumberOfParams()
10031003
}
10041004
}
1005+
1006+
/**
1007+
* The control-flow of an #if block.
1008+
* The active elements are already listed in the containing scope, so we can just flow through
1009+
* this as a leaf.
1010+
*/
1011+
class IfConfigDeclTree extends AstLeafTree {
1012+
override IfConfigDecl ast;
1013+
}
10051014
}
10061015

10071016
module Exprs {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,14 @@ private module Impl {
262262
private Element getImmediateChildOfIfConfigDecl(
263263
IfConfigDecl e, int index, string partialPredicateCall
264264
) {
265-
exists(int b, int bDecl, int n, int nActiveElement |
265+
exists(int b, int bDecl, int n |
266266
b = 0 and
267267
bDecl = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfDecl(e, i, _)) | i) and
268268
n = bDecl and
269-
nActiveElement = n + 1 + max(int i | i = -1 or exists(e.getImmediateActiveElement(i)) | i) and
270269
(
271270
none()
272271
or
273272
result = getImmediateChildOfDecl(e, index - b, partialPredicateCall)
274-
or
275-
result = e.getImmediateActiveElement(index - n) and
276-
partialPredicateCall = "ActiveElement(" + (index - n).toString() + ")"
277273
)
278274
)
279275
}

swift/ql/test/extractor-tests/declarations/all.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,7 @@
263263
| declarations.swift:155:1:155:28 | var ... = ... |
264264
| declarations.swift:155:1:155:28 | { ... } |
265265
| declarations.swift:155:5:155:5 | d |
266+
| declarations.swift:157:1:180:1 | ifConfig() |
267+
| declarations.swift:158:3:166:3 | #if ... |
268+
| declarations.swift:168:3:171:3 | #if ... |
269+
| declarations.swift:173:3:179:3 | #if ... |

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,28 @@ class Derived : Baz {}
153153

154154
// multiple conversions
155155
var d: Baz? = Derived() as Baz
156+
157+
func ifConfig() {
158+
#if FOO
159+
1
160+
2
161+
3
162+
#else
163+
4
164+
5
165+
6
166+
#endif
167+
168+
#if BAR
169+
7
170+
8
171+
#endif
172+
173+
#if true
174+
9
175+
10
176+
#else
177+
11
178+
12
179+
#endif
180+
}

swift/ql/test/library-tests/ast/PrintAst.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,17 @@ declarations.swift:
706706
# 155| getTypeRepr(): [TypeRepr] Baz?
707707
# 155| [ConcreteVarDecl] d
708708
# 155| Type = Baz?
709+
# 157| [ConcreteFuncDecl] ifConfig()
710+
# 157| InterfaceType = () -> ()
711+
# 157| getBody(): [BraceStmt] { ... }
712+
# 158| getElement(0): [IfConfigDecl] #if ...
713+
# 163| getElement(1): [IntegerLiteralExpr] 4
714+
# 164| getElement(2): [IntegerLiteralExpr] 5
715+
# 165| getElement(3): [IntegerLiteralExpr] 6
716+
# 168| getElement(4): [IfConfigDecl] #if ...
717+
# 173| getElement(5): [IfConfigDecl] #if ...
718+
# 174| getElement(6): [IntegerLiteralExpr] 9
719+
# 175| getElement(7): [IntegerLiteralExpr] 10
709720
expressions.swift:
710721
# 1| [TopLevelCodeDecl] { ... }
711722
# 1| getBody(): [BraceStmt] { ... }

swift/ql/test/library-tests/ast/declarations.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,28 @@ class Derived : Baz {}
153153

154154
// multiple conversions
155155
var d: Baz? = Derived() as Baz
156+
157+
func ifConfig() {
158+
#if FOO
159+
1
160+
2
161+
3
162+
#else
163+
4
164+
5
165+
6
166+
#endif
167+
168+
#if BAR
169+
7
170+
8
171+
#endif
172+
173+
#if true
174+
9
175+
10
176+
#else
177+
11
178+
12
179+
#endif
180+
}

swift/ql/test/library-tests/controlflow/graph/Cfg.expected

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6295,3 +6295,44 @@ cfg.swift:
62956295

62966296
# 464| kpGet_mayB_x
62976297
#-----| -> (KeyPath<A, Int?>) ...
6298+
6299+
# 467| enter testIfConfig()
6300+
#-----| -> testIfConfig()
6301+
6302+
# 467| exit testIfConfig()
6303+
6304+
# 467| exit testIfConfig() (normal)
6305+
#-----| -> exit testIfConfig()
6306+
6307+
# 467| testIfConfig()
6308+
#-----| -> #if ...
6309+
6310+
# 468| #if ...
6311+
#-----| -> 3
6312+
6313+
# 472| 3
6314+
#-----| -> 4
6315+
6316+
# 473| 4
6317+
#-----| -> 5
6318+
6319+
# 476| 5
6320+
#-----| -> #if ...
6321+
6322+
# 478| #if ...
6323+
#-----| -> 8
6324+
6325+
# 483| 8
6326+
#-----| -> #if ...
6327+
6328+
# 485| #if ...
6329+
#-----| -> 11
6330+
6331+
# 489| 11
6332+
#-----| -> 12
6333+
6334+
# 490| 12
6335+
#-----| -> 13
6336+
6337+
# 493| 13
6338+
#-----| -> exit testIfConfig() (normal)

swift/ql/test/library-tests/controlflow/graph/cfg.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func testInOut() -> Int {
8888
func addOptional(a: inout Int?) {
8989
a = nil
9090
}
91-
91+
9292
add(a:&temp)
9393
var tempOptional : Int? = 10
9494
addOptional(a:&tempOptional)
@@ -463,3 +463,32 @@ func test(a : A) {
463463
var apply_kpGet_mayB_force_x = a[keyPath: kpGet_mayB_force_x]
464464
var apply_kpGet_mayB_x = a[keyPath: kpGet_mayB_x]
465465
}
466+
467+
func testIfConfig() {
468+
#if FOO
469+
1
470+
2
471+
#else
472+
3
473+
4
474+
#endif
475+
476+
5
477+
478+
#if BAR
479+
6
480+
7
481+
#endif
482+
483+
8
484+
485+
#if FOO
486+
9
487+
10
488+
#elseif true
489+
11
490+
12
491+
#endif
492+
493+
13
494+
}

0 commit comments

Comments
 (0)