From aad282eb9ab36f09e88ae2e0c7f8eeb9b9b3cf82 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Mon, 2 Dec 2024 15:57:31 +0100 Subject: [PATCH] First part of scope cleanup (#1856) * First part of scope cleanup * Renamed `BlockScope` to `LocalScope` * Removed `LoopScope` * Removed `TryScope` * Removed `SwitchScope` * Added new NamespaceScope * Formatting * Cleanup of previous block functions * Fixed Go tests * More squashing --------- Co-authored-by: KuechA <31155350+KuechA@users.noreply.github.com> --- .../de/fraunhofer/aisec/cpg/ScopeManager.kt | 64 +++++++------------ .../aisec/cpg/graph/scopes/BlockScope.kt | 44 ------------- .../scopes/{TryScope.kt => LocalScope.kt} | 17 ++--- .../aisec/cpg/graph/scopes/LoopScope.kt | 54 ---------------- .../aisec/cpg/graph/scopes/NameScope.kt | 6 +- .../{SwitchScope.kt => NamespaceScope.kt} | 22 +++---- .../aisec/cpg/graph/scopes/Scope.kt | 10 +-- .../aisec/cpg/graph/scopes/ScopeTraits.kt | 43 ------------- .../graph/scopes/StructureDeclarationScope.kt | 10 ++- .../cpg/graph/scopes/ValueDeclarationScope.kt | 2 +- .../fraunhofer/aisec/cpg/graph/FluentTest.kt | 10 +-- .../aisec/cpg/graph/scopes/ScopeTest.kt | 2 +- .../aisec/cpg/frontends/golang/GoHandler.kt | 5 +- .../cpg/frontends/python/StatementHandler.kt | 7 +- 14 files changed, 62 insertions(+), 234 deletions(-) delete mode 100644 cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/BlockScope.kt rename cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/{TryScope.kt => LocalScope.kt} (74%) delete mode 100644 cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/LoopScope.kt rename cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/{SwitchScope.kt => NamespaceScope.kt} (65%) delete mode 100644 cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ScopeTraits.kt diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt index ba3781213b..01530f6db2 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt @@ -79,9 +79,6 @@ class ScopeManager : ScopeProvider { */ private val symbolTable = mutableMapOf>() - /** True, if the scope manager is currently in a [BlockScope]. */ - val isInBlock: Boolean - get() = this.firstScopeOrNull { it is BlockScope } != null /** True, if the scope manager is currently in a [FunctionScope]. */ val isInFunction: Boolean get() = this.firstScopeOrNull { it is FunctionScope } != null @@ -92,13 +89,16 @@ class ScopeManager : ScopeProvider { val globalScope: GlobalScope? get() = scopeMap[null] as? GlobalScope - /** The current block, according to the scope that is currently active. */ - val currentBlock: Block? - get() = this.firstScopeIsInstanceOrNull()?.astNode as? Block /** The current function, according to the scope that is currently active. */ val currentFunction: FunctionDeclaration? get() = this.firstScopeIsInstanceOrNull()?.astNode as? FunctionDeclaration + /** The current block, according to the scope that is currently active. */ + val currentBlock: Block? + get() = + currentScope?.astNode as? Block + ?: currentScope?.astNode?.firstParentOrNull { it is Block } as? Block + /** * The current method in the active scope tree, this ensures that 'this' keywords are mapped * correctly if a method contains a lambda or other types of function declarations @@ -218,11 +218,11 @@ class ScopeManager : ScopeProvider { * new scope, this function needs to be called. Appropriate scopes will then be created * on-the-fly, if they do not exist. * - * The scope manager has an internal association between the type of scope, e.g. a [BlockScope] + * The scope manager has an internal association between the type of scope, e.g. a [LocalScope] * and the CPG node it represents, e.g. a [Block]. * - * Afterwards, all calls to [addDeclaration] will be distributed to the - * [de.fraunhofer.aisec.cpg.graph.DeclarationHolder] that is currently in-scope. + * Afterward, all calls to [addDeclaration] will be distributed to the [DeclarationHolder] that + * is currently in-scope. */ fun enterScope(nodeToScope: Node) { var newScope: Scope? = null @@ -231,21 +231,21 @@ class ScopeManager : ScopeProvider { if (!scopeMap.containsKey(nodeToScope)) { newScope = when (nodeToScope) { - is Block -> BlockScope(nodeToScope) is WhileStatement, is DoStatement, - is AssertStatement -> LoopScope(nodeToScope) + is AssertStatement, is ForStatement, - is ForEachStatement -> LoopScope(nodeToScope as Statement) - is SwitchStatement -> SwitchScope(nodeToScope) + is ForEachStatement, + is SwitchStatement, + is TryStatement, + is IfStatement, + is CatchClause, + is Block -> LocalScope(nodeToScope) is FunctionDeclaration -> FunctionScope(nodeToScope) - is IfStatement -> ValueDeclarationScope(nodeToScope) - is CatchClause -> ValueDeclarationScope(nodeToScope) is RecordDeclaration -> RecordScope(nodeToScope) is TemplateDeclaration -> TemplateScope(nodeToScope) - is TryStatement -> TryScope(nodeToScope) is TranslationUnitDeclaration -> FileScope(nodeToScope) - is NamespaceDeclaration -> newNameScopeIfNecessary(nodeToScope) + is NamespaceDeclaration -> newNamespaceIfNecessary(nodeToScope) else -> { LOGGER.error( "No known scope for AST node of type {}", @@ -266,7 +266,7 @@ class ScopeManager : ScopeProvider { } /** - * A small internal helper function used by [enterScope] to create a [NameScope]. + * A small internal helper function used by [enterScope] to create a [NamespaceScope]. * * The issue with name scopes, such as a namespace, is that it can exist across several files, * i.e. translation units, represented by different [NamespaceDeclaration] nodes. But, in order @@ -274,15 +274,15 @@ class ScopeManager : ScopeProvider { * all declarations, such as classes, independently of the translation units. Therefore, we need * to check, whether such as node already exists. If it does already exist: * - we update the scope map so that the current [NamespaceDeclaration] points to the existing - * [NameScope] + * [NamespaceScope] * - we return null, indicating to [enterScope], that no new scope needs to be pushed by * [enterScope]. * - * Otherwise, we return a new name scope. + * Otherwise, we return a new namespace scope. */ - private fun newNameScopeIfNecessary(nodeToScope: NamespaceDeclaration): NameScope? { + private fun newNamespaceIfNecessary(nodeToScope: NamespaceDeclaration): NamespaceScope? { val existingScope = - filterScopes { it is NameScope && it.name == nodeToScope.name }.firstOrNull() + filterScopes { it is NamespaceScope && it.name == nodeToScope.name }.firstOrNull() return if (existingScope != null) { // update the AST node to this namespace declaration @@ -296,25 +296,7 @@ class ScopeManager : ScopeProvider { // does not need to push a new scope null } else { - NameScope(nodeToScope) - } - } - - /** - * Similar to [enterScope], but does so in a "read-only" mode, e.g. it does not modify the scope - * tree and does not create new scopes on the fly, as [enterScope] does. - */ - fun enterScopeIfExists(nodeToScope: Node?) { - if (scopeMap.containsKey(nodeToScope)) { - val scope = scopeMap[nodeToScope] - - // we need a special handling of name spaces, because - // they are associated to more than one AST node - if (scope is NameScope) { - // update AST (see enterScope for an explanation) - scope.astNode = nodeToScope - } - currentScope = scope + NamespaceScope(nodeToScope) } } diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/BlockScope.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/BlockScope.kt deleted file mode 100644 index 1b038e8b80..0000000000 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/BlockScope.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2019, 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.scopes - -import de.fraunhofer.aisec.cpg.graph.statements.BreakStatement -import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block - -/** - * Scope of validity associated to a block of statements. Variables declared inside a block are not - * visible outside. - */ -class BlockScope(blockStatement: Block) : ValueDeclarationScope(blockStatement), Breakable { - private val breaks: MutableList = ArrayList() - - override fun addBreakStatement(breakStatement: BreakStatement) { - breaks.add(breakStatement) - } - - override val breakStatements: List - get() = breaks -} diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/TryScope.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/LocalScope.kt similarity index 74% rename from cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/TryScope.kt rename to cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/LocalScope.kt index de1d34ec75..07f969080c 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/TryScope.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/LocalScope.kt @@ -26,16 +26,9 @@ package de.fraunhofer.aisec.cpg.graph.scopes import de.fraunhofer.aisec.cpg.graph.Node -import de.fraunhofer.aisec.cpg.graph.statements.BreakStatement -class TryScope(astNode: Node?) : ValueDeclarationScope(astNode), Breakable { - - private val breaks = mutableListOf() - - override fun addBreakStatement(breakStatement: BreakStatement) { - breaks.add(breakStatement) - } - - override val breakStatements: List - get() = breaks -} +/** + * Scope of validity associated to the local statement. Variables declared inside this statement are + * not visible outside. + */ +class LocalScope(astNode: Node) : ValueDeclarationScope(astNode) {} diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/LoopScope.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/LoopScope.kt deleted file mode 100644 index 6b92520021..0000000000 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/LoopScope.kt +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019, 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.scopes - -import de.fraunhofer.aisec.cpg.graph.statements.* -import org.slf4j.LoggerFactory - -class LoopScope(loopStatement: Statement) : - ValueDeclarationScope(loopStatement), Breakable, Continuable { - - private val breaks = mutableListOf() - private val continues = mutableListOf() - - override fun addBreakStatement(breakStatement: BreakStatement) { - breaks.add(breakStatement) - } - - override fun addContinueStatement(continueStatement: ContinueStatement) { - continues.add(continueStatement) - } - - override val breakStatements: List - get() = breaks - - override val continueStatements: List - get() = continues - - companion object { - private val LOGGER = LoggerFactory.getLogger(LoopScope::class.java) - } -} diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/NameScope.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/NameScope.kt index fd48c7230d..4280e774b9 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/NameScope.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/NameScope.kt @@ -29,10 +29,10 @@ import de.fraunhofer.aisec.cpg.graph.Node /** * A scope which acts as a namespace with a certain name, which is prefixed to all local names - * declared in it. This could be a package or other structural elements, like a class. In the latter - * case, the derived [RecordScope] should be used. + * declared in it. This could be a package or other structural elements, like a class. In the first + * case, the derived [NamespaceScope], in the latter case, the derived [RecordScope] should be used. */ -open class NameScope(node: Node?) : StructureDeclarationScope(node) { +sealed class NameScope(node: Node?) : StructureDeclarationScope(node) { init { astNode = node diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/SwitchScope.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/NamespaceScope.kt similarity index 65% rename from cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/SwitchScope.kt rename to cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/NamespaceScope.kt index c57c9de64b..01c6266c00 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/SwitchScope.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/NamespaceScope.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, Fraunhofer AISEC. All rights reserved. + * Copyright (c) 2024, 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. @@ -25,17 +25,11 @@ */ package de.fraunhofer.aisec.cpg.graph.scopes -import de.fraunhofer.aisec.cpg.graph.statements.BreakStatement -import de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement +import de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration -class SwitchScope(switchStatement: SwitchStatement) : - ValueDeclarationScope(switchStatement), Breakable { - private val breaks = mutableListOf() - - override fun addBreakStatement(breakStatement: BreakStatement) { - breaks.add(breakStatement) - } - - override val breakStatements: List - get() = breaks -} +/** + * This scope is opened up by a [NamespaceDeclaration] and represents the scope of the whole + * namespace. This scope is special in a way that it will only exist once (per [GlobalScope]) and + * contains all symbols declared in this namespace, even if they are spread across multiple files. + */ +class NamespaceScope(astNode: NamespaceDeclaration) : NameScope(astNode) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/Scope.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/Scope.kt index 30ee7dcd16..cf322f7ff1 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/Scope.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/Scope.kt @@ -56,7 +56,7 @@ typealias SymbolMap = MutableMap> * restriction and can act as namespaces to avoid name collisions. */ @NodeEntity -abstract class Scope( +sealed class Scope( @Relationship(value = "SCOPE", direction = Relationship.Direction.INCOMING) @JsonBackReference open var astNode: Node? @@ -191,14 +191,6 @@ abstract class Scope( labelStatement.label?.let { labelStatements[it] = labelStatement } } - fun isBreakable(): Boolean { - return this is LoopScope || this is SwitchScope - } - - fun isContinuable(): Boolean { - return this is LoopScope - } - override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ScopeTraits.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ScopeTraits.kt deleted file mode 100644 index 3aec62b34d..0000000000 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ScopeTraits.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.scopes - -import de.fraunhofer.aisec.cpg.graph.statements.BreakStatement -import de.fraunhofer.aisec.cpg.graph.statements.ContinueStatement - -/** Represents scopes that can be interrupted by a [BreakStatement]. */ -interface Breakable { - fun addBreakStatement(breakStatement: BreakStatement) - - val breakStatements: List -} - -/** Represents scopes that can be interrupted by a [ContinueStatement]. */ -interface Continuable { - fun addContinueStatement(continueStatement: ContinueStatement) - - val continueStatements: List -} diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/StructureDeclarationScope.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/StructureDeclarationScope.kt index f26c806c29..76a2795f68 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/StructureDeclarationScope.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/StructureDeclarationScope.kt @@ -25,12 +25,18 @@ */ package de.fraunhofer.aisec.cpg.graph.scopes +import de.fraunhofer.aisec.cpg.ScopeManager import de.fraunhofer.aisec.cpg.graph.DeclarationHolder import de.fraunhofer.aisec.cpg.graph.Node import de.fraunhofer.aisec.cpg.graph.declarations.* -open class StructureDeclarationScope(final override var astNode: Node?) : - ValueDeclarationScope(astNode) { +/** + * This sealed (and abstract) class represents a [Scope] that in addition to declare variables also + * defines structures, such as classes, namespaces, etc. + * + * This is actually only needed because of the legacy [ScopeManager.resolve] function. + */ +sealed class StructureDeclarationScope(astNode: Node?) : ValueDeclarationScope(astNode) { val structureDeclarations: List get() { return symbols diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ValueDeclarationScope.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ValueDeclarationScope.kt index 8cad5bbb97..462b4a00e8 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ValueDeclarationScope.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ValueDeclarationScope.kt @@ -39,7 +39,7 @@ import org.slf4j.LoggerFactory * Is a scope where local variables can be declared and independent of specific language constructs. * Works for if, for, and extends to the block scope */ -open class ValueDeclarationScope(override var astNode: Node?) : Scope(astNode) { +sealed class ValueDeclarationScope(astNode: Node?) : Scope(astNode) { val valueDeclarations: List get() { return symbols.flatMap { it.value }.filterIsInstance() diff --git a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/FluentTest.kt b/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/FluentTest.kt index 5bbb2fc77e..3ce4feaaac 100644 --- a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/FluentTest.kt +++ b/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/FluentTest.kt @@ -30,9 +30,9 @@ import de.fraunhofer.aisec.cpg.frontends.TestLanguageFrontend import de.fraunhofer.aisec.cpg.frontends.testFrontend import de.fraunhofer.aisec.cpg.graph.builder.* import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration -import de.fraunhofer.aisec.cpg.graph.scopes.BlockScope import de.fraunhofer.aisec.cpg.graph.scopes.FunctionScope import de.fraunhofer.aisec.cpg.graph.scopes.GlobalScope +import de.fraunhofer.aisec.cpg.graph.scopes.LocalScope import de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement import de.fraunhofer.aisec.cpg.graph.statements.IfStatement import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement @@ -97,22 +97,22 @@ class FluentTest { // First line should be a DeclarationStatement val declarationStatement = main[0] as? DeclarationStatement assertNotNull(declarationStatement) - assertTrue(declarationStatement.scope is BlockScope) + assertTrue(declarationStatement.scope is LocalScope) val variable = declarationStatement.singleDeclaration as? VariableDeclaration assertNotNull(variable) - assertTrue(variable.scope is BlockScope) + assertTrue(variable.scope is LocalScope) assertLocalName("a", variable) var lit1 = variable.initializer as? Literal<*> assertNotNull(lit1) - assertTrue(lit1.scope is BlockScope) + assertTrue(lit1.scope is LocalScope) assertEquals(1, lit1.value) // Second line should be an IfStatement val ifStatement = main[1] as? IfStatement assertNotNull(ifStatement) - assertTrue(ifStatement.scope is BlockScope) + assertTrue(ifStatement.scope is LocalScope) val condition = ifStatement.condition as? BinaryOperator assertNotNull(condition) diff --git a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ScopeTest.kt b/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ScopeTest.kt index 77f6db5b6a..ae1b56910b 100644 --- a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ScopeTest.kt +++ b/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/graph/scopes/ScopeTest.kt @@ -44,7 +44,7 @@ class ScopeTest { // two scopes, global and local val globalScope = GlobalScope() globalScope.addSymbol("a", globalA) - val scope = BlockScope(Block()) + val scope = LocalScope(Block()) scope.parent = globalScope scope.addSymbol("a", localA) diff --git a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoHandler.kt b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoHandler.kt index 656894b58d..4c9483d9b2 100644 --- a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoHandler.kt +++ b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoHandler.kt @@ -29,7 +29,7 @@ import de.fraunhofer.aisec.cpg.frontends.Handler import de.fraunhofer.aisec.cpg.graph.Node import de.fraunhofer.aisec.cpg.graph.ProblemNode import de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration -import de.fraunhofer.aisec.cpg.graph.scopes.NameScope +import de.fraunhofer.aisec.cpg.graph.scopes.NamespaceScope import de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal import de.fraunhofer.aisec.cpg.helpers.Util import java.util.function.Supplier @@ -100,7 +100,8 @@ abstract class GoHandler