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

Skip to content

Commit 43a92f6

Browse files
committed
Kotlin: Give context to diagnostics
We now get e.g. [2022-03-09 13:59:04 K] [ERROR] Diagnostic(com.github.codeql.KotlinUsesExtractor.useSimpleType(KotlinUsesExtractor.kt:505)): Type alias ignored for <root>.Test<kotlin.String>{ <root>.Alias1<kotlin.String> } ...while extracting a function at file:///home/ian/code/dev/ql/java/ql/test/kotlin/library-tests/type_aliases/aliases_with_type_parameters.kt:7:1:7:41 ...while extracting a function if real at file:///home/ian/code/dev/ql/java/ql/test/kotlin/library-tests/type_aliases/aliases_with_type_parameters.kt:7:1:7:41 ...while extracting a declaration at file:///home/ian/code/dev/ql/java/ql/test/kotlin/library-tests/type_aliases/aliases_with_type_parameters.kt:7:1:7:41 ...while extracting a file at file:///home/ian/code/dev/ql/java/ql/test/kotlin/library-tests/type_aliases/aliases_with_type_parameters.kt:1:1:8:0
1 parent a7e6ec9 commit 43a92f6

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import java.io.File
99
import java.io.FileOutputStream
1010
import java.nio.file.Files
1111
import java.nio.file.Paths
12-
import java.util.Stack
1312
import com.semmle.util.files.FileUtil
1413
import kotlin.system.exitProcess
1514

@@ -121,8 +120,6 @@ class KotlinExtractorExtension(
121120
}
122121
}
123122

124-
data class ExtractorContext(val kind: String, val element: IrElement)
125-
126123
class KotlinExtractorGlobalState {
127124
val genericSpecialisationsExtracted = HashSet<String>()
128125
// These three record mappings of classes, functions and fields that should be replaced wherever they are found.
@@ -132,8 +129,6 @@ class KotlinExtractorGlobalState {
132129
val syntheticToRealClassMap = HashMap<IrClass, IrClass?>()
133130
val syntheticToRealFunctionMap = HashMap<IrSimpleFunction, IrSimpleFunction?>()
134131
val syntheticToRealFieldMap = HashMap<IrField, IrField?>()
135-
// TODO: This could be less global; it's really per-file
136-
val context = Stack<ExtractorContext>()
137132
}
138133

139134
/*

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ open class KotlinFileExtractor(
4040

4141
inline fun <T> with(kind: String, element: IrElement, f: () -> T): T {
4242
val loc = tw.getLocationString(element)
43-
globalExtensionState.context.push(ExtractorContext(kind, element))
43+
val context = logger.loggerBase.extractorContextStack
44+
context.push(ExtractorContext(kind, element, loc))
4445
try {
45-
val depth = globalExtensionState.context.size
46+
val depth = context.size
4647
val depthDescription = "${"-".repeat(depth)} (${depth.toString()})"
4748
val name = (element as? IrDeclarationWithName)?.name?.asString() ?: "<no name>"
4849
logger.trace("$depthDescription: Starting a $kind ($name) at $loc")
@@ -52,7 +53,7 @@ open class KotlinFileExtractor(
5253
} catch(exception: Exception) {
5354
throw Exception("While extracting a $kind at $loc", exception)
5455
} finally {
55-
globalExtensionState.context.pop()
56+
context.pop()
5657
}
5758
}
5859

java/kotlin-extractor/src/main/kotlin/utils/Logger.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.io.OutputStreamWriter
66
import java.io.Writer
77
import java.text.SimpleDateFormat
88
import java.util.Date
9+
import java.util.Stack
910
import org.jetbrains.kotlin.ir.IrElement
1011

1112
class LogCounter() {
@@ -32,7 +33,11 @@ enum class Severity(val sev: Int) {
3233
ErrorGlobal(8)
3334
}
3435

36+
data class ExtractorContext(val kind: String, val element: IrElement, val loc: String)
37+
3538
open class LoggerBase(val logCounter: LogCounter) {
39+
val extractorContextStack = Stack<ExtractorContext>()
40+
3641
private val verbosity: Int
3742
init {
3843
verbosity = System.getenv("CODEQL_EXTRACTOR_KOTLIN_VERBOSITY")?.toIntOrNull() ?: 3
@@ -85,7 +90,19 @@ open class LoggerBase(val logCounter: LogCounter) {
8590
else -> ""
8691
}
8792
}
88-
val fullMsg = "$msg\n$extraInfoStr$suffix"
93+
val fullMsgBuilder = StringBuilder()
94+
fullMsgBuilder.append(msg)
95+
fullMsgBuilder.append('\n')
96+
fullMsgBuilder.append(extraInfoStr)
97+
98+
val iter = extractorContextStack.listIterator(extractorContextStack.size)
99+
while (iter.hasPrevious()) {
100+
val x = iter.previous()
101+
fullMsgBuilder.append(" ...while extracting a ${x.kind} at ${x.loc}\n")
102+
}
103+
fullMsgBuilder.append(suffix)
104+
105+
val fullMsg = fullMsgBuilder.toString()
89106
val ts = timestamp()
90107
// We don't actually make the location until after the `return` above
91108
val locationId = mkLocationId()

0 commit comments

Comments
 (0)