Skip to content

Commit

Permalink
Nodes can have multiple overlay nodes now
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Dec 17, 2024
1 parent c663c5c commit a72e139
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
13 changes: 4 additions & 9 deletions cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgesOf
import de.fraunhofer.aisec.cpg.graph.edges.flows.*
import de.fraunhofer.aisec.cpg.graph.edges.overlay.OverlaySingleEdge
import de.fraunhofer.aisec.cpg.graph.edges.overlay.*
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.graph.scopes.GlobalScope
import de.fraunhofer.aisec.cpg.graph.scopes.RecordScope
Expand Down Expand Up @@ -279,14 +279,9 @@ abstract class Node :
val additionalProblems: MutableSet<ProblemNode> = mutableSetOf()

@Relationship(value = "OVERLAY", direction = Relationship.Direction.OUTGOING)
val overlayNodeEdge: OverlaySingleEdge =
OverlaySingleEdge(
this,
of = null,
mirrorProperty = OverlayNode::underlyingNodeEdge,
outgoing = true
)
var overlayNode by unwrapping(Node::overlayNodeEdge)
val overlayEdges: Overlays =
Overlays(this, mirrorProperty = OverlayNode::underlyingNodeEdge, outgoing = true)
var overlays by unwrapping(Node::overlayEdges)

/**
* If a node should be removed from the graph, just removing it from the AST is not enough (see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class OverlayNode() : Node() {
OverlaySingleEdge(
this,
of = null,
mirrorProperty = Node::overlayNodeEdge,
mirrorProperty = Node::overlayEdges,
outgoing = false,
)
var underlyingNode by unwrapping(OverlayNode::underlyingNodeEdge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.graph.edges.overlay

import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.edges.Edge
import de.fraunhofer.aisec.cpg.graph.edges.collections.EdgeSet
import de.fraunhofer.aisec.cpg.graph.edges.collections.EdgeSingletonList
import de.fraunhofer.aisec.cpg.graph.edges.collections.MirroredEdgeCollection
import kotlin.reflect.KProperty
Expand All @@ -48,3 +49,11 @@ class OverlaySingleEdge(
of = of,
),
MirroredEdgeCollection<Node, OverlayEdge>

class Overlays(
thisRef: Node,
override var mirrorProperty: KProperty<MutableCollection<OverlayEdge>>,
outgoing: Boolean,
) :
EdgeSet<Node, OverlayEdge>(thisRef = thisRef, init = ::OverlayEdge, outgoing = outgoing),
MirroredEdgeCollection<Node, OverlayEdge>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import java.math.BigInteger
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import org.junit.jupiter.api.Tag

@Tag("integration")
Expand Down Expand Up @@ -64,23 +65,38 @@ class Neo4JTest {
fun testPushConcepts() {
val (application, result) = createTranslationResult()

val tu = result.translationUnits.firstOrNull()
assertNotNull(tu)

val connectCall = result.calls["connect"]
assertNotNull(connectCall)

abstract class NetworkingOperation(
concept: Concept<out Operation>,
) : Operation(concept)
class Connect(concept: Concept<out Operation>) : NetworkingOperation(concept)
class Networking() : Concept<NetworkingOperation>()

abstract class FileOperation(
concept: Concept<out Operation>,
) : Operation(concept)
class FileHandling() : Concept<FileOperation>()

val nw = Networking()
nw.underlyingNode = result.translationUnits.first()
nw.name = Name("Networking")
nw.underlyingNode = tu

val connect = Connect(concept = nw)
connect.underlyingNode = connectCall
connect.name = Name("connect")
nw.ops += connect

assertEquals(connect, connectCall?.overlayNode)
val f = FileHandling()
f.name = Name("FileHandling")
f.underlyingNode = tu

assertEquals(setOf<Node>(connect), connectCall.overlays)
assertEquals(setOf<Node>(nw, f), tu.overlays)

application.pushToNeo4j(result)
}
Expand Down

0 comments on commit a72e139

Please sign in to comment.