Repository: joernio/joern (scala)

Issue:
Unable to catch inline enum members
Joern is able to find enum members for a named enum declaration but not for an inline enum declaration

Named enum declaration:
```
enum testing2 {
	id1 = 1,
	id2,
	id3,
};
```

```
joern> cpg.typeDecl("testing2").member.l 
res2: List[Member] = List(
  Member(
    id = 8L,
    code = "id1 = 1",
    columnNumber = Some(value = 2),
    dynamicTypeHintFullName = ArraySeq(),
    lineNumber = Some(value = 2),
    name = "id1",
    order = 1,
    typeFullName = "ANY"
  ),
  Member(
    id = 9L,
    code = "id2",
    columnNumber = Some(value = 2),
    dynamicTypeHintFullName = ArraySeq(),
    lineNumber = Some(value = 3),
    name = "id2",
    order = 2,
    typeFullName = "ANY"
  ),
  Member(
    id = 10L,
    code = "id3",
    columnNumber = Some(value = 2),
    dynamicTypeHintFullName = ArraySeq(),
    lineNumber = Some(value = 4),
    name = "id3",
    order = 3,
    typeFullName = "ANY"
  )
)
```

Inline enum declaration (unable to find members):
```
enum {
	id1 = 1,
	id2,
	id3,
}testing;
```

```
joern> cpg.typeDecl("testing").member.l 
res1: List[Member] = List()
```

Pull request description:
Fixes: https://github.com/joernio/joern/issues/2987

Patch:
diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala
index 2105d7f3d2ea..c4bfad5bc4c8 100644
--- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala
+++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala
@@ -338,8 +338,20 @@ trait AstForTypesCreator { this: AstCreator =>
     val (name, fullname) =
       uniqueName("enum", ASTStringUtil.getSimpleName(typeSpecifier.getName), fullName(typeSpecifier))
     val alias = decls.headOption.map(d => registerType(shortName(d))).filter(_.nonEmpty)
+
+    val (deAliasedName, deAliasedFullName, newAlias) = if (name.contains("anonymous_enum") && alias.isDefined) {
+      (alias.get, fullname.substring(0, fullname.indexOf("anonymous_enum")) + alias.get, None)
+    } else { (name, fullname, alias) }
+
     val typeDecl =
-      typeDeclNode(typeSpecifier, name, registerType(fullname), filename, nodeSignature(typeSpecifier), alias = alias)
+      typeDeclNode(
+        typeSpecifier,
+        deAliasedName,
+        registerType(deAliasedFullName),
+        filename,
+        nodeSignature(typeSpecifier),
+        alias = newAlias
+      )
     methodAstParentStack.push(typeDecl)
     scope.pushNewScope(typeDecl)
 
@@ -355,7 +367,7 @@ trait AstForTypesCreator { this: AstCreator =>
     } else {
       val init = staticInitMethodAst(
         calls,
-        s"$fullname:${io.joern.x2cpg.Defines.StaticInitMethodName}",
+        s"$deAliasedFullName:${io.joern.x2cpg.Defines.StaticInitMethodName}",
         None,
         Defines.anyTypeName,
         Some(filename),


Test patch:
diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/EnumTypeTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/EnumTypeTests.scala
index fb6f7d6fde59..18ff133d5cfc 100644
--- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/EnumTypeTests.scala
+++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/EnumTypeTests.scala
@@ -146,6 +146,23 @@ class EnumTypeTests extends CCodeToCpgSuite(fileSuffix = FileDefaults.CPP_EXT) {
       }
     }
 
+    "be correct for anonymous enum with alias" in {
+      val cpg = code("""
+          |enum
+          |{
+          |    d,
+          |    e,
+          |    f
+          |} testing;""".stripMargin)
+      val List(anon) = cpg.typeDecl.name("testing").l
+      inside(anon.member.l) { case List(d, e, f) =>
+        d.name shouldBe "d"
+        e.name shouldBe "e"
+        f.name shouldBe "f"
+      }
+      anon.aliasTypeFullName shouldBe None
+    }
+
     "be correct for enum access" in {
       val cpg = code("""
        |enum X: int