Repository: joernio/joern (scala)

Issue:
`cpg.local.dump` outputs empty strings
Currently, `cpg.local.dump` returns empty strings, at least for the C frontends, while `cpg.local.method.dump` works fine. My guess is that locations for locals are not returned correctly or that line number information is missing.

Pull request description:
Fixes https://github.com/joernio/joern/issues/847

Patch:
diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala
index 1bd819109e9e..5d4c1cf21168 100644
--- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala
+++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala
@@ -117,6 +117,7 @@ trait AstForTypesCreator {
           .name(name)
           .typeFullName(registerType(tpe))
           .order(order)
+          .lineNumber(line(declarator))
         scope.addToScope(name, (l, tpe))
         Ast(l)
       case _ =>
@@ -125,6 +126,7 @@ trait AstForTypesCreator {
           .name(name)
           .typeFullName(registerType(tpe))
           .order(order)
+          .lineNumber(line(declarator))
         scope.addToScope(name, (l, tpe))
         Ast(l)
     }


Test patch:
diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/querying/LocationTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/querying/LocationTests.scala
index cf59cb20ce05..eeaa058c4cfd 100644
--- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/querying/LocationTests.scala
+++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/querying/LocationTests.scala
@@ -83,7 +83,19 @@ class LocationTests extends CCodeToCpgSuite {
     loc.lineNumber shouldBe Some(3)
     loc.filename should endWith(".c")
     loc.nodeLabel shouldBe "IDENTIFIER"
+  }
+
+  "should return location for local" in {
+    val locations = cpg.local.name("x").location.l
+    locations.size shouldBe 1
 
+    val loc = locations.head
+    loc.methodFullName shouldBe "my_func"
+    loc.methodShortName shouldBe "my_func"
+    loc.symbol shouldBe "x"
+    loc.lineNumber shouldBe Some(3)
+    loc.filename should endWith(".c")
+    loc.nodeLabel shouldBe "LOCAL"
   }
 
 }