Skip to content

Commit

Permalink
importFrom now returns a list
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Dec 16, 2024
1 parent c1a1ce1 commit b061e1c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1021,26 +1021,36 @@ val Node.translationUnit: TranslationUnitDeclaration?
* 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?>
val Expression.importedFrom: List<ImportDeclaration>
get() {
if (this is CallExpression) {
return this.callee.isImported
return this.callee.importedFrom
} else if (this is MemberExpression) {
return this.base.isImported
return this.base.importedFrom
} 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.lastPartsMatch(name) }
} else {
// Otherwise, the parent name could be the import
imports.firstOrNull { it.name == this.name.parent }
}
return Pair(import != null, import)
return 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.filter { it.name.lastPartsMatch(name) }
} else {
// Otherwise, the parent name could be the import
imports.filter { it.name == this.name.parent }
} ?: listOf<ImportDeclaration>()
}

return Pair(false, null)
return listOf<ImportDeclaration>()

Check warning on line 1043 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#L1043

Added line #L1043 was not covered by tests
}

/**
* Determines whether the expression is imported from another source.
*
* This property evaluates to `true` if the expression originates from an external or supplemental
* source by checking if the [importedFrom] property contains any entries. Otherwise, it evaluates
* to `false`.
*/
val Expression.isImported: Boolean
get() {
return this.importedFrom.isNotEmpty()
}
Original file line number Diff line number Diff line change
Expand Up @@ -1428,8 +1428,7 @@ class PythonFrontendTest : BaseTest() {
assertNotNull(call)
assertInvokes(call, aFunc)

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

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

@Test
Expand All @@ -1469,19 +1467,19 @@ class PythonFrontendTest : BaseTest() {

val barCall = tu.calls["bar"]
assertIs<CallExpression>(barCall)
assertTrue(barCall.isImported.first)
assertTrue(barCall.isImported)

val bazCall = tu.calls["baz"]
assertIs<CallExpression>(bazCall)
assertTrue(bazCall.isImported.first)
assertTrue(bazCall.isImported)

val fooCall = tu.calls["foo"]
assertIs<CallExpression>(fooCall)
assertTrue(fooCall.isImported.first)
assertTrue(fooCall.isImported)

val foo3Call = tu.calls["foo3"]
assertIs<CallExpression>(foo3Call)
assertTrue(foo3Call.isImported.first)
assertTrue(foo3Call.isImported)
}

@Test
Expand Down

0 comments on commit b061e1c

Please sign in to comment.