Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Dec 15, 2024
1 parent 77f9148 commit f7cdc01
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1011,13 +1011,36 @@ fun Expression?.unwrapReference(): Reference? {
/** Returns the [TranslationUnitDeclaration] where this node is located in. */
val Node.translationUnit: TranslationUnitDeclaration?
get() {
var node: Node? = this
while (node != null) {
if (node is TranslationUnitDeclaration) {
return node
}
node = node.astParent
return firstParentOrNull { it is TranslationUnitDeclaration } as? TranslationUnitDeclaration
}

/**
* This helper function be used to find out if a particular expression (usually a [CallExpression]
* or a [Reference]) is imported through a [ImportDeclaration].
*
* It returns a [Pair], with the [Pair.first] being a boolean value whether it was imported and
* [Pair.second] the [ImportDeclaration] if applicable.
*/
val Expression.isImported: Pair<Boolean, ImportDeclaration?>
get() {
if (this is CallExpression) {
return this.callee.isImported
} else if (this is MemberExpression) {
return this.base.isImported

Check warning on line 1029 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Extensions.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Extensions.kt#L1029

Added line #L1029 was not covered by tests
} else if (this is Reference) {
val imports = this.translationUnit.imports

val import =
if (name.parent == null) {
// If the name does not have a parent, this reference could directly be the name
// of an import, let's check
imports.firstOrNull { it.name == this.name }

Check warning on line 1037 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Extensions.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Extensions.kt#L1037

Added line #L1037 was not covered by tests
} else {
// Otherwise, the parent name could be the import
imports.firstOrNull { it.name == this.name.parent }
}
return Pair(import != null, import)
}

return null
return Pair(false, null)

Check warning on line 1045 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Extensions.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Extensions.kt#L1045

Added line #L1045 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,23 @@ class ResolveMemberExpressionAmbiguityPass(ctx: TranslationContext) : Translatio
}
}

private fun isImportedNamespace(name: Name, me: MemberExpression): Boolean {
private fun isImportedNamespace(name: Name, hint: Expression): Boolean {
val resolved =
scopeManager.lookupSymbolByName(
name,
language = me.language,
location = me.location,
startScope = me.scope
language = hint.language,
location = hint.location,
startScope = hint.scope
)
var isImportedNamespace = resolved.singleOrNull() is NamespaceDeclaration
if (!isImportedNamespace) {
// It still could be an imported namespace of an imported package that we do not know.
// The problem is that we do not really know at this point whether we import a
// (sub)module or a global variable of the namespace. We tend to assume that this is a
// namespace
val imports = me.translationUnit.imports
val imports = hint.translationUnit.imports
isImportedNamespace =
imports.any {
var toMatch = it.name
it.alias?.let { toMatch = it }
toMatch.lastPartsMatch(name) || toMatch.startsWith(name)
}
imports.any { it.name.lastPartsMatch(name) || it.name.startsWith(name) }
}
return isImportedNamespace
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import de.fraunhofer.aisec.cpg.graph.types.ObjectType
import de.fraunhofer.aisec.cpg.graph.types.SetType
import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker
import de.fraunhofer.aisec.cpg.passes.ControlDependenceGraphPass
import de.fraunhofer.aisec.cpg.query.value
import de.fraunhofer.aisec.cpg.sarif.Region
import de.fraunhofer.aisec.cpg.test.*
import java.nio.file.Path
Expand Down Expand Up @@ -1426,6 +1425,9 @@ class PythonFrontendTest : BaseTest() {
assertNotNull(call)
assertInvokes(call, aFunc)

var pair = call.isImported
assertTrue(pair.first)

call = result.calls["a_func"]
assertNotNull(call)
assertInvokes(call, aFunc)
Expand All @@ -1445,6 +1447,8 @@ class PythonFrontendTest : BaseTest() {
call = result.calls["different.completely_different_func"]
assertNotNull(call)
assertInvokes(call, cCompletelyDifferentFunc)
pair = call.isImported
assertTrue(pair.first)
}

@Test
Expand Down

0 comments on commit f7cdc01

Please sign in to comment.