@@ -3,7 +3,6 @@ package com.github.codeql
33import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
44import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
55import org.jetbrains.kotlin.ir.declarations.*
6- import org.jetbrains.kotlin.ir.util.*
76import java.io.File
87import java.io.FileOutputStream
98import java.nio.file.Files
@@ -89,30 +88,33 @@ fun doFile(invocationTrapFile: String,
8988 fileTrapWriter : FileTrapWriter ,
9089 checkTrapIdentical : Boolean ,
9190 logCounter : LogCounter ,
92- trapDir : File ,
93- srcDir : File ,
94- file : IrFile ,
91+ dbTrapDir : File ,
92+ dbSrcDir : File ,
93+ srcFile : IrFile ,
9594 primitiveTypeMapping : PrimitiveTypeMapping ,
9695 pluginContext : IrPluginContext ,
9796 genericSpecialisationsExtracted : MutableSet <String >) {
98- val filePath = file .path
97+ val srcFilePath = srcFile .path
9998 val logger = FileLogger (logCounter, fileTrapWriter)
100- logger.info(" Extracting file $filePath " )
99+ logger.info(" Extracting file $srcFilePath " )
101100 logger.flush()
102- val dest = Paths .get(" $srcDir /${file.path} " )
103- val destDir = dest.getParent()
104- Files .createDirectories(destDir)
105- val srcTmpFile = File .createTempFile(dest.getFileName().toString() + " ." , " .src.tmp" , destDir.toFile())
106- val srcTmpOS = FileOutputStream (srcTmpFile)
107- Files .copy(Paths .get(file.path), srcTmpOS)
108- srcTmpOS.close()
109- srcTmpFile.renameTo(dest.toFile())
110101
111- val trapFile = File (" $trapDir /$filePath .trap" )
112- val trapFileDir = trapFile.getParentFile()
102+ val dbSrcFilePath = Paths .get(" $dbSrcDir /$srcFilePath " )
103+ val dbSrcDirPath = dbSrcFilePath.parent
104+ Files .createDirectories(dbSrcDirPath)
105+ val srcTmpFile = File .createTempFile(dbSrcFilePath.fileName.toString() + " ." , " .src.tmp" , dbSrcDirPath.toFile())
106+ srcTmpFile.outputStream().use {
107+ Files .copy(Paths .get(srcFilePath), it)
108+ }
109+ srcTmpFile.renameTo(dbSrcFilePath.toFile())
110+
111+ val trapFile = File (" $dbTrapDir /$srcFilePath .trap" )
112+ val trapFileDir = trapFile.parentFile
113113 trapFileDir.mkdirs()
114+
114115 if (checkTrapIdentical || ! trapFile.exists()) {
115- val trapTmpFile = File .createTempFile(" $filePath ." , " .trap.tmp" , trapFileDir)
116+ val trapTmpFile = File .createTempFile(" $srcFilePath ." , " .trap.tmp" , trapFileDir)
117+ var hasError = false
116118 trapTmpFile.bufferedWriter().use { trapFileBW ->
117119 // We want our comments to be the first thing in the file,
118120 // so start off with a mere TrapWriter
@@ -121,29 +123,41 @@ fun doFile(invocationTrapFile: String,
121123 tw.writeComment(" Part of invocation $invocationTrapFile " )
122124 // Now elevate to a SourceFileTrapWriter, and populate the
123125 // file information
124- val sftw = tw.makeSourceFileTrapWriter(file, true )
125- val externalClassExtractor = ExternalClassExtractor (logger, invocationTrapFile, file.path, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
126- val fileExtractor = KotlinFileExtractor (logger, sftw, file.path, null , externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
127- fileExtractor.extractFileContents(file, sftw.fileId)
128- externalClassExtractor.extractExternalClasses()
126+ val sftw = tw.makeSourceFileTrapWriter(srcFile, true )
127+ val externalClassExtractor = ExternalClassExtractor (logger, invocationTrapFile, srcFilePath, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
128+ val fileExtractor = KotlinFileExtractor (logger, sftw, srcFilePath, null , externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
129+ try {
130+ fileExtractor.extractFileContents(srcFile, sftw.fileId)
131+ externalClassExtractor.extractExternalClasses()
132+ } catch (e: Exception ) {
133+ hasError = true
134+ logger.error(" Failed to extract '$srcFilePath '" , e)
135+ }
129136 }
130- if (checkTrapIdentical && trapFile.exists()) {
131- if (equivalentTrap(trapTmpFile, trapFile)) {
132- if (! trapTmpFile.delete()) {
133- logger.warn(Severity .WarnLow , " Failed to delete $trapTmpFile " )
137+
138+ if (hasError) {
139+ if (! trapTmpFile.delete()) {
140+ logger.warn(Severity .WarnLow , " Failed to delete $trapTmpFile " )
141+ }
142+ } else {
143+ if (checkTrapIdentical && trapFile.exists()) {
144+ if (equivalentTrap(trapTmpFile, trapFile)) {
145+ if (! trapTmpFile.delete()) {
146+ logger.warn(Severity .WarnLow , " Failed to delete $trapTmpFile " )
147+ }
148+ } else {
149+ val trapDifferentFile = File .createTempFile(" $srcFilePath ." , " .trap.different" , dbTrapDir)
150+ if (trapTmpFile.renameTo(trapDifferentFile)) {
151+ logger.warn(Severity .Warn , " TRAP difference: $trapFile vs $trapDifferentFile " )
152+ } else {
153+ logger.warn(Severity .WarnLow , " Failed to rename $trapTmpFile to $trapFile " )
154+ }
134155 }
135156 } else {
136- val trapDifferentFile = File .createTempFile(" $filePath ." , " .trap.different" , trapDir)
137- if (trapTmpFile.renameTo(trapDifferentFile)) {
138- logger.warn(Severity .Warn , " TRAP difference: $trapFile vs $trapDifferentFile " )
139- } else {
157+ if (! trapTmpFile.renameTo(trapFile)) {
140158 logger.warn(Severity .WarnLow , " Failed to rename $trapTmpFile to $trapFile " )
141159 }
142160 }
143- } else {
144- if (! trapTmpFile.renameTo(trapFile)) {
145- logger.warn(Severity .WarnLow , " Failed to rename $trapTmpFile to $trapFile " )
146- }
147161 }
148162 }
149163}
0 commit comments