Skip to content

Commit

Permalink
more experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jul 31, 2024
1 parent b8fa2c5 commit 0514aec
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ fun MetadataProvider.newUnaryOperator(
operatorCode: String,
postfix: Boolean,
prefix: Boolean,
rawNode: Any? = null
rawNode: Any? = null,
init: (UnaryOperator.() -> Unit)? = null,
): UnaryOperator {
val node = UnaryOperator()
node.applyMetadata(this, operatorCode, rawNode, true)
Expand All @@ -108,6 +109,10 @@ fun MetadataProvider.newUnaryOperator(
node.isPostfix = postfix
node.isPrefix = prefix

if (init != null) {
init(node)
}

log(node)

return node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,21 @@ private fun <AstNode> Node.setCodeAndLocation(
this.location = provider.locationOf(rawNode)
}

context(ContextProvider)
fun <T : Node> T.withScope(init: T.() -> Unit): T {
val scopeManager =
this@ContextProvider.ctx?.scopeManager
?: throw TranslationException(
"Trying to create node children without a ContextProvider. This will fail."
)

scopeManager.enterScope(this)
init(this)
scopeManager.leaveScope(this)

return this
}

context(ContextProvider)
fun <T : Node> T.withChildren(
hasScope: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
package de.fraunhofer.aisec.cpg.graph

import de.fraunhofer.aisec.cpg.graph.edge.AstPropertyEdge
import de.fraunhofer.aisec.cpg.graph.edge.Properties
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.Companion.unwrap
Expand Down Expand Up @@ -55,6 +56,7 @@ interface StatementHolder : Holder<Statement> {
return unwrap(statementEdges)
}
set(value) {
// TODO: we also need to set the ast parent in this case
statementEdges = wrap(value, this as Node)
}

Expand All @@ -66,7 +68,7 @@ interface StatementHolder : Holder<Statement> {
* @param s the statement
*/
fun addStatement(s: Statement) {
val propertyEdge = PropertyEdge((this as Node), s)
val propertyEdge = AstPropertyEdge((this as Node), s)
propertyEdge.addProperty(Properties.INDEX, statementEdges.size)
statementEdges.add(propertyEdge)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ import org.neo4j.ogm.annotation.*
import org.neo4j.ogm.annotation.typeconversion.Convert
import org.slf4j.LoggerFactory

class AstPropertyEdge<T : Node> : PropertyEdge<T> {
constructor(start: Node, end: T) : super(start, end) {
end.astParent = start
}
}

/**
* This class represents an edge between two [Node] objects in a Neo4J graph. It can be used to
* store additional information that relate to the relationship between the two nodes that belong to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

Expand Down Expand Up @@ -92,4 +93,21 @@ class ExpressionBuilderTest {
assertEquals(binOp, lit2.astParent)
}
}

@Test
fun testBlock() {
with(TestLanguageFrontend()) {
val block =
newBlock().withScope {
this +=
newUnaryOperator("++", prefix = false, postfix = true) {
input = newReference("a")
}
}

val binOp = block.statements.firstOrNull()
assertNotNull(binOp)
assertEquals(block, binOp.astParent)
}
}
}

0 comments on commit 0514aec

Please sign in to comment.