Skip to content

Commit

Permalink
[c#] add disable-builtin-summaries (#5168)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierpinho authored Dec 6, 2024
1 parent f68412c commit 982e7ee
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ class CSharpSrc2Cpg extends X2CpgFrontend[Config] {
}
.foldLeft(CSharpProgramSummary(imports = CSharpProgramSummary.initialImports))(_ ++= _)

val builtinSummary = CSharpProgramSummary(
mutable.Map
.fromSpecific(CSharpProgramSummary.BuiltinTypes.view.filterKeys(internalProgramSummary.imports(_)))
.result()
)
val builtinSummary = if (config.useBuiltinSummaries) {
CSharpProgramSummary(
mutable.Map
.fromSpecific(CSharpProgramSummary.BuiltinTypes.view.filterKeys(internalProgramSummary.imports(_)))
.result()
)
} else {
CSharpProgramSummary()
}

val internalAndBuiltinSummary = internalProgramSummary ++= builtinSummary

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import scopt.OParser

import java.nio.file.Paths

final case class Config(downloadDependencies: Boolean = false)
final case class Config(downloadDependencies: Boolean = false, useBuiltinSummaries: Boolean = true)
extends X2CpgConfig[Config]
with DependencyDownloadConfig[Config]
with TypeRecoveryParserConfig[Config]
Expand All @@ -24,6 +24,10 @@ final case class Config(downloadDependencies: Boolean = false)
copy(downloadDependencies = value).withInheritedFields(this)
}

def withUseBuiltinSummaries(value: Boolean): Config = {
copy(useBuiltinSummaries = value).withInheritedFields(this)
}

}

object Frontend {
Expand All @@ -35,7 +39,10 @@ object Frontend {
OParser.sequence(
programName("csharpsrc2cpg"),
DependencyDownloadConfig.parserOptions,
XTypeRecoveryConfig.parserOptionsForParserConfig
XTypeRecoveryConfig.parserOptionsForParserConfig,
opt[Unit]("disable-builtin-summaries")
.text("do not use the built-in type summaries")
.action((_, c) => c.withUseBuiltinSummaries(false))
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,51 @@ class DependencyTests extends CSharpCode2CpgFixture {
fail("Expected a call node for `Entity`")
}
}
}

"a `csproj` file specifying a built-in dependency but built-in type summaries are disabled" when {
val csCode = """
|using Microsoft.EntityFrameworkCore;
|
|public class Foo
|{
| static void bar(ModelBuilder modelBuilder)
| {
| modelBuilder.Entity("test");
| }
|}""".stripMargin
val csProj = """
|<Project Sdk="Microsoft.NET.Sdk">
| <ItemGroup>
| <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.36" />
| </ItemGroup>
|</Project>
|""".stripMargin

"the ability to download dependencies is also turned off" should {
val cpg = code(csCode)
.moreCode(csProj, "Foo.csproj")
.withConfig(Config().withUseBuiltinSummaries(false).withDownloadDependencies(false))
"not resolve the call since there are no type summaries available for it" in {
inside(cpg.call("Entity").headOption) {
case Some(entity) => entity.methodFullName shouldBe "ModelBuilder.Entity:<unresolvedSignature>"
case None => fail("Expected call node for `Entity`")
}
}
}

"the ability to download dependencies is turned on" should {
val cpg = code(csCode)
.moreCode(csProj, "Foo.csproj")
.withConfig(Config().withUseBuiltinSummaries(false).withDownloadDependencies(true))
"resolve the call since the dependency shall be downloaded and a type summary for it be built" in {
inside(cpg.call("Entity").headOption) {
case Some(entity) =>
entity.methodFullName shouldBe "Microsoft.EntityFrameworkCore.ModelBuilder.Entity:Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder(System.String)"
case None => fail("Expected call node for `Entity`")
}
}
}
}

"a `csproj` file specifying a dependency with the `Update` attribute" should {
Expand Down

0 comments on commit 982e7ee

Please sign in to comment.