Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,11 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) {
else astForTryAsExpression(expr, argIdx, argNameMaybe, annotations)
}

def astForBreak(expr: KtBreakExpression): Ast = {
val node = controlStructureNode(expr, ControlStructureTypes.BREAK, code(expr))
Ast(node)
}
def astForBreak(expr: KtBreakExpression): Ast =
breakAst(expr, code(expr), Option(expr.getLabelName))

def astForContinue(expr: KtContinueExpression): Ast = {
val node = controlStructureNode(expr, ControlStructureTypes.CONTINUE, code(expr))
Ast(node)
}
def astForContinue(expr: KtContinueExpression): Ast =
continueAst(expr, code(expr), Option(expr.getLabelName))

def astForThrowExpression(
expr: KtThrowExpression,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.joern.kotlin2cpg.querying

import io.joern.kotlin2cpg.testfixtures.KotlinCode2CpgFixture
import io.shiftleft.codepropertygraph.generated.nodes.{Block, Call, ControlStructure, Identifier, Local}
import io.shiftleft.codepropertygraph.generated.nodes.{Block, Call, ControlStructure, Identifier, JumpLabel, Local}
import io.shiftleft.codepropertygraph.generated.{ControlStructureTypes, DispatchTypes, EdgeTypes, Operators}
import io.shiftleft.semanticcpg.language.*
import io.shiftleft.codepropertygraph.generated.nodes.Literal
Expand Down Expand Up @@ -717,4 +717,79 @@ class ControlStructureTests extends KotlinCode2CpgFixture(withOssDataflow = fals
}

// TODO: also add test for the loop range, when it is with downTo or whatever

"CPG for code with labeled break statement" should {
val cpg = code("""
|package mypkg
|fun foo() {
| outer@ for (i in 0..10) {
| for (j in 0..10) {
| if (j == 5) break@outer
| }
| }
|}""".stripMargin)

"should contain a BREAK with a JUMP_LABEL child" in {
val List(breakNode) = cpg.controlStructure.isBreak.l
breakNode.code shouldBe "break@outer"
val List(jumpLabel) = breakNode.astChildren.collectAll[JumpLabel].l
jumpLabel.name shouldBe "outer"
jumpLabel.order shouldBe 1
}

"should have a JUMP_ARGUMENT edge from break to the JUMP_LABEL" in {
val List(breakNode) = cpg.controlStructure.isBreak.l
val List(jumpLabel) = breakNode.jumpArgumentOut.collectAll[JumpLabel].l
jumpLabel.name shouldBe "outer"
}
}

"CPG for code with labeled continue statement" should {
val cpg = code("""
|package mypkg
|fun foo() {
| outer@ for (i in 0..10) {
| for (j in 0..10) {
| if (j == 3) continue@outer
| }
| }
|}""".stripMargin)

"should contain a CONTINUE with a JUMP_LABEL child" in {
val List(continueNode) = cpg.controlStructure.isContinue.l
continueNode.code shouldBe "continue@outer"
val List(jumpLabel) = continueNode.astChildren.collectAll[JumpLabel].l
jumpLabel.name shouldBe "outer"
jumpLabel.order shouldBe 1
}

"should have a JUMP_ARGUMENT edge from continue to the JUMP_LABEL" in {
val List(continueNode) = cpg.controlStructure.isContinue.l
val List(jumpLabel) = continueNode.jumpArgumentOut.collectAll[JumpLabel].l
jumpLabel.name shouldBe "outer"
}
}

"CPG for code with unlabeled break/continue" should {
val cpg = code("""
|package mypkg
|fun foo() {
| for (i in 0..10) {
| if (i == 5) break
| if (i == 3) continue
| }
|}""".stripMargin)

"should have no JUMP_LABEL child and no JUMP_ARGUMENT edge on break" in {
val List(breakNode) = cpg.controlStructure.isBreak.l
breakNode.astChildren.collectAll[JumpLabel].size shouldBe 0
breakNode.jumpArgumentOut.size shouldBe 0
}

"should have no JUMP_LABEL child and no JUMP_ARGUMENT edge on continue" in {
val List(continueNode) = cpg.controlStructure.isContinue.l
continueNode.astChildren.collectAll[JumpLabel].size shouldBe 0
continueNode.jumpArgumentOut.size shouldBe 0
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,42 @@ abstract class AstCreatorBase[Node, NodeProcessor](filename: String)(implicit wi
}
}

/** Creates an AST for a break statement. When `labelName` is present a `JumpLabel` child is created at order 1 and
* connected to the break node via a `JUMP_ARGUMENT` edge.
*/
def breakAst(node: Node, codeStr: String, labelName: Option[String]): Ast =
labeledJumpAst(node, ControlStructureTypes.BREAK, codeStr, labelName)

/** Creates an AST for a continue statement. When `labelName` is present a `JumpLabel` child is created at order 1 and
* connected to the continue node via a `JUMP_ARGUMENT` edge.
*/
def continueAst(node: Node, codeStr: String, labelName: Option[String]): Ast =
labeledJumpAst(node, ControlStructureTypes.CONTINUE, codeStr, labelName)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you go for optional labelName? We currently have two kinds of break/continue statements we support. One with a label and one with an integer indicating the number of levels to jump out. The None case would be a third option.
I suggest you create one breakAst which take String and which take Int to implement those two cases. Same for continue.

Copy link
Copy Markdown
Contributor Author

@SuperUserDone SuperUserDone Jun 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was under the impression that there are 3 cases:

  • Label, eg break x
  • Integer, eg break 1
  • No argument, eg break

No argument would be handled by emitting no edge and no label or integer, only the control structure node.

Made it so the break without an argument is equivalent to break 1.


def labeledJumpAst(node: Node, jumpType: String, codeStr: String, labelName: Option[String]): Ast = {
val jumpNode = NewControlStructure()
.parserTypeName(node.getClass.getSimpleName)
.controlStructureType(jumpType)
.code(codeStr)
.lineNumber(line(node))
.columnNumber(column(node))
labelName match {
case Some(name) =>
val jumpLabelNode = NewJumpLabel()
.parserTypeName(node.getClass.getSimpleName)
.name(name)
.code(name)
.lineNumber(line(node))
.columnNumber(column(node))
.order(1)
Ast(jumpNode)
.withChild(Ast(jumpLabelNode))
.withJumpArgumentEdge(jumpNode, jumpLabelNode)
case None =>
Ast(jumpNode)
}
}

/** For the given try body, catch ASTs and finally AST, create a try-catch-finally AST with orders set correctly for
* the ossdataflow engine.
*/
Expand Down