-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add concept of data items for modelling
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...ds/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/DataItemCpgDsl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
12 changes: 12 additions & 0 deletions
12
.../kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/DataItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|