Skip to content

Commit

Permalink
Lowering loop do-block statements as do-while loops
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBakerEffendi committed May 2, 2024
1 parent 109a056 commit 291a003
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,25 @@ class RubyNodeCreator extends RubyParserBaseVisitor[RubyNode] {
override def visitMethodCallWithBlockExpression(ctx: RubyParser.MethodCallWithBlockExpressionContext): RubyNode = {
ctx.methodIdentifier().getText match {
case Defines.Proc | Defines.Lambda => ProcOrLambdaExpr(visit(ctx.block()).asInstanceOf[Block])(ctx.toTextSpan)
case Defines.Loop =>
visit(ctx.block())
DoWhileExpression(
SimpleIdentifier(Option(Defines.getBuiltInType(Defines.TrueClass)))(
ctx.methodIdentifier().toTextSpan.spanStart("true")
),
visit(ctx.block())
)(ctx.toTextSpan)
case _ =>
SimpleCallWithBlock(visit(ctx.methodIdentifier()), List(), visit(ctx.block()).asInstanceOf[Block])(
ctx.toTextSpan
)
}
}

override def visitDoBlockBlock(ctx: RubyParser.DoBlockBlockContext): RubyNode = {
visit(ctx.doBlock().bodyStatement())
}

override def visitLambdaExpression(ctx: RubyParser.LambdaExpressionContext): RubyNode = {
val parameters = Option(ctx.parameterList()).fold(List())(_.parameters).map(visit)
val body = visit(ctx.block())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object Defines {
val Lambda: String = "lambda"
val Proc: String = "proc"
val This: String = "this"
val Loop: String = "loop"

val Program: String = ":program"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class MethodTests extends RubyCode2CpgFixture(withPostProcessing = true, withDat
sink.reachableByFlows(src).l.size shouldBe 2
}

// Works in deprecated
"Data flow through do-while loop" ignore {
"Data flow through do-while loop" in {
val cpg = code("""
|x = 0
|num = -1
Expand All @@ -42,7 +41,7 @@ class MethodTests extends RubyCode2CpgFixture(withPostProcessing = true, withDat

val source = cpg.identifier.name("x").l
val sink = cpg.call.name("puts").l
sink.reachableByFlows(source).l.size shouldBe 2
sink.reachableByFlows(source).size shouldBe 5
}

"Data flow through methodOnlyIdentifier usage" in {
Expand Down

0 comments on commit 291a003

Please sign in to comment.