Skip to content

Commit

Permalink
[x2cpg] Deprecated tryCatchAst with order (#4644)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser committed Jun 7, 2024
1 parent 27c45e6 commit 7200ad4
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,21 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t

private def astForTryStatement(tryStmt: ICPPASTTryBlockStatement): Ast = {
val tryNode = controlStructureNode(tryStmt, ControlStructureTypes.TRY, "try")
val body = nullSafeAst(tryStmt.getTryBody, 1)
val catches = tryStmt.getCatchHandlers.zipWithIndex.map { case (h, index) =>
astForCatchHandler(h, index + 2)
}.toIndexedSeq
Ast(tryNode).withChildren(body).withChildren(catches)
val bodyAst = nullSafeAst(tryStmt.getTryBody) match {
case Nil => Ast()
case elem :: Nil => elem
case elements =>
setArgumentIndices(elements)
blockAst(blockNode(tryStmt.getTryBody)).withChildren(elements)
}
val catchAsts = tryStmt.getCatchHandlers.toSeq.map(astForCatchHandler)
tryCatchAst(tryNode, bodyAst, catchAsts, None)
}

private def astForCatchHandler(catchHandler: ICPPASTCatchHandler, argIndex: Int): Ast = {
val catchNode =
controlStructureNode(catchHandler, ControlStructureTypes.CATCH, "catch").order(argIndex).argumentIndex(argIndex)
val declAst = nullSafeAst(catchHandler.getDeclaration)
val bodyAst = nullSafeAst(catchHandler.getCatchBody)
private def astForCatchHandler(catchHandler: ICPPASTCatchHandler): Ast = {
val catchNode = controlStructureNode(catchHandler, ControlStructureTypes.CATCH, "catch")
val declAst = nullSafeAst(catchHandler.getDeclaration)
val bodyAst = nullSafeAst(catchHandler.getCatchBody)
Ast(catchNode).withChildren(declAst).withChildren(bodyAst)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import io.shiftleft.codepropertygraph.generated.nodes.NewControlStructure
import io.shiftleft.codepropertygraph.generated.nodes.NewIdentifier

import scala.::
import scala.util.Success
import scala.util.Try

trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { this: AstCreator =>
Expand Down Expand Up @@ -244,43 +243,29 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForTryStatement(tryStmt: DotNetNodeInfo): Seq[Ast] = {
val tryNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.TRY)
.code(code(tryStmt))
.lineNumber(line(tryStmt))
.columnNumber(column(tryStmt))
val tryNode = controlStructureNode(tryStmt, ControlStructureTypes.TRY, code(tryStmt))
val tryBlockNodeInfo = createDotNetNodeInfo(tryStmt.json(ParserKeys.Block))
val tryAst = astForBlock(tryBlockNodeInfo, Option(code(tryBlockNodeInfo)))

val catchAsts = Try(tryStmt.json(ParserKeys.Catches))
.map(_.arr.toSeq)
.map { c =>
c.map { value =>
val nodeInfo = createDotNetNodeInfo(value)
val catchNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.CATCH)
.code(code(nodeInfo))
.lineNumber(line(nodeInfo))
.columnNumber(column(nodeInfo))
val children = astForNode(nodeInfo)
val nodeInfo = createDotNetNodeInfo(value)
val catchNode = controlStructureNode(nodeInfo, ControlStructureTypes.CATCH, code(nodeInfo))
val children = astForNode(nodeInfo)
Ast(catchNode).withChildren(children)
}
}
.getOrElse(Seq.empty)

val finallyAst = Try(createDotNetNodeInfo(tryStmt.json(ParserKeys.Finally))) match {
case Success(finallyNodeInfo) =>
val finallyNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.FINALLY)
.code(code(finallyNodeInfo))
.lineNumber(line(finallyNodeInfo))
.columnNumber(column(finallyNodeInfo))
val finallyClauseAst = astForFinallyClause(finallyNodeInfo)
Ast(finallyNode).withChildren(finallyClauseAst)
case _ => Ast()
val finallyAst = Try(createDotNetNodeInfo(tryStmt.json(ParserKeys.Finally))).toOption.map { finallyNodeInfo =>
val finallyNode = controlStructureNode(finallyNodeInfo, ControlStructureTypes.FINALLY, code(finallyNodeInfo))
val finallyClauseAst = astForFinallyClause(finallyNodeInfo)
Ast(finallyNode).withChildren(finallyClauseAst)
}

val controlStructureAst = tryCatchAst(tryNode, tryAst, catchAsts, Option(finallyAst))
val controlStructureAst = tryCatchAst(tryNode, tryAst, catchAsts, finallyAst)
Seq(controlStructureAst)
}

Expand All @@ -295,11 +280,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
* Thus, this is lowered as a try-finally, with finally making a call to `Dispose` on the declared variable.
*/
private def astForUsingStatement(usingStmt: DotNetNodeInfo): Seq[Ast] = {
val tryNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.TRY)
.code(code(usingStmt))
.lineNumber(line(usingStmt))
.columnNumber(column(usingStmt))
val tryNode = controlStructureNode(usingStmt, ControlStructureTypes.TRY, code(usingStmt))
val tryNodeInfo = createDotNetNodeInfo(usingStmt.json(ParserKeys.Statement))
val tryAst = astForBlock(tryNodeInfo, Option("try"))
val declNode = createDotNetNodeInfo(usingStmt.json(ParserKeys.Declaration))
Expand All @@ -319,11 +300,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
)
val disposeAst = callAst(disposeCall, receiver = Option(Ast(id)))
val childrenAst = Ast(blockNode(usingStmt)).withChild(disposeAst)
val finallyNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.FINALLY)
.code("finally")
.lineNumber(line(usingStmt))
.columnNumber(column(usingStmt))
val finallyNode = controlStructureNode(usingStmt, ControlStructureTypes.FINALLY, "finally")
Ast(finallyNode).withChild(childrenAst)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,35 +281,19 @@ trait AstForSimpleStatementsCreator { this: AstCreator =>
}

private[statements] def astsForTry(stmt: TryStmt): Seq[Ast] = {
val tryNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.TRY)
.code("try")
.lineNumber(line(stmt))
.columnNumber(column(stmt))

val tryNode = controlStructureNode(stmt, ControlStructureTypes.TRY, "try")
val resources = stmt.getResources.asScala.flatMap(astsForExpression(_, expectedType = ExpectedType.empty)).toList

val tryAst = astForBlockStatement(stmt.getTryBlock, codeStr = "try")
val catchAsts = stmt.getCatchClauses.asScala.toList.map { catchClause =>
val catchNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.CATCH)
.code("catch")
.lineNumber(line(catchClause))
.columnNumber(column(catchClause))
val catchNode = controlStructureNode(catchClause, ControlStructureTypes.CATCH, "catch")
Ast(catchNode).withChild(astForCatchClause(catchClause))
}
val finallyAst = stmt.getFinallyBlock.toScala.map { finallyBlock =>
val finallyNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.FINALLY)
.code("finally")
.lineNumber(line(finallyBlock))
.columnNumber(column(finallyBlock))
val finallyNode = controlStructureNode(finallyBlock, ControlStructureTypes.FINALLY, "finally")
Ast(finallyNode).withChild(astForBlockStatement(finallyBlock, "finally"))
}.toList

val childrenAsts = tryAst +: (catchAsts ++ finallyAst)
setArgumentIndices(childrenAsts)
val controlStructureAst = Ast(tryNode).withChildren(childrenAsts)
}
val controlStructureAst = tryCatchAst(tryNode, tryAst, catchAsts, finallyAst)
resources.appended(controlStructureAst)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.shiftleft.codepropertygraph.generated.DispatchTypes
import io.shiftleft.codepropertygraph.generated.EdgeTypes
import io.shiftleft.codepropertygraph.generated.Operators
import io.shiftleft.codepropertygraph.generated.nodes.NewJumpLabel
import io.shiftleft.codepropertygraph.generated.nodes.NewJumpTarget
import ujson.Obj
import ujson.Value

Expand Down Expand Up @@ -102,37 +101,31 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForTryStatement(tryStmt: BabelNodeInfo): Ast = {
val tryNode = createControlStructureNode(tryStmt, ControlStructureTypes.TRY)
val tryNode = controlStructureNode(tryStmt, ControlStructureTypes.TRY, code(tryStmt))
val bodyAst = astForNodeWithFunctionReference(tryStmt.json("block"))
val catchAst = safeObj(tryStmt.json, "handler")
val catchAst = safeObj(tryStmt.json, "handler").toList
.map { handler =>
val catchNodeInfo = createBabelNodeInfo(Obj(handler))
val catchNode = createControlStructureNode(catchNodeInfo, ControlStructureTypes.CATCH)
val catchNode = controlStructureNode(catchNodeInfo, ControlStructureTypes.CATCH, code(catchNodeInfo))
val catchAst = astForCatchClause(catchNodeInfo)
Ast(catchNode).withChild(catchAst)
}
.getOrElse(Ast())
val finalizerAst = safeObj(tryStmt.json, "finalizer")
.map { finalizer =>
val finalNodeInfo = createBabelNodeInfo(Obj(finalizer))
val finalNode = createControlStructureNode(finalNodeInfo, ControlStructureTypes.FINALLY)
val finalNode = controlStructureNode(finalNodeInfo, ControlStructureTypes.FINALLY, code(finalNodeInfo))
val finalAst = astForNodeWithFunctionReference(finalNodeInfo.json)
Ast(finalNode).withChild(finalAst)
}
.getOrElse(Ast())
val childrenAsts = List(bodyAst, catchAst, finalizerAst)
setArgumentIndices(childrenAsts)
Ast(tryNode).withChildren(childrenAsts)
tryCatchAst(tryNode, bodyAst, catchAst, finalizerAst)
}

def astForIfStatement(ifStmt: BabelNodeInfo): Ast = {
val ifNode = createControlStructureNode(ifStmt, ControlStructureTypes.IF)
val ifNode = controlStructureNode(ifStmt, ControlStructureTypes.IF, code(ifStmt))
val testAst = astForNodeWithFunctionReference(ifStmt.json("test"))
val consequentAst = astForNodeWithFunctionReference(ifStmt.json("consequent"))
val alternateAst = safeObj(ifStmt.json, "alternate")
.map { alternate =>
astForNodeWithFunctionReference(Obj(alternate))
}
.map { alternate => astForNodeWithFunctionReference(Obj(alternate)) }
.getOrElse(Ast())
// The semantics of if statement children is partially defined by their order value.
// The consequentAst must have order == 2 and alternateAst must have order == 3.
Expand All @@ -149,7 +142,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForDoWhileStatement(doWhileStmt: BabelNodeInfo): Ast = {
val whileNode = createControlStructureNode(doWhileStmt, ControlStructureTypes.DO)
val whileNode = controlStructureNode(doWhileStmt, ControlStructureTypes.DO, code(doWhileStmt))
val testAst = astForNodeWithFunctionReference(doWhileStmt.json("test"))
val bodyAst = astForNodeWithFunctionReference(doWhileStmt.json("body"))
// The semantics of do-while statement children is partially defined by their order value.
Expand All @@ -161,7 +154,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForWhileStatement(whileStmt: BabelNodeInfo): Ast = {
val whileNode = createControlStructureNode(whileStmt, ControlStructureTypes.WHILE)
val whileNode = controlStructureNode(whileStmt, ControlStructureTypes.WHILE, code(whileStmt))
val testAst = astForNodeWithFunctionReference(whileStmt.json("test"))
val bodyAst = astForNodeWithFunctionReference(whileStmt.json("body"))
// The semantics of while statement children is partially defined by their order value.
Expand All @@ -173,7 +166,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForForStatement(forStmt: BabelNodeInfo): Ast = {
val forNode = createControlStructureNode(forStmt, ControlStructureTypes.FOR)
val forNode = controlStructureNode(forStmt, ControlStructureTypes.FOR, code(forStmt))
val initAst = safeObj(forStmt.json, "init")
.map { init =>
astForNodeWithFunctionReference(Obj(init))
Expand Down Expand Up @@ -202,13 +195,8 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForLabeledStatement(labelStmt: BabelNodeInfo): Ast = {
val labelName = code(labelStmt.json("label"))
val labeledNode = NewJumpTarget()
.parserTypeName(labelStmt.node.toString)
.name(labelName)
.code(s"$labelName:")
.lineNumber(labelStmt.lineNumber)
.columnNumber(labelStmt.columnNumber)
val labelName = code(labelStmt.json("label"))
val labeledNode = jumpTargetNode(labelStmt, labelName, s"$labelName:", Option(labelStmt.node.toString))

val blockNode = createBlockNode(labelStmt)
scope.pushNewBlockScope(blockNode)
Expand All @@ -223,26 +211,24 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForBreakStatement(breakStmt: BabelNodeInfo): Ast = {
val labelAst = safeObj(breakStmt.json, "label")
.map { label =>
val labelNode = Obj(label)
val labelCode = code(labelNode)
Ast(
NewJumpLabel()
.parserTypeName(breakStmt.node.toString)
.name(labelCode)
.code(labelCode)
.lineNumber(breakStmt.lineNumber)
.columnNumber(breakStmt.columnNumber)
.order(1)
)
}
.getOrElse(Ast())
Ast(createControlStructureNode(breakStmt, ControlStructureTypes.BREAK)).withChild(labelAst)
val labelAst = safeObj(breakStmt.json, "label").toList.map { label =>
val labelNode = Obj(label)
val labelCode = code(labelNode)
Ast(
NewJumpLabel()
.parserTypeName(breakStmt.node.toString)
.name(labelCode)
.code(labelCode)
.lineNumber(breakStmt.lineNumber)
.columnNumber(breakStmt.columnNumber)
.order(1)
)
}
Ast(controlStructureNode(breakStmt, ControlStructureTypes.BREAK, code(breakStmt))).withChildren(labelAst)
}

protected def astForContinueStatement(continueStmt: BabelNodeInfo): Ast = {
val labelAst = safeObj(continueStmt.json, "label")
val labelAst = safeObj(continueStmt.json, "label").toList
.map { label =>
val labelNode = Obj(label)
val labelCode = code(labelNode)
Expand All @@ -256,8 +242,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
.order(1)
)
}
.getOrElse(Ast())
Ast(createControlStructureNode(continueStmt, ControlStructureTypes.CONTINUE)).withChild(labelAst)
Ast(controlStructureNode(continueStmt, ControlStructureTypes.CONTINUE, code(continueStmt))).withChildren(labelAst)
}

protected def astForThrowStatement(throwStmt: BabelNodeInfo): Ast = {
Expand All @@ -276,7 +261,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForSwitchStatement(switchStmt: BabelNodeInfo): Ast = {
val switchNode = createControlStructureNode(switchStmt, ControlStructureTypes.SWITCH)
val switchNode = controlStructureNode(switchStmt, ControlStructureTypes.SWITCH, code(switchStmt))

// The semantics of switch statement children is partially defined by their order value.
// The blockAst must have order == 2. Only to avoid collision we set switchExpressionAst to 1
Expand All @@ -287,10 +272,8 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
val blockNode = createBlockNode(switchStmt).order(2)
scope.pushNewBlockScope(blockNode)
localAstParentStack.push(blockNode)

val casesAsts = switchStmt.json("cases").arr.flatMap(c => astsForSwitchCase(createBabelNodeInfo(c)))
setArgumentIndices(casesAsts.toList)

scope.popScope()
localAstParentStack.pop()

Expand Down Expand Up @@ -364,7 +347,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
scope.addVariableReference(loopVariableName, loopVariableNode)

// while loop:
val whileLoopNode = createControlStructureNode(forInOfStmt, ControlStructureTypes.WHILE)
val whileLoopNode = controlStructureNode(forInOfStmt, ControlStructureTypes.WHILE, code(forInOfStmt))

// while loop test:
val testCallNode =
Expand Down Expand Up @@ -507,7 +490,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
scope.addVariableReference(resultName, resultNode)

// while loop:
val whileLoopNode = createControlStructureNode(forInOfStmt, ControlStructureTypes.WHILE)
val whileLoopNode = controlStructureNode(forInOfStmt, ControlStructureTypes.WHILE, code(forInOfStmt))

// while loop test:
val testCallNode =
Expand Down Expand Up @@ -658,7 +641,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

// while loop:
val whileLoopNode = createControlStructureNode(forInOfStmt, ControlStructureTypes.WHILE)
val whileLoopNode = controlStructureNode(forInOfStmt, ControlStructureTypes.WHILE, code(forInOfStmt))

// while loop test:
val testCallNode =
Expand Down Expand Up @@ -811,7 +794,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

// while loop:
val whileLoopNode = createControlStructureNode(forInOfStmt, ControlStructureTypes.WHILE)
val whileLoopNode = controlStructureNode(forInOfStmt, ControlStructureTypes.WHILE, code(forInOfStmt))

// while loop test:
val testCallNode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ trait AstNodeBuilder(implicit withSchemaValidation: ValidationMode) { this: AstC
.columnNumber(switchCase.columnNumber)
}

protected def createControlStructureNode(node: BabelNodeInfo, controlStructureType: String): NewControlStructure = {
val line = node.lineNumber
val column = node.columnNumber
val code = node.code
NewControlStructure()
.controlStructureType(controlStructureType)
.code(code)
.lineNumber(line)
.columnNumber(column)
}

protected def codeOf(node: NewNode): String = node match {
case astNodeNew: AstNodeNew => astNodeNew.code
case _ => ""
Expand Down
Loading

0 comments on commit 7200ad4

Please sign in to comment.