From 225c982755473680c06eb6d2be11d9a48d484e99 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Thu, 30 Nov 2023 12:36:00 +0100 Subject: [PATCH] Implement `ScopeProvider` in `Pass` This PR implements the `ScopeProvider` interface in the `Pass` class. This should now enable passes to use the information in the stored `scopeManager` when creating new nodes with the node builder. --- .../de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt | 6 ++++++ .../kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt | 12 +++++++++++- .../de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt | 6 +----- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt index 8561f50d8a..c7d93dc711 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt @@ -28,6 +28,7 @@ package de.fraunhofer.aisec.cpg.graph import de.fraunhofer.aisec.cpg.TranslationContext import de.fraunhofer.aisec.cpg.frontends.* import de.fraunhofer.aisec.cpg.graph.Node.Companion.EMPTY_NAME +import de.fraunhofer.aisec.cpg.graph.NodeBuilder.LOGGER import de.fraunhofer.aisec.cpg.graph.NodeBuilder.log import de.fraunhofer.aisec.cpg.graph.scopes.Scope import de.fraunhofer.aisec.cpg.graph.statements.expressions.* @@ -115,6 +116,11 @@ fun Node.applyMetadata( if (provider is ScopeProvider) { this.scope = provider.scope + } else { + LOGGER.warn( + "No scope provider was provided when creating the node {}. This might be an error", + name + ) } if (provider is ContextProvider) { diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt index 0a7c6b82c8..108a114aa9 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt @@ -31,7 +31,9 @@ import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend import de.fraunhofer.aisec.cpg.frontends.TranslationException import de.fraunhofer.aisec.cpg.graph.* import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration +import de.fraunhofer.aisec.cpg.graph.scopes.Scope import de.fraunhofer.aisec.cpg.helpers.Benchmark +import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker.ScopedWalker import de.fraunhofer.aisec.cpg.passes.order.RequiredFrontend import java.util.concurrent.CompletableFuture import java.util.function.Consumer @@ -80,7 +82,7 @@ open class PassConfiguration {} * [ComponentPass] or [TranslationUnitPass] must be used. */ sealed class Pass(final override val ctx: TranslationContext) : - Consumer, ContextProvider { + Consumer, ContextProvider, ScopeProvider { var name: String protected set @@ -92,6 +94,14 @@ sealed class Pass(final override val ctx: TranslationContext) : name = this.javaClass.name } + /** + * The current [Scope] of the [scopeManager]. Please note, that each pass is responsible for + * actually setting the correct scope within the [scopeManager], e.g., by using the + * [ScopedWalker]. + */ + override val scope: Scope? + get() = scopeManager.currentScope + abstract fun cleanup() /** diff --git a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt index 7dd976d2fa..84ff89bec1 100644 --- a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt +++ b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt @@ -29,7 +29,6 @@ import de.fraunhofer.aisec.cpg.TranslationContext import de.fraunhofer.aisec.cpg.frontends.golang.* import de.fraunhofer.aisec.cpg.graph.* import de.fraunhofer.aisec.cpg.graph.declarations.* -import de.fraunhofer.aisec.cpg.graph.scopes.Scope import de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement import de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement import de.fraunhofer.aisec.cpg.graph.statements.expressions.* @@ -109,10 +108,7 @@ import de.fraunhofer.aisec.cpg.passes.order.ExecuteBefore @ExecuteBefore(SymbolResolver::class) @ExecuteBefore(EvaluationOrderGraphPass::class) @ExecuteBefore(DFGPass::class) -class GoExtraPass(ctx: TranslationContext) : ComponentPass(ctx), ScopeProvider { - - override val scope: Scope? - get() = scopeManager.currentScope +class GoExtraPass(ctx: TranslationContext) : ComponentPass(ctx) { override fun accept(component: Component) { // Add built-int functions, but only if one of the components contains a GoLanguage