Skip to content

Commit

Permalink
[c2cpg] Fixed constructor initializer handling (#5322)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Mar 1, 2025
1 parent 144d818 commit 13c4037
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,24 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
callAst(callNode_, List(left, right))
case i: ICPPASTConstructorInitializer =>
val name = ASTStringUtil.getSimpleName(declarator.getName)
val (idAst, tpe) = scope.lookupVariable(name) match {
case Some((local, tpe)) =>
val idNode = identifierNode(declarator.getName, name, name, registerType(tpe))
(Ast(idNode).withRefEdge(idNode, local), tpe)
case None => (Ast(identifierNode(declarator.getName, name, name, Defines.Any)), Defines.Any)
}
val operatorName = Operators.assignment
val callNode_ =
callNode(declarator, code(declarator), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val args = i.getArguments.toList.map(x => astForNode(x))
callNode(
declarator,
s"$name = $tpe${code(i)}",
operatorName,
operatorName,
DispatchTypes.STATIC_DISPATCH,
None,
Some(tpe)
)
val args = List(idAst, astForNode(i))
callAst(callNode_, args)
case i: IASTInitializerList =>
val operatorName = Operators.assignment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class Cpp17FeaturesTests extends AstC2CpgSuite(fileSuffix = FileDefaults.CppExt)
.l shouldBe List(
"std::lock_guard<std::mutex> lk(mx)",
"if (std::lock_guard<std::mutex> lk(mx); v.empty()) { v.push_back(val); }",
"gadget(args)",
"gadget = Foo(args)",
"s = gadget.status()",
"switch (Foo gadget(args); auto s = gadget.status()) { case OK: gadget.zip(); break; case Bad: throw BadFoo(s.message()); }"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,25 @@ class AstCreationPassTests extends AstC2CpgSuite {
}
}

"be correct for decl assignment with parentheses" in {
val cpg = code(
"""
|void method() {
| int *val (new int[3]);
|}
|""".stripMargin,
"test.cpp"
)
inside(cpg.method.nameExact("method").block.astChildren.isCall.l) { case List(assignment: Call) =>
val List(identifier) = assignment.argument.isIdentifier.l
identifier.argumentIndex shouldBe 1
identifier.name shouldBe "val"
val List(call) = assignment.argument.isCall.l
call.code shouldBe "(new int[3])"
call.argumentIndex shouldBe 2
}
}

"be correct for decl assignment with typedecl" in {
val cpg = code(
"""
Expand Down Expand Up @@ -1088,9 +1107,8 @@ class AstCreationPassTests extends AstC2CpgSuite {
.fullNameExact("Foo")
.l
.size shouldBe 1
inside(cpg.call.codeExact("f1(0)").l) { case List(call: Call) =>
call.name shouldBe "f1"
call.argument(1).code shouldBe "0"
inside(cpg.call.codeExact("f1 = Foo(0)").l) { case List(call: Call) =>
call.name shouldBe Operators.assignment
}
}

Expand Down

0 comments on commit 13c4037

Please sign in to comment.