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

io.joern.rubysrc2cpg.passes.AstCreationPass.scala Maven / Gradle / Ivy

There is a newer version: 4.0.78
Show newest version
package io.joern.rubysrc2cpg.passes

import flatgraph.DiffGraphApplier
import io.joern.rubysrc2cpg.astcreation.AstCreator
import io.shiftleft.codepropertygraph.generated.Cpg
import io.shiftleft.codepropertygraph.generated.NodeTypes
import io.shiftleft.codepropertygraph.generated.nodes.NewTypeDecl
import io.shiftleft.passes.ForkJoinParallelCpgPass
import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal
import org.slf4j.LoggerFactory

class AstCreationPass(cpg: Cpg, astCreators: List[AstCreator]) extends ForkJoinParallelCpgPass[AstCreator](cpg) {

  private val logger = LoggerFactory.getLogger(getClass)

  override def generateParts(): Array[AstCreator] = astCreators.toArray

  override def init(): Unit = {
    // The first entry will be the  type, which is often found on fieldAccess nodes
    //   (which may be receivers to calls)
    val diffGraph = Cpg.newDiffGraphBuilder
    val emptyType =
      NewTypeDecl()
        .astParentType(NodeTypes.NAMESPACE_BLOCK)
        .astParentFullName(NamespaceTraversal.globalNamespaceName)
        .isExternal(true)
    val anyType =
      NewTypeDecl()
        .name(Defines.Any)
        .fullName(Defines.Any)
        .astParentType(NodeTypes.NAMESPACE_BLOCK)
        .astParentFullName(NamespaceTraversal.globalNamespaceName)
        .isExternal(true)
    diffGraph.addNode(emptyType).addNode(anyType)
    DiffGraphApplier.applyDiff(cpg.graph, diffGraph)
  }

  override def runOnPart(diffGraph: DiffGraphBuilder, astCreator: AstCreator): Unit = {
    try {
      val ast = astCreator.createAst()
      diffGraph.absorb(ast)
    } catch {
      case ex: Exception =>
        logger.error(s"Error while processing AST for file - ${astCreator.fileName} - ", ex)
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy