Skip to content

Commit

Permalink
[TypeRecovery] include <returnValue> dummy for index accesses of calls (
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierpinho committed Jun 12, 2024
1 parent e7940fe commit 706a17b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.joern.pysrc2cpg.passes

import io.joern.pysrc2cpg.PySrc2CpgFixture
import io.joern.x2cpg.passes.frontend.XTypeHintCallLinker
import io.shiftleft.codepropertygraph.generated.Operators
import io.shiftleft.codepropertygraph.generated.nodes.{Call, Identifier, Member}
import io.shiftleft.semanticcpg.language.*
import io.shiftleft.semanticcpg.language.importresolver.*
Expand Down Expand Up @@ -1457,4 +1458,47 @@ class TypeRecoveryPassTests extends PySrc2CpgFixture(withOssDataflow = false) {
}
}
}

"assignment to imported method call" should {
val cpg = code("""
|from helpers import foo
|x = foo()
|""".stripMargin)

"provide meaningful typeFullName for the target of the assignment" in {
cpg.assignment.target.isIdentifier.name("x").l match {
case List(x) => x.typeFullName shouldBe "helpers.py:<module>.foo.<returnValue>"
case result => fail(s"Expected single assignment to x, but got $result")
}
}
}

"assignment to non-chained index access of an imported method call" should {
val cpg = code("""
|from helpers import foo
|x = foo()
|y = x[0]
|""".stripMargin)

"provide meaningful typeFullName for the target of the assignment" in {
cpg.assignment.target.isIdentifier.name("y").l match {
case List(y) => y.typeFullName shouldBe "helpers.py:<module>.foo.<returnValue>.<indexAccess>"
case result => fail(s"Expected single assignment to y, but got $result")
}
}
}

"assignment to chained index access of an imported method call" should {
val cpg = code("""
|from helpers import foo
|x = foo()[0]
|""".stripMargin)

"provide meaningful typeFullName for the target of the assignment" in {
cpg.assignment.target.isIdentifier.name("x").l match {
case List(x) => x.typeFullName shouldBe "helpers.py:<module>.foo.<returnValue>.<indexAccess>"
case result => fail(s"Expected single assignment to x, but got $result")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,10 @@ abstract class RecoverForXCompilationUnit[CompilationUnitType <: AstNode](
protected def getIndexAccessTypes(ia: Call): Set[String] = indexAccessToCollectionVar(ia) match {
case Some(cVar) if symbolTable.contains(cVar) =>
symbolTable.get(cVar)
case Some(cVar) if ia.argument(1).isCall && symbolTable.contains(CallAlias(cVar.identifier)) =>
symbolTable
.get(CallAlias(cVar.identifier))
.map(x => s"$x$pathSep${XTypeRecovery.DummyReturnType}$pathSep${XTypeRecovery.DummyIndexAccess}")
case Some(cVar) if symbolTable.contains(LocalVar(cVar.identifier)) =>
symbolTable.get(LocalVar(cVar.identifier)).map(x => s"$x$pathSep${XTypeRecovery.DummyIndexAccess}")
case _ => Set.empty
Expand Down

0 comments on commit 706a17b

Please sign in to comment.