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

[rubysrc2cpg]: Move ImportResolverPass to PostProcessingPasses #3091

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 @@ -27,28 +27,28 @@ import scala.util.{Failure, Success, Try}

class RubySrc2Cpg extends X2CpgFrontend[Config] {

val global = new Global()
private val logger = LoggerFactory.getLogger(this.getClass)

val global = new Global()

override def createCpg(config: Config): Try[Cpg] = {
withNewEmptyCpg(config.outputPath, config: Config) { (cpg, config) =>
val packageTableInfo = new PackageTable()

new MetaDataPass(cpg, Languages.RUBYSRC, config.inputPath).createAndApply()
new ConfigFileCreationPass(cpg).createAndApply()
if (config.enableDependencyDownload && !scala.util.Properties.isWin) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely we can keep this code? I would recommend adding val packageTableInfo = new PackageTable() as a field to RubySrc2Cpg which can then later be given to the post-processing passes to use.

This way we can still run AstCreationPass with downloaded dependency info without forcing us to run the rest of the enhancements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, Check now @DavidBakerEffendi

val tempDir = File.newTemporaryDirectory()
try {
downloadDependency(config.inputPath, tempDir.toString())
new AstPackagePass(cpg, tempDir.toString(), global, packageTableInfo, config.inputPath).createAndApply()
new AstPackagePass(cpg, tempDir.toString(), global, RubySrc2Cpg.packageTableInfo, config.inputPath)
.createAndApply()
} finally {
tempDir.delete()
}
}
val astCreationPass = new AstCreationPass(config.inputPath, cpg, global, packageTableInfo)

val astCreationPass = new AstCreationPass(config.inputPath, cpg, global, RubySrc2Cpg.packageTableInfo)
astCreationPass.createAndApply()
TypeNodePass.withRegisteredTypes(astCreationPass.allUsedTypes(), cpg).createAndApply()
new ImportResolverPass(cpg, packageTableInfo).createAndApply()
}
}

Expand All @@ -73,9 +73,12 @@ class RubySrc2Cpg extends X2CpgFrontend[Config] {

object RubySrc2Cpg {

val packageTableInfo = new PackageTable()

def postProcessingPasses(cpg: Cpg, config: Option[Config] = None): List[CpgPassBase] =
List(
// TODO commented below two passes, as waiting on Dependency download PR to get merged
new ImportResolverPass(cpg, packageTableInfo),
new RubyTypeRecoveryPass(cpg),
new RubyTypeHintCallLinker(cpg),
new NaiveCallLinker(cpg),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ class PackageTable() {
case Some(value) => value.toList
case None => List[MethodTableModel]()
}

def set(table: PackageTable): Unit = {
methodTableMap.addAll(table.methodTableMap)
}
def clear(): Unit = methodTableMap.clear
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class RubyTypeRecoveryTests extends RubyCode2CpgFixture {

"Type information for nodes with external dependency" should {
"be present in (Case 1)" ignore {

val cpg = code(
"""
|require "sendgrid-ruby"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.joern.rubysrc2cpg.testfixtures

import io.joern.dataflowengineoss.layers.dataflows.{OssDataFlow, OssDataFlowOptions}
import io.joern.dataflowengineoss.queryengine.EngineContext
import io.joern.rubysrc2cpg.utils.PackageTable
import io.joern.rubysrc2cpg.{Config, RubySrc2Cpg}
import io.joern.x2cpg.X2Cpg
import io.joern.x2cpg.testfixtures.{Code2CpgFixture, DefaultTestCpg, LanguageFrontend, TestCpg}
Expand All @@ -24,13 +25,19 @@ trait RubyFrontend extends LanguageFrontend {

}

class DefaultTestCpgWithRuby(withPostProcessing: Boolean, withDataFlow: Boolean)
class DefaultTestCpgWithRuby(withPostProcessing: Boolean, withDataFlow: Boolean, packageTable: Option[PackageTable])
extends DefaultTestCpg
with RubyFrontend {
override def applyPasses(): Unit = {
X2Cpg.applyDefaultOverlays(this)

if (withPostProcessing) {
packageTable match {
case Some(table) =>
RubySrc2Cpg.packageTableInfo.clear()
RubySrc2Cpg.packageTableInfo.set(table)
case None =>
}
RubySrc2Cpg.postProcessingPasses(this).foreach(_.createAndApply())
}

Expand All @@ -43,8 +50,11 @@ class DefaultTestCpgWithRuby(withPostProcessing: Boolean, withDataFlow: Boolean)

}

class RubyCode2CpgFixture(withPostProcessing: Boolean = false, withDataFlow: Boolean = false)
extends Code2CpgFixture(() => new DefaultTestCpgWithRuby(withPostProcessing, withDataFlow)) {
class RubyCode2CpgFixture(
withPostProcessing: Boolean = false,
withDataFlow: Boolean = false,
packageTable: Option[PackageTable] = None
) extends Code2CpgFixture(() => new DefaultTestCpgWithRuby(withPostProcessing, withDataFlow, packageTable)) {

implicit val resolver: ICallResolver = NoResolve
implicit lazy val engineContext: EngineContext = EngineContext()
Expand Down