Skip to content

Commit

Permalink
Consistent callAST creation across frontends (joernio#2265)
Browse files Browse the repository at this point in the history
* Replace `receiver` with `base` in `callAst`

* Adapt `callAst` to that used in jssrc2cpg

* Do not use `receiver` outside jssrc2cpg

* Use `callAst` in `jssrc2cpg`

* Fix build

* Add comment

---------

Co-authored-by: Fabian Yamaguchi <fabs@joern.io>
  • Loading branch information
fabsx00 and Fabian Yamaguchi authored Feb 13, 2023
1 parent 0be9697 commit 8db5d70
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,10 @@ class AstCreationPassTests extends AbstractPassTest {
lit.code shouldBe "10"
}

inside(lambda2call.argument.l) { case List(lit: Literal) =>
lit.code shouldBe "10"
}
inside(lambda2call.receiver.l) { case List(ref: MethodRef) =>
inside(lambda2call.argument.l) { case List(ref: MethodRef, lit: Literal) =>
ref.methodFullName shouldBe lambda2Name
ref.code shouldBe "int anonymous_lambda_1 (int n)"
lit.code shouldBe "10"
}
}
}
Expand Down Expand Up @@ -1103,6 +1101,7 @@ class AstCreationPassTests extends AbstractPassTest {
.name("Foo")
.l
.size shouldBe 1

inside(cpg.call.code("f.method()").l) { case List(call: Call) =>
call.methodFullName shouldBe Operators.fieldAccess
call.argument(1).code shouldBe "f"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
}

val iteratorCallAst =
callAst(iteratorCallNode, receiver = actualIteratorAst, withRecvArgEdge = true)
callAst(iteratorCallNode, base = actualIteratorAst)

callAst(iteratorAssignNode, List(Ast(iteratorAssignIdentifier), iteratorCallAst))
.withRefEdge(iteratorAssignIdentifier, iteratorLocalNode)
Expand All @@ -1623,7 +1623,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
val iteratorHasNextCallReceiver =
identifierNode(iteratorLocalNode.name, Some(iteratorLocalNode.typeFullName), lineNo)

callAst(iteratorHasNextCallNode, receiver = Some(Ast(iteratorHasNextCallReceiver)), withRecvArgEdge = true)
callAst(iteratorHasNextCallNode, base = Some(Ast(iteratorHasNextCallReceiver)))
.withRefEdge(iteratorHasNextCallReceiver, iteratorLocalNode)
}

Expand All @@ -1644,7 +1644,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
)
val iterNextCallReceiver = identifierNode(iteratorLocalNode.name, Some(iteratorLocalNode.typeFullName), lineNo)
val iterNextCallAst =
callAst(iterNextCallNode, receiver = Some(Ast(iterNextCallReceiver)), withRecvArgEdge = true)
callAst(iterNextCallNode, base = Some(Ast(iterNextCallReceiver)))
.withRefEdge(iterNextCallReceiver, iteratorLocalNode)

callAst(varLocalAssignNode, List(Ast(varLocalAssignIdentifier), iterNextCallAst))
Expand Down Expand Up @@ -2172,7 +2172,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
case _ => // Nothing to do in this case
}

callAst(initNode, args.toList, Some(targetAst), withRecvArgEdge = true)
callAst(initNode, args.toList, Some(targetAst))
}

def astsForVariableDecl(varDecl: VariableDeclarationExpr): Seq[Ast] = {
Expand Down Expand Up @@ -2468,7 +2468,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
val identifierWithDefaultOrder = identifier.copy.order(PropertyDefaults.Order)
val identifierForInit = identifierWithDefaultOrder.copy
val initWithDefaultOrder = initNode.order(PropertyDefaults.Order)
val initAst = callAst(initWithDefaultOrder, args, Some(Ast(identifierForInit)), withRecvArgEdge = true)
val initAst = callAst(initWithDefaultOrder, args, Some(Ast(identifierForInit)))

val returnAst = Ast(identifierWithDefaultOrder.copy)

Expand Down Expand Up @@ -2517,7 +2517,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
}
val thisAst = Ast(thisNode)

callAst(callRoot, args, Some(thisAst), withRecvArgEdge = true)
callAst(callRoot, args, Some(thisAst))
}

private def astsForExpression(expression: Expression, expectedType: ExpectedType): Seq[Ast] = {
Expand Down Expand Up @@ -3147,7 +3147,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
.columnNumber(column(call))
.typeFullName(expressionTypeFullName.getOrElse(TypeConstants.Any))

callAst(callRoot, argumentAsts, scopeAsts.headOption, withRecvArgEdge = true)
callAst(callRoot, argumentAsts, scopeAsts.headOption)
}

def astForSuperExpr(superExpr: SuperExpr, expectedType: ExpectedType): Ast = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class NewCallTests extends JavaSrcCode2CpgFixture {
.fullNameExact(s"Foo.${io.joern.x2cpg.Defines.ConstructorMethodName}:void()")
.call
.nameExact(io.joern.x2cpg.Defines.ConstructorMethodName)
.receiver
.argument(0)
.l match {
case List(thisNode: Identifier) =>
thisNode.outE.collectAll[Ref].map(_.inNode).l match {
Expand All @@ -120,7 +120,7 @@ class NewCallTests extends JavaSrcCode2CpgFixture {
| public void foo(int x) {}
|}""".stripMargin)

cpg.method.name("test").call.name("foo").receiver.outE.collectAll[Ref].l match {
cpg.method.name("test").call.name("foo").argument(0).outE.collectAll[Ref].l match {
case List(ref) =>
ref.inNode match {
case param: MethodParameterIn =>
Expand All @@ -145,7 +145,7 @@ class NewCallTests extends JavaSrcCode2CpgFixture {
| public void foo(int x) {}
|}""".stripMargin)

cpg.method.name("test").call.name("foo").receiver.outE.collectAll[Ref].l match {
cpg.method.name("test").call.name("foo").argument(0).outE.collectAll[Ref].l match {
case List(ref) =>
ref.inNode match {
case param: MethodParameterIn =>
Expand Down Expand Up @@ -320,7 +320,7 @@ class NewCallTests extends JavaSrcCode2CpgFixture {
|""".stripMargin)

"create a `super` receiver with fields correctly set" in {
val superReceiver = cpg.call.name("toString").receiver.collectAll[Identifier].head
val superReceiver = cpg.call.name("toString").argument(0).collectAll[Identifier].head
superReceiver.name shouldBe "this"
superReceiver.code shouldBe "super"
superReceiver.typeFullName shouldBe "java.lang.Object"
Expand Down Expand Up @@ -400,7 +400,7 @@ class CallTests extends JavaSrcCode2CpgFixture {

"should allow traversing from call to arguments" in {
cpg.call("add").argument.size shouldBe 3
val List(arg0) = cpg.call("add").receiver.l
val List(arg0) = cpg.call("add").argument(0).l
arg0.isInstanceOf[nodes.Identifier] shouldBe true
arg0.asInstanceOf[nodes.Identifier].name shouldBe "this"
arg0.code shouldBe "this"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ class NewControlStructureTests extends JavaSrcCode2CpgFixture {
iteratorCall.order shouldBe 2
iteratorCall.argumentIndex shouldBe 2

iteratorCall.receiver.l match {
iteratorCall.argument(0).l match {
case List(items: Identifier) =>
items.name shouldBe "items"
items.typeFullName shouldBe "java.util.List"
Expand Down Expand Up @@ -631,7 +631,7 @@ class NewControlStructureTests extends JavaSrcCode2CpgFixture {
conditionCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
conditionCall.order shouldBe 1

conditionCall.receiver.l match {
conditionCall.argument(0).l match {
case List(receiver: Identifier) =>
receiver.name shouldBe "$iterLocal0"
receiver.typeFullName shouldBe "java.util.Iterator"
Expand Down Expand Up @@ -688,7 +688,7 @@ class NewControlStructureTests extends JavaSrcCode2CpgFixture {
assignSource.typeFullName shouldBe "java.lang.Object"
assignSource.order shouldBe 2
assignSource.argumentIndex shouldBe 2
assignSource.receiver.l match {
assignSource.argument(0).l match {
case List(iterIdent: Identifier) =>
iterIdent.name shouldBe "$iterLocal0"
iterIdent.typeFullName shouldBe "java.util.Iterator"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class TypeTests extends JavaSrcCode2CpgFixture {
call.typeFullName shouldBe "void"
call.dispatchType shouldBe DispatchTypes.STATIC_DISPATCH.toString

call.receiver.collect { case identifier: Identifier => identifier }.l match {
call.argument(0).collect { case identifier: Identifier => identifier }.l match {
case identifier :: Nil =>
identifier.name shouldBe "this"
identifier.typeFullName shouldBe "java.lang.Object"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class AstCreator(
createParameterInNode("this", "this", 0, isVariadic = false, line = lineNumber, column = columnNumber)

val methodChildren = astsForFile(astNodeInfo)
setArgIndices(methodChildren)
setArgumentIndices(methodChildren)

val methodReturn = methodReturnNode(Defines.Any, line = None, column = None)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ trait AstForDeclarationsCreator { this: AstCreator =>
declaration.columnNumber
)
val sourceAst =
createCallAst(sourceCall, List(Ast(sourceCallArgNode)))
callAst(sourceCall, List(Ast(sourceCallArgNode)))
val assignmentCallAst = createAssignmentCallAst(
Ast(id),
sourceAst,
Expand Down Expand Up @@ -207,7 +207,7 @@ trait AstForDeclarationsCreator { this: AstCreator =>
}

val asts = fromAst +: (specifierAsts ++ declAsts.flatten)
setArgIndices(asts)
setArgumentIndices(asts)
blockAst(createBlockNode(declaration), asts)
}

Expand All @@ -221,7 +221,7 @@ trait AstForDeclarationsCreator { this: AstCreator =>
}

val asts = declAsts.toList.flatten
setArgIndices(asts)
setArgumentIndices(asts)
blockAst(createBlockNode(assignment), asts)
}

Expand All @@ -237,7 +237,7 @@ trait AstForDeclarationsCreator { this: AstCreator =>
}

val asts = declAsts.toList.flatten
setArgIndices(asts)
setArgumentIndices(asts)
blockAst(createBlockNode(declaration), asts)
}

Expand All @@ -254,7 +254,7 @@ trait AstForDeclarationsCreator { this: AstCreator =>
val assignmentCallAst = createExportAssignmentCallAst(s"_$name", exportCallAst, declaration, None)

val asts = List(fromCallAst, assignmentCallAst)
setArgIndices(asts)
setArgumentIndices(asts)
blockAst(createBlockNode(declaration), asts)
}

Expand All @@ -268,7 +268,7 @@ trait AstForDeclarationsCreator { this: AstCreator =>
val declAsts = declaration.json("declarations").arr.toList.map(astForVariableDeclarator(_, scopeType, kind))
declAsts match {
case head :: tail =>
setArgIndices(declAsts)
setArgumentIndices(declAsts)
tail.foreach { declAst =>
declAst.root.foreach(diffGraph.addEdge(localAstParentStack.head, _, EdgeTypes.AST))
Ast.storeInDiffGraph(declAst, diffGraph)
Expand Down Expand Up @@ -398,22 +398,22 @@ trait AstForDeclarationsCreator { this: AstCreator =>
val receiverNode = createIdentifierNode(RequireKeyword, nodeInfo)
val thisNode = createIdentifierNode("this", nodeInfo)
scope.addVariableReference(thisNode.name, thisNode)
val callAst = createCallAst(
val cAst = callAst(
sourceCall,
List(Ast(sourceCallArgNode)),
receiver = Option(Ast(receiverNode)),
base = Option(Ast(thisNode))
)
val sourceAst = if (isImportN) {
val fieldAccessCall = createFieldAccessCallAst(
callAst,
cAst,
createFieldIdentifierNode(name, nodeInfo.lineNumber, nodeInfo.columnNumber),
nodeInfo.lineNumber,
nodeInfo.columnNumber
)
fieldAccessCall
} else {
callAst
cAst
}
val assigmentCallAst =
createAssignmentCallAst(
Expand Down Expand Up @@ -724,7 +724,7 @@ trait AstForDeclarationsCreator { this: AstCreator =>
localAstParentStack.pop()

val blockChildren = assignmentTmpCallAst +: subTreeAsts :+ Ast(returnTmpNode)
setArgIndices(blockChildren)
setArgumentIndices(blockChildren)
Ast(blockNode).withChildren(blockChildren)
}

Expand Down
Loading

0 comments on commit 8db5d70

Please sign in to comment.