Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixup initialisation order of cpg generators (#5137) #159

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import io.joern.x2cpg.passes.frontend.XTypeRecoveryConfig
import io.shiftleft.codepropertygraph.generated.Cpg

import java.nio.file.Path
import scala.compiletime.uninitialized
import scala.util.Try

/** Source-based front-end for Java
*/
case class JavaSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path = if (isWin) rootPath.resolve("javasrc2cpg.bat") else rootPath.resolve("javasrc2cpg")
private var enableTypeRecovery = false
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized
private lazy val cmdLineArgs = config.cmdLineParams.toSeq
private lazy val enableTypeRecovery = cmdLineArgs.exists(_ == s"--${javasrc2cpg.ParameterNames.EnableTypeRecovery}")
private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(cmdLineArgs)

/** Generate a CPG for the given input path. Returns the output path, or None, if no CPG was generated.
*/
override def generate(inputPath: String, outputPath: String = "cpg.bin"): Try[String] = {
val arguments = config.cmdLineParams.toSeq ++ Seq(inputPath, "--output", outputPath)
enableTypeRecovery = arguments.exists(_ == s"--${javasrc2cpg.ParameterNames.EnableTypeRecovery}")
if (enableTypeRecovery) typeRecoveryConfig = XTypeRecoveryConfig.parse(arguments)
val arguments = cmdLineArgs ++ Seq(inputPath, "--output", outputPath)
runShellCommand(command.toString, arguments).map(_ => outputPath)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ import io.joern.x2cpg.passes.frontend.XTypeRecoveryConfig
import io.shiftleft.codepropertygraph.generated.Cpg

import java.nio.file.Path
import scala.compiletime.uninitialized
import scala.util.Try

case class JsSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path = if (isWin) rootPath.resolve("jssrc2cpg.bat") else rootPath.resolve("jssrc2cpg.sh")
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized
private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)

/** Generate a CPG for the given input path. Returns the output path, or None, if no CPG was generated.
*/
override def generate(inputPath: String, outputPath: String = "cpg.bin.zip"): Try[String] = {
typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)

if (File(inputPath).isDirectory) {
invoke(inputPath, outputPath)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import io.shiftleft.codepropertygraph.generated.Cpg
import scopt.OParser

import java.nio.file.Path
import scala.compiletime.uninitialized
import scala.util.Try

case class PhpCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path = if (isWin) rootPath.resolve("php2cpg.bat") else rootPath.resolve("php2cpg")
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized
private var setKnownTypesConfig: XTypeStubsParserConfig = uninitialized

override def generate(inputPath: String, outputPath: String): Try[String] = {
val cmdLineArgs = config.cmdLineParams.toSeq
typeRecoveryConfig = XTypeRecoveryConfig.parse(cmdLineArgs)
setKnownTypesConfig = OParser
private lazy val command: Path = if (isWin) rootPath.resolve("php2cpg.bat") else rootPath.resolve("php2cpg")
private lazy val cmdLineArgs = config.cmdLineParams.toSeq
private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(cmdLineArgs)
private lazy val setKnownTypesConfig: XTypeStubsParserConfig = {
OParser
.parse(XTypeStubsParser.parserOptions2, cmdLineArgs, XTypeStubsParserConfig())
.getOrElse(
throw new RuntimeException(
s"unable to parse XTypeStubsParserConfig from commandline arguments ${cmdLineArgs.mkString(" ")}"
)
)
}

override def generate(inputPath: String, outputPath: String): Try[String] = {
val arguments = List(inputPath) ++ Seq("-o", outputPath) ++ config.cmdLineParams
runShellCommand(command.toString, arguments).map(_ => outputPath)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import scala.util.Try
import scala.compiletime.uninitialized

case class PythonSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path = if (isWin) rootPath.resolve("pysrc2cpg.bat") else rootPath.resolve("pysrc2cpg")
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized
private lazy val command: Path = if (isWin) rootPath.resolve("pysrc2cpg.bat") else rootPath.resolve("pysrc2cpg")
private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)

/** Generate a CPG for the given input path. Returns the output path, or None, if no CPG was generated.
*/
override def generate(inputPath: String, outputPath: String = "cpg.bin.zip"): Try[String] = {
typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)
val arguments = Seq(inputPath, "-o", outputPath) ++ config.cmdLineParams
runShellCommand(command.toString, arguments).map(_ => outputPath)
}
Expand All @@ -30,7 +29,6 @@ case class PythonSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends

override def applyPostProcessingPasses(cpg: Cpg): Cpg = {
pysrc2cpg.postProcessingPasses(cpg, typeRecoveryConfig).foreach(_.createAndApply())

cpg
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import io.joern.x2cpg.passes.frontend.XTypeRecoveryConfig
import io.shiftleft.codepropertygraph.generated.Cpg

import java.nio.file.Path
import scala.compiletime.uninitialized
import scala.util.Try

case class SwiftSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path =
if (isWin) rootPath.resolve("swiftsrc2cpg.bat") else rootPath.resolve("swiftsrc2cpg.sh")
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized

private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)

/** Generate a CPG for the given input path. Returns the output path, or None, if no CPG was generated.
*/
Expand All @@ -29,7 +29,6 @@ case class SwiftSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends

private def invoke(inputPath: String, outputPath: String): Try[String] = {
val arguments = Seq(inputPath, "--output", outputPath) ++ config.cmdLineParams
typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)
runShellCommand(command.toString, arguments).map(_ => outputPath)
}

Expand Down
Loading