Skip to content

Commit a702527

Browse files
pysrc2cpg: Local Node Line & Column Numbers (#2314)
* Extract the earliest identifier node line and column number info and add this to the local node
1 parent 9da01a9 commit a702527

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

joern-cli/frontends/pysrc2cpg/src/main/scala/io/joern/pysrc2cpg/ContextStack.scala

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
package io.joern.pysrc2cpg
22

3-
import io.shiftleft.codepropertygraph.generated.nodes
4-
import io.shiftleft.codepropertygraph.generated.nodes.{NewClosureBinding, NewIdentifier, NewLocal, NewMethod, NewNode}
3+
import io.joern.pysrc2cpg.ContextStack.transferLineColInfo
54
import io.joern.pysrc2cpg.memop._
5+
import io.shiftleft.codepropertygraph.generated.nodes
6+
import io.shiftleft.codepropertygraph.generated.nodes._
67
import org.slf4j.LoggerFactory
78

89
import scala.collection.mutable
910

1011
object ContextStack {
1112
private val logger = LoggerFactory.getLogger(getClass)
13+
14+
def transferLineColInfo(src: NewIdentifier, tgt: NewLocal): Unit = {
15+
src.lineNumber match {
16+
// If there are multiple occurrences and the local is already set, ignore later updates
17+
case Some(srcLineNo) if tgt.lineNumber.isEmpty || !tgt.lineNumber.exists(_ < srcLineNo) =>
18+
tgt.lineNumber(src.lineNumber)
19+
tgt.columnNumber(src.columnNumber)
20+
case _ =>
21+
}
22+
}
1223
}
1324

1425
class ContextStack {
@@ -156,6 +167,7 @@ class ContextStack {
156167
!moduleMethodContext.get.variables.contains(name)
157168
) {
158169
val localNode = createLocal(name, None)
170+
transferLineColInfo(identifier, localNode)
159171
createAstEdge(localNode, moduleMethodContext.get.methodBlockNode.get, moduleMethodContext.get.order.getAndInc)
160172
moduleMethodContext.get.variables.put(name, localNode)
161173
}
@@ -202,7 +214,8 @@ class ContextStack {
202214
} else if (memOp == Store) {
203215
var variableNode = lookupVariableInMethod(name, contextStack)
204216
if (variableNode.isEmpty) {
205-
val localNode = createLocal(name, None)
217+
val localNode = createLocal(name, None)
218+
transferLineColInfo(identifier, localNode)
206219
val enclosingMethodContext = findEnclosingMethodContext(contextStack)
207220
createAstEdge(localNode, enclosingMethodContext.methodBlockNode.get, enclosingMethodContext.order.getAndInc)
208221
enclosingMethodContext.variables.put(name, localNode)
@@ -253,6 +266,7 @@ class ContextStack {
253266
if (!contextHasVariable) {
254267
if (context != moduleMethodContext.get) {
255268
val localNode = createLocal(name, Some(closureBindingId))
269+
transferLineColInfo(identifier, localNode)
256270
createAstEdge(localNode, methodContext.methodBlockNode.get, methodContext.order.getAndInc)
257271
methodContext.variables.put(name, localNode)
258272
} else {
@@ -262,6 +276,7 @@ class ContextStack {
262276
// For example this happens when there are wildcard imports directly into the
263277
// modules namespace.
264278
val localNode = createLocal(name, None)
279+
transferLineColInfo(identifier, localNode)
265280
createAstEdge(localNode, methodContext.methodBlockNode.get, methodContext.order.getAndInc)
266281
methodContext.variables.put(name, localNode)
267282
}

joern-cli/frontends/pysrc2cpg/src/test/scala/io/joern/pysrc2cpg/cpg/VariableReferencingCpgTests.scala

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.joern.pysrc2cpg.cpg
22

33
import io.joern.pysrc2cpg.Py2CpgTestContext
4+
import io.shiftleft.codepropertygraph.generated.EvaluationStrategies
45
import io.shiftleft.semanticcpg.language._
5-
import io.shiftleft.codepropertygraph.generated.{EvaluationStrategies, nodes}
66
import org.scalatest.freespec.AnyFreeSpec
77
import org.scalatest.matchers.should.Matchers
88

@@ -22,6 +22,19 @@ class VariableReferencingCpgTests extends AnyFreeSpec with Matchers {
2222
localNode.referencingIdentifiers.lineNumber(2).code.head shouldBe "x"
2323
localNode.referencingIdentifiers.lineNumber(3).code.head shouldBe "x"
2424
}
25+
26+
"test local variable line and column numbers" in {
27+
val f = cpg.local.nameExact("f").head
28+
f.lineNumber shouldBe Some(1)
29+
f.columnNumber shouldBe Some(1)
30+
val x = cpg.method.name("f").local.nameExact("x").head
31+
val y = cpg.method.name("f").local.nameExact("y").head
32+
x.lineNumber shouldBe Some(2)
33+
x.columnNumber shouldBe Some(3)
34+
y.lineNumber shouldBe Some(3)
35+
y.columnNumber shouldBe Some(3)
36+
}
37+
2538
}
2639

2740
"parameter variable reference" - {
@@ -38,6 +51,20 @@ class VariableReferencingCpgTests extends AnyFreeSpec with Matchers {
3851
paramNode.referencingIdentifiers.lineNumber(2).code.head shouldBe "x"
3952
paramNode.referencingIdentifiers.lineNumber(3).code.head shouldBe "x"
4053
}
54+
55+
"test local variable line and column numbers" in {
56+
val f = cpg.local.nameExact("f").head
57+
f.lineNumber shouldBe Some(1)
58+
f.columnNumber shouldBe Some(1)
59+
60+
val x = cpg.method.name("f").parameter.name("x").head
61+
val y = cpg.method.name("f").local.name("y").head
62+
x.lineNumber shouldBe Some(1)
63+
x.columnNumber shouldBe Some(7)
64+
y.lineNumber shouldBe Some(3)
65+
y.columnNumber shouldBe Some(3)
66+
}
67+
4168
}
4269

4370
"comprehension variable reference" - {
@@ -63,6 +90,19 @@ class VariableReferencingCpgTests extends AnyFreeSpec with Matchers {
6390
val localXNode = cpg.local("x").filter(_.definingBlock.astParent.isMethod.isEmpty).head
6491
cpg.identifier("x").lineNumber(2).refsTo.foreach(local => local shouldBe localXNode)
6592
}
93+
94+
"test local variable line and column numbers" in {
95+
val f = cpg.local.nameExact("f").head
96+
f.lineNumber shouldBe Some(3)
97+
f.columnNumber shouldBe Some(1)
98+
99+
val x = cpg.local("x").filterNot(_.definingBlock.astParent.isMethod.isEmpty).head
100+
val y = cpg.local("y").filterNot(_.definingBlock.astParent.isMethod.isEmpty).head
101+
x.lineNumber shouldBe Some(1)
102+
x.columnNumber shouldBe Some(1)
103+
y.lineNumber shouldBe Some(2)
104+
y.columnNumber shouldBe Some(13)
105+
}
66106
}
67107

68108
"comprehension tuple variable reference" - {

0 commit comments

Comments
 (0)