Skip to content

Commit b85667d

Browse files
fix for review comment
1 parent 4a1b6da commit b85667d

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import io.joern.x2cpg.frontendspecific.swiftsrc2cpg.Defines
88
import io.joern.x2cpg.{Ast, ValidationMode}
99
import io.shiftleft.codepropertygraph.generated.*
1010
import io.shiftleft.codepropertygraph.generated.nodes.*
11+
import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal
1112

12-
import scala.collection.mutable
1313
import scala.annotation.unused
1414

1515
trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
@@ -631,12 +631,13 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
631631

632632
val parameterAsts = node match {
633633
case f: FunctionDeclSyntax =>
634-
val selfAst = if (isStaticMember(f)) {
635-
val parameterNode =
636-
parameterInNode(node, "self", "self", 0, false, EvaluationStrategies.BY_SHARING, parentFullName)
637-
scope.addVariable("self", parameterNode, parentFullName, VariableScopeManager.ScopeType.MethodScope)
638-
Seq(Ast(parameterNode))
639-
} else Seq.empty
634+
val selfAst =
635+
if (!isStaticMember(f) && !typeForSelfExpression().endsWith(NamespaceTraversal.globalNamespaceName)) {
636+
val parameterNode =
637+
parameterInNode(node, "self", "self", 0, false, EvaluationStrategies.BY_SHARING, parentFullName)
638+
scope.addVariable("self", parameterNode, parentFullName, VariableScopeManager.ScopeType.MethodScope)
639+
Seq(Ast(parameterNode))
640+
} else Seq.empty
640641
selfAst ++ f.signature.parameterClause.parameters.children.map(astForNode)
641642
case a: AccessorDeclSyntax =>
642643
val parameterNode =
@@ -659,10 +660,17 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
659660
scope.addVariable("self", parameterNode, parentFullName, VariableScopeManager.ScopeType.MethodScope)
660661
Ast(parameterNode) +: s.parameterClause.parameters.children.map(astForNode)
661662
case c: ClosureExprSyntax =>
662-
c.signature.flatMap(_.parameterClause) match
663+
val selfAst = if (!typeForSelfExpression().endsWith(NamespaceTraversal.globalNamespaceName)) {
664+
val parameterNode =
665+
parameterInNode(node, "self", "self", 0, false, EvaluationStrategies.BY_SHARING, parentFullName)
666+
scope.addVariable("self", parameterNode, parentFullName, VariableScopeManager.ScopeType.MethodScope)
667+
Seq(Ast(parameterNode))
668+
} else Seq.empty
669+
selfAst ++ (c.signature.flatMap(_.parameterClause) match {
663670
case Some(p: ClosureShorthandParameterListSyntax) => p.children.map(astForNode)
664671
case Some(p: ClosureParameterClauseSyntax) => p.parameters.children.map(astForNode)
665672
case None => Seq.empty
673+
})
666674
}
667675

668676
val body: Option[CodeBlockSyntax | AccessorDeclListSyntax | CodeBlockItemListSyntax] = node match {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.joern.x2cpg.frontendspecific.swiftsrc2cpg.Defines
88
import io.joern.x2cpg.{Ast, ValidationMode}
99
import io.shiftleft.codepropertygraph.generated.*
1010
import io.shiftleft.codepropertygraph.generated.nodes.NewCall
11+
import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal
1112

1213
import scala.annotation.unused
1314

@@ -277,7 +278,14 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
277278
val trailingClosureAsts = expr.trailingClosure.toList.map(astForNode)
278279
val additionalTrailingClosuresAsts = expr.additionalTrailingClosures.children.map(c => astForNode(c.closure))
279280

280-
val args = expr.arguments.children.map(astForNode) ++ trailingClosureAsts ++ additionalTrailingClosuresAsts
281+
val selfTpe = typeForSelfExpression()
282+
val selfNodeAst = if (!selfTpe.endsWith(NamespaceTraversal.globalNamespaceName)) {
283+
val selfNode = identifierNode(expr, "self", "self", selfTpe)
284+
scope.addVariableReference(selfNode.name, selfNode, selfTpe, EvaluationStrategies.BY_REFERENCE)
285+
Seq(Ast(selfNode))
286+
} else Seq.empty
287+
val args =
288+
selfNodeAst ++ expr.arguments.children.map(astForNode) ++ trailingClosureAsts ++ additionalTrailingClosuresAsts
281289

282290
val callExprCode = code(expr)
283291
val callNode_ = callNode(

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,9 @@ class ClosureWithCompilerTests extends SwiftCompilerSrc2CpgSuite {
222222
compareClosureCall.receiver.isIdentifier.typeFullName.l shouldBe List(
223223
"Swift.Function<(Swift.String,Swift.String)->Swift.Bool>"
224224
)
225-
compareClosureCall.argument(1).code shouldBe """"1""""
226-
compareClosureCall.argument(2).code shouldBe """"2""""
225+
compareClosureCall.argument(1).code shouldBe "self"
226+
compareClosureCall.argument(2).code shouldBe """"1""""
227+
compareClosureCall.argument(3).code shouldBe """"2""""
227228
}
228229

229230
"create type decls and bindings correctly (closure as function parameter)" in {

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,10 @@ class SimpleAstCreationPassTest extends AstSwiftSrc2CpgSuite {
298298
fieldName.canonicalName shouldBe "f"
299299
}
300300

301-
val List(fooMethod) = cpg.method.nameExact("foo").l
302-
val List(fooBlock) = fooMethod.astChildren.isBlock.l
303-
val List(barRef) = fooBlock.astChildren.isMethodRef.l
304-
val List(closureBindingSelf) = barRef.captureOut.l
305-
closureBindingSelf.closureBindingId shouldBe Option("Test0.swift:<global>.Foo.foo.bar:self")
301+
val List(fooMethod) = cpg.method.nameExact("foo").l
302+
val List(fooBlock) = fooMethod.astChildren.isBlock.l
303+
val List(barRef) = fooBlock.astChildren.isMethodRef.l
304+
barRef.captureOut shouldBe empty
306305

307306
val List(barMethod) = cpg.method.nameExact("bar").l
308307
val List(barMethodBlock) = barMethod.astChildren.isBlock.l
@@ -331,15 +330,14 @@ class SimpleAstCreationPassTest extends AstSwiftSrc2CpgSuite {
331330
fLocal.name shouldBe "f"
332331
fLocal.typeFullName shouldBe Defines.String
333332
}
334-
val List(fooMethod) = cpg.method.nameExact("foo").l
335-
val List(fooBlock) = fooMethod.astChildren.isBlock.l
336-
val List(fooLocalF) = fooBlock.astChildren.isLocal.nameExact("f").l
337-
val List(barRef) = fooBlock.astChildren.isMethodRef.l
338-
val List(closureBindingF, closureBindingSelf) = barRef.captureOut.l
333+
val List(fooMethod) = cpg.method.nameExact("foo").l
334+
val List(fooBlock) = fooMethod.astChildren.isBlock.l
335+
val List(fooLocalF) = fooBlock.astChildren.isLocal.nameExact("f").l
336+
val List(barRef) = fooBlock.astChildren.isMethodRef.l
337+
val List(closureBindingF) = barRef.captureOut.l
339338
closureBindingF.closureBindingId shouldBe Option("Test0.swift:<global>.Foo.foo.bar:f")
340339
closureBindingF.refOut.head shouldBe fooLocalF
341340
closureBindingF.evaluationStrategy shouldBe EvaluationStrategies.BY_REFERENCE
342-
closureBindingSelf.closureBindingId shouldBe Option("Test0.swift:<global>.Foo.foo.bar:self")
343341

344342
val List(barMethod) = cpg.method.nameExact("bar").l
345343
val List(barMethodBlock) = barMethod.astChildren.isBlock.l

0 commit comments

Comments
 (0)