Skip to content

Commit

Permalink
Multiple assignments in Go
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Mar 8, 2023
1 parent 9ba037f commit a48b074
Show file tree
Hide file tree
Showing 42 changed files with 1,123 additions and 384 deletions.
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 @@ -57,7 +57,11 @@ class FunctionType : Type {
override fun reference(pointer: PointerType.PointerOrigin?): Type {
// TODO(oxisto): In the future, we actually could just remove the FunctionPointerType
// and just have a regular PointerType here
return FunctionPointerType(parameters.toList(), returnTypes.first(), language)
return FunctionPointerType(
parameters.toList(),
returnTypes.firstOrNull() ?: UnknownType.getUnknownType(),
language
)
}

override fun dereference(): Type {
Expand Down
60 changes: 51 additions & 9 deletions cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ class ScopeManager : ScopeProvider {

/** This function returns the [Scope] associated with a node. */
fun lookupScope(node: Node): Scope? {
return scopeMap[node]
return if (node is TranslationUnitDeclaration) {
globalScope
} else scopeMap[node]
}

/** This function looks up scope by its FQN. This only works for [NameScope]s */
Expand Down Expand Up @@ -635,32 +637,72 @@ class ScopeManager : ScopeProvider {
call: CallExpression,
scope: Scope? = currentScope
): List<FunctionDeclaration> {
val s = extractScope(call, scope)

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

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

// First, we need to check, whether we have some kind of scoping.
if (call.language != null && call.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 = call.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 current scope",
scopeName,
call.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"
)
currentScope
s
} else {
scopes[0]
}
}

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

/**
* Directly jumps to a given scope.
*
* Handle with care, here be dragons. Should not be exposed outside of the cpg-core module.
*/
@PleaseBeCareful
internal fun jumpTo(scope: Scope?): Scope? {
val oldScope = currentScope
currentScope = scope
return oldScope
}

/**
* Directly jumps to the scope a given node defines (if it exists).
*
* Handle with care, here be dragons. Should not be exposed outside of the cpg-core module.
*/
@PleaseBeCareful
internal fun jumpTo(node: Node): Scope? {
return jumpTo(lookupScope(node))
}

/**
* This function can be used to wrap multiple statements contained in [init] into the scope of
* [node]. It will execute [enterScope] before calling [init] and [leaveScope] afterwards.
*/
fun <T : Node> withScope(node: T, init: (T.() -> Unit)) {
enterScope(node)

init(node)

leaveScope(node)
}

fun resolveFunctionStopScopeTraversalOnDefinition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CPPLanguage :
HasDefaultArguments,
HasTemplates,
HasComplexCallResolution,
HasStructs,
HasClasses,
HasUnknownType {
override val fileExtensions = listOf("cpp", "cc", "cxx", "hpp", "hh")
Expand Down Expand Up @@ -300,7 +301,7 @@ class CPPLanguage :
// If we want to use an inferred functionTemplateDeclaration, this needs to be provided.
// Otherwise, we could not resolve to a template and no modifications are made
val functionTemplateDeclaration =
holder.startInference().createInferredFunctionTemplate(templateCall)
holder.startInference(scopeManager).createInferredFunctionTemplate(templateCall)
templateCall.templateInstantiation = functionTemplateDeclaration
val edges = templateCall.templateParameterEdges
// Set instantiation propertyEdges
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,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
import de.fraunhofer.aisec.cpg.graph.types.UnknownType

Expand Down Expand Up @@ -412,6 +413,24 @@ fun MetadataProvider.newArraySubscriptionExpression(
return node
}

/**
* Creates a new [SliceExpression]. 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.newSliceExpression(
code: String? = null,
rawNode: Any? = null
): SliceExpression {
val node = SliceExpression()
node.applyMetadata(this, EMPTY_NAME, rawNode, code, true)

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
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
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 [SliceExpression].
*/
@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 [SliceExpression] 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 SliceExpression -> 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 @@ -129,5 +120,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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,26 @@ open class CallResolver : SymbolResolverPass() {
val suitableBases = getPossibleContainingTypes(call)
candidates =
if (suitableBases.isEmpty()) {
listOf(currentTU.inferFunction(call))
// This is not really the most ideal place, but for now this will do. While this
// is definitely a function, it could still be a function inside a namespace. In
// this case, we want to start inference in that particular namespace and not in
// the TU. It is also a little bit redundant, since ScopeManager.resolveFunction
// (which gets called before) already extracts the scope, but this information
// gets lost.
val scope = scopeManager.extractScope(call, scopeManager.globalScope)

// We have two possible start points, a namespace declaration or a translation
// unit. Nothing else is allowed (fow now)
val func =
when (val start = scope?.astNode) {
is TranslationUnitDeclaration ->
start.inferFunction(call, scopeManager = scopeManager)
is NamespaceDeclaration ->
start.inferFunction(call, scopeManager = scopeManager)
else -> null
}

listOfNotNull(func)
} else {
createMethodDummies(suitableBases, call)
}
Expand Down Expand Up @@ -390,13 +409,13 @@ open class CallResolver : SymbolResolverPass() {
.mapNotNull {
var record = recordMap[it.root.name]
if (record == null && config?.inferenceConfiguration?.inferRecords == true) {
record = it.startInference().inferRecordDeclaration(it, currentTU)
record = it.startInference(scopeManager).inferRecordDeclaration(it, currentTU)
// update the record map
if (record != null) recordMap[it.root.name] = record
}
record
}
.map { record -> record.inferMethod(call) }
.map { record -> record.inferMethod(call, scopeManager = scopeManager) }
}

/**
Expand Down Expand Up @@ -597,7 +616,7 @@ open class CallResolver : SymbolResolverPass() {

return constructorCandidate
?: recordDeclaration
.startInference()
.startInference(scopeManager)
.createInferredConstructor(constructExpression.signature)
}

Expand All @@ -606,7 +625,7 @@ open class CallResolver : SymbolResolverPass() {
recordDeclaration: RecordDeclaration
): ConstructorDeclaration {
return recordDeclaration.constructors.firstOrNull { it.hasSignature(signature) }
?: recordDeclaration.startInference().createInferredConstructor(signature)
?: recordDeclaration.startInference(scopeManager).createInferredConstructor(signature)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ open class VariableUsageResolver : SymbolResolverPass() {
} else {
"class"
}
val record = base.startInference().inferRecordDeclaration(base, currentTU, kind)
val record =
base.startInference(scopeManager).inferRecordDeclaration(base, currentTU, kind)
// update the record map
if (record != null) recordMap[base.name] = record
}
Expand Down Expand Up @@ -404,7 +405,7 @@ open class VariableUsageResolver : SymbolResolverPass() {
// If we didn't find anything, we create a new function or method declaration
return target
?: (declarationHolder ?: currentTU)
.startInference()
.startInference(scopeManager)
.createInferredFunctionDeclaration(
name,
null,
Expand Down
Loading

0 comments on commit a48b074

Please sign in to comment.