Skip to content

Commit

Permalink
c2cpg: handle method bodies for lambdas (#3523)
Browse files Browse the repository at this point in the history
We only generated stubs for CPP lambdas. This PR fixes that.

Fixes: #3522
  • Loading branch information
max-leuthaeuser authored Aug 20, 2023
1 parent a9338ea commit d94fdcd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) { th
Ast(functionBinding).withBindsEdge(parentNode, functionBinding).withRefEdge(functionBinding, method)
}

private def parameters(funct: IASTNode): Seq[IASTNode] = funct match {
private def parameters(functionNode: IASTNode): Seq[IASTNode] = functionNode match {
case arr: IASTArrayDeclarator => parameters(arr.getNestedDeclarator)
case decl: CPPASTFunctionDeclarator => decl.getParameters.toIndexedSeq ++ parameters(decl.getNestedDeclarator)
case decl: CASTFunctionDeclarator => decl.getParameters.toIndexedSeq ++ parameters(decl.getNestedDeclarator)
Expand All @@ -64,7 +64,7 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) { th
}

@tailrec
private def isVariadic(funct: IASTNode): Boolean = funct match {
private def isVariadic(functionNode: IASTNode): Boolean = functionNode match {
case decl: CPPASTFunctionDeclarator => decl.takesVarArgs()
case decl: CASTFunctionDeclarator => decl.takesVarArgs()
case defn: IASTFunctionDefinition => isVariadic(defn.getDeclarator)
Expand Down Expand Up @@ -114,10 +114,14 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) { th

scope.popScope()

val stubAst =
methodStubAst(methodNode_, parameterNodes, newMethodReturnNode(lambdaExpression, registerType(returnType)))
val astForLambda = methodAst(
methodNode_,
parameterNodes.map(Ast(_)),
astForMethodBody(Option(lambdaExpression.getBody)),
newMethodReturnNode(lambdaExpression, registerType(returnType))
)
val typeDeclAst = createFunctionTypeAndTypeDecl(lambdaExpression, methodNode_, name, fullname, signature)
Ast.storeInDiffGraph(stubAst.merge(typeDeclAst), diffGraph)
Ast.storeInDiffGraph(astForLambda.merge(typeDeclAst), diffGraph)

Ast(methodRefNode(lambdaExpression, code, fullname, methodNode_.astParentFullName))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
import io.joern.c2cpg.astcreation.AstCreatorHelper.OptionSafeAst

protected def astForBlockStatement(blockStmt: IASTCompoundStatement, order: Int = -1): Ast = {
val node = blockNode(blockStmt, Defines.empty, registerType(Defines.voidTypeName)).order(order).argumentIndex(order)
val code = nodeSignature(blockStmt)
val blockCode = if (code == "{}" || code.isEmpty) Defines.empty else code
val node = blockNode(blockStmt, blockCode, registerType(Defines.voidTypeName))
.order(order)
.argumentIndex(order)
scope.pushNewScope(node)
var currOrder = 1
val childAsts = blockStmt.getStatements.flatMap { stmt =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,9 @@ class AstCreationPassTests extends AbstractPassTest {
"be correct for simple lambda expressions" in AstFixture(
"""
|auto x = [] (int a, int b) -> int
|{
| return a + b;
|};
| { return a + b; };
|auto y = [] (string a, string b) -> string
|{
| return a + b;
|};
| { return a + b; };
|""".stripMargin,
"test.cpp"
) { cpg =>
Expand All @@ -129,12 +125,14 @@ class AstCreationPassTests extends AbstractPassTest {
l1.name shouldBe lambda1FullName
l1.code should startWith("[] (int a, int b) -> int")
l1.signature shouldBe "int anonymous_lambda_0 (int,int)"
l1.body.code shouldBe "{ return a + b; }"
}

inside(cpg.method.fullNameExact(lambda2FullName).l) { case List(l2) =>
l2.name shouldBe lambda2FullName
l2.code should startWith("[] (string a, string b) -> string")
l2.signature shouldBe "string anonymous_lambda_1 (string,string)"
l2.body.code shouldBe "{ return a + b; }"
}

inside(cpg.typeDecl(NamespaceTraversal.globalNamespaceName).head.bindsOut.l) {
Expand Down

0 comments on commit d94fdcd

Please sign in to comment.