Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ScopeProvider in Pass #1385

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading