All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.joern.php2cpg.passes.AstParentInfoPass.scala Maven / Gradle / Ivy

There is a newer version: 4.0.131
Show newest version
package io.joern.php2cpg.passes

import io.shiftleft.codepropertygraph.generated.{Cpg, Properties, PropertyNames}
import io.shiftleft.codepropertygraph.generated.nodes.{AstNode, NamespaceBlock, Method, TypeDecl}
import io.shiftleft.passes.ForkJoinParallelCpgPass
import io.shiftleft.semanticcpg.language.*

class AstParentInfoPass(cpg: Cpg) extends ForkJoinParallelCpgPass[AstNode](cpg) {

  override def generateParts(): Array[AstNode] = {
    (cpg.method ++ cpg.typeDecl).toArray
  }

  override def runOnPart(diffGraph: DiffGraphBuilder, node: AstNode): Unit = {
    findParent(node).foreach { parentNode =>
      val astParentType     = parentNode.label
      val astParentFullName = parentNode.property(Properties.FullName)

      diffGraph.setNodeProperty(node, PropertyNames.AST_PARENT_TYPE, astParentType)
      diffGraph.setNodeProperty(node, PropertyNames.AST_PARENT_FULL_NAME, astParentFullName)
    }
  }

  private def hasValidContainingNodes(nodes: Iterator[AstNode]): Iterator[AstNode] = {
    nodes.collect {
      case m: Method         => m
      case t: TypeDecl       => t
      case n: NamespaceBlock => n
    }
  }

  def findParent(node: AstNode): Option[AstNode] = {
    node.start
      .repeat(_.astParent)(_.until(hasValidContainingNodes(_)).emit(hasValidContainingNodes(_)))
      .find(_ != node)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy