Skip to content

Commit

Permalink
Removed EdgeCachePass and its old astParent
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Aug 14, 2024
1 parent 9208655 commit 2b03e96
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ import de.fraunhofer.aisec.cpg.graph.invoke
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.astParentLegacy
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
* This [ValueEvaluator] can resolve multiple possible values of a node.
*
* It requires running the [EdgeCachePass] after the translation to add all necessary edges.
*/
/** This [ValueEvaluator] can resolve multiple possible values of a node. */
class MultiValueEvaluator : ValueEvaluator() {
companion object {
const val MAX_DEPTH: Int = 20
Expand Down Expand Up @@ -186,7 +180,7 @@ class MultiValueEvaluator : ValueEvaluator() {
}
}
"--" -> {
if (expr.astParentLegacy is ForStatement) {
if (expr.astParent is ForStatement) {
evaluateInternal(expr.input, depth + 1)
} else {
when (val input = evaluateInternal(expr.input, depth + 1)) {
Expand All @@ -196,7 +190,7 @@ class MultiValueEvaluator : ValueEvaluator() {
}
}
"++" -> {
if (expr.astParentLegacy is ForStatement) {
if (expr.astParent is ForStatement) {
evaluateInternal(expr.input, depth + 1)
} else {
when (val input = evaluateInternal(expr.input, depth + 1)) {
Expand Down Expand Up @@ -256,9 +250,8 @@ class MultiValueEvaluator : ValueEvaluator() {

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

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

Expand All @@ -269,22 +262,21 @@ class MultiValueEvaluator : ValueEvaluator() {
forStatement.initializerStatement == node || // The node is the initialization
(initializerDecl != null &&
initializerDecl ==
node.astParentLegacy) || // The parent of the node is the initializer of the
node.astParent) || // 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.astParentLegacy
forStatement.iterationStatement == node.astParent
}

private fun handleSimpleLoopVariable(expr: Reference, depth: Int): Collection<Any?> {
val loop =
expr.prevDFG.firstOrNull { it.astParentLegacy is ForStatement }?.astParentLegacy
as? ForStatement
expr.prevDFG.firstOrNull { it.astParent is ForStatement }?.astParent as? ForStatement
?: expr.prevDFG
.firstOrNull { it.astParentLegacy?.astParentLegacy is ForStatement }
?.astParentLegacy
?.astParentLegacy as? ForStatement
.firstOrNull { it.astParent?.astParent is ForStatement }
?.astParent
?.astParent 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,6 @@ 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.astParentLegacy
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -304,7 +303,7 @@ open class DFAOrderEvaluator(
fun getBaseOfNode(node: CallExpression) =
when {
node is MemberCallExpression -> node.base
node is ConstructExpression -> node.astParentLegacy?.getSuitableDFGTarget()
node is ConstructExpression -> node.astParent?.getSuitableDFGTarget()
node.thisPosition != null ->
node.getBaseOfCallExpressionUsingArgument(node.thisPosition!!)
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import de.fraunhofer.aisec.cpg.frontends.TestLanguage
import de.fraunhofer.aisec.cpg.frontends.TestLanguageFrontend
import de.fraunhofer.aisec.cpg.graph.array
import de.fraunhofer.aisec.cpg.graph.builder.*
import de.fraunhofer.aisec.cpg.passes.EdgeCachePass
import de.fraunhofer.aisec.cpg.passes.UnreachableEOGPass

class GraphExamples {
Expand All @@ -45,7 +44,6 @@ class GraphExamples {
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<UnreachableEOGPass>()
.registerPass<EdgeCachePass>()
.build()
) =
testFrontend(config).build {
Expand Down Expand Up @@ -244,7 +242,6 @@ class GraphExamples {
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<UnreachableEOGPass>()
.registerPass<EdgeCachePass>()
.build()
) =
testFrontend(config).build {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import de.fraunhofer.aisec.cpg.graph.array
import de.fraunhofer.aisec.cpg.graph.builder.*
import de.fraunhofer.aisec.cpg.graph.newNewArrayExpression
import de.fraunhofer.aisec.cpg.graph.pointer
import de.fraunhofer.aisec.cpg.passes.EdgeCachePass

class Query {
companion object {
Expand Down Expand Up @@ -99,7 +98,6 @@ class Query {
TranslationConfiguration.builder()
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<EdgeCachePass>()
.inferenceConfiguration(InferenceConfiguration.builder().enabled(false).build())
.build()
) =
Expand Down Expand Up @@ -176,7 +174,6 @@ class Query {
TranslationConfiguration.builder()
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<EdgeCachePass>()
.inferenceConfiguration(
InferenceConfiguration.builder().inferFunctions(false).build()
)
Expand Down Expand Up @@ -255,7 +252,6 @@ class Query {
TranslationConfiguration.builder()
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<EdgeCachePass>()
.build()
) =
testFrontend(config).build {
Expand Down Expand Up @@ -392,7 +388,6 @@ class Query {
config: TranslationConfiguration =
TranslationConfiguration.builder()
.defaultPasses()
.registerPass<EdgeCachePass>()
.registerLanguage(TestLanguage("."))
.build()
) =
Expand Down Expand Up @@ -438,7 +433,6 @@ class Query {
config: TranslationConfiguration =
TranslationConfiguration.builder()
.defaultPasses()
.registerPass<EdgeCachePass>()
.registerLanguage(TestLanguage("."))
.build()
) =
Expand Down Expand Up @@ -498,7 +492,6 @@ class Query {
config: TranslationConfiguration =
TranslationConfiguration.builder()
.defaultPasses()
.registerPass<EdgeCachePass>()
.registerLanguage(TestLanguage("."))
.build()
) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import de.fraunhofer.aisec.cpg.TranslationConfiguration
import de.fraunhofer.aisec.cpg.frontends.TestLanguage
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.builder.*
import de.fraunhofer.aisec.cpg.passes.EdgeCachePass
import de.fraunhofer.aisec.cpg.passes.UnreachableEOGPass

class ValueEvaluationTests {
Expand All @@ -40,7 +39,6 @@ class ValueEvaluationTests {
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<UnreachableEOGPass>()
.registerPass<EdgeCachePass>()
.build()
) =
testFrontend(config).build {
Expand Down Expand Up @@ -100,7 +98,6 @@ class ValueEvaluationTests {
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<UnreachableEOGPass>()
.registerPass<EdgeCachePass>()
.build()
) =
testFrontend(config).build {
Expand Down Expand Up @@ -143,7 +140,6 @@ class ValueEvaluationTests {
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<UnreachableEOGPass>()
.registerPass<EdgeCachePass>()
.build()
) =
testFrontend(config).build {
Expand Down Expand Up @@ -249,7 +245,6 @@ class ValueEvaluationTests {
.defaultPasses()
.registerLanguage(TestLanguage("."))
.registerPass<UnreachableEOGPass>()
.registerPass<EdgeCachePass>()
.build()
) =
testFrontend(config).build {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ 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.astParentLegacy
import kotlin.math.absoluteValue

/**
Expand Down Expand Up @@ -694,7 +693,7 @@ fun Node.controlledBy(): List<Node> {
val result = mutableListOf<Node>()
var checkedNode: Node? = this
while (checkedNode !is FunctionDeclaration) {
checkedNode = checkedNode?.astParentLegacy
checkedNode = checkedNode?.astParent
if (checkedNode == null) {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ open class AstEdges<NodeType : Node, PropertyEdgeType : AstEdge<NodeType>>(
init = init,
onAdd = onAdd,
onRemove = onRemove,
) {}
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package de.fraunhofer.aisec.cpg.graph.edges.collections

import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.edges.Edge
import de.fraunhofer.aisec.cpg.passes.EdgeType
import de.fraunhofer.aisec.cpg.graph.types.HasType.TypeObserver

/**
* This interfaces is an extension of [MutableCollection] that holds specific functions for the
Expand Down Expand Up @@ -84,7 +84,6 @@ interface EdgeCollection<
* If [outgoing] is true, the edge is created from [thisRef] -> [target], otherwise from
* [target] to [thisRef].
*/
@Suppress("UNCHECKED_CAST")
fun add(
target: NodeType,
init: ((Node, NodeType) -> EdgeType) = this.init,
Expand All @@ -102,6 +101,7 @@ interface EdgeCollection<
outgoing: Boolean = true,
builder: (PropertyEdgeType.() -> Unit)? = null,
): PropertyEdgeType {
@Suppress("UNCHECKED_CAST")
val edge =
if (outgoing) {
init(thisRef, target)
Expand Down
Loading

0 comments on commit 2b03e96

Please sign in to comment.