-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[c#] refactor local summary creator into its own util (#5236)
- Loading branch information
1 parent
f7d270d
commit 4b5da19
Showing
2 changed files
with
45 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...nds/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/utils/ProgramSummaryCreator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.joern.csharpsrc2cpg.utils | ||
|
||
import io.joern.csharpsrc2cpg.Config | ||
import io.joern.csharpsrc2cpg.astcreation.AstCreator | ||
import io.joern.csharpsrc2cpg.datastructures.CSharpProgramSummary | ||
import io.joern.x2cpg.utils.ConcurrentTaskUtil | ||
import org.slf4j.LoggerFactory | ||
|
||
import scala.util.{Failure, Success} | ||
|
||
/** Builds a `CSharpProgramSummary` by pre-parsing AST creators for high level structures, taking into account related | ||
* frontend options. | ||
*/ | ||
object ProgramSummaryCreator { | ||
|
||
private val logger = LoggerFactory.getLogger(getClass) | ||
|
||
def from(astCreators: Seq[AstCreator], config: Config): CSharpProgramSummary = { | ||
val internalSummary = summarizeAstCreators(astCreators) | ||
val externalSummary = buildExternalSummary(config.useBuiltinSummaries, config.externalSummaryPaths) | ||
internalSummary ++= externalSummary.filter(namespacePred = (ns, _) => internalSummary.imports.contains(ns)) | ||
} | ||
|
||
private def summarizeAstCreators(astCreators: Seq[AstCreator]): CSharpProgramSummary = { | ||
ConcurrentTaskUtil | ||
.runUsingThreadPool(astCreators.map(x => () => x.summarize()).iterator) | ||
.flatMap { | ||
case Failure(exception) => | ||
logger.warn(s"Unable to pre-parse C# file, skipping - ", exception) | ||
None | ||
case Success(summary) => Option(summary) | ||
} | ||
.foldLeft(CSharpProgramSummary(imports = CSharpProgramSummary.initialImports))(_ ++= _) | ||
} | ||
|
||
private def buildExternalSummary(withBuiltinTypes: Boolean, withJsonFiles: Set[String]): CSharpProgramSummary = { | ||
val builtin = if withBuiltinTypes then CSharpProgramSummary.builtinTypesSummary else CSharpProgramSummary() | ||
val fromJson = | ||
if withJsonFiles.nonEmpty then CSharpProgramSummary.externalTypesSummary(withJsonFiles) | ||
else CSharpProgramSummary() | ||
builtin ++= fromJson | ||
} | ||
} |