Skip to content

Commit f1a8fad

Browse files
[swiftsrc2cpg] Added support for more missing operators and expressions (#4257)
1 parent a02559d commit f1a8fad

File tree

4 files changed

+95
-42
lines changed

4 files changed

+95
-42
lines changed

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

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,24 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
6565
astForNode(node.literal)
6666
}
6767

68-
private def astForBorrowExprSyntax(node: BorrowExprSyntax): Ast = notHandledYet(node)
68+
private def astForBorrowExprSyntax(node: BorrowExprSyntax): Ast = {
69+
astForNodeWithFunctionReference(node.expression)
70+
}
71+
6972
private def astForCanImportExprSyntax(node: CanImportExprSyntax): Ast = notHandledYet(node)
7073
private def astForCanImportVersionInfoSyntax(node: CanImportVersionInfoSyntax): Ast = notHandledYet(node)
7174

7275
private def astForClosureExprSyntax(node: ClosureExprSyntax): Ast = {
7376
astForNodeWithFunctionReference(node)
7477
}
7578

76-
private def astForConsumeExprSyntax(node: ConsumeExprSyntax): Ast = notHandledYet(node)
77-
private def astForCopyExprSyntax(node: CopyExprSyntax): Ast = notHandledYet(node)
79+
private def astForConsumeExprSyntax(node: ConsumeExprSyntax): Ast = {
80+
astForNodeWithFunctionReference(node.expression)
81+
}
82+
83+
private def astForCopyExprSyntax(node: CopyExprSyntax): Ast = {
84+
astForNodeWithFunctionReference(node.expression)
85+
}
7886

7987
private def astForDeclReferenceExprSyntax(node: DeclReferenceExprSyntax): Ast = {
8088
val name = code(node)
@@ -108,7 +116,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
108116
}
109117

110118
private def astForForceUnwrapExprSyntax(node: ForceUnwrapExprSyntax): Ast = {
111-
astForNode(node.expression)
119+
astForNodeWithFunctionReference(node.expression)
112120
}
113121

114122
private def createBuiltinStaticCall(callExpr: FunctionCallExprSyntax, callee: ExprSyntax, fullName: String): Ast = {
@@ -220,7 +228,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
220228
}
221229

222230
private def astForInOutExprSyntax(node: InOutExprSyntax): Ast = {
223-
astForNode(node.expression)
231+
astForNodeWithFunctionReference(node.expression)
224232
}
225233

226234
private def astForInfixOperatorExprSyntax(node: InfixOperatorExprSyntax): Ast = {
@@ -241,8 +249,12 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
241249
case "||=" => Operators.assignmentOr
242250
case "||" => Operators.logicalOr
243251
case "^=" => Operators.assignmentXor
252+
case "&<<" => Operators.shiftLeft
244253
case "<<" => Operators.shiftLeft
254+
case "&>>=" => Operators.assignmentArithmeticShiftRight
255+
case "&>>" => Operators.arithmeticShiftRight
245256
case ">>" => Operators.arithmeticShiftRight
257+
case "&<<=" => Operators.assignmentShiftLeft
246258
case "<<=" => Operators.assignmentShiftLeft
247259
case ">>=" => Operators.assignmentArithmeticShiftRight
248260
case ">>>=" => Operators.assignmentLogicalShiftRight
@@ -272,6 +284,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
272284
case "!" => Operators.logicalNot
273285
case "^" => Operators.xor
274286
case "??" => Operators.elvis
287+
case "~=" => Operators.in
275288
case _ =>
276289
notHandledYet(node.operator)
277290
"<operator>.unknown"
@@ -426,14 +439,17 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
426439

427440
private def astForPrefixOperatorExprSyntax(node: PrefixOperatorExprSyntax): Ast = {
428441
val operatorMethod = code(node.operator) match {
429-
case "-" => Operators.preDecrement
430-
case "+" => Operators.preIncrement
431-
case "~" => Operators.not
432-
case "!" => Operators.logicalNot
433-
case "<" => Operators.lessThan
434-
case ">" => Operators.greaterThan
435-
case "==" => Operators.equals
436-
case "%" => Operators.modulo
442+
case "-" => Operators.preDecrement
443+
case "+" => Operators.preIncrement
444+
case "~" => Operators.not
445+
case "!" => Operators.logicalNot
446+
case "..<" => Operators.lessThan
447+
case "<" => Operators.lessThan
448+
case "..>" => Operators.greaterThan
449+
case ">" => Operators.greaterThan
450+
case "==" => Operators.equals
451+
case "%" => Operators.modulo
452+
case "..." => "<operator>.splat"
437453
case _ =>
438454
notHandledYet(node.operator)
439455
"<operator>.unknown"
@@ -443,9 +459,15 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
443459
callAst(unaryCall, List(expressionAst))
444460
}
445461

446-
private def astForRegexLiteralExprSyntax(node: RegexLiteralExprSyntax): Ast = notHandledYet(node)
447-
private def astForSequenceExprSyntax(node: SequenceExprSyntax): Ast = notHandledYet(node)
448-
private def astForSimpleStringLiteralExprSyntax(node: SimpleStringLiteralExprSyntax): Ast = notHandledYet(node)
462+
private def astForRegexLiteralExprSyntax(node: RegexLiteralExprSyntax): Ast = notHandledYet(node)
463+
464+
private def astForSequenceExprSyntax(node: SequenceExprSyntax): Ast = {
465+
astForNode(node.elements)
466+
}
467+
468+
private def astForSimpleStringLiteralExprSyntax(node: SimpleStringLiteralExprSyntax): Ast = {
469+
astForNode(node.segments)
470+
}
449471

450472
private def astForStringLiteralExprSyntax(node: StringLiteralExprSyntax): Ast = {
451473
astForNode(node.segments)
@@ -566,7 +588,12 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
566588
}
567589
}
568590

569-
private def astForTypeExprSyntax(node: TypeExprSyntax): Ast = notHandledYet(node)
591+
private def astForTypeExprSyntax(node: TypeExprSyntax): Ast = {
592+
val nodeCode = code(node)
593+
registerType(nodeCode)
594+
Ast(identifierNode(node, nodeCode, dynamicTypeHints = Seq(nodeCode)))
595+
}
596+
570597
private def astForUnresolvedAsExprSyntax(node: UnresolvedAsExprSyntax): Ast = notHandledYet(node)
571598
private def astForUnresolvedIsExprSyntax(node: UnresolvedIsExprSyntax): Ast = notHandledYet(node)
572599
private def astForUnresolvedTernaryExprSyntax(node: UnresolvedTernaryExprSyntax): Ast = notHandledYet(node)

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,21 @@ trait AstForSyntaxCollectionCreator(implicit withSchemaValidation: ValidationMod
7171
private def astForDifferentiabilityArgumentListSyntax(node: DifferentiabilityArgumentListSyntax): Ast = notHandledYet(
7272
node
7373
)
74-
private def astForDocumentationAttributeArgumentListSyntax(node: DocumentationAttributeArgumentListSyntax): Ast =
75-
notHandledYet(node)
76-
private def astForEffectsAttributeArgumentListSyntax(node: EffectsAttributeArgumentListSyntax): Ast = notHandledYet(
77-
node
78-
)
79-
private def astForEnumCaseElementListSyntax(node: EnumCaseElementListSyntax): Ast = notHandledYet(node)
80-
private def astForEnumCaseParameterListSyntax(node: EnumCaseParameterListSyntax): Ast = notHandledYet(node)
81-
private def astForExprListSyntax(node: ExprListSyntax): Ast = notHandledYet(node)
74+
private def astForDocumentationAttributeArgumentListSyntax(node: DocumentationAttributeArgumentListSyntax): Ast = {
75+
Ast(literalNode(node, code(node), Option(Defines.String)))
76+
}
77+
78+
private def astForEffectsAttributeArgumentListSyntax(node: EffectsAttributeArgumentListSyntax): Ast = {
79+
Ast(literalNode(node, code(node), Option(Defines.String)))
80+
}
81+
82+
private def astForEnumCaseElementListSyntax(node: EnumCaseElementListSyntax): Ast = notHandledYet(node)
83+
private def astForEnumCaseParameterListSyntax(node: EnumCaseParameterListSyntax): Ast = notHandledYet(node)
84+
85+
private def astForExprListSyntax(node: ExprListSyntax): Ast = {
86+
astForListSyntaxChildren(node, node.children)
87+
}
88+
8289
private def astForFunctionParameterListSyntax(node: FunctionParameterListSyntax): Ast = notHandledYet(node)
8390
private def astForGenericArgumentListSyntax(node: GenericArgumentListSyntax): Ast = notHandledYet(node)
8491
private def astForGenericParameterListSyntax(node: GenericParameterListSyntax): Ast = notHandledYet(node)
@@ -106,10 +113,14 @@ trait AstForSyntaxCollectionCreator(implicit withSchemaValidation: ValidationMod
106113
)
107114
private def astForPrecedenceGroupNameListSyntax(node: PrecedenceGroupNameListSyntax): Ast = notHandledYet(node)
108115
private def astForPrimaryAssociatedTypeListSyntax(node: PrimaryAssociatedTypeListSyntax): Ast = notHandledYet(node)
109-
private def astForSimpleStringLiteralSegmentListSyntax(node: SimpleStringLiteralSegmentListSyntax): Ast =
110-
notHandledYet(node)
111-
private def astForSpecializeAttributeArgumentListSyntax(node: SpecializeAttributeArgumentListSyntax): Ast =
112-
notHandledYet(node)
116+
117+
private def astForSimpleStringLiteralSegmentListSyntax(node: SimpleStringLiteralSegmentListSyntax): Ast = {
118+
astForListSyntaxChildren(node, node.children)
119+
}
120+
121+
private def astForSpecializeAttributeArgumentListSyntax(node: SpecializeAttributeArgumentListSyntax): Ast = {
122+
Ast(literalNode(node, code(node), Option(Defines.String)))
123+
}
113124

114125
private def astForStringLiteralSegmentListSyntax(node: StringLiteralSegmentListSyntax): Ast = {
115126
astForListSyntaxChildren(node, node.children)

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this:
7575
private def astForAvailabilityLabeledArgumentSyntax(node: AvailabilityLabeledArgumentSyntax): Ast = notHandledYet(
7676
node
7777
)
78-
private def astForBackDeployedAttributeArgumentsSyntax(node: BackDeployedAttributeArgumentsSyntax): Ast =
79-
notHandledYet(node)
78+
private def astForBackDeployedAttributeArgumentsSyntax(node: BackDeployedAttributeArgumentsSyntax): Ast = {
79+
Ast(literalNode(node, code(node), Option(Defines.String)))
80+
}
8081

8182
private def astForCatchClauseSyntax(node: CatchClauseSyntax): Ast = {
8283
astForListSyntaxChildren(node, List(node.catchItems, node.body))
@@ -166,9 +167,11 @@ trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this:
166167
private def astForDeinitializerEffectSpecifiersSyntax(node: DeinitializerEffectSpecifiersSyntax): Ast = notHandledYet(
167168
node
168169
)
169-
private def astForDerivativeAttributeArgumentsSyntax(node: DerivativeAttributeArgumentsSyntax): Ast = notHandledYet(
170-
node
171-
)
170+
171+
private def astForDerivativeAttributeArgumentsSyntax(node: DerivativeAttributeArgumentsSyntax): Ast = {
172+
Ast(literalNode(node, code(node), Option(Defines.String)))
173+
}
174+
172175
private def astForDesignatedTypeSyntax(node: DesignatedTypeSyntax): Ast = notHandledYet(node)
173176

174177
private def astForDictionaryElementSyntax(node: DictionaryElementSyntax): Ast = {
@@ -183,8 +186,11 @@ trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this:
183186
private def astForDifferentiabilityWithRespectToArgumentSyntax(
184187
node: DifferentiabilityWithRespectToArgumentSyntax
185188
): Ast = notHandledYet(node)
186-
private def astForDifferentiableAttributeArgumentsSyntax(node: DifferentiableAttributeArgumentsSyntax): Ast =
187-
notHandledYet(node)
189+
190+
private def astForDifferentiableAttributeArgumentsSyntax(node: DifferentiableAttributeArgumentsSyntax): Ast = {
191+
Ast(literalNode(node, code(node), Option(Defines.String)))
192+
}
193+
188194
private def astForDocumentationAttributeArgumentSyntax(node: DocumentationAttributeArgumentSyntax): Ast =
189195
notHandledYet(node)
190196
private def astForDynamicReplacementAttributeArgumentsSyntax(node: DynamicReplacementAttributeArgumentsSyntax): Ast =
@@ -231,9 +237,11 @@ trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this:
231237
private def astForGenericRequirementSyntax(node: GenericRequirementSyntax): Ast = notHandledYet(node)
232238
private def astForGenericWhereClauseSyntax(node: GenericWhereClauseSyntax): Ast = notHandledYet(node)
233239
private def astForIfConfigClauseSyntax(node: IfConfigClauseSyntax): Ast = notHandledYet(node)
234-
private def astForImplementsAttributeArgumentsSyntax(node: ImplementsAttributeArgumentsSyntax): Ast = notHandledYet(
235-
node
236-
)
240+
241+
private def astForImplementsAttributeArgumentsSyntax(node: ImplementsAttributeArgumentsSyntax): Ast = {
242+
Ast(literalNode(node, code(node), Option(Defines.String)))
243+
}
244+
237245
private def astForImportPathComponentSyntax(node: ImportPathComponentSyntax): Ast = notHandledYet(node)
238246
private def astForInheritanceClauseSyntax(node: InheritanceClauseSyntax): Ast = notHandledYet(node)
239247
private def astForInheritedTypeSyntax(node: InheritedTypeSyntax): Ast = notHandledYet(node)
@@ -370,9 +378,13 @@ trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this:
370378
private def astForTypeAnnotationSyntax(node: TypeAnnotationSyntax): Ast = notHandledYet(node)
371379
private def astForTypeEffectSpecifiersSyntax(node: TypeEffectSpecifiersSyntax): Ast = notHandledYet(node)
372380
private def astForTypeInitializerClauseSyntax(node: TypeInitializerClauseSyntax): Ast = notHandledYet(node)
381+
373382
private def astForUnavailableFromAsyncAttributeArgumentsSyntax(
374383
node: UnavailableFromAsyncAttributeArgumentsSyntax
375-
): Ast = notHandledYet(node)
384+
): Ast = {
385+
Ast(literalNode(node, code(node.message), Option(Defines.String)))
386+
}
387+
376388
private def astForUnderscorePrivateAttributeArgumentsSyntax(node: UnderscorePrivateAttributeArgumentsSyntax): Ast =
377389
notHandledYet(node)
378390
private def astForVersionComponentSyntax(node: VersionComponentSyntax): Ast = notHandledYet(node)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ trait AstForTypeSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
3636
private def astForMemberTypeSyntax(node: MemberTypeSyntax): Ast =
3737
notHandledYet(node)
3838

39-
private def astForMetatypeTypeSyntax(node: MetatypeTypeSyntax): Ast =
40-
notHandledYet(node)
39+
private def astForMetatypeTypeSyntax(node: MetatypeTypeSyntax): Ast = {
40+
val nodeCode = code(node)
41+
registerType(nodeCode)
42+
Ast(identifierNode(node, nodeCode, dynamicTypeHints = Seq(nodeCode)))
43+
}
4144

4245
private def astForMissingTypeSyntax(node: MissingTypeSyntax): Ast =
4346
notHandledYet(node)

0 commit comments

Comments
 (0)