From 164087d975dde532357b5633d52a2e7c510b72a4 Mon Sep 17 00:00:00 2001 From: Xavier Pinho Date: Mon, 3 Feb 2025 19:39:43 +0000 Subject: [PATCH] [c#] fix anonymous functions' fullname (#5289) --- .../astcreation/AstForDeclarationsCreator.scala | 8 ++++++-- .../csharpsrc2cpg/querying/ast/LambdaTests.scala | 12 ++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/joern-cli/frontends/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/astcreation/AstForDeclarationsCreator.scala b/joern-cli/frontends/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/astcreation/AstForDeclarationsCreator.scala index 6fcadf04e01b..c9129e03c39a 100644 --- a/joern-cli/frontends/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/astcreation/AstForDeclarationsCreator.scala +++ b/joern-cli/frontends/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/astcreation/AstForDeclarationsCreator.scala @@ -605,8 +605,12 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) { paramTypeHint: Option[String] = None ): Seq[Ast] = { // Create method declaration - val name = nextClosureName() - val fullName = s"${scope.surroundingScopeFullName.getOrElse(Defines.UnresolvedNamespace)}.$name" + val name = nextClosureName() + val fullName = { + val baseType = withoutSignature(scope.surroundingScopeFullName.getOrElse(Defines.UnresolvedNamespace)) + val signature = Defines.UnresolvedSignature + composeMethodFullName(baseType, name, signature) + } // Set parameter type if necessary, which may require the type hint val paramType = paramTypeHint.flatMap(AstCreatorHelper.elementTypesFromCollectionType).headOption val paramAsts = Try(lambdaExpression.json(ParserKeys.Parameter)).toOption match { diff --git a/joern-cli/frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/LambdaTests.scala b/joern-cli/frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/LambdaTests.scala index c3507890482c..b303d03fbd90 100644 --- a/joern-cli/frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/LambdaTests.scala +++ b/joern-cli/frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/LambdaTests.scala @@ -19,7 +19,7 @@ class LambdaTests extends CSharpCode2CpgFixture { inside(cpg.method("Main").astChildren.collectAll[Method].l) { case anon :: Nil => anon.name shouldBe "0" - anon.fullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).0" + anon.fullName shouldBe "HelloWorld.Program.Main.0:" inside(anon.parameter.l) { case x :: Nil => @@ -37,7 +37,7 @@ class LambdaTests extends CSharpCode2CpgFixture { inside(cpg.method("Main").astChildren.collectAll[TypeDecl].l) { case anon :: Nil => anon.name shouldBe "0" - anon.fullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).0" + anon.fullName shouldBe "HelloWorld.Program.Main.0:" case xs => fail(s"Expected a single anonymous type declaration, got [${xs.code.mkString(",")}]") } } @@ -48,7 +48,7 @@ class LambdaTests extends CSharpCode2CpgFixture { numbers.name shouldBe "numbers" numbers.typeFullName shouldBe s"${DotNetTypeMap(BuiltinTypes.Int)}[]" - closure.methodFullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).0" + closure.methodFullName shouldBe "HelloWorld.Program.Main.0:" closure.referencedMethod.name shouldBe "0" case xs => fail(s"Expected two `Select` call argument, got [${xs.code.mkString(",")}]") } @@ -69,7 +69,7 @@ class LambdaTests extends CSharpCode2CpgFixture { inside(cpg.method("Main").astChildren.collectAll[Method].l) { case anon :: Nil => anon.name shouldBe "0" - anon.fullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).0" + anon.fullName shouldBe "HelloWorld.Program.Main.0:" inside(anon.parameter.l) { case x :: y :: Nil => @@ -91,7 +91,7 @@ class LambdaTests extends CSharpCode2CpgFixture { inside(cpg.method("Main").astChildren.collectAll[TypeDecl].l) { case anon :: Nil => anon.name shouldBe "0" - anon.fullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).0" + anon.fullName shouldBe "HelloWorld.Program.Main.0:" case xs => fail(s"Expected a single anonymous type declaration, got [${xs.code.mkString(",")}]") } } @@ -102,7 +102,7 @@ class LambdaTests extends CSharpCode2CpgFixture { numbers.name shouldBe "numbers" numbers.typeFullName shouldBe s"${DotNetTypeMap(BuiltinTypes.Int)}[]" - closure.methodFullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).0" + closure.methodFullName shouldBe "HelloWorld.Program.Main.0:" closure.referencedMethod.name shouldBe "0" case xs => fail(s"Expected two `Select` call argument, got [${xs.code.mkString(",")}]") }