Skip to content

Commit

Permalink
More cleanup of old typedef remnants
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Nov 24, 2023
1 parent b45310a commit e27db37
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 149 deletions.
21 changes: 3 additions & 18 deletions cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ class ScopeManager : ScopeProvider {
*/
private val aliases = mutableMapOf<PhysicalLocation.ArtifactLocation, MutableSet<Alias>>()

/**
* The language frontend tied to the scope manager. Can be used to implement language specific
* scope resolution or lookup.
*/
var lang: LanguageFrontend<*, *>? = null

/** True, if the scope manager is currently in a [BlockScope]. */
val isInBlock: Boolean
get() = this.firstScopeOrNull { it is BlockScope } != null
Expand Down Expand Up @@ -568,10 +562,7 @@ class ScopeManager : ScopeProvider {
}
}

/**
* Only used by the [de.fraunhofer.aisec.cpg.graph.TypeManager], adds typedefs to the current
* [ValueDeclarationScope].
*/
/** Only used by the [TypeManager], adds typedefs to the current [ValueDeclarationScope]. */
fun addTypedef(typedef: TypedefDeclaration) {
val scope = this.firstScopeIsInstanceOrNull<ValueDeclarationScope>()
if (scope == null) {
Expand All @@ -580,12 +571,6 @@ class ScopeManager : ScopeProvider {
}

scope.addTypedef(typedef)

if (scope.astNode == null) {
lang?.currentTU?.addTypedef(typedef)
} else {
scope.astNode?.addTypedef(typedef)
}
}

private fun getCurrentTypedefs(searchScope: Scope?): Collection<TypedefDeclaration> {
Expand Down Expand Up @@ -878,14 +863,14 @@ class ScopeManager : ScopeProvider {
list += Alias(from, to)
}

fun typeDefFor(finalToCheck: Type): Type? {
fun typedefFor(alias: Type): Type? {
var current = currentScope

// We need to build a path from the current scope to the top most one. This ensures us that
// a local definition overwrites / shadows one that was there on a higher scope.
while (current != null) {
if (current is ValueDeclarationScope) {
val decl = current.typedefs[finalToCheck]
val decl = current.typedefs[alias]
if (decl != null) {
return decl.type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class TypeManager {

fun resolvePossibleTypedef(alias: Type, scopeManager: ScopeManager): Type {
val finalToCheck = alias.root
val applicable = scopeManager.typeDefFor(finalToCheck)
val applicable = scopeManager.typedefFor(finalToCheck)
return applicable ?: alias
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ abstract class LanguageFrontend<in AstNode, TypeNode>(
val typeManager: TypeManager = ctx.typeManager
val config: TranslationConfiguration = ctx.config

init {
this.scopeManager.lang = this
}

var currentTU: TranslationUnitDeclaration? = null

@Throws(TranslationException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.TypedefDeclaration
import de.fraunhofer.aisec.cpg.graph.edge.*
import de.fraunhofer.aisec.cpg.graph.edge.Properties
import de.fraunhofer.aisec.cpg.graph.scopes.GlobalScope
Expand Down Expand Up @@ -203,8 +202,6 @@ open class Node : IVisitable<Node>, Persistable, LanguageProvider, ScopeProvider
/** Virtual property for accessing the parents of the Program Dependence Graph (PDG). */
var prevPDG: MutableSet<Node> by PropertyEdgeSetDelegate(Node::prevPDGEdges, false)

var typedefs: MutableSet<TypedefDeclaration> = HashSet()

/**
* If a node is marked as being inferred, it means that it was created artificially and does not
* necessarily have a real counterpart in the scanned source code. However, the nodes
Expand Down Expand Up @@ -333,10 +330,6 @@ open class Node : IVisitable<Node>, Persistable, LanguageProvider, ScopeProvider
}
}

fun addTypedef(typedef: TypedefDeclaration) {
typedefs.add(typedef)
}

fun addAnnotations(annotations: Collection<Annotation>) {
this.annotations.addAll(annotations)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ val Statement.cyclomaticComplexity: Int
// add one for the do statement (and include the children)
i += (stmt.statement?.cyclomaticComplexity ?: 0) + 1
}
is WhileStatement -> {
// add one for the while statement (and include the children)
i += (stmt.statement?.cyclomaticComplexity ?: 0) + 1
}
is GotoStatement -> {
// add one
i++
Expand Down
Loading

0 comments on commit e27db37

Please sign in to comment.