Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dbc3f29

Browse files
committed
Kotlin: Put diagnostics in a TRAP file
Currently we just put everything in as severe with no location.
1 parent a40ebd2 commit dbc3f29

2 files changed

Lines changed: 39 additions & 19 deletions

File tree

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@ import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
2828

2929
class 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

5054
fun 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

153171
fun 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

java/ql/consistency-queries/locations.ql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ Location unusedLocation() {
2020
not exists(Top t | t.getLocation() = result) and
2121
not exists(XMLLocatable x | x.getLocation() = result) and
2222
not exists(ConfigLocatable c | c.getLocation() = result) and
23+
not exists(@diagnostic d | diagnostics(d, _, _, _, _, result)) and
2324
not (result.getFile().getExtension() = "xml" and
2425
result.getStartLine() = 0 and
2526
result.getStartColumn() = 0 and
2627
result.getEndLine() = 0 and
2728
result.getEndColumn() = 0)
2829
}
2930

30-
from Location l
31-
where l = badLocation()
32-
or l = backwardsLocation()
33-
or l = unusedLocation()
34-
select l
31+
from string reason, Location l
32+
where reason = "Bad location" and l = badLocation()
33+
or reason = "Backwards location" and l = backwardsLocation()
34+
or reason = "Unused location" and l = unusedLocation()
35+
select reason, l
3536

0 commit comments

Comments
 (0)