Skip to content

Commit 9c513f5

Browse files
simple call to static function detection
1 parent d99b019 commit 9c513f5

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForExprSyntaxCreator.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,15 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
286286
if (GlobalBuiltins.builtins.contains(calleeCode)) {
287287
createBuiltinStaticCall(node, callee, calleeCode)
288288
} else {
289-
// TODO: extend the GsonTypeInfoReader to query for information whether
290-
// the call is a call to a static function and generate a proper static call here
291289
callee match {
292290
case m: MemberAccessExprSyntax if m.base.isEmpty || code(m.base.get) == "self" =>
293291
// referencing implicit self
294292
val selfTpe = typeForSelfExpression()
295293
val selfNode = identifierNode(node, "self", "self", selfTpe)
296294
scope.addVariableReference("self", selfNode, selfTpe, EvaluationStrategies.BY_REFERENCE)
297295
handleCallNodeArgs(node, Ast(selfNode), code(m.declName.baseName))
296+
case m: MemberAccessExprSyntax if isRefToStaticFunction(calleeCode) =>
297+
createBuiltinStaticCall(node, callee, calleeCode)
298298
case m: MemberAccessExprSyntax =>
299299
val memberCode = code(m.declName)
300300
handleCallNodeArgs(node, astForNode(m.base.get), memberCode)
@@ -354,6 +354,11 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
354354
}
355355
}
356356

357+
private def isRefToStaticFunction(calleeCode: String): Boolean = {
358+
// TODO: extend the GsonTypeInfoReader to query for information whether the call is a call to a static function
359+
calleeCode.headOption.exists(_.isUpper) && !calleeCode.contains("(") && !calleeCode.contains(")")
360+
}
361+
357362
private def isRefToConstructor(func: FunctionCallExprSyntax, node: ExprSyntax): Boolean = {
358363
if (!config.swiftBuild) {
359364
// Early exit; without types from the compiler we will be unable to identify constructor calls anyway.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object GlobalBuiltins {
2121
"Set", // Creates a set from a sequence
2222

2323
// Types and type casting
24-
"type", // Returns the dynamic type of a value
24+
"type", // Returns the dynamic type of some value
2525
"numericCast", // Returns the given integer as the equivalent value in a different integer type
2626
"unsafeDowncast", // Returns the given instance cast unconditionally to the specified type
2727
"unsafeBitCast", // Returns the bits of the given instance, interpreted as having the specified type
@@ -89,7 +89,7 @@ object GlobalBuiltins {
8989

9090
// C Interoperability
9191
"withVaList", // Invokes the given closure with a C va_list argument derived from the given array of arguments
92-
"getVaList", // Returns a CVaListPointer that is backed by autoreleased storage, built from the given array of arguments
92+
"getVaList", // Returns a CVaListPointer that is backed by auto-released storage, built from the given array of arguments
9393

9494
// Miscellaneous Functions
9595
"swap", // Exchanges the values of two variables

joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/CallTests.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ class CallTests extends SwiftCompilerSrc2CpgSuite {
103103
returnId.typeFullName shouldBe "SwiftTest.Foo"
104104
}
105105

106+
"be correct for simple call to static function" in {
107+
// TODO: extend the GsonTypeInfoReader to query for information whether the call is a call to a static function
108+
val testCode =
109+
"""
110+
|func main() {
111+
| Foo.staticFunc()
112+
|}
113+
|""".stripMargin
114+
val cpg = code(testCode)
115+
116+
val List(staticFuncCall) = cpg.call.nameExact("staticFunc").l
117+
staticFuncCall.methodFullName shouldBe "Foo.staticFunc"
118+
staticFuncCall.dispatchType shouldBe DispatchTypes.STATIC_DISPATCH
119+
staticFuncCall.argument shouldBe empty
120+
}
121+
106122
}
107123

108124
}

0 commit comments

Comments
 (0)