Add optional compilation start plugin option + remove exitProcess

This commit is contained in:
Tamas Vajk
2021-11-09 16:06:57 +01:00
committed by Ian Lynagh
parent 6fb5854589
commit 69e8db06cb
3 changed files with 31 additions and 15 deletions

View File

@@ -23,6 +23,13 @@ class KotlinExtractorCommandLineProcessor : CommandLineProcessor {
description = "Check whether different invocations produce identical TRAP",
required = false,
allowMultipleOccurrences = false
),
CliOption(
optionName = OPTION_COMPILATION_STARTTIME,
valueDescription = "The start time of the compilation as a Unix timestamp",
description = "The start time of the compilation as a Unix timestamp",
required = false,
allowMultipleOccurrences = false
)
)
@@ -31,13 +38,18 @@ class KotlinExtractorCommandLineProcessor : CommandLineProcessor {
value: String,
configuration: CompilerConfiguration
) = when (option.optionName) {
"invocationTrapFile" -> configuration.put(KEY_INVOCATION_TRAP_FILE, value)
"checkTrapIdentical" ->
OPTION_INVOCATION_TRAP_FILE -> configuration.put(KEY_INVOCATION_TRAP_FILE, value)
OPTION_CHECK_TRAP_IDENTICAL ->
when (value) {
"true" -> configuration.put(KEY_CHECK_TRAP_IDENTICAL, true)
"false" -> configuration.put(KEY_CHECK_TRAP_IDENTICAL, false)
else -> error("kotlin extractor: Bad argument $value for checkTrapIdentical")
"true" -> configuration.put(KEY_CHECK_TRAP_IDENTICAL, true)
"false" -> configuration.put(KEY_CHECK_TRAP_IDENTICAL, false)
else -> error("kotlin extractor: Bad argument $value for $OPTION_CHECK_TRAP_IDENTICAL")
}
OPTION_COMPILATION_STARTTIME ->
when (val v = value.toLongOrNull()) {
is Long -> configuration.put(KEY_COMPILATION_STARTTIME, v)
else -> error("kotlin extractor: Bad argument $value for $OPTION_COMPILATION_STARTTIME")
}
else -> error("kotlin extractor: Bad option: ${option.optionName}")
}
}
@@ -46,3 +58,5 @@ private val OPTION_INVOCATION_TRAP_FILE = "invocationTrapFile"
val KEY_INVOCATION_TRAP_FILE = CompilerConfigurationKey<String>(OPTION_INVOCATION_TRAP_FILE)
private val OPTION_CHECK_TRAP_IDENTICAL = "checkTrapIdentical"
val KEY_CHECK_TRAP_IDENTICAL= CompilerConfigurationKey<Boolean>(OPTION_CHECK_TRAP_IDENTICAL)
private val OPTION_COMPILATION_STARTTIME = "compilationStartTime"
val KEY_COMPILATION_STARTTIME= CompilerConfigurationKey<Long>(OPTION_COMPILATION_STARTTIME)

View File

@@ -11,10 +11,14 @@ class KotlinExtractorComponentRegistrar : ComponentRegistrar {
configuration: CompilerConfiguration
) {
val invocationTrapFile = configuration[KEY_INVOCATION_TRAP_FILE]
if(invocationTrapFile == null) {
if (invocationTrapFile == null) {
throw Exception("Required argument for TRAP invocation file not given")
}
val checkTrapIdentical = configuration[KEY_CHECK_TRAP_IDENTICAL]
IrGenerationExtension.registerExtension(project, KotlinExtractorExtension(invocationTrapFile, checkTrapIdentical ?: false))
val compilationStartTime = configuration[KEY_COMPILATION_STARTTIME]
IrGenerationExtension.registerExtension(project, KotlinExtractorExtension(
invocationTrapFile,
checkTrapIdentical ?: false,
compilationStartTime))
}
}

View File

@@ -14,7 +14,6 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.name.FqName
import java.io.File
import java.io.FileOutputStream
import java.io.PrintWriter
@@ -23,26 +22,26 @@ import java.nio.file.Files
import java.nio.file.Paths
import java.util.*
import java.util.zip.GZIPOutputStream
import com.intellij.openapi.vfs.StandardFileSystems
import com.semmle.extractor.java.OdasaOutput
import com.semmle.extractor.java.OdasaOutput.TrapFileManager
import com.semmle.util.files.FileUtil
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.types.Variance
import kotlin.system.exitProcess
class KotlinExtractorExtension(private val invocationTrapFile: String, private val checkTrapIdentical: Boolean) : IrGenerationExtension {
class KotlinExtractorExtension(private val invocationTrapFile: String, private val checkTrapIdentical: Boolean, private val compilationStartTime: Long?) : IrGenerationExtension {
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
val startTimeMs = System.currentTimeMillis()
// This default should be kept in sync with language-packs/java/tools/kotlin-extractor
// This default should be kept in sync with com.semmle.extractor.java.interceptors.KotlinInterceptor.initializeExtractionContext
val trapDir = File(System.getenv("CODEQL_EXTRACTOR_JAVA_TRAP_DIR").takeUnless { it.isNullOrEmpty() } ?: "kotlin-extractor/trap")
FileOutputStream(File(invocationTrapFile), true).bufferedWriter().use { invocationTrapFileBW ->
val lm = TrapLabelManager()
val tw = TrapWriter(lm, invocationTrapFileBW)
// The python wrapper has already defined #compilation = *
// The interceptor has already defined #compilation = *
val compilation: Label<DbCompilation> = StringLabel("compilation")
tw.writeCompilation_started(compilation)
if (compilationStartTime != null) {
tw.writeCompilation_compiler_times(compilation, -1.0, (System.currentTimeMillis()-compilationStartTime)/1000.0)
}
tw.flush()
val logCounter = LogCounter()
val logger = Logger(logCounter, tw)
@@ -67,7 +66,6 @@ class KotlinExtractorExtension(private val invocationTrapFile: String, private v
tw.writeCompilation_finished(compilation, -1.0, compilationTimeMs.toDouble() / 1000)
tw.flush()
}
exitProcess(0)
}
}