Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing astParent for single nodes using a setter #1647

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 @@ 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 @@ -84,7 +84,12 @@ open class CallExpression :
* but will be in the future. In most cases, this is a [Reference] and its [Reference.refersTo]
* is intentionally left empty. It is not filled by the [SymbolResolver].
*/
@AST var callee: Expression = ProblemExpression("could not parse callee")
@AST
var callee: Expression = ProblemExpression("could not parse callee")
set(value) {
value.astParent = this
field = value
}

/**
* The [Name] of this call expression, based on its [callee].
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
Loading