Skip to content

Commit 19dbdce

Browse files
[swiftsrc2cpg] Fix for @objc annotations and typerefs from type declarations (#5588)
1 parent e9b90ab commit 19dbdce

File tree

12 files changed

+174
-174
lines changed

12 files changed

+174
-174
lines changed

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,8 @@ class AstCreator(val config: Config, val global: Global, val parserResult: Parse
9090
)
9191
}
9292

93-
protected def astForNodeWithFunctionReferenceAndCall(node: SwiftNode): Ast = {
94-
node match {
95-
case func: FunctionDeclLike =>
96-
astForFunctionLike(func, shouldCreateFunctionReference = true, shouldCreateAssignmentCall = true).ast
97-
case _ =>
98-
astForNode(node)
99-
}
100-
}
101-
102-
protected def astForNodeWithFunctionReference(node: SwiftNode): Ast = {
103-
node match {
104-
case func: FunctionDeclLike =>
105-
astForFunctionLike(func, shouldCreateFunctionReference = true).ast
106-
case _ =>
107-
astForNode(node)
108-
}
109-
}
110-
11193
protected def astForNode(node: SwiftNode): Ast = node match {
94+
case func: FunctionDeclLike => astForFunctionLike(func)
11295
case swiftToken: SwiftToken => astForSwiftToken(swiftToken)
11396
case syntax: Syntax => astForSyntax(syntax)
11497
case exprSyntax: ExprSyntax => astForExprSyntax(exprSyntax)

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

Lines changed: 69 additions & 85 deletions
Large diffs are not rendered by default.

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
3131

3232
val clauses = elements.slice(0, MaxInitializers)
3333

34-
val args = clauses.map(x => astForNodeWithFunctionReference(x))
34+
val args = clauses.map(astForNode)
3535

3636
val ast = callAst(initCallNode, args)
3737
if (elements.sizeIs > MaxInitializers) {
@@ -55,8 +55,8 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
5555

5656
val propertiesAsts = slicedElements.map {
5757
case dictElement: DictionaryElementSyntax =>
58-
val lhsAst = astForNodeWithFunctionReference(dictElement.key)
59-
val rhsAst = astForNodeWithFunctionReference(dictElement.value)
58+
val lhsAst = astForNode(dictElement.key)
59+
val rhsAst = astForNode(dictElement.value)
6060

6161
val lhsTmpNode = Ast(identifierNode(dictElement, tmpName))
6262
val lhsIndexAccessCallAst =
@@ -69,7 +69,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
6969
line(dictElement),
7070
column(dictElement)
7171
)
72-
case other => astForNodeWithFunctionReference(other)
72+
case other => astForNode(other)
7373
}
7474

7575
val tmpNode = identifierNode(node, tmpName)
@@ -103,7 +103,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
103103
val tpe = cleanType(tpeCode)
104104
registerType(tpe)
105105
val cpgCastExpression = callNode(node, code(node), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(tpe))
106-
val expr = astForNodeWithFunctionReference(node.expression)
106+
val expr = astForNode(node.expression)
107107
val typeRefNode_ = typeRefNode(tpeNode, tpeCode, tpe)
108108
val arg = Ast(typeRefNode_)
109109
callAst(cpgCastExpression, List(arg, expr))
@@ -113,7 +113,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
113113

114114
private def astForAwaitExprSyntax(node: AwaitExprSyntax): Ast = {
115115
val callNode_ = callNode(node, code(node), "<operator>.await", DispatchTypes.STATIC_DISPATCH)
116-
val argAsts = List(astForNodeWithFunctionReference(node.expression))
116+
val argAsts = List(astForNode(node.expression))
117117
callAst(callNode_, argAsts)
118118
}
119119

@@ -124,22 +124,22 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
124124
}
125125

126126
private def astForBorrowExprSyntax(node: BorrowExprSyntax): Ast = {
127-
astForNodeWithFunctionReference(node.expression)
127+
astForNode(node.expression)
128128
}
129129

130130
private def astForCanImportExprSyntax(node: CanImportExprSyntax): Ast = notHandledYet(node)
131131
private def astForCanImportVersionInfoSyntax(node: CanImportVersionInfoSyntax): Ast = notHandledYet(node)
132132

133133
private def astForClosureExprSyntax(node: ClosureExprSyntax): Ast = {
134-
astForNodeWithFunctionReference(node)
134+
astForNode(node)
135135
}
136136

137137
private def astForConsumeExprSyntax(node: ConsumeExprSyntax): Ast = {
138-
astForNodeWithFunctionReference(node.expression)
138+
astForNode(node.expression)
139139
}
140140

141141
private def astForCopyExprSyntax(node: CopyExprSyntax): Ast = {
142-
astForNodeWithFunctionReference(node.expression)
142+
astForNode(node.expression)
143143
}
144144

145145
private def astForDeclReferenceExprSyntax(node: DeclReferenceExprSyntax): Ast = {
@@ -171,7 +171,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
171171
}
172172

173173
private def astForForceUnwrapExprSyntax(node: ForceUnwrapExprSyntax): Ast = {
174-
astForNodeWithFunctionReference(node.expression)
174+
astForNode(node.expression)
175175
}
176176

177177
private def createBuiltinStaticCall(callExpr: FunctionCallExprSyntax, callee: ExprSyntax, fullName: String): Ast = {
@@ -222,25 +222,25 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
222222
base match {
223223
case None =>
224224
// referencing implicit this
225-
val receiverAst = astForNodeWithFunctionReference(callee)
225+
val receiverAst = astForNode(callee)
226226
val baseNode = identifierNode(m, "this")
227227
scope.addVariableReference("this", baseNode, Defines.Any, EvaluationStrategies.BY_REFERENCE)
228228
(receiverAst, baseNode, code(member))
229229
case Some(d: DeclReferenceExprSyntax) if code(d) == "this" || code(d) == "self" =>
230-
val receiverAst = astForNodeWithFunctionReference(callee)
230+
val receiverAst = astForNode(callee)
231231
val baseNode = identifierNode(d, code(d))
232232
scope.addVariableReference(code(d), baseNode, Defines.Any, EvaluationStrategies.BY_REFERENCE)
233233
(receiverAst, baseNode, code(member))
234234
case Some(d: DeclReferenceExprSyntax) =>
235-
val receiverAst = astForNodeWithFunctionReference(callee)
235+
val receiverAst = astForNode(callee)
236236
val baseNode = identifierNode(d, code(d))
237237
scope.addVariableReference(code(d), baseNode, Defines.Any, EvaluationStrategies.BY_REFERENCE)
238238
(receiverAst, baseNode, code(member))
239239
case Some(otherBase) =>
240240
val tmpVarName = scopeLocalUniqueName("tmp")
241241
val baseTmpNode = identifierNode(otherBase, tmpVarName)
242242
scope.addVariableReference(tmpVarName, baseTmpNode, Defines.Any, EvaluationStrategies.BY_REFERENCE)
243-
val baseAst = astForNodeWithFunctionReference(otherBase)
243+
val baseAst = astForNode(otherBase)
244244
val codeField = s"(${codeOf(baseTmpNode)} = ${codeOf(baseAst.nodes.head)})"
245245
val tmpAssignmentAst =
246246
createAssignmentCallAst(Ast(baseTmpNode), baseAst, codeField, line(otherBase), column(otherBase))
@@ -251,7 +251,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
251251
(fieldAccessAst, thisTmpNode, code(member))
252252
}
253253
case _ =>
254-
val receiverAst = astForNodeWithFunctionReference(callee)
254+
val receiverAst = astForNode(callee)
255255
val thisNode = identifierNode(callee, "this")
256256
scope.addVariableReference(thisNode.name, thisNode, Defines.Any, EvaluationStrategies.BY_REFERENCE)
257257
(receiverAst, thisNode, calleeCode)
@@ -278,15 +278,15 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
278278

279279
private def astForInOutExprSyntax(node: InOutExprSyntax): Ast = {
280280
val op = Defines.PrefixOperatorMap(code(node.ampersand))
281-
val argAst = astForNodeWithFunctionReference(node.expression)
281+
val argAst = astForNode(node.expression)
282282
val callNode_ = callNode(node, code(node), op, DispatchTypes.STATIC_DISPATCH)
283283
callAst(callNode_, List(argAst))
284284
}
285285

286286
private def astForInfixOperatorExprSyntax(node: InfixOperatorExprSyntax): Ast = {
287287
val op = Defines.InfixOperatorMap(code(node.operator))
288-
val lhsAst = astForNodeWithFunctionReference(node.leftOperand)
289-
val rhsAst = astForNodeWithFunctionReference(node.rightOperand)
288+
val lhsAst = astForNode(node.leftOperand)
289+
val rhsAst = astForNode(node.rightOperand)
290290
val callNode_ = callNode(node, code(node), op, DispatchTypes.STATIC_DISPATCH)
291291
val argAsts = List(lhsAst, rhsAst)
292292
callAst(callNode_, argAsts)
@@ -297,7 +297,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
297297
}
298298

299299
private def astForIsExprSyntax(node: IsExprSyntax): Ast = {
300-
val lhsAst = astForNodeWithFunctionReference(node.expression)
300+
val lhsAst = astForNode(node.expression)
301301
val rhsAst = astForNode(node.`type`)
302302
val callNode_ = callNode(node, code(node), Operators.instanceOf, DispatchTypes.STATIC_DISPATCH)
303303
val argAsts = List(lhsAst, rhsAst)
@@ -309,7 +309,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
309309
private def astForMacroExpansionExprSyntax(node: MacroExpansionExprSyntax): Ast = {
310310
val name = code(node.macroName)
311311
val argAsts = astForNode(node.arguments) +:
312-
node.trailingClosure.toList.map(astForNodeWithFunctionReference) :+
312+
node.trailingClosure.toList.map(astForNode) :+
313313
astForNode(node.additionalTrailingClosures)
314314
val callNode =
315315
NewCall()
@@ -337,7 +337,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
337337
scope.addVariableReference(code(d), baseNode, Defines.Any, EvaluationStrategies.BY_REFERENCE)
338338
Ast(baseNode)
339339
case Some(otherBase) =>
340-
astForNodeWithFunctionReference(otherBase)
340+
astForNode(otherBase)
341341
}
342342

343343
member.baseName match {
@@ -358,15 +358,15 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
358358
}
359359

360360
private def astForOptionalChainingExprSyntax(node: OptionalChainingExprSyntax): Ast = {
361-
astForNodeWithFunctionReference(node.expression)
361+
astForNode(node.expression)
362362
}
363363

364364
private def astForPackElementExprSyntax(node: PackElementExprSyntax): Ast = {
365-
astForNodeWithFunctionReference(node.pack)
365+
astForNode(node.pack)
366366
}
367367

368368
private def astForPackExpansionExprSyntax(node: PackExpansionExprSyntax): Ast = {
369-
astForNodeWithFunctionReference(node.repetitionPattern)
369+
astForNode(node.repetitionPattern)
370370
}
371371

372372
private def astForPatternExprSyntax(node: PatternExprSyntax): Ast = {
@@ -418,14 +418,14 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
418418
private def astForPostfixOperatorExprSyntax(node: PostfixOperatorExprSyntax): Ast = {
419419
val operatorMethod = Defines.PostfixOperatorMap(code(node.operator))
420420
val unaryCall = callNode(node, code(node), operatorMethod, operatorMethod, DispatchTypes.STATIC_DISPATCH)
421-
val expressionAst = astForNodeWithFunctionReference(node.expression)
421+
val expressionAst = astForNode(node.expression)
422422
callAst(unaryCall, List(expressionAst))
423423
}
424424

425425
private def astForPrefixOperatorExprSyntax(node: PrefixOperatorExprSyntax): Ast = {
426426
val operatorMethod = Defines.PrefixOperatorMap(code(node.operator))
427427
val unaryCall = callNode(node, code(node), operatorMethod, operatorMethod, DispatchTypes.STATIC_DISPATCH)
428-
val expressionAst = astForNodeWithFunctionReference(node.expression)
428+
val expressionAst = astForNode(node.expression)
429429
callAst(unaryCall, List(expressionAst))
430430
}
431431

@@ -444,7 +444,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
444444
}
445445

446446
private def astForSubscriptCallExprSyntax(node: SubscriptCallExprSyntax): Ast = {
447-
val baseAst = astForNodeWithFunctionReference(node.calledExpression)
447+
val baseAst = astForNode(node.calledExpression)
448448
val memberAst = astForNode(node.arguments)
449449
val additionalArgsAsts = node.trailingClosure.toList.map(astForNode) ++
450450
node.additionalTrailingClosures.children.map(c => astForNode(c.closure))
@@ -462,13 +462,13 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
462462
val (tAsts, flowAst) = s.label match {
463463
case i: SwitchCaseLabelSyntax =>
464464
val children = i.caseItems.children
465-
val childrenTestAsts = children.map(c => astForNodeWithFunctionReference(c.pattern))
465+
val childrenTestAsts = children.map(c => astForNode(c.pattern))
466466
val childrenFlowAsts = children.collect {
467467
case child if child.whereClause.isDefined =>
468468
val whereClause = child.whereClause.get
469469
val ifNode =
470470
controlStructureNode(whereClause.condition, ControlStructureTypes.IF, code(whereClause.condition))
471-
val whereAst = astForNodeWithFunctionReference(whereClause)
471+
val whereAst = astForNode(whereClause)
472472
val whereClauseCallNode = callNode(
473473
whereClause.condition,
474474
s"!(${code(whereClause.condition)})",
@@ -509,7 +509,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
509509
// The semantics of switch statement children is partially defined by their order value.
510510
// The blockAst must have order == 2. Only to avoid collision we set switchExpressionAst to 1
511511
// because the semantics of it is already indicated via the condition edge.
512-
val switchExpressionAst = astForNodeWithFunctionReference(node.subject)
512+
val switchExpressionAst = astForNode(node.subject)
513513
setOrderExplicitly(switchExpressionAst, 1)
514514

515515
val blockNode_ = blockNode(node).order(2)
@@ -529,9 +529,9 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
529529
val name = Operators.conditional
530530
val call = callNode(node, code(node), name, DispatchTypes.STATIC_DISPATCH)
531531

532-
val condAst = astForNodeWithFunctionReference(node.condition)
533-
val posAst = astForNodeWithFunctionReference(node.thenExpression)
534-
val negAst = astForNodeWithFunctionReference(node.elseExpression)
532+
val condAst = astForNode(node.condition)
533+
val posAst = astForNode(node.thenExpression)
534+
val negAst = astForNode(node.elseExpression)
535535

536536
val children = List(condAst, posAst, negAst)
537537
callAst(call, children)
@@ -546,7 +546,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
546546
private def astForTupleExprSyntax(node: TupleExprSyntax): Ast = {
547547
node.elements.children.toList match {
548548
case Nil => astForListLikeExpr(node, Seq.empty)
549-
case head :: Nil => astForNodeWithFunctionReference(head)
549+
case head :: Nil => astForNode(head)
550550
case other => astForListLikeExpr(node, other)
551551
}
552552
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait AstForPatternSyntaxCreator(implicit withSchemaValidation: ValidationMode)
1212
this: AstCreator =>
1313

1414
private def astForExpressionPatternSyntax(node: ExpressionPatternSyntax): Ast = {
15-
astForNodeWithFunctionReference(node.expression)
15+
astForNode(node.expression)
1616
}
1717

1818
private def astForIdentifierPatternSyntax(node: IdentifierPatternSyntax): Ast = {

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
7070
}
7171

7272
private def astForExpressionStmtSyntax(node: ExpressionStmtSyntax): Ast = {
73-
astForNodeWithFunctionReference(node.expression)
73+
astForNode(node.expression)
7474
}
7575

7676
private def astForFallThroughStmtSyntax(node: FallThroughStmtSyntax): Ast = {
@@ -104,7 +104,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
104104
}
105105

106106
private def astForForStmtSyntaxWithWildcard(node: ForStmtSyntax): Ast = {
107-
val initAsts = Seq(astForNodeWithFunctionReference(node.sequence))
107+
val initAsts = Seq(astForNode(node.sequence))
108108
val bodyAst = astForForStmtBody(node)
109109
val forNode = controlStructureNode(node, ControlStructureTypes.FOR, code(node))
110110
forAst(forNode, Nil, initAsts, Nil, Nil, bodyAst)
@@ -139,7 +139,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
139139
// TODO: add operator to schema
140140
callNode(node, s"<operator>.iterator($collectionName)", "<operator>.iterator", DispatchTypes.STATIC_DISPATCH)
141141

142-
val objectKeysCallArgs = List(astForNodeWithFunctionReference(collection))
142+
val objectKeysCallArgs = List(astForNode(collection))
143143
val objectKeysCallAst = callAst(iteratorCall, objectKeysCallArgs)
144144

145145
val iteratorAssignmentNode =
@@ -272,7 +272,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
272272
// TODO: add operator to schema
273273
callNode(node, s"<operator>.iterator($collectionName)", "<operator>.iterator", DispatchTypes.STATIC_DISPATCH)
274274

275-
val objectKeysCallArgs = List(astForNodeWithFunctionReference(collection))
275+
val objectKeysCallArgs = List(astForNode(collection))
276276
val objectKeysCallAst = callAst(iteratorCall, objectKeysCallArgs)
277277

278278
val iteratorAssignmentNode =
@@ -395,7 +395,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
395395
val iteratorCall =
396396
callNode(node, s"<operator>.iterator($collectionName)", "<operator>.iterator", DispatchTypes.STATIC_DISPATCH)
397397

398-
val objectKeysCallArgs = List(astForNodeWithFunctionReference(collection))
398+
val objectKeysCallArgs = List(astForNode(collection))
399399
val objectKeysCallAst = callAst(iteratorCall, objectKeysCallArgs)
400400

401401
val iteratorAssignmentNode =
@@ -540,7 +540,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
540540
val blockNode_ = blockNode(node)
541541
scope.pushNewBlockScope(blockNode_)
542542
localAstParentStack.push(blockNode_)
543-
val bodyAst = astForNodeWithFunctionReference(node.statement)
543+
val bodyAst = astForNode(node.statement)
544544
scope.popScope()
545545
localAstParentStack.pop()
546546
blockAst(blockNode_, List(Ast(labeledNode), bodyAst))
@@ -552,7 +552,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
552552
val code = this.code(node)
553553
// In Swift, a repeat-while loop is semantically the same as a C do-while loop
554554
val doNode = controlStructureNode(node, ControlStructureTypes.DO, code)
555-
val conditionAst = astForNodeWithFunctionReference(node.condition)
555+
val conditionAst = astForNode(node.condition)
556556
val bodyAst = astForNode(node.body)
557557
setOrderExplicitly(conditionAst, 1)
558558
setOrderExplicitly(bodyAst, 2)
@@ -563,7 +563,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
563563
val cpgReturn = returnNode(node, code(node))
564564
node.expression match {
565565
case Some(value) =>
566-
val expr = astForNodeWithFunctionReference(value)
566+
val expr = astForNode(value)
567567
val ast = Ast(cpgReturn).withChild(expr)
568568
expr.root match {
569569
case Some(value) => ast.withArgEdge(cpgReturn, value)
@@ -579,13 +579,13 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
579579
private def astForThrowStmtSyntax(node: ThrowStmtSyntax): Ast = {
580580
val op = "<operator>.throw"
581581
val callNode_ = callNode(node, code(node), op, op, DispatchTypes.STATIC_DISPATCH)
582-
val exprAst = astForNodeWithFunctionReference(node.expression)
582+
val exprAst = astForNode(node.expression)
583583
callAst(callNode_, List(exprAst))
584584
}
585585

586586
private def astForWhileStmtSyntax(node: WhileStmtSyntax): Ast = {
587587
val code = this.code(node)
588-
val conditionAst = astForNodeWithFunctionReference(node.conditions)
588+
val conditionAst = astForNode(node.conditions)
589589
val bodyAst = astForNode(node.body)
590590
setOrderExplicitly(conditionAst, 1)
591591
setOrderExplicitly(bodyAst, 2)
@@ -600,7 +600,7 @@ trait AstForStmtSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
600600

601601
private def astForYieldStmtSyntax(node: YieldStmtSyntax): Ast = {
602602
val cpgReturn = returnNode(node, code(node))
603-
val expr = astForNodeWithFunctionReference(node.yieldedExpressions)
603+
val expr = astForNode(node.yieldedExpressions)
604604
val ast = Ast(cpgReturn).withChild(expr)
605605
expr.root match {
606606
case Some(value) => ast.withArgEdge(cpgReturn, value)

0 commit comments

Comments
 (0)