Skip to content

Commit 763eefa

Browse files
[swiftsrc2cpg] Fix handling of static extension methods (#5735)
1 parent dd50794 commit 763eefa

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/passes/ExtensionCallPass.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ExtensionCallPass(cpg: Cpg, extensionFullNameMapping: Map[String, String])
3030
override def generateParts(): Array[Call] =
3131
cpg.call
3232
.methodFullNameNot(x2cpg.Defines.DynamicCallUnknownFullName)
33-
.dispatchType(DispatchTypes.DYNAMIC_DISPATCH)
33+
.filterNot(_.methodFullName.startsWith("<operator"))
3434
.toArray
3535

3636
/** For a given call (part), if a mapping exists for its methodFullName, set the call to static dispatch, update the
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.joern.swiftsrc2cpg.passes.ast
2+
3+
import io.joern.swiftsrc2cpg.testfixtures.SwiftCompilerSrc2CpgSuite
4+
import io.shiftleft.semanticcpg.language.*
5+
6+
class ExtensionWithCompilerTests extends SwiftCompilerSrc2CpgSuite {
7+
8+
"ExtensionWithCompilerTests" should {
9+
10+
"be correct for static extension methods" in {
11+
val cpg = codeWithSwiftSetup("""
12+
|struct Factory {}
13+
|
14+
|extension Factory {
15+
| static func id(x: Int) -> Int {
16+
| return x
17+
| }
18+
|}
19+
|func main(source: Int) {
20+
| Factory.id(x: source)
21+
|}
22+
|""".stripMargin)
23+
val List(idCall) = cpg.call("id").l
24+
idCall.code shouldBe "Factory.id(x: source)"
25+
idCall.methodFullName shouldBe "SwiftTest.Factory<extension>.id:(x:Swift.Int)->Swift.Int"
26+
val List(idMethod) = cpg.method("id").l
27+
idMethod.fullName shouldBe "SwiftTest.Factory<extension>.id:(x:Swift.Int)->Swift.Int"
28+
}
29+
30+
}
31+
32+
}

0 commit comments

Comments
 (0)