diff --git a/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstCreatorHelper.scala b/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstCreatorHelper.scala index fe42ca9f8cc4..5c530297b584 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstCreatorHelper.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstCreatorHelper.scala @@ -18,6 +18,7 @@ import io.shiftleft.codepropertygraph.generated.nodes.NewNamespaceBlock import io.shiftleft.codepropertygraph.generated.nodes.NewTypeDecl import io.shiftleft.codepropertygraph.generated.ControlStructureTypes import io.shiftleft.codepropertygraph.generated.PropertyNames +import io.shiftleft.passes.IntervalKeyPool import scala.collection.mutable @@ -34,6 +35,10 @@ object AstCreatorHelper { trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: AstCreator => + private val anonClassKeyPool = new IntervalKeyPool(first = 0, last = Long.MaxValue) + + protected def nextAnonClassName(): String = s"${anonClassKeyPool.next}" + protected def notHandledYet(node: SwiftNode): Ast = { val text = s"""Node type '${node.toString}' not handled yet! diff --git a/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForDeclSyntaxCreator.scala b/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForDeclSyntaxCreator.scala index 245a00e1e317..2b04ec82512f 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForDeclSyntaxCreator.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForDeclSyntaxCreator.scala @@ -584,6 +584,19 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) { case class AstAndMethod(ast: Ast, method: NewMethod, methodBlock: Ast) + private def paramSignature( + node: FunctionParameterClauseSyntax | ClosureShorthandParameterListSyntax | ClosureParameterClauseSyntax + ): String = { + node match { + case f: FunctionParameterClauseSyntax => + f.parameters.children.map(c => code(c.`type`)).mkString("(", ",", ")") + case c: ClosureParameterClauseSyntax => + c.parameters.children.map(c => c.`type`.fold(Defines.Any)(code)).mkString("(", ",", ")") + case c: ClosureShorthandParameterListSyntax => + c.children.map(_ => Defines.Any).mkString("(", ",", ")") + } + } + protected def astForFunctionLike( node: FunctionDeclLike, shouldCreateFunctionReference: Boolean = false, @@ -607,16 +620,39 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) { val modifiers = modifiersForFunctionLike(node) val (methodName, methodFullName) = calcMethodNameAndFullName(node) val filename = parserResult.filename + val (signature, returnType) = node match { + case f: FunctionDeclSyntax => + val returnType = f.signature.returnClause.fold(Defines.Any)(c => code(c.`type`)) + (s"$returnType${paramSignature(f.signature.parameterClause)}", returnType) + case a: AccessorDeclSyntax => + val returnType = Defines.Any + (s"$returnType${a.parameters.fold("()")(code)}", returnType) + case i: InitializerDeclSyntax => + val (_, returnType) = astParentInfo() + (s"$returnType${paramSignature(i.signature.parameterClause)}", returnType) + case _: DeinitializerDeclSyntax => + val returnType = Defines.Any + (s"$returnType()", returnType) + case s: SubscriptDeclSyntax => + val returnType = code(s.returnClause.`type`) + (s"$returnType${paramSignature(s.parameterClause)}", returnType) + case c: ClosureExprSyntax => + val returnType = c.signature.flatMap(_.returnClause).fold(Defines.Any)(r => code(r.`type`)) + val paramClauseCode = c.signature.flatMap(_.parameterClause).fold("()")(paramSignature) + (s"$returnType$paramClauseCode", returnType) + } + registerType(returnType) + val methodFullNameAndSignature = s"$methodFullName:$signature" val methodRefNode_ = if (!shouldCreateFunctionReference) { None } else { - Option(methodRefNode(node, methodName, methodFullName, methodFullName)) + Option(methodRefNode(node, methodName, methodFullNameAndSignature, methodFullNameAndSignature)) } val callAst = if (shouldCreateAssignmentCall && shouldCreateFunctionReference) { val idNode = identifierNode(node, methodName) - val idLocal = localNode(node, methodName, methodName, methodFullName).order(0) + val idLocal = localNode(node, methodName, methodName, methodFullNameAndSignature).order(0) diffGraph.addEdge(localAstParentStack.head, idLocal, EdgeTypes.AST) scope.addVariable(methodName, idLocal, BlockScope) scope.addVariableReference(methodName, idNode) @@ -634,32 +670,8 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) { typeRefIdStack.headOption } - val (signature, returnType) = node match { - case f: FunctionDeclSyntax => - val returnType = f.signature.returnClause.fold(Defines.Any)(c => code(c.`type`)) - (s"$returnType $methodFullName ${code(f.signature.parameterClause)}", returnType) - case a: AccessorDeclSyntax => - val returnType = Defines.Any - (s"$returnType $methodFullName ${a.parameters.fold("()")(code)}", returnType) - case i: InitializerDeclSyntax => - val (_, returnType) = astParentInfo() - (s"$returnType $methodFullName ${i.signature.parameterClause.parameters.children.map(code)}", returnType) - case _: DeinitializerDeclSyntax => - val returnType = Defines.Any - (s"$returnType $methodFullName ()", returnType) - case s: SubscriptDeclSyntax => - val returnType = code(s.returnClause.`type`) - (s"$returnType $methodFullName ${s.parameterClause.parameters.children.map(code)}", returnType) - case c: ClosureExprSyntax => - val returnType = c.signature.flatMap(_.returnClause).fold(Defines.Any)(r => code(r.`type`)) - val paramClauseCode = - c.signature.flatMap(_.parameterClause).fold("")(code).stripPrefix("(").stripSuffix(")") - (s"$returnType $methodFullName ($paramClauseCode)", returnType) - } - registerType(returnType) - val codeString = code(node) - val methodNode_ = methodNode(node, methodName, codeString, methodFullName, Option(signature), filename) + val methodNode_ = methodNode(node, methodName, codeString, methodFullNameAndSignature, Option(signature), filename) val block = blockNode(node, PropertyDefaults.Code, Defines.Any) methodAstParentStack.push(methodNode_) scope.pushNewMethodScope(methodFullName, methodName, block, capturingRefNode) @@ -744,7 +756,7 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) { localAstParentStack.pop() methodAstParentStack.pop() - val typeDeclAst = createFunctionTypeAndTypeDeclAst(node, methodNode_, methodName, methodFullName) + val typeDeclAst = createFunctionTypeAndTypeDeclAst(node, methodNode_, methodName, methodFullNameAndSignature) Ast.storeInDiffGraph(astForMethod, diffGraph) Ast.storeInDiffGraph(typeDeclAst, diffGraph) diffGraph.addEdge(methodAstParentStack.head, methodNode_, EdgeTypes.AST) @@ -839,7 +851,7 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) { private def astForSubscriptDeclSyntax(node: SubscriptDeclSyntax): Ast = notHandledYet(node) - private def handleTypeAliasInitializer(node: TypeSyntax): String = { + protected def handleTypeAliasInitializer(node: TypeSyntax): String = { astForTypeSyntax(node).root match case Some(id: NewIdentifier) => id.name case Some(typeDecl: NewTypeDecl) => typeDecl.fullName diff --git a/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForSyntaxCreator.scala b/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForSyntaxCreator.scala index 3b713d12d48a..c4ce161f15fe 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForSyntaxCreator.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForSyntaxCreator.scala @@ -215,8 +215,7 @@ trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this: // TODO: handle ellipsis // TODO: handle defaultValue val name = node.secondName.fold(code(node.firstName))(code) - val tpe = code(node.`type`) - registerType(tpe) + val tpe = handleTypeAliasInitializer(node.`type`) val parameterNode = parameterInNode( node, diff --git a/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForTypeSyntaxCreator.scala b/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForTypeSyntaxCreator.scala index 8829f33a41a4..ec7badb04b54 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForTypeSyntaxCreator.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForTypeSyntaxCreator.scala @@ -10,10 +10,8 @@ import io.shiftleft.codepropertygraph.generated.EdgeTypes trait AstForTypeSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this: AstCreator => - private val AnonTypeDeclNamePrefix = "_anon_cdecl" - private def typeDeclForTypeSyntax(node: TypeSyntax): NewTypeDecl = { - val name = generateUnusedVariableName(usedVariableNames, AnonTypeDeclNamePrefix) + val name = nextAnonClassName() val (typeName, typeFullName) = calcTypeNameAndFullName(name) registerType(typeFullName) diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/io/CodeDumperFromContentTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/io/CodeDumperFromContentTests.scala index 66151fad0214..6874403be2d0 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/io/CodeDumperFromContentTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/io/CodeDumperFromContentTests.scala @@ -19,7 +19,7 @@ class CodeDumperFromContentTests extends SwiftSrc2CpgSuite { inside(cpg.method.nameExact("my_func").dumpRaw.l) { case content :: Nil => content.linesIterator.map(_.strip).l shouldBe List( - "func my_func(param1: Int) -> Int { /* <=== Test0.swift::my_func */", + "func my_func(param1: Int) -> Int { /* <=== Test0.swift::my_func:Int(Int) */", "let x: Int = foo(p: param1)", "}" ) diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/ActorTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/ActorTests.scala index 1d520d191287..9f35b700b653 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/ActorTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/ActorTests.scala @@ -3,23 +3,37 @@ package io.joern.swiftsrc2cpg.passes.ast import io.joern.swiftsrc2cpg.testfixtures.AstSwiftSrc2CpgSuite +import io.shiftleft.codepropertygraph.generated._ +import io.shiftleft.codepropertygraph.generated.nodes._ +import io.shiftleft.semanticcpg.language._ class ActorTests extends AstSwiftSrc2CpgSuite { "ActorTests" should { - "testActor1" ignore { - val cpg = code("actor MyActor1 {}") - ??? + "testActor1" in { + val cpg = code("actor MyActor1 {}") + val List(myActor1) = cpg.typeDecl.nameExact("MyActor1").l + myActor1.fullName shouldBe "Test0.swift::MyActor1" + myActor1.member shouldBe empty + myActor1.boundMethod shouldBe empty } - "testActor2" ignore { + "testActor2" in { val cpg = code(""" |actor MyActor2 { | init() {} | func hello() {} |}""".stripMargin) - ??? + val List(myActor2) = cpg.typeDecl.nameExact("MyActor2").l + myActor2.fullName shouldBe "Test0.swift::MyActor2" + myActor2.member.name.l shouldBe List("hello") + val List(constructor) = myActor2.method.isConstructor.l + constructor.name shouldBe "init" + constructor.fullName shouldBe "Test0.swift::MyActor2:init:Test0.swift::MyActor2()" + val List(hello) = myActor2.boundMethod.l + hello.name shouldBe "hello" + hello.fullName shouldBe "Test0.swift::MyActor2:hello:ANY()" } } diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/AsyncSyntaxTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/AsyncSyntaxTests.scala index c8b2aa48453e..01d90489568f 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/AsyncSyntaxTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/AsyncSyntaxTests.scala @@ -3,28 +3,48 @@ package io.joern.swiftsrc2cpg.passes.ast import io.joern.swiftsrc2cpg.testfixtures.AstSwiftSrc2CpgSuite - -import io.joern.swiftsrc2cpg.testfixtures.AstSwiftSrc2CpgSuite +import io.shiftleft.codepropertygraph.generated.* +import io.shiftleft.codepropertygraph.generated.nodes.* +import io.shiftleft.semanticcpg.language.* +import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal class AsyncSyntaxTests extends AstSwiftSrc2CpgSuite { "AsyncSyntaxTests" should { - "testAsyncSyntax1" ignore { + "testAsyncSyntax1" in { val cpg = code(""" |func asyncGlobal1() async { } |func asyncGlobal2() async throws { } |""".stripMargin) - ??? + val List(asyncGlobal1, asyncGlobal2) = cpg.method.internal.nameNot(NamespaceTraversal.globalNamespaceName).l + asyncGlobal1.name shouldBe "asyncGlobal1" + asyncGlobal1.fullName shouldBe "Test0.swift::asyncGlobal1:ANY()" + asyncGlobal2.name shouldBe "asyncGlobal2" + asyncGlobal2.fullName shouldBe "Test0.swift::asyncGlobal2:ANY()" } - "testAsyncSyntax2" ignore { + "testAsyncSyntax2" in { val cpg = code(""" |typealias AsyncFunc1 = () async -> () |typealias AsyncFunc2 = () async throws -> () |typealias AsyncFunc3 = (_ a: Bool, _ b: Bool) async throws -> () |""".stripMargin) - ??? + val List(f1, f2, f3) = cpg.typeDecl.where(_.aliasTypeFullName).l + f1.name shouldBe "AsyncFunc1" + f1.fullName shouldBe "Test0.swift::AsyncFunc1" + f1.aliasTypeFullName shouldBe Option("Test0.swift::0") + cpg.typeDecl.fullNameExact("Test0.swift::0").size shouldBe 1 + + f2.name shouldBe "AsyncFunc2" + f2.fullName shouldBe "Test0.swift::AsyncFunc2" + f2.aliasTypeFullName shouldBe Option("Test0.swift::1") + cpg.typeDecl.fullNameExact("Test0.swift::1").size shouldBe 1 + + f3.name shouldBe "AsyncFunc3" + f3.fullName shouldBe "Test0.swift::AsyncFunc3" + f3.aliasTypeFullName shouldBe Option("Test0.swift::2") + cpg.typeDecl.fullNameExact("Test0.swift::2").size shouldBe 1 } "testAsyncSyntax3" ignore { @@ -34,17 +54,25 @@ class AsyncSyntaxTests extends AstSwiftSrc2CpgSuite { ??? } - "testAsyncSyntax4" ignore { - val cpg = code("let _ = await asyncGlobal1()") - ??? + "testAsyncSyntax4" in { + val cpg = code("let _ = await asyncGlobal1()") + val List(awaitCall) = cpg.call.nameExact(".await").l + val List(call) = awaitCall.argument.isCall.l + call.name shouldBe "asyncGlobal1" } - "testAsyncSyntax5" ignore { + "testAsyncSyntax5" in { val cpg = code(""" |let _ = { () async in 5 } |let _ = { () throws in 5 } |let _ = { () async throws in 5 }""".stripMargin) - ??? + val List(f1, f2, f3) = cpg.method.internal.nameNot(NamespaceTraversal.globalNamespaceName).l + f1.name shouldBe "0" + f1.fullName shouldBe "Test0.swift::0:ANY()" + f2.name shouldBe "1" + f2.fullName shouldBe "Test0.swift::1:ANY()" + f3.name shouldBe "2" + f3.fullName shouldBe "Test0.swift::2:ANY()" } "testAsyncSyntax6" ignore { @@ -52,7 +80,10 @@ class AsyncSyntaxTests extends AstSwiftSrc2CpgSuite { |func testAwait() async { | let _ = await asyncGlobal1() |}""".stripMargin) - ??? + val List(f) = cpg.method.nameExact("testAwait").l + val List(awaitCall) = f.call.nameExact(".await").l + val List(call) = awaitCall.argument.isCall.l + call.name shouldBe "asyncGlobal1" } } diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/AsyncTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/AsyncTests.scala index 89db4a2b8b36..d4b1b9a2eaee 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/AsyncTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/AsyncTests.scala @@ -3,47 +3,71 @@ package io.joern.swiftsrc2cpg.passes.ast import io.joern.swiftsrc2cpg.testfixtures.AstSwiftSrc2CpgSuite +import io.shiftleft.codepropertygraph.generated.* +import io.shiftleft.codepropertygraph.generated.nodes.* +import io.shiftleft.semanticcpg.language.* +import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal class AsyncTests extends AstSwiftSrc2CpgSuite { "AsyncTests" should { - "testAsync1" ignore { + "testAsync1" in { val cpg = code(""" |func asyncGlobal1() async { } |func asyncGlobal2() async throws { } |""".stripMargin) - ??? + val List(asyncGlobal1, asyncGlobal2) = cpg.method.internal.nameNot(NamespaceTraversal.globalNamespaceName).l + asyncGlobal1.name shouldBe "asyncGlobal1" + asyncGlobal1.fullName shouldBe "Test0.swift::asyncGlobal1:ANY()" + asyncGlobal2.name shouldBe "asyncGlobal2" + asyncGlobal2.fullName shouldBe "Test0.swift::asyncGlobal2:ANY()" } - "testAsync2" ignore { - val cpg = code("func asyncGlobal3() async throws { }") - ??? + "testAsync3" in { + val cpg = code("func asyncGlobal3(fn: () throws -> Int) async rethrows { }") + val List(asyncGlobal3) = cpg.method.internal.nameNot(NamespaceTraversal.globalNamespaceName).l + asyncGlobal3.name shouldBe "asyncGlobal3" + asyncGlobal3.fullName shouldBe "Test0.swift::asyncGlobal3:ANY(() throws -> Int)" + val List(fn) = asyncGlobal3.parameter.l + fn.name shouldBe "fn" + fn.typeFullName shouldBe "Test0.swift::asyncGlobal3:0" + val List(t) = cpg.typeDecl.fullNameExact("Test0.swift::asyncGlobal3:0").l + t.code shouldBe "() throws -> Int" } - "testAsync3" ignore { - val cpg = code("func asyncGlobal3(fn: () throws -> Int) async rethrows { }") - ??? + "testAsync4" in { + val cpg = code("func asyncGlobal4() async -> Int { }") + val List(asyncGlobal4) = cpg.method.internal.nameNot(NamespaceTraversal.globalNamespaceName).l + asyncGlobal4.name shouldBe "asyncGlobal4" + asyncGlobal4.fullName shouldBe "Test0.swift::asyncGlobal4:Int()" + asyncGlobal4.methodReturn.typeFullName shouldBe "Int" } - "testAsync4" ignore { - val cpg = code("func asyncGlobal4() async -> Int { }") - ??? - } - - "testAsync6" ignore { - val cpg = code("func asyncGlobal6() async throws -> Int { }") - ??? + "testAsync6" in { + val cpg = code("func asyncGlobal6() async throws -> Int { }") + val List(asyncGlobal6) = cpg.method.internal.nameNot(NamespaceTraversal.globalNamespaceName).l + asyncGlobal6.name shouldBe "asyncGlobal6" + asyncGlobal6.fullName shouldBe "Test0.swift::asyncGlobal6:Int()" + asyncGlobal6.methodReturn.typeFullName shouldBe "Int" } - "testAsync10a" ignore { - val cpg = code("typealias AsyncFunc1 = () async -> ()") - ??? + "testAsync10a" in { + val cpg = code("typealias AsyncFunc1 = () async -> ()") + val List(f1) = cpg.typeDecl.where(_.aliasTypeFullName).l + f1.name shouldBe "AsyncFunc1" + f1.fullName shouldBe "Test0.swift::AsyncFunc1" + f1.aliasTypeFullName shouldBe Option("Test0.swift::0") + cpg.typeDecl.fullNameExact("Test0.swift::0").size shouldBe 1 } - "testAsync10b" ignore { - val cpg = code("typealias AsyncFunc2 = () async throws -> ()") - ??? + "testAsync10b" in { + val cpg = code("typealias AsyncFunc2 = () async throws -> ()") + val List(f2) = cpg.typeDecl.where(_.aliasTypeFullName).l + f2.name shouldBe "AsyncFunc2" + f2.fullName shouldBe "Test0.swift::AsyncFunc2" + f2.aliasTypeFullName shouldBe Option("Test0.swift::0") + cpg.typeDecl.fullNameExact("Test0.swift::0").size shouldBe 1 } "testAsync11a" ignore { @@ -61,41 +85,65 @@ class AsyncTests extends AstSwiftSrc2CpgSuite { ??? } - "testAsync12" ignore { + "testAsync12" in { val cpg = code(""" |struct MyFuture { | func await() -> Int { 0 } |}""".stripMargin) - ??? + val List(struct) = cpg.typeDecl.nameExact("MyFuture").l + val List(await) = struct.boundMethod.l + await.name shouldBe "await" + await.fullName shouldBe "Test0.swift::MyFuture:await:Int()" + await.methodReturn.typeFullName shouldBe "Int" } - "testAsync13" ignore { + "testAsync13" in { val cpg = code(""" |func testAwaitExpr() async { | let _ = await asyncGlobal1() | let myFuture = MyFuture() | let _ = myFuture.await() |}""".stripMargin) - ??? + cpg.method.nameExact("testAwaitExpr").call.code.sorted.l shouldBe List( + "MyFuture()", + "asyncGlobal1()", + "await asyncGlobal1()", + "let myFuture = MyFuture()", + "let wildcard_0 = await asyncGlobal1()", + "let wildcard_1 = myFuture.await()", + "myFuture.await", + "myFuture.await()" + ) } - "testAsync14" ignore { - val cpg = code("func getIntSomeday() async -> Int { 5 }") - ??? + "testAsync14" in { + val cpg = code("func getIntSomeday() async -> Int { 5 }") + val List(getIntSomeday) = cpg.method.internal.nameNot(NamespaceTraversal.globalNamespaceName).l + getIntSomeday.name shouldBe "getIntSomeday" + getIntSomeday.fullName shouldBe "Test0.swift::getIntSomeday:Int()" + getIntSomeday.methodReturn.typeFullName shouldBe "Int" } - "testAsync15" ignore { + "testAsync15" in { val cpg = code(""" |func testAsyncLet() async { | async let x = await getIntSomeday() | _ = await x |}""".stripMargin) - ??? + cpg.method.nameExact("testAsyncLet").call.code.sorted.l shouldBe List( + "_ = await x", + "await getIntSomeday()", + "await x", + "getIntSomeday()", + "let x = await getIntSomeday()" + ) } - "testAsync16" ignore { - val cpg = code("async func asyncIncorrectly() { }") - ??? + "testAsync16" in { + val cpg = code("async func asyncIncorrectly() { }") + val List(asyncIncorrectly) = cpg.method.internal.nameNot(NamespaceTraversal.globalNamespaceName).l + asyncIncorrectly.name shouldBe "asyncIncorrectly" + asyncIncorrectly.fullName shouldBe "Test0.swift::asyncIncorrectly:ANY()" } } diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/ClosureTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/ClosureTests.scala index 493ca85aef41..a4f3204f959c 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/ClosureTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/ClosureTests.scala @@ -15,11 +15,11 @@ class ClosureTests extends AstSwiftSrc2CpgSuite { |reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 } ) |""".stripMargin) val List(closureRef) = cpg.call("sorted").argument(1).isCall.argument.isMethodRef.l - closureRef.methodFullName shouldBe "Test0.swift::0" + closureRef.methodFullName shouldBe "Test0.swift::0:Bool(String,String)" val List(closureMethod) = cpg.method.nameExact("0").l - closureMethod.fullName shouldBe "Test0.swift::0" + closureMethod.fullName shouldBe "Test0.swift::0:Bool(String,String)" closureMethod.code shouldBe "{ (s1: String, s2: String) -> Bool in return s1 > s2 }" - closureMethod.signature shouldBe "Bool Test0.swift::0 (s1: String, s2: String)" + closureMethod.signature shouldBe "Bool(String,String)" val List(p1, p2) = closureMethod.parameter.l p1.code shouldBe "s1: String" p1.name shouldBe "s1" @@ -36,11 +36,11 @@ class ClosureTests extends AstSwiftSrc2CpgSuite { |reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } ) |""".stripMargin) val List(closureRef) = cpg.call("sorted").argument(1).isCall.argument.isMethodRef.l - closureRef.methodFullName shouldBe "Test0.swift::0" + closureRef.methodFullName shouldBe "Test0.swift::0:ANY(ANY,ANY)" val List(closureMethod) = cpg.method.nameExact("0").l - closureMethod.fullName shouldBe "Test0.swift::0" + closureMethod.fullName shouldBe "Test0.swift::0:ANY(ANY,ANY)" closureMethod.code shouldBe "{ s1, s2 in s1 > s2 }" - closureMethod.signature shouldBe "ANY Test0.swift::0 (s1, s2)" + closureMethod.signature shouldBe "ANY(ANY,ANY)" val List(p1, p2) = closureMethod.parameter.l p1.code shouldBe "s1" p1.name shouldBe "s1" @@ -57,11 +57,11 @@ class ClosureTests extends AstSwiftSrc2CpgSuite { |""".stripMargin) val List(call) = cpg.call.nameExact("someFunctionThatTakesAClosure").l val List(closureRef) = call.argument.isCall.argument(2).isMethodRef.l - closureRef.methodFullName shouldBe "Test0.swift::0" + closureRef.methodFullName shouldBe "Test0.swift::0:ANY()" val List(closureMethod) = cpg.method.nameExact("0").l - closureMethod.fullName shouldBe "Test0.swift::0" + closureMethod.fullName shouldBe "Test0.swift::0:ANY()" closureMethod.code shouldBe "{ foo() }" - closureMethod.signature shouldBe "ANY Test0.swift::0 ()" + closureMethod.signature shouldBe "ANY()" closureMethod.parameter.size shouldBe 0 closureMethod.block.astChildren.isReturn.code.l shouldBe List("foo()") } @@ -72,11 +72,11 @@ class ClosureTests extends AstSwiftSrc2CpgSuite { |""".stripMargin) val List(call) = cpg.call.nameExact("someFunctionThatTakesAClosure").l val List(closureRef) = call.argument.isMethodRef.l - closureRef.methodFullName shouldBe "Test0.swift::0" + closureRef.methodFullName shouldBe "Test0.swift::0:ANY()" val List(closureMethod) = cpg.method.nameExact("0").l - closureMethod.fullName shouldBe "Test0.swift::0" + closureMethod.fullName shouldBe "Test0.swift::0:ANY()" closureMethod.code shouldBe "{ foo() }" - closureMethod.signature shouldBe "ANY Test0.swift::0 ()" + closureMethod.signature shouldBe "ANY()" closureMethod.parameter.size shouldBe 0 closureMethod.block.astChildren.isReturn.code.l shouldBe List("foo()") } @@ -88,21 +88,21 @@ class ClosureTests extends AstSwiftSrc2CpgSuite { |""".stripMargin) val List(fooAssignment, barAssignment) = cpg.call.l val closure1 = fooAssignment.argument(2).asInstanceOf[MethodRef] - closure1.methodFullName shouldBe "Test0.swift::0" + closure1.methodFullName shouldBe "Test0.swift::0:ANY(MyType)" val closure2 = barAssignment.argument(2).asInstanceOf[MethodRef] - closure2.methodFullName shouldBe "Test0.swift::1" + closure2.methodFullName shouldBe "Test0.swift::1:ANY(MyType)" val List(closureMethod1) = cpg.method.nameExact("0").l - closureMethod1.fullName shouldBe "Test0.swift::0" + closureMethod1.fullName shouldBe "Test0.swift::0:ANY(MyType)" closureMethod1.code shouldBe "{ (_ x: MyType) in }" - closureMethod1.signature shouldBe "ANY Test0.swift::0 (_ x: MyType)" + closureMethod1.signature shouldBe "ANY(MyType)" val List(p1) = closureMethod1.parameter.l p1.code shouldBe "_ x: MyType" p1.name shouldBe "x" p1.typeFullName shouldBe "MyType" val List(closureMethod2) = cpg.method.nameExact("1").l - closureMethod2.fullName shouldBe "Test0.swift::1" + closureMethod2.fullName shouldBe "Test0.swift::1:ANY(MyType)" closureMethod2.code shouldBe "{ (x y: MyType) in }" - closureMethod2.signature shouldBe "ANY Test0.swift::1 (x y: MyType)" + closureMethod2.signature shouldBe "ANY(MyType)" val List(p2) = closureMethod2.parameter.l p2.code shouldBe "x y: MyType" p2.name shouldBe "y" diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/SimpleAstCreationPassTest.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/SimpleAstCreationPassTest.scala index 85445d1a8d74..eb76d58f14ac 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/SimpleAstCreationPassTest.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/SimpleAstCreationPassTest.scala @@ -89,9 +89,9 @@ class SimpleAstCreationPassTest extends AstSwiftSrc2CpgSuite { } "have corresponding type decl with correct bindings for function" in { - val cpg = code("func method() -> {}") + val cpg = code("func method() {}") val List(typeDecl) = cpg.typeDecl.nameExact("method").l - typeDecl.fullName should endWith(".swift::method") + typeDecl.fullName should endWith(".swift::method:ANY()") val List(binding) = typeDecl.bindsOut.l binding.name shouldBe "" diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/TypealiasTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/TypealiasTests.scala index ee4f7b5d273e..97ecb3006bdc 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/TypealiasTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/TypealiasTests.scala @@ -19,9 +19,9 @@ class TypealiasTests extends AstSwiftSrc2CpgSuite { | var foo: String |} |""".stripMargin) - val expectedTypeAliasFullName = "Test0.swift::_anon_cdecl_0" + val expectedTypeAliasFullName = "Test0.swift::0" val List(tupleTypeDecl) = cpg.typeDecl.fullNameExact(expectedTypeAliasFullName).l - tupleTypeDecl.name shouldBe "_anon_cdecl_0" + tupleTypeDecl.name shouldBe "0" tupleTypeDecl.code shouldBe "(Int, Int)" val List(intPair) = cpg.typeDecl.nameExact("IntPair").l @@ -38,9 +38,9 @@ class TypealiasTests extends AstSwiftSrc2CpgSuite { |} |typealias IntPair = (Int, Int) |""".stripMargin) - val expectedTypeAliasFullName = "Test0.swift::_anon_cdecl_0" + val expectedTypeAliasFullName = "Test0.swift::0" val List(tupleTypeDecl) = cpg.typeDecl.fullNameExact(expectedTypeAliasFullName).l - tupleTypeDecl.name shouldBe "_anon_cdecl_0" + tupleTypeDecl.name shouldBe "0" tupleTypeDecl.code shouldBe "(Int, Int)" val List(intPair) = cpg.typeDecl.nameExact("IntPair").l @@ -52,9 +52,9 @@ class TypealiasTests extends AstSwiftSrc2CpgSuite { "testTypealias2b" in { val cpg = code("typealias IntTriple = (Int, Int, Int)") - val expectedTypeAliasFullName = "Test0.swift::_anon_cdecl_0" + val expectedTypeAliasFullName = "Test0.swift::0" val List(tupleTypeDecl) = cpg.typeDecl.fullNameExact(expectedTypeAliasFullName).l - tupleTypeDecl.name shouldBe "_anon_cdecl_0" + tupleTypeDecl.name shouldBe "0" tupleTypeDecl.code shouldBe "(Int, Int, Int)" val List(intTriple) = cpg.typeDecl.nameExact("IntTriple").l diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ClassExtensionTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ClassExtensionTests.scala index b8dfab593b75..97834dc8b8f2 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ClassExtensionTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ClassExtensionTests.scala @@ -95,7 +95,7 @@ class ClassExtensionTests extends SwiftSrc2CpgSuite { typeDeclFoo.modifier.modifierType.l shouldBe List(ModifierTypes.PRIVATE) val List(fooConstructor) = typeDeclFoo.method.isConstructor.l - fooConstructor.fullName shouldBe "Test0.swift::Foo:init" + fooConstructor.fullName shouldBe "Test0.swift::Foo:init:Test0.swift::Foo(String,Int)" fooConstructor.block.astChildren.assignment.code.l.sorted shouldBe List( "var a = A()", "var b = false", diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/EnumerationExtensionTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/EnumerationExtensionTests.scala index 609b3913723e..77528cf71d5d 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/EnumerationExtensionTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/EnumerationExtensionTests.scala @@ -102,7 +102,7 @@ class EnumerationExtensionTests extends SwiftSrc2CpgSuite { typeDeclFoo.modifier.modifierType.l shouldBe List(ModifierTypes.PRIVATE) val List(fooConstructor) = typeDeclFoo.method.isConstructor.l - fooConstructor.fullName shouldBe "Test0.swift::Foo:init" + fooConstructor.fullName shouldBe "Test0.swift::Foo:init:Test0.swift::Foo(String,Int)" fooConstructor.block.astChildren.assignment.code.l.sorted shouldBe List( "c1 = 1", "var a = A()", diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ProtocolExtensionTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ProtocolExtensionTests.scala index 9da3a4137e1b..d9644c72b490 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ProtocolExtensionTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ProtocolExtensionTests.scala @@ -95,7 +95,7 @@ class ProtocolExtensionTests extends SwiftSrc2CpgSuite { typeDeclFoo.modifier.modifierType.l shouldBe List(ModifierTypes.PRIVATE) val List(fooConstructor) = typeDeclFoo.method.isConstructor.l - fooConstructor.fullName shouldBe "Test0.swift::Foo:init" + fooConstructor.fullName shouldBe "Test0.swift::Foo:init:Test0.swift::Foo(String,Int)" fooConstructor.block.astChildren.assignment.code.l.sorted shouldBe List( "var a = A()", "var b = false", diff --git a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/StructureExtensionTests.scala b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/StructureExtensionTests.scala index a8d4ea5ce87f..d3abe0c37638 100644 --- a/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/StructureExtensionTests.scala +++ b/joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/StructureExtensionTests.scala @@ -93,7 +93,7 @@ class StructureExtensionTests extends SwiftSrc2CpgSuite { typeDeclFoo.modifier.modifierType.l shouldBe List(ModifierTypes.PRIVATE) val List(fooConstructor) = typeDeclFoo.method.isConstructor.l - fooConstructor.fullName shouldBe "Test0.swift::Foo:init" + fooConstructor.fullName shouldBe "Test0.swift::Foo:init:Test0.swift::Foo(String,Int)" fooConstructor.block.astChildren.assignment.code.l.sorted shouldBe List( "var a = A()", "var b = false",