[go: nahoru, domu]

Skip to content

Commit

Permalink
Added hasArguments to ArgumentHolder
Browse files Browse the repository at this point in the history
This adds a small utility function to `ArgumentHolder`, so we can check if an expression is part of their "arguments".
  • Loading branch information
oxisto committed Jul 3, 2024
1 parent 177d79a commit 2ff4961
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ interface ArgumentHolder : Holder<Expression> {
operator fun minusAssign(node: Expression) {
removeArgument(node)
}

/** Checks, if [expression] is part of the arguments. */
fun hasArgument(expression: Expression): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ fun MetadataProvider.newConstructExpression(
@JvmOverloads
fun MetadataProvider.newConditionalExpression(
condition: Expression,
thenExpression: Expression?,
elseExpression: Expression?,
thenExpression: Expression? = null,
elseExpression: Expression? = null,
type: Type = unknownType(),
rawNode: Any? = null
): ConditionalExpression {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ interface HasInitializer : HasType, ArgumentHolder, AssignmentHolder {
return true
}

override fun hasArgument(expression: Expression): Boolean {
return initializer == expression
}

override val assignments: List<Assignment>
get() {
return initializer?.let { listOf(Assignment(it, this, this)) } ?: listOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class DoStatement : Statement(), ArgumentHolder {
return false
}

override fun hasArgument(expression: Expression): Boolean {
return condition == expression
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is DoStatement) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class IfStatement : Statement(), BranchingNode, ArgumentHolder {
return true
}

override fun hasArgument(expression: Expression): Boolean {
return this.condition == expression
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is IfStatement) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class ReturnStatement : Statement(), ArgumentHolder {
return true
}

override fun hasArgument(expression: Expression): Boolean {
return this.returnValues.contains(expression)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ReturnStatement) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class WhileStatement : Statement(), BranchingNode, ArgumentHolder {
return true
}

override fun hasArgument(expression: Expression): Boolean {
return this.condition == expression
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is WhileStatement) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,8 @@ class AssignExpression :
false
}
}

override fun hasArgument(expression: Expression): Boolean {
return this.lhs.contains(expression) || this.rhs.contains(expression)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ open class BinaryOperator :
}
}

override fun hasArgument(expression: Expression): Boolean {
return lhs == expression || rhs == expression
}

override val base: Expression?
get() {
return if (operatorCode == ".*" || operatorCode == "->*") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ open class CallExpression : Expression(), HasType.TypeObserver, ArgumentHolder {
return true
}

override fun hasArgument(expression: Expression): Boolean {
return this.arguments.contains(expression)
}

override fun removeArgument(expression: Expression): Boolean {
arguments -= expression
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class CastExpression : Expression(), ArgumentHolder, HasType.TypeObserver {
return false
}

override fun hasArgument(expression: Expression): Boolean {
return this.expression == expression
}

override fun typeChanged(newType: Type, src: HasType) {
// Nothing to do, the cast type always stays the same
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ class ConditionalExpression : Expression(), ArgumentHolder, BranchingNode, HasTy
get() = condition

override fun addArgument(expression: Expression) {
// Do nothing
if (condition is ProblemExpression) {
condition = expression
} else if (thenExpression == null) {
thenExpression = expression
} else {
elseExpression = expression
}
}

override fun replaceArgument(old: Expression, new: Expression): Boolean {
Expand All @@ -87,6 +93,10 @@ class ConditionalExpression : Expression(), ArgumentHolder, BranchingNode, HasTy
}
}

override fun hasArgument(expression: Expression): Boolean {
return this.thenExpression == expression || elseExpression == expression
}

override fun typeChanged(newType: Type, src: HasType) {
val types = mutableSetOf<Type>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class InitializerListExpression : Expression(), ArgumentHolder, HasType.TypeObse
return false
}

override fun hasArgument(expression: Expression): Boolean {
return expression in this.initializers
}

override fun typeChanged(newType: Type, src: HasType) {
// Normally, we would check, if the source comes from our initializers, but we want to limit
// the iteration of the initializer list (which can potentially contain tens of thousands of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class KeyValueExpression : Expression(), ArgumentHolder {
return false
}

override fun hasArgument(expression: Expression): Boolean {
return key == expression || value == expression
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is KeyValueExpression) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class MemberExpression : Reference(), ArgumentHolder, HasBase {
return false
}

override fun hasArgument(expression: Expression): Boolean {
return base == expression
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is MemberExpression) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class SubscriptExpression : Expression(), HasBase, HasType.TypeObserver, Argumen
}
}

override fun hasArgument(expression: Expression): Boolean {
return arrayExpression == expression || subscriptExpression == expression
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is SubscriptExpression) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ class UnaryOperator : Expression(), ArgumentHolder, HasType.TypeObserver {
return false
}

override fun hasArgument(expression: Expression): Boolean {
return this.input == expression
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $$$$$$\ $$$$$$$\ $$$$$$\
* $$ __$$\ $$ __$$\ $$ __$$\
* $$ / \__|$$ | $$ |$$ / \__|
* $$ | $$$$$$$ |$$ |$$$$\
* $$ | $$ ____/ $$ |\_$$ |
* $$ | $$\ $$ | $$ | $$ |
* \$$$$$ |$$ | \$$$$$ |
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.graph

import de.fraunhofer.aisec.cpg.frontends.TestLanguageFrontend
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class ArgumentHolderTest {
@Test
fun testHasArgument() {
with(TestLanguageFrontend()) {
var ref = newReference("test")
var list =
listOf(
newCastExpression(),
newConditionalExpression(newLiteral(true)),
newDoStatement(),
newInitializerListExpression(),
newKeyValueExpression(),
newSubscriptExpression(),
newWhileStatement(),
newAssignExpression(),
)

for (node in list) {
assertFalse(node.hasArgument(ref), "hasArgument failed for ${node::class}")
}

for (node in list) {
node += ref
assertTrue(node.hasArgument(ref), "hasArgument failed for ${node::class}")
}
}
}
}

0 comments on commit 2ff4961

Please sign in to comment.