Skip to content

Commit

Permalink
Using handleHasInitializer for all things with initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jan 17, 2025
1 parent 025c803 commit 083ff24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ class MultiValueEvaluator : ValueEvaluator() {
this.path += node

when (node) {
is FieldDeclaration -> {
return evaluateInternal(node.initializer, depth + 1)
}
is NewArrayExpression -> return evaluateInternal(node.initializer, depth + 1)
is VariableDeclaration -> return handleVariableDeclaration(node, depth)
is FieldDeclaration -> return handleHasInitializer(node, depth)
is NewArrayExpression -> return handleHasInitializer(node, depth)
is VariableDeclaration -> return handleHasInitializer(node, depth)
// For a literal, we can just take its value, and we are finished
is Literal<*> -> return node.value
is Reference -> return handlePrevDFG(node, depth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package de.fraunhofer.aisec.cpg.analysis

import de.fraunhofer.aisec.cpg.graph.AccessValues
import de.fraunhofer.aisec.cpg.graph.HasInitializer
import de.fraunhofer.aisec.cpg.graph.HasOperatorCode
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
Expand Down Expand Up @@ -87,8 +88,8 @@ open class ValueEvaluator(
node?.let { this.path += it }

when (node) {
is NewArrayExpression -> return evaluateInternal(node.initializer, depth)
is VariableDeclaration -> return handleVariableDeclaration(node, depth)
is NewArrayExpression -> return handleHasInitializer(node, depth)
is VariableDeclaration -> return handleHasInitializer(node, depth)
// For a literal, we can just take its value, and we are finished
is Literal<*> -> return node.value
is Reference -> return handlePrevDFG(node, depth)
Expand All @@ -108,12 +109,16 @@ open class ValueEvaluator(
return cannotEvaluate(node, this)
}

protected fun handleVariableDeclaration(node: VariableDeclaration, depth: Int): Any? {
/**
* If a node declaration implements [HasInitializer], we can use the initializer to evaluate
* their value. If not, we can try to use [handlePrevDFG].
*/
protected fun handleHasInitializer(node: HasInitializer, depth: Int): Any? {
// If we have an initializer, we can use it. Otherwise, we can fall back to the prevDFG
return if (node.initializer != null) {
evaluateInternal(node.initializer, depth + 1)
} else {
handlePrevDFG(node, depth)
handlePrevDFG(node as Node, depth)
}
}

Expand Down Expand Up @@ -405,9 +410,7 @@ open class ValueEvaluator(
return cannotEvaluate(expr, this)
}

/**
* Tries to compute the constant value of a node based on its [Node.prevDFG].
*/
/** Tries to compute the constant value of a node based on its [Node.prevDFG]. */
protected open fun handlePrevDFG(node: Node, depth: Int): Any? {
// For a reference, we are interested into its last assignment into the reference
// denoted by the previous DFG edge. We need to filter out any self-references for READWRITE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
package de.fraunhofer.aisec.cpg.graph.statements.expressions

import de.fraunhofer.aisec.cpg.graph.HasInitializer
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.edges.Edge.Companion.propertyEqualsList
import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgesOf
Expand All @@ -38,14 +39,14 @@ import org.neo4j.ogm.annotation.Relationship
* combination with a [VariableDeclaration].
*/
// TODO Merge and/or refactor with new Expression
class NewArrayExpression : Expression() {
class NewArrayExpression : Expression(), HasInitializer {
@Relationship("INITIALIZER") var initializerEdge = astOptionalEdgeOf<Expression>()

/**
* The initializer of the expression, if present. Many languages, such as Java, either specify
* [dimensions] or an initializer.
*/
var initializer by unwrapping(NewArrayExpression::initializerEdge)
override var initializer by unwrapping(NewArrayExpression::initializerEdge)

/**
* Specifies the dimensions of the array that is to be created. Many languages, such as Java,
Expand Down

0 comments on commit 083ff24

Please sign in to comment.