Skip to content

Commit

Permalink
Fixed python test
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Oct 3, 2024
1 parent e9877ab commit ada1c2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +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.MethodDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.scopes.RecordScope
import de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.AssignExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression
Expand Down Expand Up @@ -102,22 +103,18 @@ class PythonAddDeclarationsPass(ctx: TranslationContext) : ComponentPass(ctx) {
(scopeManager.currentFunction as? MethodDeclaration)?.receiver?.name
) {
// We need to temporarily jump into the scope of the current record to
// add the field
val field =
scopeManager.withScope(scopeManager.currentRecord?.scope) {
newFieldDeclaration(node.name)
}
field
// add the field. These are instance attributes
scopeManager.withScope(
scopeManager.firstScopeIsInstanceOrNull<RecordScope>()
) {
newFieldDeclaration(node.name)
}
} else {
val v = newVariableDeclaration(node.name)
v
}
} else {
val field =
scopeManager.withScope(scopeManager.currentRecord?.scope) {
newFieldDeclaration(node.name)
}
field
newFieldDeclaration(node.name)
}
} else {
newVariableDeclaration(node.name)
Expand All @@ -127,14 +124,8 @@ class PythonAddDeclarationsPass(ctx: TranslationContext) : ComponentPass(ctx) {
decl.location = node.location
decl.isImplicit = true

if (decl is FieldDeclaration) {
scopeManager.currentRecord?.addField(decl)
scopeManager.withScope(scopeManager.currentRecord?.scope) {
scopeManager.addDeclaration(decl)
}
} else {
scopeManager.addDeclaration(decl)
}
scopeManager.withScope(decl.scope) { scopeManager.addDeclaration(decl) }

return decl
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1336,10 +1336,8 @@ class PythonFrontendTest : BaseTest() {
it.registerLanguage<PythonLanguage>()
}
assertNotNull(result)
assertEquals(2, result.variables.size)
// Note, that "pi" is incorrectly inferred as a field declaration. This is a known bug in
// the inference system (and not in the python module) and will be handled separately.
assertEquals(listOf("mypi", "pi"), result.variables.map { it.name.localName })
assertEquals(1, result.variables.size)
assertEquals(listOf("mypi"), result.variables.map { it.name.localName })
}

@Test
Expand Down

0 comments on commit ada1c2b

Please # to comment.