diff --git a/java/kotlin-extractor2/src/main/kotlin/KotlinExtractor.kt b/java/kotlin-extractor2/src/main/kotlin/KotlinExtractor.kt index 696691bebe8..4f4b4d772f4 100644 --- a/java/kotlin-extractor2/src/main/kotlin/KotlinExtractor.kt +++ b/java/kotlin-extractor2/src/main/kotlin/KotlinExtractor.kt @@ -211,41 +211,52 @@ OLD: KE1 if (psiFile is KtFile) { analyze(psiFile) { val fileExtractionProblems = FileExtractionProblems(invocationExtractionProblems) - val fileDiagnosticTrapWriter = dtw.makeSourceFileTrapWriter(psiFile, true) + try { + val fileDiagnosticTrapWriter = dtw.makeSourceFileTrapWriter(psiFile, true) /* OLD: KE1 loggerBase.setFileNumber(fileIndex) */ - fileDiagnosticTrapWriter.writeCompilation_compiling_files( - compilation, - fileIndex, - fileDiagnosticTrapWriter.fileId - ) - doFile( - compression, + fileDiagnosticTrapWriter.writeCompilation_compiling_files( + compilation, + fileIndex, + fileDiagnosticTrapWriter.fileId + ) + doFile( + compression, /* OLD: KE1 fileExtractionProblems, invocationTrapFile, */ - fileDiagnosticTrapWriter, - loggerBase, - checkTrapIdentical, - trapDir, - srcDir, - psiFile, + fileDiagnosticTrapWriter, + loggerBase, + checkTrapIdentical, + trapDir, + srcDir, + psiFile, /* OLD: KE1 primitiveTypeMapping, pluginContext, globalExtensionState */ - ) - fileDiagnosticTrapWriter.writeCompilation_compiling_files_completed( - compilation, - fileIndex, - fileExtractionProblems.extractionResult() - ) + ) + fileDiagnosticTrapWriter.writeCompilation_compiling_files_completed( + compilation, + fileIndex, + fileExtractionProblems.extractionResult() + ) + // We catch Throwable rather than Exception, as we want to + // continue trying to extract everything else even if we get a + // stack overflow or an assertion failure in one file. + } catch (e: Throwable) { +/* +OLD: KE1 + logger.error("Extraction failed while extracting '${psiFile.virtualFilePath}'.", e) + fileExtractionProblems.setNonRecoverableProblem() +*/ + } } fileIndex += 1 } else { @@ -485,65 +496,52 @@ OLD: KE1 val trapFileName = FileUtil.appendAbsolutePath(dbTrapDir, "$srcFileRelativePath.trap").getAbsolutePath() val trapFileWriter = getTrapFileWriter(compression, logger, checkTrapIdentical, trapFileName) - try { - trapFileWriter.run { trapFileBW -> - TODO() + trapFileWriter.run { trapFileBW -> + TODO() /* OLD: KE1 - // We want our comments to be the first thing in the file, - // so start off with a mere TrapWriter - val tw = - PlainTrapWriter( - loggerBase, - TrapLabelManager(), - trapFileBW, - fileTrapWriter.getDiagnosticTrapWriter() - ) - tw.writeComment("Generated by the CodeQL Kotlin extractor for kotlin source code") - tw.writeComment("Part of invocation $invocationTrapFile") - // Now elevate to a SourceFileTrapWriter, and populate the - // file information - val sftw = tw.makeSourceFileTrapWriter(srcFile, true) - val externalDeclExtractor = - ExternalDeclExtractor( - logger, - compression, - invocationTrapFile, - srcFilePath, - primitiveTypeMapping, - pluginContext, - globalExtensionState, - fileTrapWriter.getDiagnosticTrapWriter() - ) - val linesOfCode = LinesOfCode(logger, sftw, srcFile) - val fileExtractor = - KotlinFileExtractor( - logger, - sftw, - linesOfCode, - srcFilePath, - null, - externalDeclExtractor, - primitiveTypeMapping, - pluginContext, - KotlinFileExtractor.DeclarationStack(), - globalExtensionState - ) + // We want our comments to be the first thing in the file, + // so start off with a mere TrapWriter + val tw = + PlainTrapWriter( + loggerBase, + TrapLabelManager(), + trapFileBW, + fileTrapWriter.getDiagnosticTrapWriter() + ) + tw.writeComment("Generated by the CodeQL Kotlin extractor for kotlin source code") + tw.writeComment("Part of invocation $invocationTrapFile") + // Now elevate to a SourceFileTrapWriter, and populate the + // file information + val sftw = tw.makeSourceFileTrapWriter(srcFile, true) + val externalDeclExtractor = + ExternalDeclExtractor( + logger, + compression, + invocationTrapFile, + srcFilePath, + primitiveTypeMapping, + pluginContext, + globalExtensionState, + fileTrapWriter.getDiagnosticTrapWriter() + ) + val linesOfCode = LinesOfCode(logger, sftw, srcFile) + val fileExtractor = + KotlinFileExtractor( + logger, + sftw, + linesOfCode, + srcFilePath, + null, + externalDeclExtractor, + primitiveTypeMapping, + pluginContext, + KotlinFileExtractor.DeclarationStack(), + globalExtensionState + ) - fileExtractor.extractFileContents(srcFile, sftw.fileId) - externalDeclExtractor.extractExternalClasses() + fileExtractor.extractFileContents(srcFile, sftw.fileId) + externalDeclExtractor.extractExternalClasses() */ - } - - // We catch Throwable rather than Exception, as we want to - // continue trying to extract everything else even if we get a - // stack overflow or an assertion failure in one file. - } catch (e: Throwable) { -/* -OLD: KE1 - logger.error("Failed to extract '$srcFilePath'. " + trapFileWriter.debugInfo(), e) - context.clear() - fileExtractionProblems.setNonRecoverableProblem() -*/ - } + } } diff --git a/java/kotlin-extractor2/src/main/kotlin/TrapFileWriter.kt b/java/kotlin-extractor2/src/main/kotlin/TrapFileWriter.kt index 52bd6bc5c9d..f062d93bc82 100644 --- a/java/kotlin-extractor2/src/main/kotlin/TrapFileWriter.kt +++ b/java/kotlin-extractor2/src/main/kotlin/TrapFileWriter.kt @@ -83,32 +83,32 @@ abstract class TrapFileWriter( logger.info("Will write TRAP file $realFile") val tempFile = File.createTempFile(realFile.getName() + ".", ".trap.tmp" + extension, parentDir) - logger.debug("Writing temporary TRAP file $tempFile") - getWriter(tempFile).use { bw -> block(bw) } + try { + logger.debug("Writing temporary TRAP file $tempFile") + getWriter(tempFile).use { bw -> block(bw) } - if (checkTrapIdentical && exists()) { - if (equivalentTrap(getReader(tempFile), getReader(realFile))) { - deleteTemp(tempFile) + if (checkTrapIdentical && exists()) { + if (equivalentTrap(getReader(tempFile), getReader(realFile))) { + deleteTemp(tempFile) + } else { + renameTempToDifferent(tempFile) + } } else { - renameTempToDifferent(tempFile) + renameTempToReal(tempFile) } - } else { - renameTempToReal(tempFile) + // We catch Throwable rather than Exception, as we want to + // continue trying to extract everything else even if we get a + // stack overflow or an assertion failure in one file. + } catch (e: Throwable) { + logger.error("Extraction failed while writing '$tempFile'.", e) +/* +OLD: KE1 + fileExtractionProblems.setNonRecoverableProblem() +*/ } } } -/* -OLD: KE1 - fun debugInfo(): String { - if (this::tempFile.isInitialized) { - return "Partial TRAP file location is $tempFile" - } else { - return "Temporary file not yet created." - } - } -*/ - private fun exists(): Boolean { return realFile.exists() }