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

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

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

import io.joern.csharpsrc2cpg.Config
import io.joern.csharpsrc2cpg.astcreation.AstCreator
import io.joern.x2cpg.utils.{Report, TimeUtils}
import io.shiftleft.codepropertygraph.generated.Cpg
import io.shiftleft.passes.ForkJoinParallelCpgPass
import io.shiftleft.utils.IOUtils
import org.slf4j.{Logger, LoggerFactory}

import scala.util.{Try, Success, Failure}
import java.nio.file.Paths

class AstCreationPass(cpg: Cpg, astCreators: Seq[AstCreator], report: Report)
    extends ForkJoinParallelCpgPass[AstCreator](cpg) {

  private val logger: Logger = LoggerFactory.getLogger(classOf[AstCreationPass])

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

  override def runOnPart(diffGraph: DiffGraphBuilder, astCreator: AstCreator): Unit = {
    val ((success, filename), duration) = TimeUtils.time {
      val fullPath = astCreator.parserResult.fullPath
      val fileLOC  = IOUtils.readLinesInFile(Paths.get(fullPath)).size
      report.addReportInfo(astCreator.relativeFileName, fileLOC, parsed = true)
      Try {
        val localDiff = astCreator.createAst()
        diffGraph.absorb(localDiff)
      } match {
        case Failure(exception) =>
          logger.warn(s"Failed to generate a CPG for: '$fullPath'", exception)
          (false, astCreator.relativeFileName)
        case Success(_) =>
          logger.info(s"Generated a CPG for: '$fullPath'")
          (true, astCreator.relativeFileName)
      }
    }
    report.updateReport(filename, cpg = success, duration)
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy