Skip to content

Commit

Permalink
fix failing code
Browse files Browse the repository at this point in the history
  • Loading branch information
khemrajrathore committed Dec 23, 2024
1 parent 46c95ad commit 445d90a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ final case class Config(
def withKeepTypeArguments(value: Boolean): Config = {
copy(keepTypeArguments = value).withInheritedFields(this)
}

def withResolveTypes(value: Boolean): Config = {
copy(resolveTypes = value).withInheritedFields(this)
}
}

private object Frontend {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.joern.rubysrc2cpg.astcreation.RubyIntermediateAst.StatementList
import io.joern.rubysrc2cpg.datastructures.RubyProgramSummary
import io.joern.rubysrc2cpg.deprecated.parser.DeprecatedRubyParser
import io.joern.rubysrc2cpg.deprecated.parser.DeprecatedRubyParser.*
import io.joern.rubysrc2cpg.parser.*
import io.joern.rubysrc2cpg.parser.{RubyJsonParser, RubyJsonToNodeCreator, RubyAstGenRunner}
import io.joern.rubysrc2cpg.passes.{
AstCreationPass,
ConfigFileCreationPass,
Expand All @@ -19,6 +19,7 @@ import io.joern.x2cpg.frontendspecific.rubysrc2cpg.*
import io.joern.x2cpg.passes.base.AstLinkerPass
import io.joern.x2cpg.passes.callgraph.NaiveCallLinker
import io.joern.x2cpg.passes.frontend.{MetaDataPass, TypeNodePass, XTypeRecoveryConfig}
import io.joern.x2cpg.utils.ExternalCommand.ExternalCommandResult
import io.joern.x2cpg.utils.{ConcurrentTaskUtil, ExternalCommand}
import io.joern.x2cpg.{SourceFiles, X2CpgFrontend}
import io.shiftleft.codepropertygraph.generated.{Cpg, Languages}
Expand Down Expand Up @@ -49,11 +50,13 @@ class RubySrc2Cpg extends X2CpgFrontend[Config] {
}

private def newCreateCpgAction(cpg: Cpg, config: Config): Unit = {
Using.resource(
new parser.ResourceManagedParser(config.antlrCacheMemLimit, config.antlrDebug, config.antlrProfiling)
) { parser =>
File.usingTemporaryDirectory("rubysrc2cpgOut") { tmpDir =>
val astGenResult = RubyAstGenRunner(config).execute(tmpDir)

val astCreators = ConcurrentTaskUtil
.runUsingThreadPool(RubySrc2Cpg.generateParserTasks(parser, config, cpg.metaData.root.headOption))
.runUsingThreadPool(
RubySrc2Cpg.processAstGenRunnerResults(astGenResult.parsedFiles, config, cpg.metaData.root.headOption)
)
.flatMap {
case Failure(exception) => logger.warn(s"Unable to parse Ruby file, skipping -", exception); None
case Success(astCreator) => Option(astCreator)
Expand Down Expand Up @@ -135,18 +138,18 @@ class RubySrc2Cpg extends X2CpgFrontend[Config] {

private def downloadDependency(inputPath: String, tempPath: String): Unit = {
if (Files.isRegularFile(Paths.get(s"${inputPath}${java.io.File.separator}Gemfile"))) {
ExternalCommand.run(s"bundle config set --local path ${tempPath}", inputPath) match {
case Success(configOutput) =>
logger.info(s"Gem config successfully done: $configOutput")
case Failure(exception) =>
logger.error(s"Error while configuring Gem Path: ${exception.getMessage}")
ExternalCommand.run(Seq("bundle", "config", "set", "--local", "path", tempPath), inputPath) match {
case ExternalCommandResult(0, stdOut, _) =>
logger.info(s"Gem config successfully done")
case ExternalCommandResult(_, stdOut, _) =>
logger.error(s"Error while configuring Gem Path: ${stdOut.mkString(System.lineSeparator())}")
}
val command = s"bundle install"
val command = Seq("bundle", "install")
ExternalCommand.run(command, inputPath) match {
case Success(bundleOutput) =>
logger.info(s"Dependency installed successfully: $bundleOutput")
case Failure(exception) =>
logger.error(s"Error while downloading dependency: ${exception.getMessage}")
case ExternalCommandResult(0, stdOut, _) =>
logger.info(s"Dependency installed successfully")
case ExternalCommandResult(_, stdOut, _) =>
logger.error(s"Error while downloading dependency: ${stdOut.mkString(System.lineSeparator())}")
}
}
}
Expand Down Expand Up @@ -177,37 +180,6 @@ object RubySrc2Cpg {
}
}

def generateParserTasks(
resourceManagedParser: parser.ResourceManagedParser,
config: Config,
projectRoot: Option[String]
): Iterator[() => AstCreator] = {
SourceFiles
.determine(
config.inputPath,
RubySourceFileExtensions,
ignoredDefaultRegex = Option(config.defaultIgnoredFilesRegex),
ignoredFilesRegex = Option(config.ignoredFilesRegex),
ignoredFilesPath = Option(config.ignoredFiles)
)
.map { fileName => () =>
resourceManagedParser.parse(File(config.inputPath), fileName) match {
case Failure(exception) => throw exception
case Success(ctx) =>
val fileContent = (File(config.inputPath) / fileName).contentAsString
new AstCreator(
fileName,
ctx,
projectRoot,
enableFileContents = !config.disableFileContent,
fileContent = fileContent,
rootNode = Option(new RubyNodeCreator().visit(ctx).asInstanceOf[StatementList])
)(config.schemaValidation)
}
}
.iterator
}

/** Parses the generated AST Gen files in parallel and produces AstCreators from each.
*/
def processAstGenRunnerResults(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.joern.rubysrc2cpg.parser

import org.antlr.v4.runtime.ParserRuleContext
import org.antlr.v4.runtime.tree.TerminalNode

/** General purpose ANTLR parse tree printer.
*/
object AnltrAstPrinter {
private val indentationIncrement = 1

private def print(level: Int, sb: StringBuilder, context: ParserRuleContext): StringBuilder = {
val indentation = " ".repeat(level)
val contextName = context.getClass.getSimpleName.stripSuffix("Context")
val nextLevel = level + indentationIncrement
sb.append(s"$indentation$contextName\n")
Option(context.children).foreach(_.forEach {
case c: ParserRuleContext => print(nextLevel, sb, c)
case t: TerminalNode => print(nextLevel, sb, t)
})
sb
}

private def print(level: Int, sb: StringBuilder, terminal: TerminalNode): StringBuilder = {
val indentation = " ".repeat(level)
sb.append(s"$indentation${terminal.getText}\n")
sb
}

/** Pretty-prints an entire `ParserRuleContext` together with its descendants.
* @param context
* the context to pretty-print
* @return
* an indented, multiline string representation
*/
def print(context: ParserRuleContext): String = print(0, new StringBuilder, context).toString()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.joern.rubysrc2cpg.testfixtures

import java.io.File
import io.joern.dataflowengineoss.DefaultSemantics
import io.joern.dataflowengineoss.language.Path
import io.joern.dataflowengineoss.semanticsloader.{FlowSemantic, Semantics}
Expand All @@ -10,7 +11,7 @@ import io.joern.x2cpg.ValidationMode
import io.joern.x2cpg.testfixtures.*
import io.shiftleft.codepropertygraph.generated.Cpg
import io.shiftleft.semanticcpg.language.{ICallResolver, NoResolve}
import org.scalatest.Inside
import org.scalatest.{Inside, Tag}

import java.nio.file.Files
import scala.jdk.CollectionConverters.*
Expand Down

This file was deleted.

0 comments on commit 445d90a

Please sign in to comment.