Merge pull request #15477 from igfoo/igfoo/kot_path_trans

Kotlin: Add path transformer support
This commit is contained in:
Ian Lynagh
2024-02-02 15:34:14 +00:00
committed by GitHub
5 changed files with 26 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package com.github.codeql
import com.github.codeql.utils.versions.usesK2
import com.semmle.util.files.FileUtil
import com.semmle.util.trap.pathtransformers.PathTransformer
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.BufferedReader
@@ -371,9 +372,9 @@ private fun doFile(
context.clear()
}
val srcFileRelativePath = srcFilePath.replace(':', '_')
val srcFileRelativePath = PathTransformer.std().fileAsDatabaseString(File(srcFilePath))
val dbSrcFilePath = Paths.get("$dbSrcDir/$srcFileRelativePath")
val dbSrcFilePath = FileUtil.appendAbsolutePath(dbSrcDir, srcFileRelativePath).toPath()
val dbSrcDirPath = dbSrcFilePath.parent
Files.createDirectories(dbSrcDirPath)
val srcTmpFile =
@@ -385,7 +386,7 @@ private fun doFile(
srcTmpFile.outputStream().use { Files.copy(Paths.get(srcFilePath), it) }
srcTmpFile.renameTo(dbSrcFilePath.toFile())
val trapFileName = "$dbTrapDir/$srcFileRelativePath.trap"
val trapFileName = FileUtil.appendAbsolutePath(dbTrapDir, "$srcFileRelativePath.trap").getAbsolutePath()
val trapFileWriter = getTrapFileWriter(compression, logger, trapFileName)
if (checkTrapIdentical || !trapFileWriter.exists()) {

View File

@@ -0,0 +1 @@
| file:///src/kotlin_source.kt:1:1:2:1 | Foo |

View File

@@ -0,0 +1,5 @@
import java
from Class c
where c.fromSource()
select c

View File

@@ -0,0 +1,2 @@
class Foo {
}

View File

@@ -0,0 +1,14 @@
from create_database_utils import *
path_transformer_file = 'path_transformer'
root = os.getcwd().replace('\\', '/')
with open(path_transformer_file, 'w') as f:
f.write('#/src\n' + root + '//\n')
os.environ['SEMMLE_PATH_TRANSFORMER'] = root + '/' + path_transformer_file
run_codeql_database_create(["kotlinc kotlin_source.kt"], lang="java")
files = ['test-db/trap/java/src/kotlin_source.kt.trap.gz', 'test-db/src/src/kotlin_source.kt']
exists = list(map(os.path.exists, files))
if exists != [True] * 2:
print(exists)
raise Exception("Didn't get expected filed")