Skip to content

Commit

Permalink
Crash fix (#3353)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-privado authored Jul 28, 2023
1 parent e12978b commit c3274b3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1500,31 +1500,25 @@ class AstCreator(
cmdAsts ++ mNameAsts ++ apAsts
}

def astForArgumentsWithParenthesesContext(ctx: ArgumentsWithParenthesesContext): Seq[Ast] = Option(ctx) match {
case Some(ctx) =>
ctx match {
case ctx: BlankArgsArgumentsWithParenthesesContext => Seq(Ast())
case ctx: ArgsOnlyArgumentsWithParenthesesContext => astForArguments(ctx.arguments())
case ctx: ExpressionsAndChainedCommandWithDoBlockArgumentsWithParenthesesContext =>
val expAsts = ctx
.expressions()
.expression
.asScala
.flatMap(exp => {
astForExpressionContext(exp)
})
.toSeq
val ccDoBlock = astForChainedCommandWithDoBlockContext(ctx.chainedCommandWithDoBlock())
expAsts ++ ccDoBlock
case ctx: ChainedCommandWithDoBlockOnlyArgumentsWithParenthesesContext =>
astForChainedCommandWithDoBlockContext(ctx.chainedCommandWithDoBlock())
case _ =>
logger.error(s"astForArgumentsWithParenthesesContext() $filename, ${ctx.getText} All contexts mismatched.")
Seq(Ast())
}
case None =>
logger.error(s"astForArgumentsWithParenthesesContext() $filename All contexts mismatched.")
Seq()
def astForArgumentsWithParenthesesContext(ctx: ArgumentsWithParenthesesContext): Seq[Ast] = ctx match {
case ctx: BlankArgsArgumentsWithParenthesesContext => Seq(Ast())
case ctx: ArgsOnlyArgumentsWithParenthesesContext => astForArguments(ctx.arguments())
case ctx: ExpressionsAndChainedCommandWithDoBlockArgumentsWithParenthesesContext =>
val expAsts = ctx
.expressions()
.expression
.asScala
.flatMap(exp => {
astForExpressionContext(exp)
})
.toSeq
val ccDoBlock = astForChainedCommandWithDoBlockContext(ctx.chainedCommandWithDoBlock())
expAsts ++ ccDoBlock
case ctx: ChainedCommandWithDoBlockOnlyArgumentsWithParenthesesContext =>
astForChainedCommandWithDoBlockContext(ctx.chainedCommandWithDoBlock())
case _ =>
logger.error(s"astForArgumentsWithParenthesesContext() $filename, ${ctx.getText} All contexts mismatched.")
Seq(Ast())
}

def astForBlockParametersContext(ctx: BlockParametersContext): Seq[Ast] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,12 @@ trait AstForExpressionsCreator { this: AstCreator =>
def astForRangeExpressionContext(ctx: RangeExpressionContext): Seq[Ast] =
Seq(astForBinaryOperatorExpression(ctx, Operators.range, ctx.expression().asScala))

protected def astForSuperExpression(ctx: SuperExpressionPrimaryContext): Ast =
astForSuperCall(ctx, astForArgumentsWithParenthesesContext(ctx.argumentsWithParentheses))
protected def astForSuperExpression(ctx: SuperExpressionPrimaryContext): Ast = {
val argsAst = Option(ctx.argumentsWithParentheses()) match
case Some(ctxArgs) => astForArgumentsWithParenthesesContext(ctxArgs)
case None => Seq()
astForSuperCall(ctx, argsAst)
}

// TODO: Handle the optional block.
// NOTE: `super` is quite complicated semantically speaking. We'll need
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,24 @@ class MiscTests extends RubyCode2CpgFixture {
cpg.namespace.name("SomeModule").size shouldBe 1
}
}

"CPG for code with super without arguments" should {
val cpg = code("""
|class Parent
| def foo(arg)
| end
|end
|
|class Child < Parent
| def foo(arg)
| super
| end
|end
|""".stripMargin)

"recognise all call nodes" in {
cpg.call.name("<operator>.super").size shouldBe 1
cpg.method.name("foo").size shouldBe 2
}
}
}

0 comments on commit c3274b3

Please sign in to comment.