Repository: joernio/joern
Pull request #785 (2021-11-26 09:48:07)

Fix some missing type registrations for javasrc2cpg


Patch:
diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala
index fef4f42cd..61fa9b0aa 100644
--- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala
+++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala
@@ -1,7 +1,7 @@
 package io.joern.c2cpg.astcreation
 
 import io.joern.c2cpg.utils.IOUtils
-import io.shiftleft.codepropertygraph.generated.nodes.NewBlock
+import io.shiftleft.codepropertygraph.generated.nodes.{NewBlock, NewNode}
 import io.shiftleft.codepropertygraph.generated.{DispatchTypes, Operators}
 import io.shiftleft.x2cpg.Ast
 import org.eclipse.cdt.core.dom.ast._
@@ -14,10 +14,31 @@ import org.eclipse.cdt.internal.core.model.ASTStringUtil
 
 import scala.annotation.nowarn
 
+object AstCreatorHelper {
+  implicit class OptionSafeAst(val ast: Ast) extends AnyVal {
+    def withArgEdge(src: NewNode, dst: Option[NewNode]): Ast = dst match {
+      case Some(value) => ast.withArgEdge(src, value)
+      case None        => ast
+    }
+
+    def withConditionEdge(src: NewNode, dst: Option[NewNode]): Ast = dst match {
+      case Some(value) => ast.withConditionEdge(src, value)
+      case None        => ast
+    }
+
+    def withArgEdges(src: NewNode, dsts: Seq[Ast]): Ast = {
+      val args = dsts.collect { case a if a.root.isDefined => a.root.get }
+      ast.withArgEdges(src, args)
+    }
+  }
+}
+
 trait AstCreatorHelper {
 
   this: AstCreator =>
 
+  import AstCreatorHelper.OptionSafeAst
+
   private var usedNames: Int = 0
 
   protected def uniqueName(target: String, name: String, fullName: String): (String, String) = {
@@ -272,14 +293,8 @@ trait AstCreatorHelper {
   private def astforDecltypeSpecifier(decl: ICPPASTDecltypeSpecifier, order: Int): Ast = {
     val op = "operators.<typeOf>"
     val cpgUnary = newCallNode(decl, op, op, DispatchTypes.STATIC_DISPATCH, order)
-
     val operand = nullSafeAst(decl.getDecltypeExpression, 1)
-
-    val ast = Ast(cpgUnary).withChild(operand)
-    operand.root match {
-      case Some(op) => ast.withArgEdge(cpgUnary, op)
-      case None     => ast
-    }
+    Ast(cpgUnary).withChild(operand).withArgEdge(cpgUnary, operand.root)
   }
 
   private def astForCASTDesignatedInitializer(d: ICASTDesignatedInitializer, order: Int): Ast = {
@@ -295,12 +310,11 @@ trait AstCreatorHelper {
       val callNode = newCallNode(d, op, op, DispatchTypes.STATIC_DISPATCH, o)
       val left = astForNode(des, 1)
       val right = astForNode(d.getOperand, 2)
-      var ast = Ast(callNode)
+      Ast(callNode)
         .withChild(left)
         .withChild(right)
-      if (left.root.isDefined) ast = ast.withArgEdge(callNode, left.root.get)
-      if (right.root.isDefined) ast = ast.withArgEdge(callNode, right.root.get)
-      ast
+        .withArgEdge(callNode, left.root)
+        .withArgEdge(callNode, right.root)
     }
     scope.popScope()
     Ast(b).withChildren(calls)
@@ -319,12 +333,11 @@ trait AstCreatorHelper {
       val callNode = newCallNode(d, op, op, DispatchTypes.STATIC_DISPATCH, o)
       val left = astForNode(des, 1)
       val right = astForNode(d.getOperand, 2)
-      var ast = Ast(callNode)
+      Ast(callNode)
         .withChild(left)
         .withChild(right)
-      if (left.root.isDefined) ast = ast.withArgEdge(callNode, left.root.get)
-      if (right.root.isDefined) ast = ast.withArgEdge(callNode, right.root.get)
-      ast
+        .withArgEdge(callNode, left.root)
+        .withArgEdge(callNode, right.root)
     }
     scope.popScope()
     Ast(b).withChildren(calls)
@@ -334,9 +347,7 @@ trait AstCreatorHelper {
     val name = "<operator>.constructorInitializer"
     val callNode = newCallNode(c, name, name, DispatchTypes.STATIC_DISPATCH, order)
     val args = withOrder(c.getArguments) { case (a, o) => astForNode(a, o) }
-    val ast = Ast(callNode).withChildren(args)
-    val validArgs = args.collect { case a if a.root.isDefined => a.root.get }
-    ast.withArgEdges(callNode, validArgs)
+    Ast(callNode).withChildren(args).withArgEdges(callNode, args)
   }
 
   protected def astForNode(node: IASTNode, order: Int): Ast = {
diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForExpressionsCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForExpressionsCreator.scala
index abcc98bc3..a6f2f169a 100644
--- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForExpressionsCreator.scala
+++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForExpressionsCreator.scala
@@ -12,6 +12,8 @@ trait AstForExpressionsCreator {
 
   this: AstCreator =>
 
+  import AstCreatorHelper.OptionSafeAst
+
   private def astForBinaryExpression(bin: IASTBinaryExpression, order: Int): Ast = {
     val op = bin.getOperator match {
       case IASTBinaryExpression.op_multiply         => Operators.multiplication
@@ -53,12 +55,11 @@ trait AstForExpressionsCreator {
     val callNode = newCallNode(bin, op, op, DispatchTypes.STATIC_DISPATCH, order)
     val left = nullSafeAst(bin.getOperand1, 1)
     val right = nullSafeAst(bin.getOperand2, 2)
-    var ast = Ast(callNode)
+    Ast(callNode)
       .withChild(left)
       .withChild(right)
-    if (left.root.isDefined) ast = ast.withArgEdge(callNode, left.root.get)
-    if (right.root.isDefined) ast = ast.withArgEdge(callNode, right.root.get)
-    ast
+      .withArgEdge(callNode, left.root)
+      .withArgEdge(callNode, right.root)
   }
 
   private def astForExpressionList(exprList: IASTExpressionList, order: Int): Ast = {
@@ -106,9 +107,7 @@ trait AstForExpressionsCreator {
     }
 
     val cpgCall = Ast(newCallNode(call, name, name, dd, order))
-    val args = withOrder(call.getArguments) { case (a, o)     => astForNode(a, o) }
-    val validArgs = args.collect { case a if a.root.isDefined => a.root.get }
-
+    val args = withOrder(call.getArguments) { case (a, o) => astForNode(a, o) }
     rec.root match {
       // Optimization: do not include the receiver if the receiver is just the function name,
       // e.g., for `f(x)`, don't include an `f` identifier node as a first child. Since we
@@ -116,14 +115,14 @@ trait AstForExpressionsCreator {
       // Moreover, the data flow tracker does not need to track `f`, which would not make
       // much sense anyway.
       case Some(r: NewIdentifier) if r.name == name =>
-        cpgCall.withChildren(args).withArgEdges(cpgCall.root.get, validArgs)
+        cpgCall.withChildren(args).withArgEdges(cpgCall.root.get, args)
       case Some(r) =>
         cpgCall
           .withChild(rec)
           .withChildren(args)
-          .withArgEdges(cpgCall.root.get, validArgs)
+          .withArgEdges(cpgCall.root.get, args)
           .withReceiverEdge(cpgCall.root.get, r)
-      case None => cpgCall.withChildren(args).withArgEdges(cpgCall.root.get, validArgs)
+      case None => cpgCall.withChildren(args).withArgEdges(cpgCall.root.get, args)
     }
   }
 
@@ -155,14 +154,8 @@ trait AstForExpressionsCreator {
           opExpr.getOperand
         case opExpr => opExpr
       }
-
       val operand = nullSafeAst(operandExpr, 1)
-
-      val ast = Ast(cpgUnary).withChild(operand)
-      operand.root match {
-        case Some(op) => ast.withArgEdge(cpgUnary, op)
-        case None     => ast
-      }
+      Ast(cpgUnary).withChild(operand).withArgEdge(cpgUnary, operand.root)
     }
   }
 
@@ -176,11 +169,7 @@ trait AstForExpressionsCreator {
             op == IASTTypeIdExpression.op_typeof =>
         val call = newCallNode(typeId, Operators.sizeOf, Operators.sizeOf, DispatchTypes.STATIC_DISPATCH, order)
         val arg = astForNode(typeId.getTypeId.getDeclSpecifier, 1)
-        val ast = Ast(call).withChild(arg)
-        arg.root match {
-          case Some(r) => ast.withArgEdge(call, r)
-          case _       => ast
-        }
+        Ast(call).withChild(arg).withArgEdge(call, arg.root)
       case _ => notHandledYet(typeId, order)
     }
   }
@@ -192,11 +181,8 @@ trait AstForExpressionsCreator {
     val posAst = nullSafeAst(expr.getPositiveResultExpression, 2)
     val negAst = nullSafeAst(expr.getNegativeResultExpression, 3)
 
-    val children = Seq(condAst, posAst, negAst).collect {
-      case c if c.root.isDefined => c
-    }
-
-    Ast(call).withChildren(children).withArgEdges(call, children.map(_.root.get))
+    val children = Seq(condAst, posAst, negAst)
+    Ast(call).withChildren(children).withArgEdges(call, children)
   }
 
   private def astForArrayIndexExpression(arrayIndexExpression: IASTArraySubscriptExpression, order: Int): Ast = {
@@ -210,12 +196,11 @@ trait AstForExpressionsCreator {
     val expr = astForExpression(arrayIndexExpression.getArrayExpression, 1)
     val arg = astForNode(arrayIndexExpression.getArgument, 2)
 
-    var ast = Ast(cpgArrayIndexing)
+    Ast(cpgArrayIndexing)
       .withChild(expr)
       .withChild(arg)
-    if (expr.root.isDefined) ast = ast.withArgEdge(cpgArrayIndexing, expr.root.get)
-    if (arg.root.isDefined) ast = ast.withArgEdge(cpgArrayIndexing, arg.root.get)
-    ast
+      .withArgEdge(cpgArrayIndexing, expr.root)
+      .withArgEdge(cpgArrayIndexing, arg.root)
   }
 
   private def astForCastExpression(castExpression: IASTCastExpression, order: Int): Ast = {
@@ -226,12 +211,11 @@ trait AstForExpressionsCreator {
     val argNode = castExpression.getTypeId
     val arg = newUnknown(argNode, 1)
 
-    var ast = Ast(cpgCastExpression)
+    Ast(cpgCastExpression)
       .withChild(Ast(arg))
       .withChild(expr)
       .withArgEdge(cpgCastExpression, arg)
-    if (expr.root.isDefined) ast = ast.withArgEdge(cpgCastExpression, expr.root.get)
-    ast
+      .withArgEdge(cpgCastExpression, expr.root)
   }
 
   private def astForNewExpression(newExpression: ICPPASTNewExpression, order: Int): Ast = {
@@ -254,12 +238,11 @@ trait AstForExpressionsCreator {
         } else {
           Seq.empty
         }
-      val validArgs = args.filter(_.root.isDefined)
       Ast(cpgNewExpression)
         .withChild(cpgTypeId)
-        .withChildren(validArgs)
+        .withChildren(args)
         .withArgEdge(cpgNewExpression, cpgTypeId.root.get)
-        .withArgEdges(cpgNewExpression, validArgs.map(_.root.get))
+        .withArgEdges(cpgNewExpression, args)
     }
   }
 
@@ -267,35 +250,30 @@ trait AstForExpressionsCreator {
     val cpgDeleteNode =
       newCallNode(delExpression, Operators.delete, Operators.delete, DispatchTypes.STATIC_DISPATCH, order)
     val arg = astForExpression(delExpression.getOperand, 1)
-    var ast = Ast(cpgDeleteNode).withChild(arg)
-    if (arg.root.isDefined) ast = ast.withArgEdge(cpgDeleteNode, arg.root.get)
-    ast
+    Ast(cpgDeleteNode)
+      .withChild(arg)
+      .withArgEdge(cpgDeleteNode, arg.root)
   }
 
   private def astForTypeIdInitExpression(typeIdInit: IASTTypeIdInitializerExpression, order: Int): Ast = {
     val cpgCastExpression =
       newCallNode(typeIdInit, Operators.cast, Operators.cast, DispatchTypes.STATIC_DISPATCH, order)
 
+    val typeAst = newUnknown(typeIdInit.getTypeId, 1)
     val expr = astForNode(typeIdInit.getInitializer, 2)
 
-    val typeNode = typeIdInit.getTypeId
-    val typeAst = newUnknown(typeNode, 1)
-
-    val ast = Ast(cpgCastExpression)
+    Ast(cpgCastExpression)
       .withChild(Ast(typeAst))
       .withChild(expr)
       .withArgEdge(cpgCastExpression, typeAst)
-    if (expr.root.isDefined) ast.withArgEdge(cpgCastExpression, expr.root.get) else ast
+      .withArgEdge(cpgCastExpression, expr.root)
   }
 
   private def astForConstructorExpression(c: ICPPASTSimpleTypeConstructorExpression, order: Int): Ast = {
     val name = c.getDeclSpecifier.toString
     val callNode = newCallNode(c, name, name, DispatchTypes.STATIC_DISPATCH, order)
-
     val arg = astForNode(c.getInitializer, 1)
-
-    val ast = Ast(callNode).withChild(arg)
-    if (arg.root.isDefined) { ast.withArgEdge(callNode, arg.root.get) } else ast
+    Ast(callNode).withChild(arg).withArgEdge(callNode, arg.root)
   }
 
   private def astForCompoundStatementExpression(compoundExpression: IGNUASTCompoundStatementExpression,
diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForPrimitivesCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForPrimitivesCreator.scala
index ad10e48e6..c4659a30f 100644
--- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForPrimitivesCreator.scala
+++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForPrimitivesCreator.scala
@@ -11,6 +11,8 @@ trait AstForPrimitivesCreator {
 
   this: AstCreator =>
 
+  import AstCreatorHelper.OptionSafeAst
+
   protected def astForComment(comment: IASTComment): Ast =
     Ast(NewComment().code(nodeSignature(comment)).filename(fileName(comment)).lineNumber(line(comment)))
 
@@ -64,7 +66,7 @@ trait AstForPrimitivesCreator {
       .argumentIndex(2)
       .lineNumber(line(fieldRef.getFieldName))
       .columnNumber(column(fieldRef.getFieldName))
-    Ast(ma).withChild(owner).withChild(Ast(member)).withArgEdge(ma, owner.root.get).withArgEdge(ma, member)
+    Ast(ma).withChild(owner).withChild(Ast(member)).withArgEdge(ma, owner.root).withArgEdge(ma, member)
   }
 
   protected def astForInitializerList(l: IASTInitializerList, order: Int): Ast = {
@@ -79,9 +81,7 @@ trait AstForPrimitivesCreator {
       case (c, o) =>
         astForNode(c, o)
     }
-    val validArgs = args.collect { case a if a.root.isDefined => a.root.get }
-    val ast = Ast(initCallNode).withChildren(args).withArgEdges(initCallNode, validArgs)
-
+    val ast = Ast(initCallNode).withChildren(args).withArgEdges(initCallNode, args)
     if (l.getClauses.length > MAX_INITIALIZERS) {
       val placeholder = NewLiteral()
         .typeFullName("ANY")
@@ -108,13 +108,11 @@ trait AstForPrimitivesCreator {
         val callNode = newCallNode(head, op, op, DispatchTypes.STATIC_DISPATCH, order)
         val arg1 = astForNode(head, 1)
         val arg2 = fieldAccesses(tail, 2)
-        var call =
-          Ast(callNode)
-            .withChild(arg1)
-            .withChild(arg2)
-        if (arg1.root.isDefined) call = call.withArgEdge(callNode, arg1.root.get)
-        if (arg2.root.isDefined) call = call.withArgEdge(callNode, arg2.root.get)
-        call
+        Ast(callNode)
+          .withChild(arg1)
+          .withChild(arg2)
+          .withArgEdge(callNode, arg1.root)
+          .withArgEdge(callNode, arg2.root)
     }
 
     val qualifier = fieldAccesses(qualId.getQualifier.toIndexedSeq.toList, 1)
@@ -132,12 +130,7 @@ trait AstForPrimitivesCreator {
       .argumentIndex(2)
       .lineNumber(line(qualId.getLastName))
       .columnNumber(column(qualId.getLastName))
-    val ast = Ast(ma).withChild(owner).withChild(Ast(member)).withArgEdge(ma, member)
-    owner.root match {
-      case Some(value) => ast.withArgEdge(ma, value)
-      case None        => ast
-    }
-
+    Ast(ma).withChild(owner).withChild(Ast(member)).withArgEdge(ma, member).withArgEdge(ma, owner.root)
   }
 
 }
diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForStatementsCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForStatementsCreator.scala
index 850ba641a..b3b443861 100644
--- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForStatementsCreator.scala
+++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForStatementsCreator.scala
@@ -12,6 +12,8 @@ trait AstForStatementsCreator {
 
   this: AstCreator =>
 
+  import AstCreatorHelper.OptionSafeAst
+
   protected def astForBlockStatement(blockStmt: IASTCompoundStatement, order: Int): Ast = {
     val cpgBlock = NewBlock()
       .order(order)
@@ -66,11 +68,7 @@ trait AstForStatementsCreator {
       .lineNumber(line(ret))
       .columnNumber(column(ret))
     val expr = nullSafeAst(ret.getReturnValue, 1)
-    val ast = Ast(cpgReturn).withChild(expr)
-    expr.root match {
-      case Some(r) => ast.withArgEdge(cpgReturn, r)
-      case None    => ast
-    }
+    Ast(cpgReturn).withChild(expr).withArgEdge(cpgReturn, expr.root)
   }
 
   private def astForBreakStatement(br: IASTBreakStatement, order: Int): Ast = {
@@ -111,16 +109,10 @@ trait AstForStatementsCreator {
     val conditionAst = nullSafeAst(doStmt.getCondition, 2)
     val stmtAsts = nullSafeAst(doStmt.getBody, 1)
 
-    val ast = Ast(doNode)
+    Ast(doNode)
       .withChild(conditionAst)
       .withChildren(stmtAsts)
-
-    conditionAst.root match {
-      case Some(r) =>
-        ast.withConditionEdge(doNode, r)
-      case None =>
-        ast
-    }
+      .withConditionEdge(doNode, conditionAst.root)
   }
 
   private def astForSwitchStatement(switchStmt: IASTSwitchStatement, order: Int): Ast = {
@@ -131,16 +123,10 @@ trait AstForStatementsCreator {
     val conditionAst = nullSafeAst(switchStmt.getControllerExpression, 1)
     val stmtAsts = nullSafeAst(switchStmt.getBody, 2)
 
-    val ast = Ast(switchNode)
+    Ast(switchNode)
       .withChild(conditionAst)
       .withChildren(stmtAsts)
-
-    conditionAst.root match {
-      case Some(r) =>
-        ast.withConditionEdge(switchNode, r)
-      case None =>
-        ast
-    }
+      .withConditionEdge(switchNode, conditionAst.root)
   }
 
   private def astsForCaseStatement(caseStmt: IASTCaseStatement, order: Int): Seq[Ast] = {
@@ -204,17 +190,12 @@ trait AstForStatementsCreator {
     val updateAst = nullSafeAst(forStmt.getIterationExpression, continuedOrder + 2)
     val stmtAst = nullSafeAst(forStmt.getBody, continuedOrder + 3)
 
-    val ast = Ast(forNode)
+    Ast(forNode)
       .withChildren(initAsts)
       .withChild(compareAst)
       .withChild(updateAst)
       .withChildren(stmtAst)
-
-    compareAst.root match {
-      case Some(c) =>
-        ast.withConditionEdge(forNode, c)
-      case None => ast
-    }
+      .withConditionEdge(forNode, compareAst.root)
   }
 
   private def astForRangedFor(forStmt: ICPPASTRangeBasedForStatement, order: Int): Ast = {
@@ -242,16 +223,10 @@ trait AstForStatementsCreator {
     val conditionAst = nullSafeAst(whileStmt.getCondition, 1)
     val stmtAsts = nullSafeAst(whileStmt.getBody, 2)
 
-    val ast = Ast(whileNode)
+    Ast(whileNode)
       .withChild(conditionAst)
       .withChildren(stmtAsts)
-
-    conditionAst.root match {
-      case Some(r) =>
-        ast.withConditionEdge(whileNode, r)
-      case None =>
-        ast
-    }
+      .withConditionEdge(whileNode, conditionAst.root)
   }
 
   private def astForIf(ifStmt: IASTIfStatement, order: Int): Ast = {
@@ -267,17 +242,11 @@ trait AstForStatementsCreator {
       Ast(elseNode).withChildren(elseAsts)
     } else Ast()
 
-    val ast = Ast(ifNode)
+    Ast(ifNode)
       .withChild(conditionAst)
       .withChildren(stmtAsts)
       .withChild(elseChild)
-
-    conditionAst.root match {
-      case Some(r) =>
-        ast.withConditionEdge(ifNode, r)
-      case None =>
-        ast
-    }
+      .withConditionEdge(ifNode, conditionAst.root)
   }
 
 }
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 c20b44e52..485a50031 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
@@ -13,6 +13,8 @@ trait AstForTypesCreator {
 
   this: AstCreator =>
 
+  import AstCreatorHelper.OptionSafeAst
+
   private def parentIsClassDef(node: IASTNode): Boolean = Option(node.getParent) match {
     case Some(_: IASTCompositeTypeSpecifier) => true
     case _                                   => false
@@ -135,34 +137,26 @@ trait AstForTypesCreator {
       val callNode = newCallNode(declarator, operatorName, operatorName, DispatchTypes.STATIC_DISPATCH, order)
       val left = astForNode(declarator.getName, 1)
       val right = astForNode(i.getInitializerClause, 2)
-      val ast = Ast(callNode)
+      Ast(callNode)
         .withChild(left)
-        .withArgEdge(callNode, left.root.get)
         .withChild(right)
-      right.root match {
-        case Some(value) => ast.withArgEdge(callNode, value)
-        case None        => ast
-      }
+        .withArgEdge(callNode, left.root)
+        .withArgEdge(callNode, right.root)
     case i: ICPPASTConstructorInitializer =>
       val name = declarator.getName.toString
       val callNode = newCallNode(declarator, name, name, DispatchTypes.STATIC_DISPATCH, order)
       val args = withOrder(i.getArguments) { case (a, o) => astForNode(a, o) }
-      val ast = Ast(callNode).withChildren(args)
-      val validArgs = args.collect { case a if a.root.isDefined => a.root.get }
-      ast.withArgEdges(callNode, validArgs)
+      Ast(callNode).withChildren(args).withArgEdges(callNode, args)
     case i: IASTInitializerList =>
       val operatorName = Operators.assignment
       val callNode = newCallNode(declarator, operatorName, operatorName, DispatchTypes.STATIC_DISPATCH, order)
       val left = astForNode(declarator.getName, 1)
       val right = astForNode(i, 2)
-      val ast = Ast(callNode)
+      Ast(callNode)
         .withChild(left)
-        .withArgEdge(callNode, left.root.get)
         .withChild(right)
-      right.root match {
-        case Some(value) => ast.withArgEdge(callNode, value)
-        case None        => ast
-      }
+        .withArgEdge(callNode, left.root)
+        .withArgEdge(callNode, right.root)
     case _ => astForNode(init, order)
   }
 
@@ -358,10 +352,11 @@ trait AstForTypesCreator {
       val callNode = newCallNode(enumerator, operatorName, operatorName, DispatchTypes.STATIC_DISPATCH, order + 1)
       val left = astForNode(enumerator.getName, 1)
       val right = astForNode(enumerator.getValue, 2)
-
-      var ast = Ast(callNode).withChild(left).withChild(right)
-      if (left.root.isDefined) ast = ast.withArgEdge(callNode, left.root.get)
-      if (right.root.isDefined) ast = ast.withArgEdge(callNode, right.root.get)
+      val ast = Ast(callNode)
+        .withChild(left)
+        .withChild(right)
+        .withArgEdge(callNode, left.root)
+        .withArgEdge(callNode, right.root)
       Seq(Ast(cpgMember), ast)
     } else {
       Seq(Ast(cpgMember))
diff --git a/joern-cli/frontends/c2cpg/src/main/scala/org/eclipse/cdt/internal/core/parser/scanner/MacroArgumentExtractor.scala b/joern-cli/frontends/c2cpg/src/main/scala/org/eclipse/cdt/internal/core/parser/scanner/MacroArgumentExtractor.scala
index 1f05f3761..7cfc49c1d 100644
--- a/joern-cli/frontends/c2cpg/src/main/scala/org/eclipse/cdt/internal/core/parser/scanner/MacroArgumentExtractor.scala
+++ b/joern-cli/frontends/c2cpg/src/main/scala/org/eclipse/cdt/internal/core/parser/scanner/MacroArgumentExtractor.scala
@@ -54,7 +54,7 @@ class MacroArgumentExtractor(tu: IASTTranslationUnit, loc: IASTFileLocation) {
 
   private def createDictionary(expansion: IASTPreprocessorMacroExpansion): CharArrayMap[PreprocessorMacro] = {
     val refs: Array[IASTName] = Array(expansion.getMacroReference)
-    val map = new CharArrayMap[PreprocessorMacro](refs.length);
+    val map = new CharArrayMap[PreprocessorMacro](refs.length)
     refs.foreach(name => addMacroDefinition(map, name))
     map
   }
diff --git a/joern-cli/frontends/javasrc2cpg/src/main/scala/io/joern/javasrc2cpg/passes/AstCreator.scala b/joern-cli/frontends/javasrc2cpg/src/main/scala/io/joern/javasrc2cpg/passes/AstCreator.scala
index 5158cbe28..8d4b2c32b 100644
--- a/joern-cli/frontends/javasrc2cpg/src/main/scala/io/joern/javasrc2cpg/passes/AstCreator.scala
+++ b/joern-cli/frontends/javasrc2cpg/src/main/scala/io/joern/javasrc2cpg/passes/AstCreator.scala
@@ -329,9 +329,11 @@ class AstCreator(filename: String, global: Global) {
       List.empty[String]
     }
 
+    val typeFullName = registerType(typ.getFullyQualifiedName.toScala.getOrElse(""))
+
     val typeDecl = NewTypeDecl()
       .name(typ.getNameAsString)
-      .fullName(typ.getFullyQualifiedName.toScala.getOrElse(""))
+      .fullName(typeFullName)
       .inheritsFromTypeFullName(baseTypeFullNames)
       .order(order)
       .filename(filename)
@@ -1401,7 +1403,7 @@ class AstCreator(filename: String, global: Global) {
       .order(order)
 
     val identifier = NewIdentifier()
-      .typeFullName("ANY")
+      .typeFullName(registerType("ANY"))
       .code(expr.getTypeAsString)
       .lineNumber(line(expr))
       .columnNumber(column(expr))
@@ -1550,10 +1552,11 @@ class AstCreator(filename: String, global: Global) {
   }
 
   def astForThisExpr(expr: ThisExpr, order: Int): AstWithCtx = {
+    val typeFullName = registerType(Try(expr.calculateResolvedType().describe()).getOrElse("<empty>"))
     val identifier =
       NewIdentifier()
         .name("this")
-        .typeFullName(Try(expr.calculateResolvedType().describe()).getOrElse("<empty>"))
+        .typeFullName(typeFullName)
         .code(expr.toString)
         .order(order)
         .argumentIndex(order)
@@ -1654,9 +1657,11 @@ class AstCreator(filename: String, global: Global) {
       resolvedDecl: Try[ResolvedMethodDeclaration],
       order: Int
   ) = {
+    val typeFullName = registerType(Try(call.calculateResolvedType().describe()).getOrElse("<empty>"))
     val callNode = NewCall()
       .name(call.getNameAsString)
       .code(s"${call.getNameAsString}(${call.getArguments.asScala.mkString(", ")})")
+      .typeFullName(typeFullName)
       .order(order)
       .argumentIndex(order)
     resolvedDecl match {
@@ -1685,10 +1690,11 @@ class AstCreator(filename: String, global: Global) {
     resolvedDecl.toOption
       .filterNot(_.isStatic)
       .map { resolved =>
+        val typeFullName = registerType(resolved.declaringType().getQualifiedName)
         NewIdentifier()
           .name("this")
           .code("this")
-          .typeFullName(resolved.declaringType().getQualifiedName)
+          .typeFullName(typeFullName)
           .order(0)
           .argumentIndex(0)
       }


Test patch:
diff --git a/joern-cli/frontends/javasrc2cpg/src/test/scala/io/joern/javasrc2cpg/querying/TypeTests.scala b/joern-cli/frontends/javasrc2cpg/src/test/scala/io/joern/javasrc2cpg/querying/TypeTests.scala
index a09d83abe..cf8da43ac 100644
--- a/joern-cli/frontends/javasrc2cpg/src/test/scala/io/joern/javasrc2cpg/querying/TypeTests.scala
+++ b/joern-cli/frontends/javasrc2cpg/src/test/scala/io/joern/javasrc2cpg/querying/TypeTests.scala
@@ -20,6 +20,13 @@ class TypeTests extends JavaSrcCodeToCpgFixture {
       | }
       |""".stripMargin
 
+  "should create TYPE node with correct fields for class" in {
+    val List(x) = cpg.typ.name("Foo").l
+    x.name shouldBe "Foo"
+    x.fullName shouldBe "foo.Foo"
+    x.typeDeclFullName shouldBe "foo.Foo"
+  }
+
   "should create TYPE node with correct fields for class member" in {
     val List(x) = cpg.typ.name("Long").l
     x.name shouldBe "Long"