Skip to content

Commit abbb7ca

Browse files
return self from constructor
1 parent 2f8b965 commit abbb7ca

File tree

7 files changed

+39
-26
lines changed

7 files changed

+39
-26
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,20 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
154154

155155
methodAstParentStack.pop()
156156

157-
val mAst = if (methodBlockContent.isEmpty) {
158-
methodStubAst(methodNode_, Seq.empty, methodReturnNode_, modifiers)
159-
} else {
160-
val blockNode = NewBlock()
161-
localAstParentStack.push(blockNode)
162-
val methodBlockContentAsts = methodBlockContent.map(m => astForDeclMember(m, typeDeclNode))
163-
localAstParentStack.pop()
164-
methodAstWithAnnotations(
165-
methodNode_,
166-
Seq.empty,
167-
blockAst(blockNode, methodBlockContentAsts),
168-
methodReturnNode_,
169-
modifiers
170-
)
171-
}
157+
val blockNode = NewBlock()
158+
localAstParentStack.push(blockNode)
159+
val selfNode = identifierNode(node, "self", "self", typeDeclNode.fullName)
160+
scope.addVariableReference("self", selfNode, typeDeclNode.fullName, EvaluationStrategies.BY_REFERENCE)
161+
val returnAst_ = returnAst(returnNode(node, "self"), Seq(Ast(selfNode)))
162+
val methodBlockContentAsts = methodBlockContent.map(m => astForDeclMember(m, typeDeclNode)) :+ returnAst_
163+
localAstParentStack.pop()
164+
val mAst = methodAstWithAnnotations(
165+
methodNode_,
166+
Seq.empty,
167+
blockAst(blockNode, methodBlockContentAsts),
168+
methodReturnNode_,
169+
modifiers
170+
)
172171

173172
val typeDeclAst = createFunctionTypeAndTypeDecl(methodNode_)
174173
Ast.storeInDiffGraph(mAst.merge(typeDeclAst), diffGraph)
@@ -702,20 +701,23 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
702701
case Some(bodyNode: CodeBlockItemListSyntax) =>
703702
bodyNode.children.toList match {
704703
case Nil => List.empty[Ast]
705-
case head :: Nil if !head.item.isInstanceOf[ReturnStmtSyntax] =>
706-
val retCode = code(head)
707-
List(returnAst(returnNode(head, retCode), List(astForNode(head.item))))
708704
case children =>
709705
astsForBlockElements(children)
710706
}
711707
case None =>
712708
List.empty[Ast]
713709
}
714710

711+
val returnAst_ = if (isConstructor(node)) {
712+
val selfNode = identifierNode(node, "self", "self", parentFullName)
713+
scope.addVariableReference("self", selfNode, parentFullName, EvaluationStrategies.BY_REFERENCE)
714+
List(returnAst(returnNode(node, "self"), Seq(Ast(selfNode))))
715+
} else List.empty
716+
715717
val methodReturnNode_ = methodReturnNode(node, returnType)
716718

717719
val methodBlockContentAsts = methodBlockContent.map(m => astForDeclMember(m, typeDecl.get))
718-
val blockAst_ = blockAst(block, methodBlockContentAsts ++ bodyStmtAsts)
720+
val blockAst_ = blockAst(block, methodBlockContentAsts ++ bodyStmtAsts ++ returnAst_)
719721
val astForMethod =
720722
methodAstWithAnnotations(
721723
methodNode_,

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class ActorTests extends SwiftCompilerSrc2CpgSuite {
2727
myActorSwiftc.fullName shouldBe "SwiftTest.MyActor1"
2828
myActorSwiftc.member shouldBe empty
2929
myActorSwiftc.boundMethod.fullName.l shouldBe List("SwiftTest.MyActor1.init:()->SwiftTest.MyActor1")
30+
31+
val List(constructor) = myActorSwiftc.method.isConstructor.l
32+
constructor.name shouldBe "init"
33+
constructor.fullName shouldBe "SwiftTest.MyActor1.init:()->SwiftTest.MyActor1"
34+
val List(constructorReturnSelf) = constructor.ast.isReturn.astChildren.isIdentifier.l
35+
constructorReturnSelf.name shouldBe "self"
36+
constructorReturnSelf.typeFullName shouldBe "SwiftTest.MyActor1"
3037
}
3138

3239
"testActor2" in {
@@ -51,6 +58,10 @@ class ActorTests extends SwiftCompilerSrc2CpgSuite {
5158
foo.name shouldBe "foo"
5259
foo.fullName shouldBe "Sources/main.swift:<global>.MyActor2.foo:(x:Swift.String)->Swift.Int"
5360

61+
val List(constructorReturnSelf) = constructor.ast.isReturn.astChildren.isIdentifier.l
62+
constructorReturnSelf.name shouldBe "self"
63+
constructorReturnSelf.typeFullName shouldBe "Sources/main.swift:<global>.MyActor2"
64+
5465
val List(myActorSwiftc) = compilerCpg.typeDecl.nameExact("MyActor2").l
5566
myActorSwiftc.fullName shouldBe "SwiftTest.MyActor2"
5667
val List(constructorSwiftc) = myActorSwiftc.method.isConstructor.l

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ClosureTests extends AstSwiftSrc2CpgSuite {
4848
p2.code shouldBe "s2"
4949
p2.name shouldBe "s2"
5050
p2.typeFullName shouldBe "ANY"
51-
closureMethod.block.astChildren.isReturn.code.l shouldBe List("s1 > s2")
51+
closureMethod.block.astChildren.code.l shouldBe List("s1 > s2")
5252
}
5353

5454
"testClosure3" in {
@@ -63,7 +63,7 @@ class ClosureTests extends AstSwiftSrc2CpgSuite {
6363
closureMethod.code shouldBe "{ foo() }"
6464
closureMethod.signature shouldBe "()->ANY"
6565
closureMethod.parameter.size shouldBe 0
66-
closureMethod.block.astChildren.isReturn.code.l shouldBe List("foo()")
66+
closureMethod.block.astChildren.isCall.code.l shouldBe List("foo()")
6767
}
6868

6969
"testClosure4" in {
@@ -78,7 +78,7 @@ class ClosureTests extends AstSwiftSrc2CpgSuite {
7878
closureMethod.code shouldBe "{ foo() }"
7979
closureMethod.signature shouldBe "()->ANY"
8080
closureMethod.parameter.size shouldBe 0
81-
closureMethod.block.astChildren.isReturn.code.l shouldBe List("foo()")
81+
closureMethod.block.astChildren.isCall.code.l shouldBe List("foo()")
8282
}
8383

8484
"testClosure5" in {

joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ClassExtensionTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ClassExtensionTests extends SwiftSrc2CpgSuite {
7575
typeDeclB.modifier.modifierType.l shouldBe List(ModifierTypes.PRIVATE)
7676
val List(bConstructor) = typeDeclB.method.isConstructor.l
7777
bConstructor.fullName shouldBe s"Test0.swift:<global>.B.init:()->Test0.swift:<global>.B"
78-
bConstructor.block.astChildren.code.l shouldBe List("var b = 0.0")
78+
bConstructor.block.astChildren.code.l shouldBe List("var b = 0.0", "self")
7979

8080
val List(typeDeclFoo) = cpg.typeDecl.nameExact("Foo").l
8181
typeDeclFoo.fullName shouldBe "Test0.swift:<global>.Foo"

joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/EnumerationExtensionTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class EnumerationExtensionTests extends SwiftSrc2CpgSuite {
7777
typeDeclB.modifier.modifierType.l shouldBe List(ModifierTypes.PRIVATE)
7878
val List(bConstructor) = typeDeclB.method.isConstructor.l
7979
bConstructor.fullName shouldBe s"Test0.swift:<global>.B.init:()->Test0.swift:<global>.B"
80-
bConstructor.block.astChildren.code.l shouldBe List("var b = 0.0")
80+
bConstructor.block.astChildren.code.l shouldBe List("var b = 0.0", "self")
8181

8282
val List(typeDeclFoo) = cpg.typeDecl.nameExact("Foo").l
8383
typeDeclFoo.fullName shouldBe "Test0.swift:<global>.Foo"

joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/ProtocolExtensionTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ProtocolExtensionTests extends SwiftSrc2CpgSuite {
7575
typeDeclB.modifier.modifierType.l shouldBe List(ModifierTypes.PRIVATE)
7676
val List(bConstructor) = typeDeclB.method.isConstructor.l
7777
bConstructor.fullName shouldBe s"Test0.swift:<global>.B.init:()->Test0.swift:<global>.B"
78-
bConstructor.block.astChildren.code.l shouldBe List("var b = 0.0")
78+
bConstructor.block.astChildren.code.l shouldBe List("var b = 0.0", "self")
7979

8080
val List(typeDeclFoo) = cpg.typeDecl.nameExact("Foo").l
8181
typeDeclFoo.fullName shouldBe "Test0.swift:<global>.Foo"

joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/inheritance/StructureExtensionTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class StructureExtensionTests extends SwiftSrc2CpgSuite {
7373
typeDeclB.modifier.modifierType.l shouldBe List(ModifierTypes.PRIVATE)
7474
val List(bConstructor) = typeDeclB.method.isConstructor.l
7575
bConstructor.fullName shouldBe s"Test0.swift:<global>.B.init:()->Test0.swift:<global>.B"
76-
bConstructor.block.astChildren.code.l shouldBe List("var b = 0.0")
76+
bConstructor.block.astChildren.code.l shouldBe List("var b = 0.0", "self")
7777

7878
val List(typeDeclFoo) = cpg.typeDecl.nameExact("Foo").l
7979
typeDeclFoo.fullName shouldBe "Test0.swift:<global>.Foo"

0 commit comments

Comments
 (0)