@@ -28,17 +28,21 @@ import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
2828
2929class KotlinExtractorExtension (private val tests : List <String >) : IrGenerationExtension {
3030 override fun generate (moduleFragment : IrModuleFragment , pluginContext : IrPluginContext ) {
31- val logger = Logger ()
32- logger.info(" Extraction started" )
3331 val trapDir = File (System .getenv(" CODEQL_EXTRACTOR_JAVA_TRAP_DIR" ).takeUnless { it.isNullOrEmpty() } ? : " kotlin-extractor/trap" )
34- trapDir.mkdirs()
35- val srcDir = File (System .getenv(" CODEQL_EXTRACTOR_JAVA_SOURCE_ARCHIVE_DIR" ).takeUnless { it.isNullOrEmpty() } ? : " kotlin-extractor/src" )
36- srcDir.mkdirs()
37- moduleFragment.files.map { doFile(logger, trapDir, srcDir, it) }
38- logger.printLimitedWarningCounts()
39- // We don't want the compiler to continue and generate class
40- // files etc, so we just exit when we are finished extracting.
41- logger.info(" Extraction completed" )
32+ val invocationTrapDir = File (" $trapDir /invocations" )
33+ invocationTrapDir.mkdirs()
34+ val invocationTrapFile = File .createTempFile(" kotlin." , " .trap" , invocationTrapDir);
35+ invocationTrapFile.bufferedWriter().use { invocationTrapFileBW ->
36+ val logger = Logger (invocationTrapFileBW)
37+ logger.info(" Extraction started" )
38+ val srcDir = File (System .getenv(" CODEQL_EXTRACTOR_JAVA_SOURCE_ARCHIVE_DIR" ).takeUnless { it.isNullOrEmpty() } ? : " kotlin-extractor/src" )
39+ srcDir.mkdirs()
40+ moduleFragment.files.map { doFile(logger, trapDir, srcDir, it) }
41+ logger.printLimitedWarningCounts()
42+ // We don't want the compiler to continue and generate class
43+ // files etc, so we just exit when we are finished extracting.
44+ logger.info(" Extraction completed" )
45+ }
4246 exitProcess(0 )
4347 }
4448}
@@ -49,7 +53,14 @@ class Label<T>(val name: Int) {
4953
5054fun escapeTrapString (str : String ) = str.replace(" \" " , " \"\" " )
5155
52- class Logger () {
56+ class Logger (val invocationTrapFileBW : BufferedWriter ) {
57+ private val unknownLocation by lazy {
58+ invocationTrapFileBW.write(" #noFile = *\n " )
59+ invocationTrapFileBW.write(" #unknownLocation = *\n " )
60+ invocationTrapFileBW.write(" files(#noFile, \"\" , \"\" , \"\" , 0)\n " )
61+ invocationTrapFileBW.write(" locations_default(#unknownLocation, #noFile, 0, 0, 0, 0)\n " )
62+ " #unknownLocation"
63+ }
5364 private val warningCounts = mutableMapOf<String , Int >()
5465 private val warningLimit: Int
5566 init {
@@ -60,7 +71,9 @@ class Logger() {
6071 }
6172
6273 fun info (msg : String ) {
63- print (" ${timestamp()} $msg \n " )
74+ val fullMsg = " ${timestamp()} $msg "
75+ invocationTrapFileBW.write(" // " + fullMsg.replace(" \n " , " \n //" ) + " \n " )
76+ println (fullMsg)
6477 }
6578 fun warn (msg : String ) {
6679 val st = Exception ().stackTrace
@@ -78,12 +91,17 @@ class Logger() {
7891 else -> " "
7992 }
8093 }
81- print (" ${timestamp()} Warning: $msg \n $suffix " )
94+ val fullMsg = " ${timestamp()} Warning: $msg \n $suffix "
95+ val severity = 8 // Pessimistically: "Severe extractor errors likely to affect multiple source files"
96+ invocationTrapFileBW.write(" diagnostics(*, $severity , \"\" , \" ${escapeTrapString(msg)} \" , \" ${escapeTrapString(fullMsg)} \" , $unknownLocation )\n " )
97+ print (fullMsg)
8298 }
8399 fun printLimitedWarningCounts () {
84100 for ((caller, count) in warningCounts) {
85101 if (count >= warningLimit) {
86- println (" Total of $count warnings from $caller ." )
102+ val msg = " Total of $count warnings from $caller .\n "
103+ invocationTrapFileBW.write(" // $msg " )
104+ print (msg)
87105 }
88106 }
89107 }
@@ -152,6 +170,7 @@ class TrapWriter (
152170
153171fun doFile (logger : Logger , trapDir : File , srcDir : File , declaration : IrFile ) {
154172 val filePath = declaration.path
173+ logger.info(" Extracting file $filePath " )
155174 val file = File (filePath)
156175 val fileLabel = " @\" $filePath ;sourcefile\" "
157176 val basename = file.nameWithoutExtension
0 commit comments