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

Multiple improvements in the Go language #1108

Merged
merged 8 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -31,10 +31,7 @@
import de.fraunhofer.aisec.cpg.frontends.Language;
import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend;
import de.fraunhofer.aisec.cpg.frontends.cpp.CLanguage;
import de.fraunhofer.aisec.cpg.graph.declarations.Declaration;
import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration;
import de.fraunhofer.aisec.cpg.graph.declarations.TemplateDeclaration;
import de.fraunhofer.aisec.cpg.graph.declarations.TypedefDeclaration;
import de.fraunhofer.aisec.cpg.graph.declarations.*;
import de.fraunhofer.aisec.cpg.graph.scopes.NameScope;
import de.fraunhofer.aisec.cpg.graph.scopes.RecordScope;
import de.fraunhofer.aisec.cpg.graph.scopes.Scope;
Expand Down Expand Up @@ -113,6 +110,7 @@ public ParameterizedType getTypeParameter(RecordDeclaration recordDeclaration, S
}
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,32 +637,32 @@ class ScopeManager : ScopeProvider {
call: CallExpression,
scope: Scope? = currentScope
): List<FunctionDeclaration> {
val s = extractScope(call.name, scope)
val s = extractScope(call, scope)

return resolve(s) { it.name.lastPartsMatch(call.name) && it.hasSignature(call.signature) }
}

fun extractScope(name: Name, scope: Scope? = currentScope): Scope? {
fun extractScope(node: Node, scope: Scope? = currentScope): Scope? {
var s = scope

// First, we need to check, whether we have some kind of scoping.
if (name.parent != null) {
if (node.name.parent != null) {
// extract the scope name, it is usually a name space, but could probably be something
// else as well in other languages
val scopeName = name.parent
val scopeName = node.name.parent

// TODO: proper scope selection

// this is a scoped call. we need to explicitly jump to that particular scope
val scopes = filterScopes { (it is NameScope && it.name == scopeName) }
s =
if (scopes.isEmpty()) {
LOGGER.error(
"Could not find the scope {} needed to resolve the call {}. Falling back to the default (current) scope",
scopeName,
name
Util.errorWithFileLocation(
node,
LOGGER,
"Could not find the scope $scopeName needed to resolve the call ${node.name}. Falling back to the default (current) scope"
)
scope
s
} else {
scopes[0]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,12 @@ class ExpressionHandler(lang: CXXLanguageFrontend) :
}
is CPPASTArrayRangeDesignator -> {
oneLhs =
newArrayRangeExpression(
newRangeExpression(
handle(des.rangeFloor),
handle(des.rangeCeiling),
des.getRawSignature()
)
oneLhs.operatorCode = "..."
}
else -> {
Util.errorWithFileLocation(
Expand Down Expand Up @@ -643,11 +644,12 @@ class ExpressionHandler(lang: CXXLanguageFrontend) :
}
is CPPASTArrayRangeDesignator -> {
oneLhs =
newArrayRangeExpression(
newRangeExpression(
handle(des.rangeFloor),
handle(des.rangeCeiling),
des.getRawSignature()
)
oneLhs.operatorCode = "..."
}
else -> {
Util.errorWithFileLocation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ fun MetadataProvider.newTranslationUnitDeclaration(
fun MetadataProvider.newFunctionDeclaration(
name: CharSequence?,
code: String? = null,
rawNode: Any? = null
rawNode: Any? = null,
localNameOnly: Boolean = false
): FunctionDeclaration {
val node = FunctionDeclaration()
node.applyMetadata(this, name, rawNode, code)
node.applyMetadata(this, name, rawNode, code, localNameOnly)

log(node)
return node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend
import de.fraunhofer.aisec.cpg.graph.Node.Companion.EMPTY_NAME
import de.fraunhofer.aisec.cpg.graph.NodeBuilder.log
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.AssignExpression
import de.fraunhofer.aisec.cpg.graph.types.Type

/**
Expand Down Expand Up @@ -411,6 +412,29 @@ fun MetadataProvider.newArraySubscriptionExpression(
return node
}

/**
* Creates a new [RangeExpression]. The [MetadataProvider] receiver will be used to fill different
* meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin requires
* an appropriate [MetadataProvider], such as a [LanguageFrontend] as an additional prepended
* argument.
*/
@JvmOverloads
fun MetadataProvider.newRangeExpression(
floor: Expression? = null,
ceiling: Expression? = null,
code: String? = null,
rawNode: Any? = null
): RangeExpression {
val node = RangeExpression()
node.applyMetadata(this, EMPTY_NAME, rawNode, code, true)

node.floor = floor
node.ceiling = ceiling

log(node)
return node
}

/**
* Creates a new [ArrayCreationExpression]. The [MetadataProvider] receiver will be used to fill
* different meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin
Expand Down Expand Up @@ -451,29 +475,6 @@ fun MetadataProvider.newDeclaredReferenceExpression(
return node
}

/**
* Creates a new [ArrayRangeExpression]. The [MetadataProvider] receiver will be used to fill
* different meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin
* requires an appropriate [MetadataProvider], such as a [LanguageFrontend] as an additional
* prepended argument.
*/
@JvmOverloads
fun MetadataProvider.newArrayRangeExpression(
floor: Expression?,
ceil: Expression?,
code: String? = null,
rawNode: Any? = null
): ArrayRangeExpression {
val node = ArrayRangeExpression()
node.applyMetadata(this, EMPTY_NAME, rawNode, code, true)

node.floor = floor
node.ceiling = ceil

log(node)
return node
}

/**
* Creates a new [DeleteExpression]. The [MetadataProvider] receiver will be used to fill different
* meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin requires
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ interface StatementHolder : Holder<Statement> {
statementEdges.add(propertyEdge)
}

/** Inserts the statement [s] before the statement specified in [before]. */
fun insertStatementBefore(s: Statement, before: Statement) {
val statements = this.statements
val idx = statements.indexOf(before)
if (idx != -1) {
val before = statements.subList(0, idx)
val after = statements.subList(idx, statements.size)

this.statements = listOf(*before.toTypedArray(), s, *after.toTypedArray())
}
}

override operator fun plusAssign(node: Statement) {
addStatement(node)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class NamespaceDeclaration : Declaration(), DeclarationHolder, StatementHolder {
@AST
override var statementEdges: MutableList<PropertyEdge<Statement>> = ArrayList()

/**
* In some languages, there is a relationship between paths / directories and the package
* structure. Therefore, we need to be aware of the path this namespace / package is in.
*/
var path: String? = null

/**
* Returns a non-null, possibly empty `Set` of the declaration of a specified type and clazz.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class VariableDeclaration : ValueDeclaration(), HasType.TypeListener, HasInitial
.toString()
}

override val assignments: List<Assignment>
get() {
return initializer?.let { listOf(Assignment(it, this, this)) } ?: listOf()
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class ArraySubscriptionExpression : Expression(), HasType.TypeListener, HasBase

/**
* The expression which represents the "subscription" or index on which the array is accessed.
* This can for example be a reference to another variable ([DeclaredReferenceExpression]) or a
* [Literal].
* This can for example be a reference to another variable ([DeclaredReferenceExpression]), a
* [Literal] or a [RangeExpression].
*/
@AST var subscriptExpression: Expression = ProblemExpression("could not parse index expression")

Expand All @@ -61,8 +61,18 @@ class ArraySubscriptionExpression : Expression(), HasType.TypeListener, HasBase
override val operatorCode: String
get() = "[]"

/**
* This helper function returns the subscript type of the [arrayType]. We have to differentiate
* here between to types of subscripts:
* * Slices (in the form of a [RangeExpression] return the same type as the array
* * Everything else (for example a [Literal] or any other [Expression] that is being evaluated)
* returns the de-referenced type
*/
private fun getSubscriptType(arrayType: Type): Type {
return arrayType.dereference()
return when (subscriptExpression) {
is RangeExpression -> arrayType
else -> arrayType.dereference()
}
}

override fun typeChanged(src: HasType, root: MutableList<HasType>, oldType: Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ package de.fraunhofer.aisec.cpg.graph.statements.expressions
import de.fraunhofer.aisec.cpg.graph.AST
import de.fraunhofer.aisec.cpg.graph.HasType
import de.fraunhofer.aisec.cpg.graph.TypeManager
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.propertyEqualsList
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdgeDelegate
Expand Down Expand Up @@ -60,14 +59,6 @@ class InitializerListExpression : Expression(), HasType.TypeListener {
/** Virtual property to access [initializerEdges] without property edges. */
var initializers by PropertyEdgeDelegate(InitializerListExpression::initializerEdges)

fun addInitializer(initializer: Expression) {
val edge = PropertyEdge(this, initializer)
edge.addProperty(Properties.INDEX, initializerEdges.size)
initializer.registerTypeListener(this)
addPrevDFG(initializer)
initializerEdges.add(edge)
}

override fun typeChanged(src: HasType, root: MutableList<HasType>, oldType: Type) {
if (!TypeManager.isTypeSystemActive()) {
return
Expand Down Expand Up @@ -128,5 +119,10 @@ class InitializerListExpression : Expression(), HasType.TypeListener {
propertyEqualsList(initializerEdges, other.initializerEdges)
}

override fun hashCode() = Objects.hash(super.hashCode(), initializers)
override fun hashCode(): Int {
// Including initializerEdges directly is a HUGE performance loss in the calculation of each
// hash code. Therefore, we only include the array's size, which should hopefully be sort of
// unique to avoid too many hash collisions.
return Objects.hash(super.hashCode(), initializerEdges.size)
}
}
Loading