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 07755b54be..2dfa9203a8 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 @@ -878,6 +878,25 @@ class ScopeManager : ScopeProvider { list += Alias(from, to) } + fun typeDefFor(finalToCheck: 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] + if (decl != null) { + return decl.type + } + } + + current = current.parent + } + + return null + } + /** Returns the current scope for the [ScopeProvider] interface. */ override val scope: Scope? get() = currentScope diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TypeManager.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TypeManager.kt index dab639a511..56c431b4b2 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TypeManager.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TypeManager.kt @@ -252,10 +252,7 @@ class TypeManager { fun resolvePossibleTypedef(alias: Type, scopeManager: ScopeManager): Type { val finalToCheck = alias.root - val applicable = - scopeManager.currentTypedefs - .firstOrNull { t: TypedefDeclaration -> t.alias.root == finalToCheck } - ?.type + val applicable = scopeManager.typeDefFor(finalToCheck) return applicable ?: alias } } diff --git a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclarationHandler.kt b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclarationHandler.kt index bdb686ca4b..a371816613 100644 --- a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclarationHandler.kt +++ b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclarationHandler.kt @@ -716,6 +716,7 @@ class DeclarationHandler(lang: CXXLanguageFrontend) : frontend.scopeManager.resetToGlobal(node) frontend.currentTU = node val problematicIncludes = HashMap>() + for (declaration in translationUnit.declarations) { if (declaration is CPPASTLinkageSpecification) { continue // do not care about these for now