Add break/continue QL and tests

This commit is contained in:
Tamas Vajk
2021-09-21 16:06:23 +02:00
committed by Ian Lynagh
parent aa190f9d65
commit 32a61c16cb
6 changed files with 71 additions and 0 deletions

View File

@@ -887,3 +887,27 @@ class SuperConstructorInvocationStmt extends Stmt, ConstructorCall, @superconstr
override string getAPrimaryQlClass() { result = "SuperConstructorInvocationStmt" }
}
/** A Kotlin loop statement. */
class KtLoopStmt extends Stmt, @ktloopstmt {
KtLoopStmt() {
this instanceof WhileStmt or
this instanceof DoStmt
}
}
/** A Kotlin `break` or `continue` statement. */
abstract class KtBreakContinueStmt extends Stmt, @breakcontinuestmt {
KtLoopStmt loop;
KtBreakContinueStmt() { ktBreakContinueTarget(this, loop) }
/** Gets the target loop statement of this `break`. */
KtLoopStmt getLoopStmt() { result = loop }
}
/** A Kotlin `break` statement. */
class KtBreakStmt extends BreakStmt, KtBreakContinueStmt { }
/** A Kotlin `continue` statement. */
class KtContinueStmt extends ContinueStmt, KtBreakContinueStmt { }

View File

@@ -43,3 +43,16 @@
| stmts.kt:19:12:19:12 | x | VarAccess |
| stmts.kt:19:12:19:16 | ... + ... | AddExpr |
| stmts.kt:19:16:19:16 | y | VarAccess |
| stmts.kt:23:18:23:18 | x | VarAccess |
| stmts.kt:23:18:23:24 | ... < ... | LTExpr |
| stmts.kt:23:22:23:24 | 100 | IntegerLiteral |
| stmts.kt:25:13:25:33 | when ... | WhenExpr |
| stmts.kt:25:17:25:17 | x | VarAccess |
| stmts.kt:25:17:25:21 | ... > ... | GTExpr |
| stmts.kt:25:21:25:21 | y | VarAccess |
| stmts.kt:26:18:26:18 | y | VarAccess |
| stmts.kt:26:18:26:24 | ... > ... | GTExpr |
| stmts.kt:26:22:26:24 | 100 | IntegerLiteral |
| stmts.kt:28:11:28:11 | x | VarAccess |
| stmts.kt:28:11:28:15 | ... > ... | GTExpr |
| stmts.kt:28:15:28:15 | y | VarAccess |

View File

@@ -0,0 +1,7 @@
breakLabel
| stmts.kt:25:24:25:33 | break | loop |
continueLabel
breakTarget
| stmts.kt:25:24:25:33 | break | stmts.kt:23:11:27:5 | while (...) |
continueTarget
| stmts.kt:29:9:29:16 | continue | stmts.kt:28:5:29:16 | while (...) |

View File

@@ -0,0 +1,9 @@
import java
query predicate breakLabel(BreakStmt s, string label) { s.getLabel() = label }
query predicate continueLabel(ContinueStmt s, string label) { s.getLabel() = label }
query predicate breakTarget(KtBreakStmt s, KtLoopStmt l) { s.getLoopStmt() = l }
query predicate continueTarget(KtContinueStmt s, KtLoopStmt l) { s.getLoopStmt() = l }

View File

@@ -14,3 +14,12 @@
| stmts.kt:17:35:17:43 | { ... } | BlockStmt |
| stmts.kt:17:50:17:58 | { ... } | BlockStmt |
| stmts.kt:19:5:19:16 | return ... | ReturnStmt |
| stmts.kt:22:27:30:1 | { ... } | BlockStmt |
| stmts.kt:23:11:27:5 | while (...) | WhileStmt |
| stmts.kt:23:27:27:5 | { ... } | BlockStmt |
| stmts.kt:24:9:26:25 | do ... while (...) | DoStmt |
| stmts.kt:24:9:26:25 | { ... } | BlockStmt |
| stmts.kt:24:13:26:9 | { ... } | BlockStmt |
| stmts.kt:25:24:25:33 | break | BreakStmt |
| stmts.kt:28:5:29:16 | while (...) | WhileStmt |
| stmts.kt:29:9:29:16 | continue | ContinueStmt |

View File

@@ -19,3 +19,12 @@ fun topLevelMethod(x: Int, y: Int): Int {
return x + y
}
fun loops(x: Int, y: Int) {
loop@ while (x < 100) {
do {
if (x > y) break@loop
} while (y > 100)
}
while(x > y)
continue
}