Skip to content

Commit

Permalink
[c2cpg|jimple2cpg] Fixed name for nested declarators (#4996)
Browse files Browse the repository at this point in the history
This happened for parameter and variable declarations in parentheses.
  • Loading branch information
max-leuthaeuser authored Oct 10, 2024
1 parent c69ecf5 commit cf3834d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,10 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) { th
private def parameterNodeInfo(parameter: IASTNode, paramIndex: Int): CGlobal.ParameterInfo = {
val (name, codeString, tpe, variadic) = parameter match {
case p: CASTParameterDeclaration =>
(
ASTStringUtil.getSimpleName(p.getDeclarator.getName),
code(p),
cleanType(typeForDeclSpecifier(p.getDeclSpecifier)),
false
)
(shortName(p.getDeclarator), code(p), cleanType(typeForDeclSpecifier(p.getDeclSpecifier)), false)
case p: CPPASTParameterDeclaration =>
(
ASTStringUtil.getSimpleName(p.getDeclarator.getName),
shortName(p.getDeclarator),
code(p),
cleanType(typeForDeclSpecifier(p.getDeclSpecifier)),
p.getDeclarator.declaresParameterPack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ trait AstForPrimitivesCreator(implicit withSchemaValidation: ValidationMode) { t
private def nameForIdentifier(ident: IASTNode): String = {
ident match {
case id: IASTIdExpression => ASTStringUtil.getSimpleName(id.getName)
case id: IASTName if ASTStringUtil.getSimpleName(id).isEmpty && id.getBinding != null => id.getBinding.getName
case id: IASTName if ASTStringUtil.getSimpleName(id).isEmpty => uniqueName("name", "", "")._1
case _ => code(ident)
case id: IASTName =>
val name = ASTStringUtil.getSimpleName(id)
if (name.isEmpty) Try(id.resolveBinding().getName).getOrElse(uniqueName("name", "", "")._1)
else name
case _ => code(ident)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
}

protected def astForDeclarator(declaration: IASTSimpleDeclaration, declarator: IASTDeclarator, index: Int): Ast = {
val name = ASTStringUtil.getSimpleName(declarator.getName)
val name = shortName(declarator)
declaration match {
case d if isTypeDef(d) && shortName(d.getDeclSpecifier).nonEmpty =>
val filename = fileName(declaration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,12 @@ trait FullNameProvider { this: AstCreator =>
}

private def shortNameForIASTDeclarator(declarator: IASTDeclarator): String = {
if (ASTStringUtil.getSimpleName(declarator.getName).isEmpty && declarator.getNestedDeclarator != null) {
shortName(declarator.getNestedDeclarator)
} else {
ASTStringUtil.getSimpleName(declarator.getName)
Try(declarator.getName.resolveBinding().getName).getOrElse {
if (ASTStringUtil.getSimpleName(declarator.getName).isEmpty && declarator.getNestedDeclarator != null) {
shortName(declarator.getNestedDeclarator)
} else {
ASTStringUtil.getSimpleName(declarator.getName)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ class MethodTests extends C2CpgSuite {
}
}

"Name for method parameter in parentheses" should {
"be correct" in {
val cpg = code("""
|int foo(int * (a)) {
| int (x) = a;
| return 2 * *a;
|}
|""".stripMargin)
val List(paramA) = cpg.method("foo").parameter.l
paramA.code shouldBe "int * (a)"
paramA.typeFullName shouldBe "int*"
paramA.name shouldBe "a"
cpg.identifier.nameExact("x").size shouldBe 1
cpg.method("foo").local.nameExact("x").size shouldBe 1
}
}

"Method name, signature and full name tests" should {
"be correct for plain C method" in {
val cpg = code(
Expand Down

0 comments on commit cf3834d

Please sign in to comment.