Skip to content

Commit

Permalink
[rubysrc2cpg] Revert Refactoring Commits to Preserve Stability (joern…
Browse files Browse the repository at this point in the history
…io#3732)

* Revert "[rubysrc2cpg] Do-Block Function as Conditional (joernio#3729)"

This reverts commit c726bb5.

* Revert "[rubysrc2cpg] Chained Higher Order Methods (joernio#3727)"

This reverts commit 39dd417.

* Revert "[rubysrc2cpg] Fixed Duplicate Do-Blocks Due to Side-Effect (joernio#3720)"

This reverts commit 3f0e745.

* Revert "[rubysrc2cpg] Comprehensive follow up to joernio#3708's fix (joernio#3714)"

This reverts commit 662bd23.

* Revert "[rubysrc2cpg] Fixed Bug with Higher-Order Functions (joernio#3708)"

This reverts commit 3e9ea97.

* Revert "[rubysrc2cpg] General Do-Block Function Fixes (joernio#3676)"

This reverts commit 4ef5cdc.
  • Loading branch information
khemrajrathore authored Oct 11, 2023
1 parent d011067 commit 9644ce8
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 551 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -209,37 +209,6 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As

}

implicit class AstIterExt(a: Iterable[Ast]) {

/** Partitions a sequence of Ast objects into those with roots start with an expression, and those that don't.
* @return
* a tuple of sequences where the first has expression roots and the second does not.
*/
def partitionExprAst: (Seq[Ast], Seq[Ast]) = {
val (as, bs) = a.partition(_.root match
case Some(_: ExpressionNew) => true
case _ => false
)
(as.toSeq, bs.toSeq)
}

/** Partitions a sequence of Ast objects into the boilerplate for do-block functions and the call node at the end.
*
* @return
* a tuple where the first element is the closure boilerplate and the latter is the last expression.
*/
def partitionClosureFromExpr: (Seq[Ast], Option[Ast]) = {
val (as, bs) = a.partition(_.root match
case Some(_: NewMethod) => true
case Some(_: NewTypeDecl) => true
case Some(x: NewCall) if x.name.startsWith(Operators.assignment) => true
case _ => false
)
(as.toSeq, bs.lastOption)
}

}

}

object RubyOperators {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,55 +132,4 @@ trait AstForControlStructuresCreator(implicit withSchemaValidation: ValidationMo
tryCatchAst(tryNode, tryBodyAst, catchAsts, finallyAst)
}

protected def astForUntilExpression(ctx: UntilExpressionContext): Seq[Ast] = {
val (boilerplate, exprAst) = astForExpressionOrCommand(ctx.expressionOrCommand()).partitionClosureFromExpr
val bodyAst = astForCompoundStatement(ctx.doClause().compoundStatement())
// TODO: testAst should be negated if it's going to be modelled as a while stmt.
boilerplate :+ whileAst(exprAst, bodyAst, Some(text(ctx)), line(ctx), column(ctx))
}

protected def astForForExpression(ctx: ForExpressionContext): Seq[Ast] = {
val forVarAst = astForForVariableContext(ctx.forVariable())
val (boilerplate, forExprAst) = astForExpressionOrCommand(ctx.expressionOrCommand()).partitionClosureFromExpr
val forBodyAst = astForCompoundStatement(ctx.doClause().compoundStatement())
// TODO: for X in Y is not properly modelled by while Y
val forRootAst = whileAst(forExprAst, forBodyAst, Some(text(ctx)), line(ctx), column(ctx))
boilerplate :+ forVarAst.headOption.map(forRootAst.withChild).getOrElse(forRootAst)
}

private def astForForVariableContext(ctx: ForVariableContext): Seq[Ast] = {
if (ctx.singleLeftHandSide() != null) {
astForSingleLeftHandSideContext(ctx.singleLeftHandSide())
} else if (ctx.multipleLeftHandSide() != null) {
astForMultipleLeftHandSideContext(ctx.multipleLeftHandSide())
} else {
Seq(Ast())
}
}

protected def astForWhileExpression(ctx: WhileExpressionContext): Seq[Ast] = {
val (boilerplate, exprAst) = astForExpressionOrCommand(ctx.expressionOrCommand()).partitionClosureFromExpr
val bodyAst = astForCompoundStatement(ctx.doClause().compoundStatement())
boilerplate :+ whileAst(exprAst, bodyAst, Some(text(ctx)), line(ctx), column(ctx))
}

protected def astForIfExpression(ctx: IfExpressionContext): Seq[Ast] = {
val (boilerplate, exprAst) = astForExpressionOrCommand(ctx.expressionOrCommand()).partitionClosureFromExpr
val thenAst = astForCompoundStatement(ctx.thenClause().compoundStatement())
val elsifAsts = Option(ctx.elsifClause).map(_.asScala).getOrElse(Seq()).flatMap(astForElsifClause)
val elseAst = Option(ctx.elseClause()).map(ctx => astForCompoundStatement(ctx.compoundStatement())).getOrElse(Seq())
val ifNode = controlStructureNode(ctx, ControlStructureTypes.IF, text(ctx))
boilerplate :+ controlStructureAst(ifNode, exprAst)
.withChildren(thenAst)
.withChildren(elsifAsts.toSeq)
.withChildren(elseAst)
}

private def astForElsifClause(ctx: ElsifClauseContext): Seq[Ast] = {
val ifNode = controlStructureNode(ctx, ControlStructureTypes.IF, text(ctx))
val (boilerplate, exprAst) = astForExpressionOrCommand(ctx.expressionOrCommand()).partitionClosureFromExpr
val bodyAst = astForCompoundStatement(ctx.thenClause().compoundStatement())
boilerplate :+ controlStructureAst(ifNode, exprAst, bodyAst)
}

}
Loading

0 comments on commit 9644ce8

Please sign in to comment.