Skip to content

Commit

Permalink
[swiftsrc2cpg] Implemented more tests (#4411)
Browse files Browse the repository at this point in the history
(availability query, borrow expr, builtinword, conflictmarkers, copy expr)
  • Loading branch information
max-leuthaeuser authored Mar 29, 2024
1 parent 6c9630d commit ce9ae68
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 351 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import io.shiftleft.codepropertygraph.generated.DispatchTypes
import io.shiftleft.codepropertygraph.generated.nodes.File.PropertyDefaults
import io.shiftleft.codepropertygraph.generated.nodes.NewIdentifier

import scala.annotation.unused

trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
this: AstCreator =>

Expand Down Expand Up @@ -843,11 +845,11 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
callAst(callNode, argAsts)
}

private def astForOperatorDeclSyntax(node: OperatorDeclSyntax): Ast = Ast()
private def astForOperatorDeclSyntax(@unused node: OperatorDeclSyntax): Ast = Ast()

private def astForPoundSourceLocationSyntax(node: PoundSourceLocationSyntax): Ast = notHandledYet(node)

private def astForPrecedenceGroupDeclSyntax(node: PrecedenceGroupDeclSyntax): Ast = Ast()
private def astForPrecedenceGroupDeclSyntax(@unused node: PrecedenceGroupDeclSyntax): Ast = Ast()

private def astForSubscriptDeclSyntax(node: SubscriptDeclSyntax): Ast = notHandledYet(node)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import io.shiftleft.codepropertygraph.generated.Operators
import io.shiftleft.codepropertygraph.generated.nodes.NewCall
import io.shiftleft.codepropertygraph.generated.nodes.NewNode

import scala.annotation.unused

trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
this: AstCreator =>

Expand Down Expand Up @@ -233,64 +235,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
}

private def astForInfixOperatorExprSyntax(node: InfixOperatorExprSyntax): Ast = {
val op = code(node.operator) match {
case "=" => Operators.assignment
case "+=" => Operators.assignmentPlus
case "-=" => Operators.assignmentMinus
case "*=" => Operators.assignmentMultiplication
case "/=" => Operators.assignmentDivision
case "%=" => Operators.assignmentModulo
case "**=" => Operators.assignmentExponentiation
case "&&" => Operators.logicalAnd
case "&" => Operators.and
case "&=" => Operators.assignmentAnd
case "&&=" => Operators.assignmentAnd
case "|=" => Operators.assignmentOr
case "|" => Operators.or
case "||=" => Operators.assignmentOr
case "||" => Operators.logicalOr
case "^=" => Operators.assignmentXor
case "&<<" => Operators.shiftLeft
case "<<" => Operators.shiftLeft
case "&>>=" => Operators.assignmentArithmeticShiftRight
case "&>>" => Operators.arithmeticShiftRight
case ">>" => Operators.arithmeticShiftRight
case "&<<=" => Operators.assignmentShiftLeft
case "<<=" => Operators.assignmentShiftLeft
case ">>=" => Operators.assignmentArithmeticShiftRight
case ">>>=" => Operators.assignmentLogicalShiftRight
case "??=" => Operators.notNullAssert
case "<=" => Operators.lessEqualsThan
case ">=" => Operators.greaterEqualsThan
case "<" => Operators.lessThan
case ">" => Operators.greaterThan
case "==" => Operators.equals
case "!=" => Operators.notEquals
case "===" => Operators.equals
case "!==" => Operators.notEquals
case "&+" => Operators.plus
case "+" => Operators.plus
case "&+=" => Operators.assignmentPlus
case "&-" => Operators.minus
case "-" => Operators.minus
case "&-=" => Operators.assignmentMinus
case "&/" => Operators.division
case "/" => Operators.division
case "&/=" => Operators.assignmentDivision
case "&*" => Operators.multiplication
case "*" => Operators.multiplication
case "&*=" => Operators.assignmentMultiplication
case "..<" | ">.." | "..." => Operators.range
case "%" => Operators.modulo
case "!" => Operators.logicalNot
case "^" => Operators.xor
case "??" => Operators.elvis
case "~=" => Operators.in
case _ =>
notHandledYet(node.operator)
"<operator>.unknown"
}

val op = Defines.InfixOperatorMap(code(node.operator))
val lhsAst = astForNodeWithFunctionReference(node.leftOperand)
val rhsAst = astForNodeWithFunctionReference(node.rightOperand)
val callNode_ = callNode(node, code(node), op, DispatchTypes.STATIC_DISPATCH)
Expand Down Expand Up @@ -357,7 +302,7 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {

}

private def astForMissingExprSyntax(node: MissingExprSyntax): Ast = Ast()
private def astForMissingExprSyntax(@unused node: MissingExprSyntax): Ast = Ast()

private def astForNilLiteralExprSyntax(node: NilLiteralExprSyntax): Ast = {
Ast(literalNode(node, code(node), Option(Defines.Nil)))
Expand Down Expand Up @@ -422,41 +367,16 @@ trait AstForExprSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
}

private def astForPostfixOperatorExprSyntax(node: PostfixOperatorExprSyntax): Ast = {
val operatorMethod = code(node.operator) match {
case "++" => Operators.postIncrement
case "--" => Operators.postDecrement
case "^" => Operators.xor
case "<" => Operators.lessThan
case ">" => Operators.greaterThan
case "..." => "<operator>.splat"
case _ =>
notHandledYet(node.operator)
"<operator>.unknown"
}
val unaryCall = callNode(node, code(node), operatorMethod, operatorMethod, DispatchTypes.STATIC_DISPATCH)
val expressionAst = astForNodeWithFunctionReference(node.expression)
val operatorMethod = Defines.PostfixOperatorMap(code(node.operator))
val unaryCall = callNode(node, code(node), operatorMethod, operatorMethod, DispatchTypes.STATIC_DISPATCH)
val expressionAst = astForNodeWithFunctionReference(node.expression)
callAst(unaryCall, List(expressionAst))
}

private def astForPrefixOperatorExprSyntax(node: PrefixOperatorExprSyntax): Ast = {
val operatorMethod = code(node.operator) match {
case "-" => Operators.preDecrement
case "+" => Operators.preIncrement
case "~" => Operators.not
case "!" => Operators.logicalNot
case "..<" => Operators.lessThan
case "<" => Operators.lessThan
case "..>" => Operators.greaterThan
case ">" => Operators.greaterThan
case "==" => Operators.equals
case "%" => Operators.modulo
case "..." => "<operator>.splat"
case _ =>
notHandledYet(node.operator)
"<operator>.unknown"
}
val unaryCall = callNode(node, code(node), operatorMethod, operatorMethod, DispatchTypes.STATIC_DISPATCH)
val expressionAst = astForNodeWithFunctionReference(node.expression)
val operatorMethod = Defines.PrefixOperatorMap(code(node.operator))
val unaryCall = callNode(node, code(node), operatorMethod, operatorMethod, DispatchTypes.STATIC_DISPATCH)
val expressionAst = astForNodeWithFunctionReference(node.expression)
callAst(unaryCall, List(expressionAst))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import io.joern.swiftsrc2cpg.passes.Defines
import io.joern.x2cpg.Ast
import io.joern.x2cpg.ValidationMode

import scala.annotation.unused

trait AstForSwiftTokenCreator(implicit withSchemaValidation: ValidationMode) { this: AstCreator =>

private def astForArrowToken(node: arrow): Ast = Ast()
private def astForAtSignToken(node: atSign): Ast = Ast()
private def astForBackslashToken(node: backslash): Ast = Ast()
private def astForBacktickToken(node: backtick): Ast = Ast()
private def astForBinaryOperatorToken(node: binaryOperator): Ast = Ast()
private def astForColonToken(node: colon): Ast = Ast()
private def astForCommaToken(node: comma): Ast = Ast()
private def astForDollarIdentifierToken(node: dollarIdentifier): Ast = Ast()
private def astForEllipsisToken(node: ellipsis): Ast = Ast()
private def astForEndOfFileToken(node: endOfFile): Ast = Ast()
private def astForEqualToken(node: equal): Ast = Ast()
private def astForExclamationMarkToken(node: exclamationMark): Ast = Ast()
private def astForArrowToken(@unused node: arrow): Ast = Ast()
private def astForAtSignToken(@unused node: atSign): Ast = Ast()
private def astForBackslashToken(@unused node: backslash): Ast = Ast()
private def astForBacktickToken(@unused node: backtick): Ast = Ast()
private def astForBinaryOperatorToken(@unused node: binaryOperator): Ast = Ast()
private def astForColonToken(@unused node: colon): Ast = Ast()
private def astForCommaToken(@unused node: comma): Ast = Ast()
private def astForDollarIdentifierToken(@unused node: dollarIdentifier): Ast = Ast()
private def astForEllipsisToken(@unused node: ellipsis): Ast = Ast()
private def astForEndOfFileToken(@unused node: endOfFile): Ast = Ast()
private def astForEqualToken(@unused node: equal): Ast = Ast()
private def astForExclamationMarkToken(@unused node: exclamationMark): Ast = Ast()

private def astForFloatLiteralToken(node: floatLiteral): Ast = {
Ast(literalNode(node, code(node), Option(Defines.Float)))
Expand All @@ -29,7 +31,7 @@ trait AstForSwiftTokenCreator(implicit withSchemaValidation: ValidationMode) { t
Ast(identifierNode(node, name, name, Defines.Any))
}

private def astForInfixQuestionMarkToken(node: infixQuestionMark): Ast = Ast()
private def astForInfixQuestionMarkToken(@unused node: infixQuestionMark): Ast = Ast()

protected def astForIntegerLiteralToken(node: integerLiteral): Ast = {
Ast(literalNode(node, code(node), Option(Defines.Int)))
Expand All @@ -42,39 +44,39 @@ trait AstForSwiftTokenCreator(implicit withSchemaValidation: ValidationMode) { t
case _ => notHandledYet(node)
}

private def astForLeftAngleToken(node: leftAngle): Ast = Ast()
private def astForLeftBraceToken(node: leftBrace): Ast = Ast()
private def astForLeftParenToken(node: leftParen): Ast = Ast()
private def astForLeftSquareToken(node: leftSquare): Ast = Ast()
private def astForMultilineStringQuoteToken(node: multilineStringQuote): Ast = Ast()
private def astForPeriodToken(node: period): Ast = Ast()
private def astForPostfixOperatorToken(node: postfixOperator): Ast = Ast()
private def astForPostfixQuestionMarkToken(node: postfixQuestionMark): Ast = Ast()
private def astForPoundToken(node: pound): Ast = Ast()
private def astForPoundAvailableToken(node: poundAvailable): Ast = Ast()
private def astForPoundElseToken(node: poundElse): Ast = Ast()
private def astForPoundElseifToken(node: poundElseif): Ast = Ast()
private def astForPoundEndifToken(node: poundEndif): Ast = Ast()
private def astForPoundIfToken(node: poundIf): Ast = Ast()
private def astForPoundSourceLocationToken(node: poundSourceLocation): Ast = Ast()
private def astForPoundUnavailableToken(node: poundUnavailable): Ast = Ast()
private def astForPrefixAmpersandToken(node: prefixAmpersand): Ast = Ast()
private def astForPrefixOperatorToken(node: prefixOperator): Ast = Ast()
private def astForRawStringPoundDelimiterToken(node: rawStringPoundDelimiter): Ast = Ast()
private def astForRegexLiteralPatternToken(node: regexLiteralPattern): Ast = Ast()
private def astForRegexPoundDelimiterToken(node: regexPoundDelimiter): Ast = Ast()
private def astForRegexSlashToken(node: regexSlash): Ast = Ast()
private def astForRightAngleToken(node: rightAngle): Ast = Ast()
private def astForRightBraceToken(node: rightBrace): Ast = Ast()
private def astForRightParenToken(node: rightParen): Ast = Ast()
private def astForRightSquareToken(node: rightSquare): Ast = Ast()
private def astForSemicolonToken(node: semicolon): Ast = Ast()
private def astForshebangToken(node: shebang): Ast = Ast()
private def astForSingleQuoteToken(node: singleQuote): Ast = Ast()
private def astForStringQuoteToken(node: stringQuote): Ast = Ast()
private def astForStringSegmentToken(node: stringSegment): Ast = Ast()
private def astForUnknownToken(node: unknown): Ast = Ast()
private def astForWildcardToken(node: wildcard): Ast = Ast()
private def astForLeftAngleToken(@unused node: leftAngle): Ast = Ast()
private def astForLeftBraceToken(@unused node: leftBrace): Ast = Ast()
private def astForLeftParenToken(@unused node: leftParen): Ast = Ast()
private def astForLeftSquareToken(@unused node: leftSquare): Ast = Ast()
private def astForMultilineStringQuoteToken(@unused node: multilineStringQuote): Ast = Ast()
private def astForPeriodToken(@unused node: period): Ast = Ast()
private def astForPostfixOperatorToken(@unused node: postfixOperator): Ast = Ast()
private def astForPostfixQuestionMarkToken(@unused node: postfixQuestionMark): Ast = Ast()
private def astForPoundToken(@unused node: pound): Ast = Ast()
private def astForPoundAvailableToken(@unused node: poundAvailable): Ast = Ast()
private def astForPoundElseToken(@unused node: poundElse): Ast = Ast()
private def astForPoundElseifToken(@unused node: poundElseif): Ast = Ast()
private def astForPoundEndifToken(@unused node: poundEndif): Ast = Ast()
private def astForPoundIfToken(@unused node: poundIf): Ast = Ast()
private def astForPoundSourceLocationToken(@unused node: poundSourceLocation): Ast = Ast()
private def astForPoundUnavailableToken(@unused node: poundUnavailable): Ast = Ast()
private def astForPrefixAmpersandToken(@unused node: prefixAmpersand): Ast = Ast()
private def astForPrefixOperatorToken(@unused node: prefixOperator): Ast = Ast()
private def astForRawStringPoundDelimiterToken(@unused node: rawStringPoundDelimiter): Ast = Ast()
private def astForRegexLiteralPatternToken(@unused node: regexLiteralPattern): Ast = Ast()
private def astForRegexPoundDelimiterToken(@unused node: regexPoundDelimiter): Ast = Ast()
private def astForRegexSlashToken(@unused node: regexSlash): Ast = Ast()
private def astForRightAngleToken(@unused node: rightAngle): Ast = Ast()
private def astForRightBraceToken(@unused node: rightBrace): Ast = Ast()
private def astForRightParenToken(@unused node: rightParen): Ast = Ast()
private def astForRightSquareToken(@unused node: rightSquare): Ast = Ast()
private def astForSemicolonToken(@unused node: semicolon): Ast = Ast()
private def astForshebangToken(@unused node: shebang): Ast = Ast()
private def astForSingleQuoteToken(@unused node: singleQuote): Ast = Ast()
private def astForStringQuoteToken(@unused node: stringQuote): Ast = Ast()
private def astForStringSegmentToken(@unused node: stringSegment): Ast = Ast()
private def astForUnknownToken(@unused node: unknown): Ast = Ast()
private def astForWildcardToken(@unused node: wildcard): Ast = Ast()

protected def astForSwiftToken(swiftToken: SwiftToken): Ast = swiftToken match {
case node: arrow => astForArrowToken(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import io.shiftleft.codepropertygraph.generated.DispatchTypes
import io.shiftleft.codepropertygraph.generated.Operators
import io.shiftleft.codepropertygraph.generated.nodes.File.PropertyDefaults

import scala.annotation.unused

trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this: AstCreator =>

private def astForAccessorBlockSyntax(node: AccessorBlockSyntax): Ast = {
Expand Down Expand Up @@ -273,7 +275,7 @@ trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this:
private def astForMatchingPatternConditionSyntax(node: MatchingPatternConditionSyntax): Ast = {
val lhsAst = astForNode(node.pattern)
val rhsAst = astForNode(node.initializer.value)
val callNode_ = callNode(node, code(node), Operators.assignment, DispatchTypes.STATIC_DISPATCH)
val callNode_ = callNode(node, code(node), Operators.equals, DispatchTypes.STATIC_DISPATCH)
val argAsts = List(lhsAst, rhsAst)
callAst(callNode_, argAsts)
}
Expand Down Expand Up @@ -376,7 +378,7 @@ trait AstForSyntaxCreator(implicit withSchemaValidation: ValidationMode) { this:
private def astForSwitchCaseLabelSyntax(node: SwitchCaseLabelSyntax): Ast = notHandledYet(node)
private def astForSwitchCaseSyntax(node: SwitchCaseSyntax): Ast = notHandledYet(node)

private def astForSwitchDefaultLabelSyntax(node: SwitchDefaultLabelSyntax): Ast = Ast()
private def astForSwitchDefaultLabelSyntax(@unused node: SwitchDefaultLabelSyntax): Ast = Ast()

private def astForTuplePatternElementSyntax(node: TuplePatternElementSyntax): Ast = notHandledYet(node)
private def astForTupleTypeElementSyntax(node: TupleTypeElementSyntax): Ast = notHandledYet(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import io.joern.x2cpg.ValidationMode
import io.shiftleft.codepropertygraph.generated.nodes.NewTypeDecl
import io.shiftleft.codepropertygraph.generated.EdgeTypes

import scala.annotation.unused

trait AstForTypeSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
this: AstCreator =>

Expand Down Expand Up @@ -66,7 +68,7 @@ trait AstForTypeSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
astForTypeSyntax(node.baseType)
}

private def astForMissingTypeSyntax(node: MissingTypeSyntax): Ast = Ast()
private def astForMissingTypeSyntax(@unused node: MissingTypeSyntax): Ast = Ast()

private def astForNamedOpaqueReturnTypeSyntax(node: NamedOpaqueReturnTypeSyntax): Ast = {
astForTypeSyntax(node.`type`)
Expand Down
Loading

0 comments on commit ce9ae68

Please sign in to comment.