Skip to content

Commit 71ea2ae

Browse files
authored
NamespaceBlock tests and cleanup (#1067)
1 parent 40bdfd6 commit 71ea2ae

8 files changed

Lines changed: 63 additions & 13 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.shiftleft.fuzzyc2cpg.querying
2+
3+
import io.shiftleft.fuzzyc2cpg.testfixtures.FuzzyCCodeToCpgSuite
4+
import io.shiftleft.semanticcpg.language._
5+
import io.shiftleft.semanticcpg.language.types.structure.{File, Namespace}
6+
7+
class NamespaceBlockTests extends FuzzyCCodeToCpgSuite {
8+
9+
// The fuzzyC parser currently just ignores namespaces. We place symbols
10+
// that can't be associated in a file into the namespace "<global>", and
11+
// those which can in `filename:<global>`
12+
13+
override val code: String =
14+
"""
15+
|""".stripMargin
16+
17+
"should contain two namespace blocks in total" in {
18+
cpg.namespaceBlock.size shouldBe 2
19+
}
20+
21+
"should contain a correct global namespace block for the `<unknown>` file" in {
22+
cpg.namespaceBlock.filename(File.UNKNOWN).l match {
23+
case List(x) =>
24+
x.name shouldBe Namespace.globalNamespaceName
25+
x.fullName shouldBe Namespace.globalNamespaceName
26+
x.order shouldBe 0
27+
case _ => fail
28+
}
29+
}
30+
31+
"should contain correct namespace block for known file" in {
32+
cpg.namespaceBlock.filenameNot(File.UNKNOWN).l match {
33+
case List(x) =>
34+
x.name shouldBe Namespace.globalNamespaceName
35+
x.fullName shouldBe s"${x.filename}:${Namespace.globalNamespaceName}"
36+
x.order shouldBe 0
37+
case _ => fail
38+
}
39+
}
40+
41+
}

fuzzyc2cpg/src/main/scala/io/shiftleft/fuzzyc2cpg/Defines.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.shiftleft.fuzzyc2cpg
22

33
object Defines {
4-
val globalNamespaceName = "<global>"
54
val anyTypeName = "ANY"
65
val voidTypeName = "void"
76
val charPointerTypeName = "char *"

fuzzyc2cpg/src/main/scala/io/shiftleft/fuzzyc2cpg/passes/AstCreationPass.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package io.shiftleft.fuzzyc2cpg.passes
33
import io.shiftleft.codepropertygraph.Cpg
44
import io.shiftleft.codepropertygraph.generated.nodes
55
import io.shiftleft.fuzzyc2cpg.passes.astcreation.{AntlrCModuleParserDriver, AstVisitor}
6-
import io.shiftleft.fuzzyc2cpg.{Defines, Global}
6+
import io.shiftleft.fuzzyc2cpg.Global
77
import io.shiftleft.passes.{DiffGraph, IntervalKeyPool, ParallelCpgPass}
8+
import io.shiftleft.semanticcpg.language.types.structure.Namespace
89
import org.slf4j.LoggerFactory
910

1011
/**
@@ -24,9 +25,10 @@ class AstCreationPass(filenames: List[String], cpg: Cpg, keyPool: IntervalKeyPoo
2425
val diffGraph = DiffGraph.newBuilder
2526
val absolutePath = new java.io.File(filename).toPath.toAbsolutePath.normalize().toString
2627
val namespaceBlock = nodes.NewNamespaceBlock(
27-
name = Defines.globalNamespaceName,
28+
name = Namespace.globalNamespaceName,
2829
fullName = CMetaDataPass.getGlobalNamespaceBlockFullName(Some(absolutePath)),
29-
filename = absolutePath
30+
filename = absolutePath,
31+
order = 0
3032
)
3133
diffGraph.addNode(namespaceBlock)
3234
val driver = createDriver(namespaceBlock)

fuzzyc2cpg/src/main/scala/io/shiftleft/fuzzyc2cpg/passes/CMetaDataPass.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package io.shiftleft.fuzzyc2cpg.passes
22

33
import io.shiftleft.codepropertygraph.Cpg
44
import io.shiftleft.codepropertygraph.generated.{Languages, nodes}
5-
import io.shiftleft.fuzzyc2cpg.Defines
65
import io.shiftleft.passes.{CpgPass, DiffGraph, KeyPool}
6+
import io.shiftleft.semanticcpg.language.types.structure.{File, Namespace}
77

88
/**
99
* A pass that creates a MetaData node, specifying that this
@@ -19,8 +19,10 @@ class CMetaDataPass(cpg: Cpg, keyPool: Option[KeyPool] = None) extends CpgPass(c
1919

2020
def addAnyNamespaceBlock(diffGraph: DiffGraph.Builder): Unit = {
2121
val node = nodes.NewNamespaceBlock(
22-
name = Defines.globalNamespaceName,
23-
fullName = CMetaDataPass.getGlobalNamespaceBlockFullName(None)
22+
name = Namespace.globalNamespaceName,
23+
fullName = CMetaDataPass.getGlobalNamespaceBlockFullName(None),
24+
filename = File.UNKNOWN,
25+
order = 0
2426
)
2527
diffGraph.addNode(node)
2628
}
@@ -37,9 +39,9 @@ object CMetaDataPass {
3739
def getGlobalNamespaceBlockFullName(fileNameOption: Option[String]): String = {
3840
fileNameOption match {
3941
case Some(fileName) =>
40-
s"$fileName:${Defines.globalNamespaceName}"
42+
s"$fileName:${Namespace.globalNamespaceName}"
4143
case None =>
42-
Defines.globalNamespaceName
44+
Namespace.globalNamespaceName
4345
}
4446
}
4547

fuzzyc2cpg/src/test/scala/io/shiftleft/fuzzyc2cpg/ProgramStructureTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.shiftleft.fuzzyc2cpg
22

33
import io.shiftleft.codepropertygraph.generated.NodeKeys
44
import io.shiftleft.proto.cpg.Cpg.CpgStruct.Node.NodeType
5+
import io.shiftleft.semanticcpg.language.types.structure.Namespace
56
import org.scalatest.matchers.should.Matchers
67
import org.scalatest.wordspec.AnyWordSpec
78
import overflowdb._
@@ -15,7 +16,7 @@ class ProgramStructureTests extends AnyWordSpec with Matchers {
1516
val namespaceBlocks =
1617
fixture.traversalSource
1718
.label(NodeType.NAMESPACE_BLOCK.toString)
18-
.has(NodeKeys.FULL_NAME -> Defines.globalNamespaceName)
19+
.has(NodeKeys.FULL_NAME -> Namespace.globalNamespaceName)
1920
.l
2021

2122
namespaceBlocks.size shouldBe 1

fuzzyc2cpg/src/test/scala/io/shiftleft/fuzzyc2cpg/passes/CMetaDataPassTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.shiftleft.fuzzyc2cpg.passes
22

33
import io.shiftleft.codepropertygraph.Cpg
4-
import io.shiftleft.fuzzyc2cpg.Defines
54
import io.shiftleft.semanticcpg.language._
5+
import io.shiftleft.semanticcpg.language.types.structure.Namespace
66
import org.scalatest.matchers.should.Matchers
77
import org.scalatest.wordspec.AnyWordSpec
88

@@ -27,7 +27,7 @@ class CMetaDataPassTests extends AnyWordSpec with Matchers {
2727
}
2828

2929
"create a '<global>' NamespaceBlock" in {
30-
cpg.namespaceBlock.name.l shouldBe List(Defines.globalNamespaceName)
30+
cpg.namespaceBlock.name.l shouldBe List(Namespace.globalNamespaceName)
3131
}
3232

3333
}

schema/src/main/resources/schemas/source-specific.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
{"name" : "FILE",
1212
"outEdges" : [
13-
// TODO: remove AST edge here
1413
{"edgeName": "AST", "inNodes": [{"name": "COMMENT"}]}
1514
]
1615
}

semanticcpg/src/main/scala/io/shiftleft/semanticcpg/language/types/structure/Namespace.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ class Namespace(val traversal: Traversal[nodes.Namespace]) extends AnyVal {
4444
traversal.where(_.typeDecl.internal)
4545

4646
}
47+
48+
object Namespace {
49+
50+
val globalNamespaceName = "<global>"
51+
52+
}

0 commit comments

Comments
 (0)