diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/Reference.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/Reference.kt index 6bedf635e4..44c4a2824a 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/Reference.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/Reference.kt @@ -103,7 +103,7 @@ open class Reference : Expression(), HasType.TypeObserver { override fun toString(): String { return ToStringBuilder(this, TO_STRING_STYLE) - .append(super.toString()) + .appendSuper(super.toString()) .append("refersTo", refersTo) .toString() } diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/SymbolResolver.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/SymbolResolver.kt index 1cc085fc45..2d7512845c 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/SymbolResolver.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/SymbolResolver.kt @@ -40,7 +40,6 @@ import de.fraunhofer.aisec.cpg.passes.inference.startInference import de.fraunhofer.aisec.cpg.passes.order.DependsOn import de.fraunhofer.aisec.cpg.processing.strategy.Strategy import java.util.* -import java.util.ArrayDeque import java.util.regex.Pattern import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -114,7 +113,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) { walker.strategy = Strategy::EOG_FORWARD walker.clearCallbacks() - walker.registerHandler(::resolveCalls) + walker.registerHandler(this::resolve) for (tu in component.translationUnits) { currentTU = tu // gather all resolution start holders and their start nodes @@ -182,11 +181,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) { ) } - protected fun resolveReference( - currentClass: RecordDeclaration?, - parent: Node?, - current: Node? - ) { + protected fun resolveReference(currentClass: RecordDeclaration?, current: Node?) { val language = current?.language if (current !is Reference || current is MemberExpression) return @@ -284,7 +279,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) { } } - protected fun resolveFieldUsages(curClass: RecordDeclaration?, parent: Node?, current: Node?) { + protected fun resolveMemberExpression(curClass: RecordDeclaration?, current: Node?) { if (current !is MemberExpression) { return } @@ -341,19 +336,11 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) { base.type = objectType } } else { - baseTarget = resolveBase(current.base as Reference) - base.refersTo = baseTarget - } - if (baseTarget is EnumDeclaration) { - val memberTarget = - baseTarget.entries.firstOrNull { it.name.lastPartsMatch(current.name) } - if (memberTarget != null) { - current.refersTo = memberTarget - return + // The base should have been resolved by now. Maybe we have some other clue about + // this base from the type system, so we can set the declaration accordingly. + if (base.refersTo == null) { + base.refersTo = base.type.recordDeclaration } - } else if (baseTarget is RecordDeclaration) { - current.refersTo = resolveMember(baseTarget.toType(), current) - return } } @@ -361,15 +348,6 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) { current.refersTo = resolveMember(baseType, current) } - protected fun resolveBase(reference: Reference): Declaration? { - val declaration = scopeManager.resolveReference(reference) - if (declaration != null) { - return declaration - } - - return reference.type.recordDeclaration - } - protected fun resolveMember(containingClass: Type, reference: Reference): ValueDeclaration? { if (isSuperclassReference(reference)) { // if we have a "super" on the member side, this is a member call. We need to resolve @@ -493,34 +471,13 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) { ) } - protected fun resolveCalls(currClass: RecordDeclaration?, parent: Node?, node: Node?) { + protected fun resolve(node: Node?, currClass: RecordDeclaration?) { when (node) { - is TranslationUnitDeclaration -> { - currentTU = node - } - is MemberExpression -> { - resolveFieldUsages(currClass, parent, node) - } - is Reference -> { - resolveReference(currClass, parent, node) - } - is ConstructorCallExpression -> { - handleConstructorCallExpression(node) - } - is ConstructExpression -> { - // We might have call expressions inside our arguments, so in order to correctly - // resolve this call's signature, we need to make sure any call expression arguments - // are fully resolved - handleArguments(node) - handleConstructExpression(node) - } - is CallExpression -> { - // We might have call expressions inside our arguments, so in order to correctly - // resolve this call's signature, we need to make sure any call expression arguments - // are fully resolved - handleArguments(node) - handleCallExpression(scopeManager.currentRecord, node) - } + is MemberExpression -> resolveMemberExpression(currClass, node) + is Reference -> resolveReference(currClass, node) + is ConstructorCallExpression -> handleConstructorCallExpression(node) + is ConstructExpression -> handleConstructExpression(node) + is CallExpression -> handleCallExpression(scopeManager.currentRecord, node) } } @@ -625,25 +582,6 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) { } } - protected fun handleArguments(call: CallExpression) { - val worklist: Deque = ArrayDeque() - call.arguments.forEach { worklist.push(it) } - while (worklist.isNotEmpty()) { - val curr = worklist.pop() - if (curr is CallExpression) { - resolveCalls(null, null, curr) - } else { - val it = Strategy.AST_FORWARD(curr) - while (it.hasNext()) { - val astChild = it.next() - if (astChild !is RecordDeclaration) { - worklist.push(astChild) - } - } - } - } - } - /** * Resolves a [CallExpression.callee] of type [Reference] to a possible list of * [FunctionDeclaration] nodes.