Skip to content

Commit

Permalink
Make non-optional things non-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
KuechA committed Oct 24, 2024
1 parent 6d6af25 commit 761c244
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.graph.statements.expressions

import de.fraunhofer.aisec.cpg.graph.AccessValues
import de.fraunhofer.aisec.cpg.graph.ArgumentHolder
import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgeOf
import de.fraunhofer.aisec.cpg.graph.edges.ast.astOptionalEdgeOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.graph.statements.Statement
Expand All @@ -39,7 +40,8 @@ import org.neo4j.ogm.annotation.Relationship
class ComprehensionExpression : Expression(), ArgumentHolder {
@Relationship("VARIABLE")
var variableEdge =
astOptionalEdgeOf<Statement>(
astEdgeOf<Statement>(
of = ProblemExpression("Missing variableEdge in ${this::class}"),
onChanged = { _, new ->
val end = new?.end
if (end is Reference) {
Expand All @@ -54,7 +56,9 @@ class ComprehensionExpression : Expression(), ArgumentHolder {
*/
var variable by unwrapping(ComprehensionExpression::variableEdge)

@Relationship("ITERABLE") var iterableEdge = astOptionalEdgeOf<Statement>()
@Relationship("ITERABLE")
var iterableEdge =
astEdgeOf<Expression>(ProblemExpression("Missing iterable in ${this::class}"))

/** This field contains the iteration subject of the loop. */
var iterable by unwrapping(ComprehensionExpression::iterableEdge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass
state.push(currentNode, it)
}
} else if (currentNode is ComprehensionExpression) {
val iterable = currentNode.iterable as? Expression
val writtenTo =
when (val variable = currentNode.variable) {
is DeclarationStatement -> {
Expand Down Expand Up @@ -391,13 +390,11 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass
}
}

iterable?.let {
state.push(writtenTo, PowersetLattice(identitySetOf(iterable)))
// Add the variable declaration (or the reference) to the list of previous
// write nodes in this path
state.declarationsState[writtenDeclaration] =
PowersetLattice(identitySetOf(writtenTo))
}
state.push(writtenTo, PowersetLattice(identitySetOf(currentNode.iterable)))
// Add the variable declaration (or the reference) to the list of previous
// write nodes in this path
state.declarationsState[writtenDeclaration] =
PowersetLattice(identitySetOf(writtenTo))
}
} else if (currentNode is ForEachStatement && currentNode.variable != null) {
// The VariableDeclaration in the ForEachStatement doesn't have an initializer, so
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@ class DFGPass(ctx: TranslationContext) : ComponentPass(ctx) {
* predicate(s).
*/
protected fun handleComprehensionExpression(comprehension: ComprehensionExpression) {
comprehension.variable?.let { variable ->
comprehension.iterable?.let { iterable -> iterable.nextDFG += variable }
comprehension.prevDFG += variable
}
comprehension.iterable.nextDFG += comprehension.variable
comprehension.prevDFG += comprehension.variable
comprehension.predicate?.let { comprehension.prevDFG += it }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa
// find out for java, but impossible for c++)

// evaluate the call target first, optional base should be the callee or in its subtree
node.callee?.let { handleEOG(it) }
handleEOG(node.callee)

// then the arguments
for (arg in node.arguments) {
Expand Down Expand Up @@ -862,7 +862,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa
* Connects the current EOG leaf nodes to the last stacked node, e.g. loop head, and removes the
* nodes.
*
* @param loopScope the loop scope
* @param loopStatement the loop statement
*/
protected fun handleContainedBreaksAndContinues(loopStatement: LoopStatement) {
// Breaks are connected to the NEXT EOG node and therefore temporarily stored after the loop
Expand Down Expand Up @@ -894,11 +894,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa
}

/**
* Builds an EOG edge from prev to next. 'eogDirection' defines how the node instances save the
* references constituting the edge. 'FORWARD': only the nodes nextEOG member contains
* references, an points to the next nodes. 'BACKWARD': only the nodes prevEOG member contains
* references and points to the previous nodes. 'BIDIRECTIONAL': nextEOG and prevEOG contain
* references and point to the previous and the next nodes.
* Builds an EOG edge from prev to next.
*
* @param prev the previous node
* @param next the next node
Expand Down Expand Up @@ -952,8 +948,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa
private fun handleComprehensionExpression(node: ComprehensionExpression) {
handleEOG(node.iterable)
// When the iterable contains another element, the variable is evaluated with the
// nextElement. Therefore we ad a
// true edge.
// nextElement. Therefore, we add a "true" edge.
nextEdgeBranch = true
handleEOG(node.variable)
handleEOG(node.predicate)
Expand Down Expand Up @@ -1233,7 +1228,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa
else -> {
LOGGER.error(
"Currently the component {} does not have a defined loop start.",
this?.javaClass
this.javaClass
)
ArrayList()
}
Expand Down

0 comments on commit 761c244

Please sign in to comment.