Skip to content

Commit

Permalink
[pysrc2cpg] Set new MODULE modifier on Python module methods (#3826)
Browse files Browse the repository at this point in the history
  • Loading branch information
ml86 authored Nov 15, 2023
1 parent dea6ab2 commit cb2a6ef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name := "joern"
ThisBuild / organization := "io.joern"
ThisBuild / scalaVersion := "3.3.1"

val cpgVersion = "1.4.25"
val cpgVersion = "1.4.29"

lazy val joerncli = Projects.joerncli
lazy val querydb = Projects.querydb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ class LambdaTests extends JavaSrcCode2CpgFixture {
cpg.all.collectAll[ClosureBinding].l match {
case myValue :: Nil =>
myValue.closureOriginalName.head shouldBe "myValue"
myValue._localViaRefOut.name shouldBe "myValue"
myValue._localViaRefOut.get.name shouldBe "myValue"
myValue._captureIn.collectFirst { case x: MethodRef =>
x.methodFullName
}.head shouldBe "Foo.lambda$0:<unresolvedSignature>(1)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class PythonAstVisitor(relFileName: String, protected val nodeToCode: NodeToCode
"<module>",
methodFullName,
Some("<module>"),
ModifierTypes.VIRTUAL :: ModifierTypes.MODULE :: Nil,
parameterProvider = () => MethodParameters.empty(),
bodyProvider = () =>
createBuiltinIdentifiers(memOpCalculator.names)
Expand Down Expand Up @@ -332,6 +333,7 @@ class PythonAstVisitor(relFileName: String, protected val nodeToCode: NodeToCode
methodName,
methodFullName,
scopeName,
ModifierTypes.VIRTUAL :: Nil,
parameterProvider,
bodyProvider,
returns,
Expand All @@ -351,6 +353,7 @@ class PythonAstVisitor(relFileName: String, protected val nodeToCode: NodeToCode
name: String,
fullName: String,
scopeName: Option[String],
modifiers: List[String],
parameterProvider: () => MethodParameters,
bodyProvider: () => Iterable[nodes.NewNode],
returns: Option[ast.iexpr],
Expand All @@ -367,8 +370,12 @@ class PythonAstVisitor(relFileName: String, protected val nodeToCode: NodeToCode

contextStack.pushMethod(scopeName, methodNode, blockNode, methodRefNode)

val virtualModifierNode = nodeBuilder.modifierNode(ModifierTypes.VIRTUAL)
edgeBuilder.astEdge(virtualModifierNode, methodNode, 0)
var order = 0
for (modifier <- modifiers) {
val modifierNode = nodeBuilder.modifierNode(modifier)
edgeBuilder.astEdge(modifierNode, methodNode, order)
order += 1
}

val methodParameter = parameterProvider()
val parameterOrder = new AutoIncIndex(methodParameter.posStartIndex)
Expand Down Expand Up @@ -601,6 +608,7 @@ class PythonAstVisitor(relFileName: String, protected val nodeToCode: NodeToCode
adapterMethodName,
adapterMethodFullName,
Some(adaptedMethodName),
ModifierTypes.VIRTUAL :: Nil,
parameterProvider = () => {
MethodParameters(
0,
Expand Down Expand Up @@ -692,6 +700,7 @@ class PythonAstVisitor(relFileName: String, protected val nodeToCode: NodeToCode
methodName,
methodFullName,
Some(methodName),
ModifierTypes.VIRTUAL :: Nil,
parameterProvider = () => {
MethodParameters(1, convert(parametersWithoutSelf, 1))
},
Expand Down Expand Up @@ -742,6 +751,7 @@ class PythonAstVisitor(relFileName: String, protected val nodeToCode: NodeToCode
newMethodName,
newMethodStubFullName,
Some(newMethodName),
ModifierTypes.VIRTUAL :: Nil,
parameterProvider = () => {
MethodParameters(
0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.joern.pysrc2cpg.cpg

import io.joern.pysrc2cpg.{Constants, Py2CpgTestContext}
import io.shiftleft.semanticcpg.language._
import io.shiftleft.codepropertygraph.generated.ModifierTypes
import io.shiftleft.semanticcpg.language.*
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

Expand Down Expand Up @@ -196,4 +197,15 @@ class FunctionDefCpgTests extends AnyFreeSpec with Matchers {
}
}

"module function" - {
lazy val cpg = Py2CpgTestContext.buildCpg("""
|""".stripMargin)
"test existence of MODULE modifier on module method node" in {
cpg.method
.name("<module>")
.modifier
.modifierType(ModifierTypes.MODULE)
.nonEmpty shouldBe true
}
}
}

0 comments on commit cb2a6ef

Please sign in to comment.