Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rubysrc2cpg: Crash fix in super call w/o arguments #3353

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}