From 31e0b65c91c0d28112c611f5e7c07aaa545482f2 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Thu, 29 Aug 2024 16:05:29 +0200 Subject: [PATCH] Adding mission relationship annotation --- .../aisec/cpg/graph/declarations/MethodDeclaration.kt | 3 ++- .../aisec/cpg/graph/statements/DoStatement.kt | 5 +++-- .../aisec/cpg/graph/statements/ForEachStatement.kt | 10 ++++++---- .../aisec/cpg/graph/statements/ForStatement.kt | 9 ++++++--- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/declarations/MethodDeclaration.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/declarations/MethodDeclaration.kt index af7c666e87..7127de42ff 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/declarations/MethodDeclaration.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/declarations/MethodDeclaration.kt @@ -29,6 +29,7 @@ import de.fraunhofer.aisec.cpg.graph.edges.ast.astOptionalEdgeOf import de.fraunhofer.aisec.cpg.graph.edges.unwrappingOptional import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference import de.fraunhofer.aisec.cpg.passes.SymbolResolver +import org.neo4j.ogm.annotation.Relationship /** * A method declaration is a [FunctionDeclaration] that is part of to a specific [RecordDeclaration] @@ -42,7 +43,7 @@ open class MethodDeclaration : FunctionDeclaration() { */ open var recordDeclaration: RecordDeclaration? = null - var receiverEdge = astOptionalEdgeOf() + @Relationship("RECEIVER") var receiverEdge = astOptionalEdgeOf() /** * The receiver variable of this method. In most cases, this variable is called `this`, but in * some languages, it is `self` (e.g. in Rust or Python) or can be freely named (e.g. in diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/DoStatement.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/DoStatement.kt index 7b9d413b61..f7b2617f16 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/DoStatement.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/DoStatement.kt @@ -32,16 +32,17 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression import java.util.* import org.apache.commons.lang3.builder.ToStringBuilder +import org.neo4j.ogm.annotation.Relationship /** Represents a conditional loop statement of the form: `do{...}while(...)`. */ class DoStatement : Statement(), ArgumentHolder { - var conditionEdge = astOptionalEdgeOf() + @Relationship("CONDITION") var conditionEdge = astOptionalEdgeOf() /** * The loop condition that is evaluated after the loop statement and may trigger reevaluation. */ var condition by unwrappingOptional(DoStatement::conditionEdge) - var statementEdge = astOptionalEdgeOf() + @Relationship("STATEMENT") var statementEdge = astOptionalEdgeOf() /** * The statement that is going to be executed and re-executed, until the condition evaluates to diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/ForEachStatement.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/ForEachStatement.kt index 06c0d851e8..6110fd4785 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/ForEachStatement.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/ForEachStatement.kt @@ -35,13 +35,15 @@ import de.fraunhofer.aisec.cpg.graph.edges.unwrappingOptional import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference import java.util.Objects +import org.neo4j.ogm.annotation.Relationship class ForEachStatement : Statement(), BranchingNode, StatementHolder { + @Relationship("VARIABLE") var variableEdge = astOptionalEdgeOf( onChanged = { _, new -> - var end = new?.end + val end = new?.end if (end is Reference) { end.access = AccessValues.WRITE } @@ -54,11 +56,11 @@ class ForEachStatement : Statement(), BranchingNode, StatementHolder { */ var variable by unwrappingOptional(ForEachStatement::variableEdge) - var iterableEdge = astOptionalEdgeOf() + @Relationship("ITERABLE") var iterableEdge = astOptionalEdgeOf() /** This field contains the iteration subject of the loop. */ var iterable by unwrappingOptional(ForEachStatement::iterableEdge) - var statementEdge = astOptionalEdgeOf() + @Relationship("STATEMENT") var statementEdge = astOptionalEdgeOf() /** This field contains the body of the loop. */ var statement by unwrappingOptional(ForEachStatement::statementEdge) @@ -91,7 +93,7 @@ class ForEachStatement : Statement(), BranchingNode, StatementHolder { block.statements += s statement = block } - else -> (statement as? Block)?.statements += s + else -> (statement as? Block)?.statements?.plusAssign(s) } } diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/ForStatement.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/ForStatement.kt index c52332d5f8..911436b566 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/ForStatement.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/ForStatement.kt @@ -31,21 +31,24 @@ import de.fraunhofer.aisec.cpg.graph.edges.ast.astOptionalEdgeOf import de.fraunhofer.aisec.cpg.graph.edges.unwrappingOptional import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression import java.util.* +import org.neo4j.ogm.annotation.Relationship class ForStatement : Statement(), BranchingNode { - var statementEdge = astOptionalEdgeOf() + @Relationship("STATEMENT") var statementEdge = astOptionalEdgeOf() var statement by unwrappingOptional(ForStatement::statementEdge) + @Relationship("INITIALIZER_STATEMENT") var initializerStatementEdge = astOptionalEdgeOf() var initializerStatement by unwrappingOptional(ForStatement::initializerStatementEdge) + @Relationship("CONDITION_DECLARATION") var conditionDeclarationEdge = astOptionalEdgeOf() var conditionDeclaration by unwrappingOptional(ForStatement::conditionDeclarationEdge) - var conditionEdge = astOptionalEdgeOf() + @Relationship("CONDITION") var conditionEdge = astOptionalEdgeOf() var condition by unwrappingOptional(ForStatement::conditionEdge) - var iterationStatementEdge = astOptionalEdgeOf() + @Relationship("ITERATION_STATEMENT") var iterationStatementEdge = astOptionalEdgeOf() var iterationStatement by unwrappingOptional(ForStatement::iterationStatementEdge) override val branchedBy: Node?