Skip to content

Commit

Permalink
Trying to parse the standard library as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Aug 20, 2023
1 parent 5b64230 commit 36e69fc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ class GoLanguageFrontend(language: Language<GoLanguageFrontend>, ctx: Translatio

@Throws(TranslationException::class)
override fun parse(file: File): TranslationUnitDeclaration {
val dependency =
ctx.config.includePaths.firstOrNull() {
file.absolutePath.contains(it.toAbsolutePath().toString())
}

// Make sure, that our top level is set either way
val topLevel =
if (config.topLevel != null) {
config.topLevel
} else {
file.parentFile
// If this file is part of an include, we set the top level to the root of the include
when {
dependency != null -> dependency.toFile()
config.topLevel != null -> config.topLevel
else -> file.parentFile
}!!

val std = GoStandardLibrary.INSTANCE
Expand Down Expand Up @@ -120,8 +126,12 @@ class GoLanguageFrontend(language: Language<GoLanguageFrontend>, ctx: Translatio
// module path as well as the current directory in relation to the topLevel
var packagePath = file.parentFile.relativeTo(topLevel)

// If we are in a module, we need to prepend the module path to it
currentModule?.let { packagePath = File(it.module.mod.path).resolve(packagePath) }
// If we are in a module, we need to prepend the module path to it. There is an
// exception if we are in the "std" module, which represents the standard library
val modulePath = currentModule?.module?.mod?.path
if (modulePath != null && modulePath != "std") {
packagePath = File(modulePath).resolve(packagePath)
}

p.path = packagePath.path
} catch (ex: IllegalArgumentException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.types.FunctionType
import de.fraunhofer.aisec.cpg.graph.types.ObjectType
import de.fraunhofer.aisec.cpg.graph.types.PointerType
import java.io.File
import java.nio.file.Path
import kotlin.test.*

Expand Down Expand Up @@ -790,4 +791,37 @@ class GoLanguageFrontendTest : BaseTest() {
assertNotNull(g)
assertLocalName("string", g.type)
}

@Test
fun testResolveStdLibImport() {
val stdLib = Path.of("src", "test", "resources", "golang-std")
val topLevel = Path.of("src", "test", "resources", "golang")
val tu =
analyzeAndGetFirstTU(
listOf(
topLevel.resolve("function.go").toFile(),
stdLib.resolve("fmt").toFile(),
),
topLevel,
true
) {
it.registerLanguage<GoLanguage>()
it.includePath("src/test/resources/golang-std")
}

assertNotNull(tu)

val p = tu.namespaces["p"]
assertNotNull(p)

val main = p.functions["main"]
assertNotNull(main)

val printfCall = main.calls["fmt.Printf"]
assertNotNull(printfCall)

val printf = printfCall.invokes.firstOrNull()
assertNotNull(printf)
assertEquals("print.go", File(printf.location?.artifactLocation?.uri?.path.toString()).name)
}
}
6 changes: 6 additions & 0 deletions cpg-language-go/src/test/resources/golang-std/fmt/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package fmt

func Printf(format string, a ...any) (n int, err error) {
// Not a real implementation, and we are ignoring it anyway
return 0, nil
}

0 comments on commit 36e69fc

Please sign in to comment.