mirror of
https://github.com/github/codeql.git
synced 2025-12-21 11:16:30 +01:00
Kotlin: Populate the compilation_compiling_files table
This commit is contained in:
@@ -46,7 +46,11 @@ class KotlinExtractorExtension(private val invocationTrapFile: String, private v
|
||||
logger.flush()
|
||||
val srcDir = File(System.getenv("CODEQL_EXTRACTOR_JAVA_SOURCE_ARCHIVE_DIR").takeUnless { it.isNullOrEmpty() } ?: "kotlin-extractor/src")
|
||||
srcDir.mkdirs()
|
||||
moduleFragment.files.map { doFile(invocationTrapFile, lm, invocationTrapFileBW, checkTrapIdentical, logCounter, trapDir, srcDir, it) }
|
||||
moduleFragment.files.mapIndexed { index: Int, file: IrFile ->
|
||||
val fileTrapWriter = FileTrapWriter(lm, invocationTrapFileBW, file)
|
||||
fileTrapWriter.writeCompilation_compiling_files(compilation, index, fileTrapWriter.fileId)
|
||||
doFile(invocationTrapFile, fileTrapWriter, checkTrapIdentical, logCounter, trapDir, srcDir, file)
|
||||
}
|
||||
logger.printLimitedWarningCounts()
|
||||
// We don't want the compiler to continue and generate class
|
||||
// files etc, so we just exit when we are finished extracting.
|
||||
@@ -213,10 +217,19 @@ open class TrapWriter (val lm: TrapLabelManager, val bw: BufferedWriter) {
|
||||
class FileTrapWriter (
|
||||
lm: TrapLabelManager,
|
||||
bw: BufferedWriter,
|
||||
val fileLabel: String,
|
||||
val file: IrFile
|
||||
val irFile: IrFile
|
||||
): TrapWriter (lm, bw) {
|
||||
private val fileEntry = file.fileEntry
|
||||
private val fileEntry = irFile.fileEntry
|
||||
val fileId = {
|
||||
val filePath = irFile.path
|
||||
val fileLabel = "@\"$filePath;sourcefile\""
|
||||
val file = File(filePath)
|
||||
val basename = file.nameWithoutExtension
|
||||
val extension = file.extension
|
||||
val id: Label<DbFile> = getLabelFor(fileLabel)
|
||||
writeFiles(id, filePath, basename, extension, 0)
|
||||
id
|
||||
}()
|
||||
|
||||
fun getLocation(e: IrElement): Label<DbLocation> {
|
||||
return getLocation(e.startOffset, e.endOffset)
|
||||
@@ -227,11 +240,11 @@ class FileTrapWriter (
|
||||
val startColumn = if(unknownLoc) 0 else fileEntry.getColumnNumber(startOffset) + 1
|
||||
val endLine = if(unknownLoc) 0 else fileEntry.getLineNumber(endOffset) + 1
|
||||
val endColumn = if(unknownLoc) 0 else fileEntry.getColumnNumber(endOffset)
|
||||
val fileId: Label<DbFile> = if (unknownLoc) unknownFileId else getLabelFor(fileLabel)
|
||||
return getLocation(fileId, startLine, startColumn, endLine, endColumn)
|
||||
val locFileId: Label<DbFile> = if (unknownLoc) unknownFileId else fileId
|
||||
return getLocation(locFileId, startLine, startColumn, endLine, endColumn)
|
||||
}
|
||||
fun getLocationString(e: IrElement): String {
|
||||
val path = file.path
|
||||
val path = irFile.path
|
||||
if (e.startOffset == -1 && e.endOffset == -1) {
|
||||
return "unknown location, while processing $path"
|
||||
} else {
|
||||
@@ -281,22 +294,17 @@ private fun equivalentTrap(f1: File, f2: File): Boolean {
|
||||
}
|
||||
}
|
||||
|
||||
fun doFile(invocationTrapFile: String, trapLabelManager: TrapLabelManager, invocationTrapFileBW: BufferedWriter, checkTrapIdentical: Boolean, logCounter: LogCounter, trapDir: File, srcDir: File, declaration: IrFile) {
|
||||
val filePath = declaration.path
|
||||
val fileLabel = "@\"$filePath;sourcefile\""
|
||||
val fileTrapWriter = FileTrapWriter(trapLabelManager, invocationTrapFileBW, fileLabel, declaration)
|
||||
fun doFile(invocationTrapFile: String, fileTrapWriter: FileTrapWriter, checkTrapIdentical: Boolean, logCounter: LogCounter, trapDir: File, srcDir: File, file: IrFile) {
|
||||
val filePath = file.path
|
||||
val logger = FileLogger(logCounter, fileTrapWriter)
|
||||
logger.info("Extracting file $filePath")
|
||||
logger.flush()
|
||||
val file = File(filePath)
|
||||
val basename = file.nameWithoutExtension
|
||||
val extension = file.extension
|
||||
val dest = Paths.get("$srcDir/${declaration.path}")
|
||||
val dest = Paths.get("$srcDir/${file.path}")
|
||||
val destDir = dest.getParent()
|
||||
Files.createDirectories(destDir)
|
||||
val srcTmpFile = File.createTempFile(dest.getFileName().toString() + ".", ".src.tmp", destDir.toFile())
|
||||
val srcTmpOS = FileOutputStream(srcTmpFile)
|
||||
Files.copy(Paths.get(declaration.path), srcTmpOS)
|
||||
Files.copy(Paths.get(file.path), srcTmpOS)
|
||||
srcTmpOS.close()
|
||||
srcTmpFile.renameTo(dest.toFile())
|
||||
|
||||
@@ -307,11 +315,9 @@ fun doFile(invocationTrapFile: String, trapLabelManager: TrapLabelManager, invoc
|
||||
val trapTmpFile = File.createTempFile("$filePath.", ".trap.tmp", trapFileDir)
|
||||
trapTmpFile.bufferedWriter().use { trapFileBW ->
|
||||
trapFileBW.write("// Generated by invocation ${invocationTrapFile.replace("\n", "\n// ")}\n")
|
||||
val tw = FileTrapWriter(TrapLabelManager(), trapFileBW, fileLabel, declaration)
|
||||
val id: Label<DbFile> = tw.getLabelFor(fileLabel)
|
||||
tw.writeFiles(id, filePath, basename, extension, 0)
|
||||
val fileExtractor = KotlinFileExtractor(logger, tw, declaration)
|
||||
fileExtractor.extractFile(id)
|
||||
val tw = FileTrapWriter(TrapLabelManager(), trapFileBW, file)
|
||||
val fileExtractor = KotlinFileExtractor(logger, tw, file)
|
||||
fileExtractor.extractFileContents(tw.fileId)
|
||||
}
|
||||
if (checkTrapIdentical && trapFile.exists()) {
|
||||
if(equivalentTrap(trapTmpFile, trapFile)) {
|
||||
@@ -350,7 +356,7 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
|
||||
extractFileClass(file)
|
||||
}
|
||||
|
||||
fun extractFile(id: Label<DbFile>) {
|
||||
fun extractFileContents(id: Label<DbFile>) {
|
||||
val pkg = file.fqName.asString()
|
||||
val pkgId = extractPackage(pkg)
|
||||
tw.writeCupackage(id, pkgId)
|
||||
|
||||
Reference in New Issue
Block a user