Skip to content

Commit 643817e

Browse files
authored
Merge pull request #15477 from igfoo/igfoo/kot_path_trans
Kotlin: Add path transformer support
2 parents 8d53bce + 68f2677 commit 643817e

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.github.codeql
22

33
import com.github.codeql.utils.versions.usesK2
44
import com.semmle.util.files.FileUtil
5+
import com.semmle.util.trap.pathtransformers.PathTransformer
56
import java.io.BufferedInputStream
67
import java.io.BufferedOutputStream
78
import java.io.BufferedReader
@@ -371,9 +372,9 @@ private fun doFile(
371372
context.clear()
372373
}
373374

374-
val srcFileRelativePath = srcFilePath.replace(':', '_')
375+
val srcFileRelativePath = PathTransformer.std().fileAsDatabaseString(File(srcFilePath))
375376

376-
val dbSrcFilePath = Paths.get("$dbSrcDir/$srcFileRelativePath")
377+
val dbSrcFilePath = FileUtil.appendAbsolutePath(dbSrcDir, srcFileRelativePath).toPath()
377378
val dbSrcDirPath = dbSrcFilePath.parent
378379
Files.createDirectories(dbSrcDirPath)
379380
val srcTmpFile =
@@ -385,7 +386,7 @@ private fun doFile(
385386
srcTmpFile.outputStream().use { Files.copy(Paths.get(srcFilePath), it) }
386387
srcTmpFile.renameTo(dbSrcFilePath.toFile())
387388

388-
val trapFileName = "$dbTrapDir/$srcFileRelativePath.trap"
389+
val trapFileName = FileUtil.appendAbsolutePath(dbTrapDir, "$srcFileRelativePath.trap").getAbsolutePath()
389390
val trapFileWriter = getTrapFileWriter(compression, logger, trapFileName)
390391

391392
if (checkTrapIdentical || !trapFileWriter.exists()) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| file:///src/kotlin_source.kt:1:1:2:1 | Foo |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
3+
from Class c
4+
where c.fromSource()
5+
select c
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Foo {
2+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from create_database_utils import *
2+
3+
path_transformer_file = 'path_transformer'
4+
root = os.getcwd().replace('\\', '/')
5+
with open(path_transformer_file, 'w') as f:
6+
f.write('#/src\n' + root + '//\n')
7+
os.environ['SEMMLE_PATH_TRANSFORMER'] = root + '/' + path_transformer_file
8+
9+
run_codeql_database_create(["kotlinc kotlin_source.kt"], lang="java")
10+
files = ['test-db/trap/java/src/kotlin_source.kt.trap.gz', 'test-db/src/src/kotlin_source.kt']
11+
exists = list(map(os.path.exists, files))
12+
if exists != [True] * 2:
13+
print(exists)
14+
raise Exception("Didn't get expected filed")

0 commit comments

Comments
 (0)