Skip to content

Commit

Permalink
Use new codeAndLocationFromChildren in Java frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jan 16, 2024
1 parent 86e9e59 commit 630db6b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import de.fraunhofer.aisec.cpg.passes.inference.IsImplicitProvider
import de.fraunhofer.aisec.cpg.passes.inference.IsInferredProvider
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import de.fraunhofer.aisec.cpg.sarif.Region
import java.net.URI
import org.slf4j.LoggerFactory

object NodeBuilder {
Expand Down Expand Up @@ -308,7 +309,7 @@ fun <T : Node, AstNode> T.codeAndLocationFromChildren(parentNode: AstNode): T {
val worklist: MutableList<Node> = this.astChildren.toMutableList()
while (worklist.isNotEmpty()) {
val current = worklist.removeFirst()
if (current.location?.region == null || current.location?.region == Region()) {
if (current.location == null || current.location?.region == Region()) {
// If the node has no location we use the same search on his children again
worklist.addAll(current.astChildren)
} else {
Expand Down Expand Up @@ -348,7 +349,8 @@ fun <T : Node, AstNode> T.codeAndLocationFromChildren(parentNode: AstNode): T {
endLine = last.location?.region?.endLine ?: -1,
endColumn = last.location?.region?.endColumn ?: -1,
)
this.location?.region = newRegion
this.location =
PhysicalLocation(first.location?.artifactLocation?.uri ?: URI(""), newRegion)

val parentCode = this@CodeAndLocationProvider.codeOf(parentNode)
val parentRegion = this@CodeAndLocationProvider.locationOf(parentNode)?.region
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ import de.fraunhofer.aisec.cpg.graph.statements.TryStatement
import de.fraunhofer.aisec.cpg.graph.statements.WhileStatement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.types.Type
import de.fraunhofer.aisec.cpg.helpers.getCodeOfSubregion
import de.fraunhofer.aisec.cpg.helpers.mergeRegions
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import de.fraunhofer.aisec.cpg.sarif.Region
import java.util.function.Supplier
Expand Down Expand Up @@ -178,8 +176,6 @@ class StatementHandler(lang: JavaLanguageFrontend?) :
val statement = this.newForStatement(rawNode = stmt)
frontend.scopeManager.enterScope(statement)
if (forStmt.initialization.size > 1) {
var ofExprList: PhysicalLocation? = null

// code will be set later
val initExprList = this.newExpressionList()
for (initExpr in forStmt.initialization) {
Expand All @@ -190,23 +186,9 @@ class StatementHandler(lang: JavaLanguageFrontend?) :
if (s?.location == null) {
continue
}
if (ofExprList == null) {
ofExprList = s.location
}
ofExprList?.region?.let { ofRegion ->
s.location?.region?.let { ofExprList?.region = mergeRegions(ofRegion, it) }
}
}

// set code and location of init list
statement.location?.let { location ->
ofExprList?.let {
val initCode = getCodeOfSubregion(statement, location.region, it.region)
initExprList.location = ofExprList
initExprList.code = initCode
}
}
statement.initializerStatement = initExprList
statement.initializerStatement = initExprList.codeAndLocationFromChildren(stmt)
} else if (forStmt.initialization.size == 1) {
statement.initializerStatement =
frontend.expressionHandler.handle(forStmt.initialization[0])
Expand Down Expand Up @@ -240,23 +222,9 @@ class StatementHandler(lang: JavaLanguageFrontend?) :
if (s?.location == null) {
continue
}
if (ofExprList == null) {
ofExprList = s.location
}
ofExprList?.region?.let { ofRegion ->
s.location?.region?.let { ofExprList.region = mergeRegions(ofRegion, it) }
}
}

// set code and location of init list
statement.location?.let { location ->
ofExprList?.let {
val updateCode = getCodeOfSubregion(statement, location.region, it.region)
iterationExprList.location = ofExprList
iterationExprList.code = updateCode
}
}
statement.iterationStatement = iterationExprList
statement.iterationStatement = iterationExprList.codeAndLocationFromChildren(stmt)
} else if (forStmt.update.size == 1) {
statement.iterationStatement = frontend.expressionHandler.handle(forStmt.update[0])
}
Expand Down

0 comments on commit 630db6b

Please sign in to comment.