Skip to content

Commit

Permalink
[swiftsrc2cpg] Fixes for format strings and identifier/local linking (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Mar 28, 2024
1 parent 5a2efa5 commit 6c9630d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,8 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
Ast()
} else {
val patternIdentifier = identifierNode(binding.pattern, name).typeFullName(typeFullName)
val patternAst = Ast(patternIdentifier)
scope.addVariableReference(name, patternIdentifier)
val patternAst = Ast(patternIdentifier)
modifiers.foreach { mod =>
diffGraph.addEdge(patternIdentifier, mod, EdgeTypes.AST)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import io.joern.x2cpg.Ast
import io.joern.x2cpg.ValidationMode
import io.joern.x2cpg.datastructures.Stack.*
import io.shiftleft.codepropertygraph.generated.nodes.File.PropertyDefaults
import io.shiftleft.codepropertygraph.generated.DispatchTypes
import io.shiftleft.codepropertygraph.generated.Operators

trait AstForSyntaxCollectionCreator(implicit withSchemaValidation: ValidationMode) {
this: AstCreator =>
Expand Down Expand Up @@ -119,15 +121,29 @@ trait AstForSyntaxCollectionCreator(implicit withSchemaValidation: ValidationMod
private def astForPrimaryAssociatedTypeListSyntax(node: PrimaryAssociatedTypeListSyntax): Ast = notHandledYet(node)

private def astForSimpleStringLiteralSegmentListSyntax(node: SimpleStringLiteralSegmentListSyntax): Ast = {
astForListSyntaxChildren(node, node.children)
node.children match {
case child :: Nil => astForNodeWithFunctionReference(child)
case children =>
val stringFormatCall = callNode(node, code(node), Operators.formatString, DispatchTypes.STATIC_DISPATCH)
val childrenAsts = children.map(astForNodeWithFunctionReference)
setArgumentIndices(childrenAsts)
callAst(stringFormatCall, childrenAsts)
}
}

private def astForSpecializeAttributeArgumentListSyntax(node: SpecializeAttributeArgumentListSyntax): Ast = {
Ast(literalNode(node, code(node), Option(Defines.String)))
}

private def astForStringLiteralSegmentListSyntax(node: StringLiteralSegmentListSyntax): Ast = {
astForListSyntaxChildren(node, node.children)
node.children match {
case child :: Nil => astForNodeWithFunctionReference(child)
case children =>
val stringFormatCall = callNode(node, code(node), Operators.formatString, DispatchTypes.STATIC_DISPATCH)
val childrenAsts = children.map(astForNodeWithFunctionReference)
setArgumentIndices(childrenAsts)
callAst(stringFormatCall, childrenAsts)
}
}

private def astForSwitchCaseItemListSyntax(node: SwitchCaseItemListSyntax): Ast = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,24 @@ class ExpressionTests extends AstSwiftSrc2CpgSuite {
???
}

"testStringLiterals" ignore {
"testStringLiterals1" ignore {
val cpg = code(""""\(x)"""")
???
}

"testStringLiterals2" in {
val cpg = code(""""Foo '\(x)' bar"""")
val List(formatCall) = cpg.call.nameExact(Operators.formatString).l
val List(arg1, arg3) = formatCall.argument.isLiteral.l
arg1.code shouldBe """"Foo '""""
arg1.argumentIndex shouldBe 1
arg3.code shouldBe """"' bar""""
arg3.argumentIndex shouldBe 3
val List(arg2) = formatCall.argument.isIdentifier.l
arg2.name shouldBe "x"
arg2.argumentIndex shouldBe 2
}

"testMoveExpression" ignore {
val cpg = code("""
|_move msg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ class SimpleAstCreationPassTest extends AstSwiftSrc2CpgSuite {
boundMethod shouldBe cpg.method.nameExact("method").head
}

"have correct ref to local for simple let" in {
val cpg = code("""
|let x = ""
|""".stripMargin)
val List(localX) = cpg.local.nameExact("x").l
val List(idX) = cpg.identifier.nameExact("x").l
idX.refOut.head shouldBe localX
}

"have correct closure bindings" in {
val cpg = code("""
|func foo() -> {
Expand Down

0 comments on commit 6c9630d

Please sign in to comment.