Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -654,16 +654,14 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
argName: Option[String],
annotations: Seq[KtAnnotationEntry] = Seq()
): Ast = {
val arrayExpr = expression.getArrayExpression
val typeFullName = registerType(exprTypeFullName(expression).getOrElse(TypeConstants.Any))
val identifier = identifierNode(arrayExpr, arrayExpr.getText, arrayExpr.getText, typeFullName)
val identifierAst = astWithRefEdgeMaybe(arrayExpr.getText, identifier)
val typeFullName = registerType(exprTypeFullName(expression).getOrElse(TypeConstants.Any))
val baseExpressionAst = astsForExpression(expression.getArrayExpression, None)
val astsForIndexExpr = expression.getIndexExpressions.asScala.zipWithIndex.flatMap { case (expr, idx) =>
astsForExpression(expr, Option(idx + 1))
}
val callNode =
operatorCallNode(expression, expression.getText, Operators.indexAccess, Option(typeFullName))
callAst(withArgumentName(withArgumentIndex(callNode, argIdx), argName), List(identifierAst) ++ astsForIndexExpr)
callAst(withArgumentName(withArgumentIndex(callNode, argIdx), argName), baseExpressionAst ++ astsForIndexExpr)
.withChildren(annotations.map(astForAnnotationEntry))
}

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.{Identifier, Literal}
import io.shiftleft.codepropertygraph.generated.nodes.{Identifier, Literal, Call}
import io.shiftleft.codepropertygraph.generated.{DispatchTypes, Operators}
import io.shiftleft.semanticcpg.language.*

Expand Down Expand Up @@ -70,4 +70,36 @@ class ArrayAccessExprsTests extends KotlinCode2CpgFixture(withOssDataflow = true
firstArg.refsTo.size shouldBe 1
}
}

"CPG for code with simple array construction and access in one line" should {
val cpg = code("""
|package mypkg
|
|fun main(args: Array<String>) {
| val foo = listOf(1, 2, 3)[0]
| println(foo)
|}
|""".stripMargin)

"should contain a listOf Call with the correct arguments" in {
cpg.arrayAccess.length shouldBe 1
val List(access) = cpg.arrayAccess.l

val firstArg = access.argument(1)
firstArg shouldBe a[Call]
firstArg.code shouldBe "listOf(1, 2, 3)"

val call = firstArg.asInstanceOf[Call]

call.name shouldBe "listOf"
call.argument.length shouldBe 3
call.argument(1).code shouldBe "1"
call.argument(2).code shouldBe "2"
call.argument(3).code shouldBe "3"

val secondArg = access.argument(2)
secondArg shouldBe a[Literal]
secondArg.code shouldBe "0"
}
}
}
Loading