Skip to content

Commit

Permalink
Adapted to new node type
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Sep 27, 2024
1 parent f4dbd1b commit 4d793e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 69 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -528,29 +528,33 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
return wrapDeclarationToStatement(result)
}

private fun handleGlobal(global: Python.AST.Global): ReferenceScopeModifierStatement {
private fun handleGlobal(global: Python.AST.Global): LookupScopeStatement {
// Technically, our global scope is not identical to the python "global" scope. The reason
// behind that is that we wrap each file in a namespace (as defined in the python spec). So
// the "global" scope is actually our current namespace scope.
var pythonGlobalScope =
frontend.scopeManager.globalScope?.children?.firstOrNull { it is NameScope }

var stmt = newReferenceScopeModifierStatement(pythonGlobalScope, rawNode = global)
stmt.references = global.names.map { newReference(it, rawNode = global) }.toMutableList()
return stmt
return newLookupScopeStatement(
global.names.map { parseName(it).localName },
pythonGlobalScope,
rawNode = global
)
}

private fun handleNonLocal(global: Python.AST.Nonlocal): ReferenceScopeModifierStatement {
private fun handleNonLocal(global: Python.AST.Nonlocal): LookupScopeStatement {
// We need to find the first outer function scope, or rather the block scope belonging to
// the function
var outerFunctionScope =
frontend.scopeManager.firstScopeOrNull {
it is BlockScope && it != frontend.scopeManager.currentScope
}

var stmt = newReferenceScopeModifierStatement(outerFunctionScope, rawNode = global)
stmt.references = global.names.map { newReference(it, rawNode = global) }.toMutableList()
return stmt
return newLookupScopeStatement(
global.names.map { parseName(it).localName },
outerFunctionScope,
rawNode = global
)
}

/** Adds the arguments to [result] which might be located in a [recordDeclaration]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ import de.fraunhofer.aisec.cpg.graph.declarations.Declaration
import de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.scopes.RecordScope
import de.fraunhofer.aisec.cpg.graph.scopes.Scope
import de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement
import de.fraunhofer.aisec.cpg.graph.statements.ReferenceScopeModifierStatement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.AssignExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression
Expand Down Expand Up @@ -90,13 +88,15 @@ class PythonAddDeclarationsPass(ctx: TranslationContext) : ComponentPass(ctx) {
}

// Look for a potential scope modifier for this reference
var targetScope = scopeManager.currentScope?.lookForScopeModifier(ref.name)
// lookupScope
var targetScope =
scopeManager.currentScope?.predefinedLookupScopes[ref.name.toString()]?.targetScope

// There are a couple of things to consider now
var symbol =
// Since this is a WRITE access, we need
// - to look for a local variable, unless
// - a global keyword is present for this variable and scope
// - to look for a local symbol, unless
// - a global keyword is present for this symbol and scope
if (targetScope != null) {
scopeManager.findSymbols(ref.name, ref.location, targetScope)
} else {
Expand Down Expand Up @@ -195,20 +195,11 @@ class PythonAddDeclarationsPass(ctx: TranslationContext) : ComponentPass(ctx) {
private fun handleForEach(node: ForEachStatement) {
when (val forVar = node.variable) {
is Reference -> {
val handled = handleWriteToReference(node.variable as Reference)
val handled = handleWriteToReference(forVar)
if (handled is Declaration) {
handled.let { node.addDeclaration(it) }
}
}
}
}
}

private fun Scope.lookForScopeModifier(name: Name): Scope? {
// Really not the best way to do that
var modifierNode =
this.astNode.allChildren<ReferenceScopeModifierStatement>().firstOrNull {
it.references.any { it.name == name }
}
return modifierNode?.targetScope
}

0 comments on commit 4d793e8

Please sign in to comment.