Skip to content

Commit

Permalink
Setting astParent in AstEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Aug 7, 2024
1 parent 84bf32e commit 383b200
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement
import de.fraunhofer.aisec.cpg.graph.statements.ForStatement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.passes.EdgeCachePass
import de.fraunhofer.aisec.cpg.passes.astParent
import de.fraunhofer.aisec.cpg.passes.astParentLegacy
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -186,7 +186,7 @@ class MultiValueEvaluator : ValueEvaluator() {
}
}
"--" -> {
if (expr.astParent is ForStatement) {
if (expr.astParentLegacy is ForStatement) {
evaluateInternal(expr.input, depth + 1)
} else {
when (val input = evaluateInternal(expr.input, depth + 1)) {
Expand All @@ -196,7 +196,7 @@ class MultiValueEvaluator : ValueEvaluator() {
}
}
"++" -> {
if (expr.astParent is ForStatement) {
if (expr.astParentLegacy is ForStatement) {
evaluateInternal(expr.input, depth + 1)
} else {
when (val input = evaluateInternal(expr.input, depth + 1)) {
Expand Down Expand Up @@ -256,8 +256,8 @@ class MultiValueEvaluator : ValueEvaluator() {

private fun isSimpleForLoop(node: Node): Boolean {
// Are we in the for statement somehow?
var forStatement = node.astParent as? ForStatement
if (forStatement == null) forStatement = node.astParent?.astParent as? ForStatement
var forStatement = node.astParentLegacy as? ForStatement
if (forStatement == null) forStatement = node.astParentLegacy?.astParentLegacy as? ForStatement

if (forStatement == null) return false // ...no, we're not.

Expand All @@ -268,20 +268,20 @@ class MultiValueEvaluator : ValueEvaluator() {
forStatement.initializerStatement == node || // The node is the initialization
(initializerDecl != null &&
initializerDecl ==
node.astParent) || // The parent of the node is the initializer of the loop
node.astParentLegacy) || // The parent of the node is the initializer of the loop
// variable
forStatement.iterationStatement ==
node || // The node or its parent are the iteration statement of the loop
forStatement.iterationStatement == node.astParent
forStatement.iterationStatement == node.astParentLegacy
}

private fun handleSimpleLoopVariable(expr: Reference, depth: Int): Collection<Any?> {
val loop =
expr.prevDFG.firstOrNull { it.astParent is ForStatement }?.astParent as? ForStatement
expr.prevDFG.firstOrNull { it.astParentLegacy is ForStatement }?.astParentLegacy as? ForStatement
?: expr.prevDFG
.firstOrNull { it.astParent?.astParent is ForStatement }
?.astParent
?.astParent as? ForStatement
.firstOrNull { it.astParentLegacy?.astParentLegacy is ForStatement }
?.astParentLegacy
?.astParentLegacy as? ForStatement
if (loop == null || loop.condition !is BinaryOperator) return setOf()

var loopVar: Any? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference
import de.fraunhofer.aisec.cpg.passes.astParent
import de.fraunhofer.aisec.cpg.passes.astParentLegacy
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -304,7 +304,7 @@ open class DFAOrderEvaluator(
fun getBaseOfNode(node: CallExpression) =
when {
node is MemberCallExpression -> node.base
node is ConstructExpression -> node.astParent?.getSuitableDFGTarget()
node is ConstructExpression -> node.astParentLegacy?.getSuitableDFGTarget()
node.thisPosition != null ->
node.getBaseOfCallExpressionUsingArgument(node.thisPosition!!)
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import de.fraunhofer.aisec.cpg.graph.statements.WhileStatement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block
import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker
import de.fraunhofer.aisec.cpg.passes.astParent
import de.fraunhofer.aisec.cpg.passes.astParentLegacy
import kotlin.math.absoluteValue

/**
Expand Down Expand Up @@ -694,7 +694,7 @@ fun Node.controlledBy(): List<Node> {
val result = mutableListOf<Node>()
var checkedNode: Node? = this
while (checkedNode !is FunctionDeclaration) {
checkedNode = checkedNode?.astParent
checkedNode = checkedNode?.astParentLegacy
if (checkedNode == null) {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ abstract class Node :
var astChildren: List<Node> = listOf()
get() = SubgraphWalker.getAstChildren(this)

@Transient
var astParent: Node? = null

/** Virtual property for accessing [prevEOGEdges] without property edges. */
@PopulatedByPass(EvaluationOrderGraphPass::class) var prevEOG by unwrapping(Node::prevEOGEdges)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ package de.fraunhofer.aisec.cpg.graph.edges.ast
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.edges.Edge
import de.fraunhofer.aisec.cpg.graph.edges.collections.EdgeList
import de.fraunhofer.aisec.cpg.graph.edges.collections.EdgeSingletonList
import org.neo4j.ogm.annotation.*

/** This property edge describes a parent/child relationship in the Abstract Syntax Tree (AST). */
@RelationshipEntity
open class AstEdge<T : Node> : Edge<T> {
constructor(start: Node, end: T) : super(start, end) {
// In a future PR, we will set the astParent here
end.astParent = start
}
}

Expand All @@ -47,17 +46,6 @@ fun <NodeType : Node> Node.astEdgesOf(
return AstEdges(this, postAdd, postRemove)
}

fun <NodeType : Node> Node.astEdgeOf(
of: NodeType,
): EdgeSingletonList<NodeType, AstEdge<NodeType>> {
return EdgeSingletonList<NodeType, AstEdge<NodeType>>(
thisRef = this,
init = ::AstEdge,
outgoing = true,
of = of
)
}

/** This property edge list describes elements that are AST children of a node. */
open class AstEdges<NodeType : Node, PropertyEdgeType : Edge<NodeType>>(
thisRef: Node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class EdgeCachePass(ctx: TranslationContext) : ComponentPass(ctx) {
}
}

val Node.astParent: Node?
val Node.astParentLegacy: Node?
get() {
return Edges.to(this, EdgeType.AST).firstOrNull()?.source
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ class ControlFlowSensitiveDFGPassTest {
val baseOfMemberRead11 = assertNotNull(next.firstOrNull()?.end)
assertEquals(11, baseOfMemberRead11.location?.region?.startLine)
assertIs<Reference>(baseOfMemberRead11)
assertIs<MemberExpression>(baseOfMemberRead11.astParent)
assertIs<MemberExpression>(baseOfMemberRead11.astParentLegacy)
assertEquals(AccessValues.READ, baseOfMemberRead11.access)

val baseOfMemberWrite13 = assertNotNull(next.getOrNull(1)?.end)
assertEquals(13, baseOfMemberWrite13.location?.region?.startLine)
assertIs<Reference>(baseOfMemberWrite13)
assertIs<MemberExpression>(baseOfMemberWrite13.astParent)
assertIs<MemberExpression>(baseOfMemberWrite13.astParentLegacy)
assertEquals(
AccessValues.READ,
baseOfMemberWrite13.access
Expand Down Expand Up @@ -180,7 +180,7 @@ class ControlFlowSensitiveDFGPassTest {
val single = assertNotNull(next.singleOrNull()?.end)
assertEquals(14, single.location?.region?.startLine)
assertIs<Reference>(single)
val me = assertIs<MemberExpression>(single.astParent)
val me = assertIs<MemberExpression>(single.astParentLegacy)
assertEquals(AccessValues.WRITE, me.access)

// The rest should be the same as s1, so we can probably skip the rest of the asserts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.types.PointerType
import de.fraunhofer.aisec.cpg.passes.EdgeCachePass
import de.fraunhofer.aisec.cpg.passes.astParent
import de.fraunhofer.aisec.cpg.passes.astParentLegacy
import de.fraunhofer.aisec.cpg.test.analyze
import de.fraunhofer.aisec.cpg.test.analyzeAndGetFirstTU
import de.fraunhofer.aisec.cpg.test.assertFullName
Expand Down Expand Up @@ -139,7 +139,7 @@ class JVMLanguageFrontendTest {
// All references (which are not part of a call) and not to the stdlib should be resolved
val refs = tu.refs
refs
.filter { it.astParent !is CallExpression }
.filter { it.astParentLegacy !is CallExpression }
.filter { !it.name.startsWith("java.") }
.forEach {
val refersTo = it.refersTo
Expand Down

0 comments on commit 383b200

Please sign in to comment.