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
This commit is contained in:
Ian Lynagh
2022-03-09 14:00:32 +00:00
parent a7e6ec9d02
commit 43a92f60b2
3 changed files with 22 additions and 9 deletions

View File

@@ -40,9 +40,10 @@ open class KotlinFileExtractor(
inline fun <T> with(kind: String, element: IrElement, f: () -> T): T {
val loc = tw.getLocationString(element)
globalExtensionState.context.push(ExtractorContext(kind, element))
val context = logger.loggerBase.extractorContextStack
context.push(ExtractorContext(kind, element, loc))
try {
val depth = globalExtensionState.context.size
val depth = context.size
val depthDescription = "${"-".repeat(depth)} (${depth.toString()})"
val name = (element as? IrDeclarationWithName)?.name?.asString() ?: "<no name>"
logger.trace("$depthDescription: Starting a $kind ($name) at $loc")
@@ -52,7 +53,7 @@ open class KotlinFileExtractor(
} catch(exception: Exception) {
throw Exception("While extracting a $kind at $loc", exception)
} finally {
globalExtensionState.context.pop()
context.pop()
}
}