Skip to content

Commit

Permalink
add concept of data items for modelling
Browse files Browse the repository at this point in the history
  • Loading branch information
seelchen committed Aug 10, 2023
1 parent f6c0a35 commit 6897140
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.fraunhofer.aisec.codyze.backends.cpg.coko.dsl

import de.fraunhofer.aisec.codyze.backends.cpg.coko.Nodes
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.CokoBackend
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.modelling.DataItem
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.modelling.ReturnValueItem
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.modelling.Value
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.evaluate
import de.fraunhofer.aisec.cpg.graph.literals
import de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression
import de.fraunhofer.aisec.cpg.graph.variables

/**
* Get all [Nodes] that are associated with this [DataItem].
*/
context(CokoBackend)
fun DataItem.cpgGetAllNodes(): Nodes =
when (this@DataItem) {
is ReturnValueItem -> op.cpgGetAllNodes().flatMap { it.getVariableInNextDFGOrThis() }
is Value -> this@DataItem.getNodes()
}

/**
* Get all [Nodes] that are associated with this [DataItem].
*/
context(CokoBackend)
fun DataItem.cpgGetNodes(): Nodes {
return when (this@DataItem) {
is ReturnValueItem -> op.cpgGetNodes().flatMap { it.getVariableInNextDFGOrThis() }
is Value -> this@DataItem.getNodes()
}
}

context(CokoBackend)
private fun Value.getNodes(): Nodes =
cpg.literals.filter {
node ->
node.value == this.value
} + cpg.variables.filter {
node ->
node.evaluate() == this.value
}

/**
* Returns all [VariableDeclaration]s and [DeclaredReferenceExpression]s that have a DFG edge from [this].
* If there are none, returns [this].
*/
private fun Node.getVariableInNextDFGOrThis(): Nodes =
this.nextDFG
.filter {
next ->
next is DeclaredReferenceExpression || next is VariableDeclaration
}.ifEmpty { listOf(this) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.modelling

Check warning

Code scanning / detekt

License text is absent or incorrect. Warning

Expected license not found or incorrect in the file: /home/runner/work/codyze/codyze/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/DataItem.kt.

import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.dsl.Op

sealed interface DataItem

data class ReturnValueItem(val op: Op) : DataItem {
override fun toString(): String = "Return value of $op"
}

data class Value(val value: Any) : DataItem

0 comments on commit 6897140

Please sign in to comment.