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

Added more basic interfaces #1607

Merged
merged 2 commits into from
Jul 25, 2024
Merged
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 @@ -180,7 +180,7 @@ open class ValueEvaluator(
"<=" -> handleLEq(lhsValue, rhsValue, expr)
"==" -> handleEq(lhsValue, rhsValue, expr)
"!=" -> handleNEq(lhsValue, rhsValue, expr)
else -> cannotEvaluate(expr as Node, this)
else -> cannotEvaluate(expr, this)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ class QueryTest {
result
.all<FunctionDeclaration>(
{ it.name.localName == "print" },
{ n2 -> dataFlow(n1 as Node, n2.parameters[0]).value }
{ n2 -> dataFlow(n1, n2.parameters[0]).value }
)
.first
}
Expand All @@ -613,7 +613,7 @@ class QueryTest {
{ n1 ->
result.allExtended<FunctionDeclaration>(
{ it.name.localName == "print" },
{ n2 -> dataFlow(n1 as Node, n2.parameters[0]) }
{ n2 -> dataFlow(n1, n2.parameters[0]) }
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ fun MetadataProvider.newCallExpression(
callee.resolutionHelper = node
}

node.callee = callee
if (callee != null) {
node.callee = callee
}
node.template = template

log(node)
Expand Down Expand Up @@ -304,7 +306,9 @@ fun MetadataProvider.newMemberCallExpression(
callee.resolutionHelper = node
}

node.callee = callee
if (callee != null) {
node.callee = callee
}
node.isStatic = isStatic

log(node)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

151 changes: 151 additions & 0 deletions cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Interfaces.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2021, 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.Language
import de.fraunhofer.aisec.cpg.graph.declarations.OperatorDeclaration
import de.fraunhofer.aisec.cpg.graph.scopes.Scope
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.types.HasType
import de.fraunhofer.aisec.cpg.graph.types.Type
import de.fraunhofer.aisec.cpg.passes.SymbolResolver
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation

/** A simple interface that a node has [language]. */
interface HasLanguage {

var language: Language<*>?
}

/** A simple interface that a node has [name] and [location]. */
interface HasNameAndLocation : HasLanguage {

val name: Name

/** Location of the finding in source code. */
val location: PhysicalLocation?
}

/** A simple interface that a node has [scope]. */
interface HasScope : HasNameAndLocation {

/** The scope this node lives in. */
val scope: Scope?
}

/** A simple interface to denote that the implementing class has some kind of [operatorCode]. */
interface HasOperatorCode : HasScope {

/** The operator code, identifying an operation executed on one or more [Expression]s */
val operatorCode: String?
}

/** Specifies that a certain node has a base on which it executes an operation. */
interface HasBase : HasOperatorCode {

/** The base. If there is no actual base, it can be null. */
val base: Expression?

/**
* The operator that is used to access the [base]. Usually either `.` or `->`, but some
* languages offer additional operator codes. If the [base] is null, the [operatorCode] should
* also be null.
*/
override val operatorCode: String?
}

/**
* Interface that allows us to mark nodes that contain a default value
*
* @param <T> type of the default node </T>
*/
interface HasDefault<T : Node?> : HasScope {
var default: T
}

/**
* Specifies that a certain node has an initializer. It is a special case of [ArgumentHolder], in
* which the initializer is treated as the first (and only) argument.
*/
interface HasInitializer : HasScope, HasType, ArgumentHolder, AssignmentHolder {

var initializer: Expression?

override fun addArgument(expression: Expression) {
this.initializer = expression
}

override fun removeArgument(expression: Expression): Boolean {
return if (this.initializer == expression) {
this.initializer = null
true
} else {
false
}
}

override fun replaceArgument(old: Expression, new: Expression): Boolean {
this.initializer = new
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()
}
}

/**
* Some nodes have aliases, i.e., it potentially references another variable. This means that
* writing to this node, also writes to its [aliases] and vice-versa.
*/
interface HasAliases : HasScope {
/** The aliases which this node has. */
var aliases: MutableSet<HasAliases>
}

/**
* Specifies that this node (e.g. a [BinaryOperator] contains an operation that can be overloaded by
* an [OperatorDeclaration].
*/
interface HasOverloadedOperation : HasOperatorCode {

/**
* Arguments forwarded to the operator. This might not necessarily be all of the regular
* "arguments", since often the the first argument is part of the [operatorBase].
*/
val operatorArguments: List<Expression>

/**
* The base expression this operator works on. The [Type] of this is also the source where the
* [SymbolResolver] is looking for an overloaded [OperatorDeclaration].
*/
val operatorBase: HasType
}
Loading
Loading