Skip to content

Commit ac0c9df

Browse files
authored
Merge pull request github#87 from github/kotlin-changed-interception
Add optional compilation start plugin option + remove exitProcess
2 parents a16eb58 + f32e393 commit ac0c9df

3 files changed

Lines changed: 54 additions & 16 deletions

File tree

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ class KotlinExtractorCommandLineProcessor : CommandLineProcessor {
2323
description = "Check whether different invocations produce identical TRAP",
2424
required = false,
2525
allowMultipleOccurrences = false
26+
),
27+
CliOption(
28+
optionName = OPTION_COMPILATION_STARTTIME,
29+
valueDescription = "The start time of the compilation as a Unix timestamp",
30+
description = "The start time of the compilation as a Unix timestamp",
31+
required = false,
32+
allowMultipleOccurrences = false
33+
),
34+
CliOption(
35+
optionName = OPTION_EXIT_AFTER_EXTRACTION,
36+
valueDescription = "Specify whether to call exitProcess after the extraction has completed",
37+
description = "Specify whether to call exitProcess after the extraction has completed",
38+
required = false,
39+
allowMultipleOccurrences = false
2640
)
2741
)
2842

@@ -31,18 +45,30 @@ class KotlinExtractorCommandLineProcessor : CommandLineProcessor {
3145
value: String,
3246
configuration: CompilerConfiguration
3347
) = when (option.optionName) {
34-
"invocationTrapFile" -> configuration.put(KEY_INVOCATION_TRAP_FILE, value)
35-
"checkTrapIdentical" ->
36-
when (value) {
37-
"true" -> configuration.put(KEY_CHECK_TRAP_IDENTICAL, true)
38-
"false" -> configuration.put(KEY_CHECK_TRAP_IDENTICAL, false)
39-
else -> error("kotlin extractor: Bad argument $value for checkTrapIdentical")
40-
}
48+
OPTION_INVOCATION_TRAP_FILE -> configuration.put(KEY_INVOCATION_TRAP_FILE, value)
49+
OPTION_CHECK_TRAP_IDENTICAL -> processBooleanOption(value, OPTION_CHECK_TRAP_IDENTICAL, KEY_CHECK_TRAP_IDENTICAL, configuration)
50+
OPTION_EXIT_AFTER_EXTRACTION -> processBooleanOption(value, OPTION_EXIT_AFTER_EXTRACTION, KEY_EXIT_AFTER_EXTRACTION, configuration)
51+
OPTION_COMPILATION_STARTTIME ->
52+
when (val v = value.toLongOrNull()) {
53+
is Long -> configuration.put(KEY_COMPILATION_STARTTIME, v)
54+
else -> error("kotlin extractor: Bad argument $value for $OPTION_COMPILATION_STARTTIME")
55+
}
4156
else -> error("kotlin extractor: Bad option: ${option.optionName}")
4257
}
58+
59+
private fun processBooleanOption(value: String, optionName: String, configKey: CompilerConfigurationKey<Boolean>, configuration: CompilerConfiguration) =
60+
when (value) {
61+
"true" -> configuration.put(configKey, true)
62+
"false" -> configuration.put(configKey, false)
63+
else -> error("kotlin extractor: Bad argument $value for $optionName")
64+
}
4365
}
4466

4567
private val OPTION_INVOCATION_TRAP_FILE = "invocationTrapFile"
4668
val KEY_INVOCATION_TRAP_FILE = CompilerConfigurationKey<String>(OPTION_INVOCATION_TRAP_FILE)
4769
private val OPTION_CHECK_TRAP_IDENTICAL = "checkTrapIdentical"
4870
val KEY_CHECK_TRAP_IDENTICAL= CompilerConfigurationKey<Boolean>(OPTION_CHECK_TRAP_IDENTICAL)
71+
private val OPTION_COMPILATION_STARTTIME = "compilationStartTime"
72+
val KEY_COMPILATION_STARTTIME= CompilerConfigurationKey<Long>(OPTION_COMPILATION_STARTTIME)
73+
private val OPTION_EXIT_AFTER_EXTRACTION = "exitAfterExtraction"
74+
val KEY_EXIT_AFTER_EXTRACTION= CompilerConfigurationKey<Boolean>(OPTION_EXIT_AFTER_EXTRACTION)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ class KotlinExtractorComponentRegistrar : ComponentRegistrar {
1111
configuration: CompilerConfiguration
1212
) {
1313
val invocationTrapFile = configuration[KEY_INVOCATION_TRAP_FILE]
14-
if(invocationTrapFile == null) {
14+
if (invocationTrapFile == null) {
1515
throw Exception("Required argument for TRAP invocation file not given")
1616
}
17-
val checkTrapIdentical = configuration[KEY_CHECK_TRAP_IDENTICAL]
18-
IrGenerationExtension.registerExtension(project, KotlinExtractorExtension(invocationTrapFile, checkTrapIdentical ?: false))
17+
IrGenerationExtension.registerExtension(project, KotlinExtractorExtension(
18+
invocationTrapFile,
19+
configuration[KEY_CHECK_TRAP_IDENTICAL] ?: false,
20+
configuration[KEY_COMPILATION_STARTTIME],
21+
configuration[KEY_EXIT_AFTER_EXTRACTION] ?: false))
1922
}
2023
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
1414
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
1515
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
1616
import org.jetbrains.kotlin.ir.types.*
17-
import org.jetbrains.kotlin.name.FqName
1817
import java.io.File
1918
import java.io.FileOutputStream
2019
import java.io.PrintWriter
@@ -23,7 +22,6 @@ import java.nio.file.Files
2322
import java.nio.file.Paths
2423
import java.util.*
2524
import java.util.zip.GZIPOutputStream
26-
import com.intellij.openapi.vfs.StandardFileSystems
2725
import com.semmle.extractor.java.OdasaOutput
2826
import com.semmle.extractor.java.OdasaOutput.TrapFileManager
2927
import com.semmle.util.files.FileUtil
@@ -32,17 +30,26 @@ import org.jetbrains.kotlin.ir.util.*
3230
import org.jetbrains.kotlin.types.Variance
3331
import kotlin.system.exitProcess
3432

35-
class KotlinExtractorExtension(private val invocationTrapFile: String, private val checkTrapIdentical: Boolean) : IrGenerationExtension {
33+
class KotlinExtractorExtension(
34+
private val invocationTrapFile: String,
35+
private val checkTrapIdentical: Boolean,
36+
private val compilationStartTime: Long?,
37+
private val exitAfterExtraction: Boolean)
38+
: IrGenerationExtension {
39+
3640
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
3741
val startTimeMs = System.currentTimeMillis()
38-
// This default should be kept in sync with language-packs/java/tools/kotlin-extractor
42+
// This default should be kept in sync with com.semmle.extractor.java.interceptors.KotlinInterceptor.initializeExtractionContext
3943
val trapDir = File(System.getenv("CODEQL_EXTRACTOR_JAVA_TRAP_DIR").takeUnless { it.isNullOrEmpty() } ?: "kotlin-extractor/trap")
4044
FileOutputStream(File(invocationTrapFile), true).bufferedWriter().use { invocationTrapFileBW ->
4145
val lm = TrapLabelManager()
4246
val tw = TrapWriter(lm, invocationTrapFileBW)
43-
// The python wrapper has already defined #compilation = *
47+
// The interceptor has already defined #compilation = *
4448
val compilation: Label<DbCompilation> = StringLabel("compilation")
4549
tw.writeCompilation_started(compilation)
50+
if (compilationStartTime != null) {
51+
tw.writeCompilation_compiler_times(compilation, -1.0, (System.currentTimeMillis()-compilationStartTime)/1000.0)
52+
}
4653
tw.flush()
4754
val logCounter = LogCounter()
4855
val logger = Logger(logCounter, tw)
@@ -67,7 +74,9 @@ class KotlinExtractorExtension(private val invocationTrapFile: String, private v
6774
tw.writeCompilation_finished(compilation, -1.0, compilationTimeMs.toDouble() / 1000)
6875
tw.flush()
6976
}
70-
exitProcess(0)
77+
if (exitAfterExtraction) {
78+
exitProcess(0)
79+
}
7180
}
7281
}
7382

0 commit comments

Comments
 (0)