Skip to content

Commit

Permalink
Update parsing function and add C-headers unzip task
Browse files Browse the repository at this point in the history
Extended the parseFile function in the klang project's LibClangParser to support filePath and headerPaths parameters. Also added a new unzipCHeaders task for unzipping C header files in build.gradle.kts. Additionally, created a new integration test to verify SDL2 parsing, and expanded the .gitignore to include these new directories.
  • Loading branch information
Alexandre Mommers committed Jan 6, 2024
1 parent 722151f commit 6353e4a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions klang/klang/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ bin/

## use to integration ##
/src/test/c/SDL2/
/src/test/c/c/
12 changes: 10 additions & 2 deletions klang/klang/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,24 @@ dependencies {
testImplementation(libs.kotest)
}

task<Copy>("unzipSDL2") {
val unzipSDL2 = task<Copy>("unzipSDL2") {
val cSourceDir = "$projectDir/src/test/c/"
val zipTree = zipTree(file("${cSourceDir}SDL2-headers.zip"))
from(zipTree)
into(cSourceDir)
}

val unzipCHeaders = task<Copy>("unzipCHeaders") {
val cSourceDir = "$projectDir/src/test/c/"
val zipTree = zipTree(file("${cSourceDir}c-headers.zip"))
from(zipTree)
into(cSourceDir)
}

tasks.withType<JavaCompile>().configureEach {
options.compilerArgs.add("--enable-preview")
dependsOn("unzipSDL2")
//dependsOn(unzipSDL2)
dependsOn(unzipCHeaders)
}

tasks.withType<Test>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@ package klang.parser.libclang

import klang.DeclarationRepository
import java.io.File
import java.nio.file.Path
import kotlin.io.path.exists

enum class ParserTechnology {
JNA,
Panama
}

fun parseFile(fileAsString: String, parserTechnology: ParserTechnology = ParserTechnology.Panama): DeclarationRepository {
val file = File(fileAsString)
assert(file.exists())
return parseFile(file, parserTechnology)
fun parseFile(
fileAsString: String,
filePathAsString: String? = null,
headerPathsAsString: Array<String> = arrayOf(),
parserTechnology: ParserTechnology = ParserTechnology.Panama
): DeclarationRepository {
val file = when (filePathAsString != null) {
true -> filePathAsString.let { "$it/$fileAsString" }
.let(::File)
false -> File(fileAsString)
}.also { assert(it.exists()) }
val path = filePathAsString?.let { Path.of(it) }
?.also { assert(it.exists()) }
val headerPaths = headerPathsAsString.map { Path.of(it).also { assert(it.exists()) } }.toTypedArray()
return parseFile(file, path, headerPaths, parserTechnology)
}

internal fun parseFile(file: File, parserTechnology: ParserTechnology = ParserTechnology.Panama) = when (parserTechnology) {
ParserTechnology.JNA -> parseFileWithJna(file.absolutePath)
ParserTechnology.Panama -> parseFileWithPanama(file.absolutePath)
private fun parseFile(
file: File,
filePath: Path? = null,
headerPaths: Array<Path> = arrayOf(),
parserTechnology: ParserTechnology = ParserTechnology.Panama
) = when (parserTechnology) {
ParserTechnology.JNA -> {
assert(filePath == null) { "file path is not supported on JNA" }
assert(headerPaths.isEmpty()) { "header paths is not supported on JNA" }
parseFileWithJna(file.absolutePath)
}
ParserTechnology.Panama -> parseFileWithPanama(file.absolutePath, filePath, headerPaths)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import java.nio.file.Paths

private val logger = KotlinLogging.logger {}

fun parseFileWithPanama(file: String): DeclarationRepository = InMemoryDeclarationRepository().apply {
fun parseFileWithPanama(file: String, filePath: Path?, headerPaths: Array<Path>): DeclarationRepository = InMemoryDeclarationRepository().apply {
val header = Path.of(file)

val clangArguments = inferPlatformIncludePath()
var clangArguments = inferPlatformIncludePath()
?.let { "-I$it" }
?.let { arrayOf(it) }
?: arrayOf()
clangArguments += filePath?.let { "-I${it.toFile().absolutePath}" }
?.let { arrayOf(it) }
?: arrayOf()
clangArguments += headerPaths.map { "-I${it.toFile().absolutePath}" }

val topLevel = parse(
listOf(header),
Expand Down
Binary file added klang/klang/src/test/c/c-headers.zip
Binary file not shown.
26 changes: 26 additions & 0 deletions klang/klang/src/test/kotlin/klang/parser/libclang/SDL2ItTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package klang.parser.libclang

import klang.parser.INTEGRATION_ENABLED
import klang.parser.ParserTestCommon


class SDL2ItTest : ParserTestCommon({

"test SDL2 parsing".config(enabled = INTEGRATION_ENABLED || true) {

// Given
val filePath = "src/test/c/"
val fileToParse = "SDL2/SDL.h"
val headerPaths = arrayOf("src/test/c/c/include")

// When
val repository = parseFile(fileToParse, filePath, headerPaths)

// Then
repository.apply {
println(declarations.size)
}


}
})

0 comments on commit 6353e4a

Please sign in to comment.