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

Add On Import Translation of Python Files in Include Path #1964

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
30c093d
Import over Language Frontend preprocessing
konradweiss Jan 21, 2025
d06286b
Add language dependent preprocessing function and file walk
konradweiss Jan 21, 2025
3e4e8df
Add language dependent preprocessing function and file walk
konradweiss Jan 22, 2025
2bf1360
Resolving imports if exactly a file is imported, e.g. import a.b and …
konradweiss Jan 22, 2025
152c004
Resolving imports when not a specific pyi file is imported but a gene…
konradweiss Jan 22, 2025
0f4bb6d
Cut prints, reduce number of files imported reduce runtime be collect…
konradweiss Jan 22, 2025
61c89e8
Adding include paths to be analyzed over include paths, i still need …
konradweiss Jan 23, 2025
fc92c87
Spotless fix
konradweiss Jan 23, 2025
7479aec
Adding Dummyfunction to IniFileFrontend
konradweiss Jan 23, 2025
cb4e025
Merge branch 'main' into feature/import-external-interfaces
konradweiss Jan 23, 2025
e8329f6
Moving component creation outside the loop
konradweiss Jan 23, 2025
4910ec7
Merge branch 'feature/import-external-interfaces' of github.com:Fraun…
konradweiss Jan 23, 2025
b29e6bf
Fixed compile error
oxisto Jan 23, 2025
a1f9360
Merge branch 'main' into feature/import-external-interfaces
oxisto Jan 23, 2025
f552f83
Add context to component
konradweiss Jan 23, 2025
d9ff2c0
Add missing function for typescript frontend
konradweiss Jan 23, 2025
c14cca7
spotless typescript
konradweiss Jan 23, 2025
3eb715e
Fix typescript error
konradweiss Jan 23, 2025
28ade73
Add dummy implementation for previously not implemented function in T…
konradweiss Jan 24, 2025
bb9527f
Set Toplevel to component and add DoNotPersist annotation to it
konradweiss Jan 24, 2025
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 @@ -39,11 +39,13 @@
import java.io.PrintWriter
import java.lang.reflect.InvocationTargetException
import java.nio.file.Files
import java.nio.file.Path
import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException
import java.util.concurrent.ExecutionException
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.io.path.name
import kotlin.reflect.full.findAnnotation
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -140,15 +142,24 @@
result: TranslationResult,
): Set<LanguageFrontend<*, *>> {
val usedFrontends = mutableSetOf<LanguageFrontend<*, *>>()

val externalSources: MutableList<File> = mutableListOf()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please have some doc here? What are these variables used for?

val importedSources: MutableSet<File> = mutableSetOf()

ctx.config.includePaths.forEach { externalSources.addAll(extractConfiguredSources(it)) }

var useParallelFrontends = ctx.config.useParallelFrontends

for (sc in ctx.config.softwareComponents.keys) {
val component = Component()
component.ctx = ctx
component.name = Name(sc)
result.addComponent(component)

// Every Component needs to reprocess the sources
var sourceLocations: List<File> = ctx.config.softwareComponents[sc] ?: listOf()

var useParallelFrontends = ctx.config.useParallelFrontends
// Todo Here we need to dispatch to the correct frontend to do preproccessing

val list =
sourceLocations.flatMap { file ->
Expand All @@ -172,6 +183,7 @@
files
} else {
val frontendClass = file.language?.frontend

val supportsParallelParsing =
file.language
?.frontend
Expand Down Expand Up @@ -243,6 +255,29 @@
sourceLocations = list
}

val processedImports: MutableList<String> = mutableListOf()

sourceLocations
.filter { it.language != null }
.forEach {
val frontend = it.language?.newFrontend(ctx)
importedSources.addAll(
frontend?.gatherExternalSources(
ctx.config.includePaths,
it,
externalSources,
processedImports,
) ?: listOf()

Check warning on line 270 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt#L270

Added line #L270 was not covered by tests
)
}
log.info(
"Extending " +
sourceLocations.size +
" source files by " +
importedSources.size +
" interface files."
)

usedFrontends.addAll(
if (useParallelFrontends) {
parseParallel(component, result, ctx, sourceLocations)
Expand All @@ -252,9 +287,43 @@
)
}

// Distribute all files by there root path prefix, parse them in individual component
// named
// like their rootPath local name
ctx.config.includePaths.forEach { includePath ->
val filesInPath =
importedSources.filter {
it.path.removePrefix(includePath.toString()) != it.path.toString()
}
if (filesInPath.isNotEmpty()) {
val component = Component()
component.ctx = ctx
component.name = Name(includePath.name)
result.addComponent(component)

Check warning on line 302 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt#L299-L302

Added lines #L299 - L302 were not covered by tests

ctx.config.topLevels.put(includePath.toString(), includePath.toFile())

Check warning on line 304 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt#L304

Added line #L304 was not covered by tests

usedFrontends.addAll(

Check warning on line 306 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt#L306

Added line #L306 was not covered by tests
if (useParallelFrontends) {
parseParallel(component, result, ctx, filesInPath)

Check warning on line 308 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt#L308

Added line #L308 was not covered by tests
} else {
parseSequentially(component, result, ctx, filesInPath)

Check warning on line 310 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt#L310

Added line #L310 was not covered by tests
}
)
}
}

return usedFrontends
}

private fun extractConfiguredSources(path: Path): MutableList<File> {
val rootFile = path.toFile()
return if (rootFile.exists())
Copy link
Contributor

Choose a reason for hiding this comment

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

Please refactor this code. Add some { as a start.

(if (rootFile.isDirectory) rootFile.walkTopDown().toMutableList()
else mutableListOf(rootFile))
else mutableListOf()

Check warning on line 324 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt#L323-L324

Added lines #L323 - L324 were not covered by tests
}

private fun parseParallel(
component: Component,
result: TranslationResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import de.fraunhofer.aisec.cpg.sarif.Region
import java.io.File
import java.nio.file.Path
import java.util.*
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -110,6 +111,13 @@
*/
abstract override fun codeOf(astNode: AstNode): String?

abstract fun gatherExternalSources(
rootPaths: List<Path>,
source: File,
externalSources: MutableList<File>,
processedImports: MutableList<String> = mutableListOf<String>(),

Check warning on line 118 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/LanguageFrontend.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/LanguageFrontend.kt#L118

Added line #L118 was not covered by tests
): List<File>

/**
* Returns the [Region] of the code with line and column, index starting at 1, generic for java
* or c++ ast nodes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgesOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.passes.ImportDependencies
import de.fraunhofer.aisec.cpg.passes.ImportResolver
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import java.io.File
import org.neo4j.ogm.annotation.Relationship
import org.neo4j.ogm.annotation.Transient
Expand Down Expand Up @@ -64,6 +65,7 @@ open class Component : Node() {
* Returns the top-level directory of this component according to
* [TranslationConfiguration.topLevels]
*/
@DoNotPersist
val topLevel: File?
get() {
return ctx?.config?.topLevels?.get(this.name.localName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import de.fraunhofer.aisec.cpg.graph.types.*
import de.fraunhofer.aisec.cpg.graph.unknownType
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import java.io.File
import java.nio.file.Path
import java.util.function.Supplier
import kotlin.reflect.KClass

Expand Down Expand Up @@ -95,6 +96,15 @@ open class TestLanguageFrontend(
TODO("Not yet implemented")
}

override fun gatherExternalSources(
rootPaths: List<Path>,
source: File,
externalSources: MutableList<File>,
processedImports: MutableList<String>,
): List<File> {
return listOf()
}

override fun locationOf(astNode: Any): PhysicalLocation? {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,15 @@ open class CXXLanguageFrontend(language: Language<CXXLanguageFrontend>, ctx: Tra
return type
}

override fun gatherExternalSources(
rootPaths: List<Path>,
source: File,
externalSources: MutableList<File>,
processedImports: MutableList<String>,
): List<File> {
return listOf()
}

companion object {
private val LOGGER = LoggerFactory.getLogger(CXXLanguageFrontend::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import de.fraunhofer.aisec.cpg.sarif.Region
import java.io.File
import java.net.URI
import java.nio.file.Path

/**
* A language frontend for the [GoLanguage]. It makes use the internal
Expand Down Expand Up @@ -419,6 +420,15 @@ class GoLanguageFrontend(language: Language<GoLanguageFrontend>, ctx: Translatio
}
}

override fun gatherExternalSources(
rootPaths: List<Path>,
source: File,
externalSources: MutableList<File>,
processedImports: MutableList<String>,
): List<File> {
return listOf()
}

companion object {
/**
* All possible goos values. See
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import de.fraunhofer.aisec.cpg.sarif.Region
import java.io.File
import java.io.FileInputStream
import java.net.URI
import java.nio.file.Path
import org.ini4j.Ini
import org.ini4j.Profile.Section

Expand Down Expand Up @@ -205,4 +206,13 @@ class IniFileFrontend(language: Language<IniFileFrontend>, ctx: TranslationConte
override fun setComment(node: Node, astNode: Any) {
return // not used as this function does not implement [Handler]
}

override fun gatherExternalSources(
rootPaths: List<Path>,
source: File,
externalSources: MutableList<File>,
processedImports: MutableList<String>,
): List<File> {
return listOf()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import de.fraunhofer.aisec.cpg.sarif.Region
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
import java.nio.file.Path
import java.util.function.Consumer
import kotlin.jvm.optionals.getOrNull

Expand Down Expand Up @@ -514,6 +515,15 @@ open class JavaLanguageFrontend(language: Language<JavaLanguageFrontend>, ctx: T
}
}

override fun gatherExternalSources(
rootPaths: List<Path>,
source: File,
externalSources: MutableList<File>,
processedImports: MutableList<String>,
): List<File> {
return listOf()
}

companion object {
const val THIS = "this"
const val ANNOTATION_MEMBER_VALUE = "value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
import de.fraunhofer.aisec.cpg.graph.types.Type
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import java.io.File
import java.nio.file.Path
import sootup.core.model.Body
import sootup.core.model.SootMethod
import sootup.core.model.SourceType
Expand Down Expand Up @@ -190,4 +191,13 @@ class JVMLanguageFrontend(
}
}
}

override fun gatherExternalSources(
rootPaths: List<Path>,
source: File,
externalSources: MutableList<File>,
processedImports: MutableList<String>,
): List<File> {
return listOf()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import de.fraunhofer.aisec.cpg.passes.configuration.RegisterExtraPass
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import java.io.File
import java.nio.ByteBuffer
import java.nio.file.Path
import org.bytedeco.javacpp.BytePointer
import org.bytedeco.javacpp.Pointer
import org.bytedeco.llvm.LLVM.*
Expand Down Expand Up @@ -264,6 +265,15 @@ class LLVMIRLanguageFrontend(language: Language<LLVMIRLanguageFrontend>, ctx: Tr
""
}
}

override fun gatherExternalSources(
rootPaths: List<Path>,
source: File,
externalSources: MutableList<File>,
processedImports: MutableList<String>,
): List<File> {
return listOf()
}
}

/**
Expand Down
Loading
Loading