Skip to content

Commit 69dfdf5

Browse files
committed
Swift: fix IfConfigDecl in QL libraries
This fixes `IfConfigDecl` for both the AST printer and control flow libraries. It turns out that the active lements of an `IfConfigDecl` are already listed in the enclosing scope (like a `BraceStmt`), so they should not be listed as children, and `IfConfigDecl` can be jsut a leaf in the control flow.
1 parent 5af739d commit 69dfdf5

File tree

12 files changed

+150
-11
lines changed

12 files changed

+150
-11
lines changed

csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraphImplShared.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ predicate last(ControlFlowTree cft, ControlFlowElement last, Completion c) {
5252
*/
5353
pragma[nomagic]
5454
predicate succ(ControlFlowElement pred, ControlFlowElement succ, Completion c) {
55-
any(ControlFlowTree cft).succ(pred, succ, c)
55+
exists(ControlFlowTree cft | cft.succ(pred, succ, c))
5656
}
5757

5858
/** An element that is executed in pre-order. */

ruby/ql/lib/codeql/ruby/controlflow/internal/ControlFlowGraphImplShared.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ predicate last(ControlFlowTree cft, ControlFlowElement last, Completion c) {
5252
*/
5353
pragma[nomagic]
5454
predicate succ(ControlFlowElement pred, ControlFlowElement succ, Completion c) {
55-
any(ControlFlowTree cft).succ(pred, succ, c)
55+
exists(ControlFlowTree cft | cft.succ(pred, succ, c))
5656
}
5757

5858
/** An element that is executed in pre-order. */

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/controlflow/internal/ControlFlowGraphImplShared.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ predicate last(ControlFlowTree cft, ControlFlowElement last, Completion c) {
5252
*/
5353
pragma[nomagic]
5454
predicate succ(ControlFlowElement pred, ControlFlowElement succ, Completion c) {
55-
any(ControlFlowTree cft).succ(pred, succ, c)
55+
exists(ControlFlowTree cft | cft.succ(pred, succ, c))
5656
}
5757

5858
/** An element that is executed in pre-order. */

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,18 +1760,14 @@ private module Impl {
17601760
private Element getImmediateChildOfIfConfigDecl(
17611761
IfConfigDecl e, int index, string partialPredicateCall
17621762
) {
1763-
exists(int b, int bDecl, int n, int nActiveElement |
1763+
exists(int b, int bDecl, int n |
17641764
b = 0 and
17651765
bDecl = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfDecl(e, i, _)) | i) and
17661766
n = bDecl and
1767-
nActiveElement = n + 1 + max(int i | i = -1 or exists(e.getImmediateActiveElement(i)) | i) and
17681767
(
17691768
none()
17701769
or
17711770
result = getImmediateChildOfDecl(e, index - b, partialPredicateCall)
1772-
or
1773-
result = e.getImmediateActiveElement(index - n) and
1774-
partialPredicateCall = "ActiveElement(" + (index - n).toString() + ")"
17751771
)
17761772
)
17771773
}

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+
}

0 commit comments

Comments
 (0)