Skip to content

Commit

Permalink
Implement ScopeProvider in Pass (#1385)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oxisto authored Nov 30, 2023
1 parent 8e2cf3e commit c81ef60
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,7 +82,7 @@ open class PassConfiguration {}
* [ComponentPass] or [TranslationUnitPass] must be used.
*/
sealed class Pass<T : Node>(final override val ctx: TranslationContext) :
Consumer<T>, ContextProvider {
Consumer<T>, ContextProvider, ScopeProvider {
var name: String
protected set

Expand All @@ -92,6 +94,14 @@ sealed class Pass<T : Node>(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()

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c81ef60

Please sign in to comment.