forked from joernio/joern
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gosrc2cpg] - Made structural changes while creating initial AST node…
…s per file joernio#3695 (joernio#3734) Earlier we were creating AST structure per file in following way. NameSpaceNode(`package prefixed with filename`)=>TypeDecl(`Fake TypeDecl node representing the given file`)=>Method(`Fake Method node representing the statements (imports, global var and constants, types and functions) in the given file`)=>Block(`all the direct statement ASTs as child nodes`) With new structure, we wanted to create Fake TypeDecl node representing one package (One package can have multiple go files in it). In order to achieve this, we are following order of nodes being created for package and then for a file. For package, i.e. per folder NameSpaceNode(`one namespace node representing one package`)=>TypeDecl(`Fake TypeDecl node representing the give package. File path mentioned in this case will be the folder path`) For each file in the package. The first fake node will be added as child node of the Package TypeDecl created in the previous cache pass. Method(`Fake Method node representing the statements (imports, global var and constants, types and functions) in the given file`)=>Block(`all the direct statement asts as child nodes`) - Added few more unit tests covering the new use cases - Changed the existing unit tests to adjust with updated AST structure. - A few memory optimisations to exclude some part of processing while processing third party dependencies TODO: - Handle the global variable and constants to be created as member fields of the package level Fake TypeDecl we are creating. As of now it's being created as `LOCAL` node under Fake file level `METHOD` node we have created. - If the Global variable or constant is getting accessed in the same package. As of now that is being treated as an `IDENTIFIER`, we need change to be converted to FieldAccess `CALL` node. - With the above change, we might not require the File level fake `METHOD` node as for Global variable or constants we are going to treat them as Member fields of level Type Decl instead of `LOCAL` nodes. So we might get rid of this file-level Fake `METHOD` node.
- Loading branch information
1 parent
9644ce8
commit 5f59dab
Showing
13 changed files
with
155 additions
and
161 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
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
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
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
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
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
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
24 changes: 17 additions & 7 deletions
24
...ds/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/passes/MethodAndTypeCacheBuilderPass.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 |
---|---|---|
@@ -1,30 +1,40 @@ | ||
package io.joern.gosrc2cpg.passes | ||
|
||
import io.joern.gosrc2cpg.Config | ||
import io.joern.gosrc2cpg.astcreation.{AstCreator, CacheBuilder} | ||
import io.joern.gosrc2cpg.astcreation.AstCreator | ||
import io.joern.gosrc2cpg.model.GoModHelper | ||
import io.joern.gosrc2cpg.parser.GoAstJsonParser | ||
import io.joern.gosrc2cpg.parser.GoAstJsonParser.ParserResult | ||
import io.joern.x2cpg.SourceFiles | ||
import io.shiftleft.codepropertygraph.Cpg | ||
import io.shiftleft.codepropertygraph.generated.DiffGraphBuilder | ||
|
||
import java.nio.file.Paths | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.{Await, Future} | ||
|
||
class MethodAndTypeCacheBuilderPass(astFiles: List[String], config: Config, goMod: GoModHelper) { | ||
class MethodAndTypeCacheBuilderPass(cpgOpt: Option[Cpg], astFiles: List[String], config: Config, goMod: GoModHelper) { | ||
def process(): Seq[AstCreator] = { | ||
val futures = astFiles | ||
.map(file => { | ||
Future { | ||
val parserResult = GoAstJsonParser.readFile(Paths.get(file)) | ||
val relPathFileName = SourceFiles.toRelativePath(parserResult.fullPath, config.inputPath) | ||
val astCreator = new AstCreator(relPathFileName, parserResult, goMod)(config.schemaValidation) | ||
astCreator.buildCache() | ||
astCreator | ||
val diffGraph = astCreator.buildCache(cpgOpt) | ||
(astCreator, diffGraph) | ||
} | ||
}) | ||
val allResults: Future[List[AstCreator]] = Future.sequence(futures) | ||
Await.result(allResults, Duration.Inf) | ||
val allResults: Future[List[(AstCreator, DiffGraphBuilder)]] = Future.sequence(futures) | ||
val results = Await.result(allResults, Duration.Inf) | ||
val (astCreators, diffGraphs) = results.unzip | ||
cpgOpt.map(cpg => { | ||
diffGraphs.foreach(diffGraph => { | ||
overflowdb.BatchedUpdate | ||
.applyDiff(cpg.graph, diffGraph, null, null) | ||
.transitiveModifications() | ||
}) | ||
}) | ||
astCreators | ||
} | ||
} |
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
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
Oops, something went wrong.