diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt b/java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt index 4c450219994..ee181383bec 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt @@ -74,15 +74,6 @@ class StarLabel(): Label { fun escapeTrapString(str: String) = str.replace("\"", "\"\"") class Logger(val tw: TrapWriter) { - private val unknownLocation by lazy { - val noFile: Label = StringLabel("noFile") - val loc: Label = StringLabel("unknownLocation") - tw.writeTrap("$noFile = *\n") - tw.writeTrap("$loc = *\n") - tw.writeFiles(noFile, "", "", "", 0) - tw.writeLocations_default(loc, noFile, 0, 0, 0, 0) - loc - } private val warningCounts = mutableMapOf() private val warningLimit: Int init { @@ -119,7 +110,7 @@ class Logger(val tw: TrapWriter) { } val fullMsg = "${timestamp()} Warning: $msg\n$suffix" val severity = 8 // Pessimistically: "Severe extractor errors likely to affect multiple source files" - tw.writeDiagnostics(StarLabel(), severity, "", msg, fullMsg, unknownLocation) + tw.writeDiagnostics(StarLabel(), severity, "", msg, fullMsg, tw.unknownLocation) print(fullMsg) } fun printLimitedWarningCounts() { @@ -134,50 +125,12 @@ class Logger(val tw: TrapWriter) { } open class TrapWriter (val bw: BufferedWriter) { - fun writeTrap(trap: String) { - bw.write(trap) - } - fun flush() { - bw.flush() - } -} + protected var nextId: Int = 100 -class FileTrapWriter ( - bw: BufferedWriter, - val fileLabel: String, - val file: IrFile -): TrapWriter (bw) { - private var nextId: Int = 100 - private val fileEntry = file.fileEntry + fun getFreshLabel(): Label { + return IntLabel(nextId++) + } - fun getLocation(e: IrElement): Label { - return getLocation(e.startOffset, e.endOffset) - } - fun getLocation(startOffset: Int, endOffset: Int): Label { - val unknownLoc = startOffset == -1 && endOffset == -1 - val startLine = if(unknownLoc) 0 else fileEntry.getLineNumber(startOffset) + 1 - val startColumn = if(unknownLoc) 0 else fileEntry.getColumnNumber(startOffset) + 1 - val endLine = if(unknownLoc) 0 else fileEntry.getLineNumber(endOffset) + 1 - val endColumn = if(unknownLoc) 0 else fileEntry.getColumnNumber(endOffset) - val id: Label = getFreshLabel() - // TODO: This isn't right for UnknownLocation - val fileId: Label = getLabelFor(fileLabel) - writeTrap("$id = @\"loc,{$fileId},$startLine,$startColumn,$endLine,$endColumn\"\n") - writeLocations_default(id, fileId, startLine, startColumn, endLine, endColumn) - return id - } - fun getLocationString(e: IrElement): String { - val path = file.path - if (e.startOffset == -1 && e.endOffset == -1) { - return "unknown location, while processing $path" - } else { - val startLine = fileEntry.getLineNumber(e.startOffset) + 1 - val startColumn = fileEntry.getColumnNumber(e.startOffset) + 1 - val endLine = fileEntry.getLineNumber(e.endOffset) + 1 - val endColumn = fileEntry.getColumnNumber(e.endOffset) - return "file://$path:$startLine:$startColumn:$endLine:$endColumn" - } - } val labelMapping: MutableMap> = mutableMapOf>() fun getExistingLabelFor(label: String): Label? { @Suppress("UNCHECKED_CAST") @@ -195,6 +148,63 @@ class FileTrapWriter ( return maybeId } } + + fun getLocation(fileId: Label, startLine: Int, startColumn: Int, endLine: Int, endColumn: Int): Label { + return getLabelFor("@\"loc,{$fileId},$startLine,$startColumn,$endLine,$endColumn\"") { + writeLocations_default(it, fileId, startLine, startColumn, endLine, endColumn) + } + } + + protected val unknownFileId: Label by lazy { + val unknownFileLabel = "@\";sourcefile\"" + getLabelFor(unknownFileLabel, { + writeFiles(it, "", "", "", 0) + }) + } + + val unknownLocation: Label by lazy { + getLocation(unknownFileId, 0, 0, 0, 0) + } + + fun writeTrap(trap: String) { + bw.write(trap) + } + fun flush() { + bw.flush() + } +} + +class FileTrapWriter ( + bw: BufferedWriter, + val fileLabel: String, + val file: IrFile +): TrapWriter (bw) { + private val fileEntry = file.fileEntry + + fun getLocation(e: IrElement): Label { + return getLocation(e.startOffset, e.endOffset) + } + fun getLocation(startOffset: Int, endOffset: Int): Label { + val unknownLoc = startOffset == -1 && endOffset == -1 + val startLine = if(unknownLoc) 0 else fileEntry.getLineNumber(startOffset) + 1 + val startColumn = if(unknownLoc) 0 else fileEntry.getColumnNumber(startOffset) + 1 + val endLine = if(unknownLoc) 0 else fileEntry.getLineNumber(endOffset) + 1 + val endColumn = if(unknownLoc) 0 else fileEntry.getColumnNumber(endOffset) + val fileId: Label = if (unknownLoc) unknownFileId else getLabelFor(fileLabel) + return getLocation(fileId, startLine, startColumn, endLine, endColumn) + } + fun getLocationString(e: IrElement): String { + val path = file.path + if (e.startOffset == -1 && e.endOffset == -1) { + return "unknown location, while processing $path" + } else { + val startLine = fileEntry.getLineNumber(e.startOffset) + 1 + val startColumn = fileEntry.getColumnNumber(e.startOffset) + 1 + val endLine = fileEntry.getLineNumber(e.endOffset) + 1 + val endColumn = fileEntry.getColumnNumber(e.endOffset) + return "file://$path:$startLine:$startColumn:$endLine:$endColumn" + } + } val variableLabelMapping: MutableMap> = mutableMapOf>() fun getVariableLabelFor(v: IrVariable): Label { val maybeId = variableLabelMapping.get(v) @@ -207,9 +217,6 @@ class FileTrapWriter ( return maybeId } } - fun getFreshLabel(): Label { - return IntLabel(nextId++) - } fun getFreshIdLabel(): Label { val label = IntLabel(nextId++) writeTrap("$label = *\n") diff --git a/java/ql/test/kotlin/library-tests/classes/classes.expected b/java/ql/test/kotlin/library-tests/classes/classes.expected index bbe78b9434d..8dbb9390f26 100644 --- a/java/ql/test/kotlin/library-tests/classes/classes.expected +++ b/java/ql/test/kotlin/library-tests/classes/classes.expected @@ -1,8 +1,8 @@ -| classes.kt:0:0:0:0 | Any | -| classes.kt:0:0:0:0 | Unit | | classes.kt:2:1:2:18 | ClassOne | | classes.kt:4:1:6:1 | ClassTwo | | classes.kt:8:1:10:1 | ClassThree | | classes.kt:12:1:15:1 | ClassFour | | classes.kt:17:1:18:1 | ClassFive | | classes.kt:28:1:29:1 | ClassSix | +| file://:0:0:0:0 | Any | +| file://:0:0:0:0 | Unit | diff --git a/java/ql/test/kotlin/library-tests/classes/superTypes.expected b/java/ql/test/kotlin/library-tests/classes/superTypes.expected index ccb5244800e..1b22f9329db 100644 --- a/java/ql/test/kotlin/library-tests/classes/superTypes.expected +++ b/java/ql/test/kotlin/library-tests/classes/superTypes.expected @@ -1,9 +1,9 @@ -| classes.kt:0:0:0:0 | Unit | classes.kt:0:0:0:0 | Any | -| classes.kt:2:1:2:18 | ClassOne | classes.kt:0:0:0:0 | Any | -| classes.kt:4:1:6:1 | ClassTwo | classes.kt:0:0:0:0 | Any | -| classes.kt:8:1:10:1 | ClassThree | classes.kt:0:0:0:0 | Any | +| classes.kt:2:1:2:18 | ClassOne | file://:0:0:0:0 | Any | +| classes.kt:4:1:6:1 | ClassTwo | file://:0:0:0:0 | Any | +| classes.kt:8:1:10:1 | ClassThree | file://:0:0:0:0 | Any | | classes.kt:12:1:15:1 | ClassFour | classes.kt:8:1:10:1 | ClassThree | | classes.kt:17:1:18:1 | ClassFive | classes.kt:12:1:15:1 | ClassFour | | classes.kt:28:1:29:1 | ClassSix | classes.kt:12:1:15:1 | ClassFour | | classes.kt:28:1:29:1 | ClassSix | classes.kt:20:1:22:1 | IF1 | | classes.kt:28:1:29:1 | ClassSix | classes.kt:24:1:26:1 | IF2 | +| file://:0:0:0:0 | Unit | file://:0:0:0:0 | Any | diff --git a/java/ql/test/kotlin/library-tests/methods/methods.expected b/java/ql/test/kotlin/library-tests/methods/methods.expected index b6ba71d83fa..2f311e042a9 100644 --- a/java/ql/test/kotlin/library-tests/methods/methods.expected +++ b/java/ql/test/kotlin/library-tests/methods/methods.expected @@ -1,23 +1,16 @@ -| methods2.kt:0:0:0:0 | | -| methods2.kt:0:0:0:0 | equals | -| methods2.kt:0:0:0:0 | equals | -| methods2.kt:0:0:0:0 | hashCode | -| methods2.kt:0:0:0:0 | hashCode | -| methods2.kt:0:0:0:0 | toString | -| methods2.kt:0:0:0:0 | toString | +| file://:0:0:0:0 | | +| file://:0:0:0:0 | equals | +| file://:0:0:0:0 | equals | +| file://:0:0:0:0 | hashCode | +| file://:0:0:0:0 | hashCode | +| file://:0:0:0:0 | toString | +| file://:0:0:0:0 | toString | | methods2.kt:4:1:5:1 | fooBarTopLevelMethod | | methods2.kt:7:1:10:1 | | | methods2.kt:7:1:10:1 | equals | | methods2.kt:7:1:10:1 | hashCode | | methods2.kt:7:1:10:1 | toString | | methods2.kt:8:5:9:5 | fooBarClassMethod | -| methods.kt:0:0:0:0 | | -| methods.kt:0:0:0:0 | equals | -| methods.kt:0:0:0:0 | equals | -| methods.kt:0:0:0:0 | hashCode | -| methods.kt:0:0:0:0 | hashCode | -| methods.kt:0:0:0:0 | toString | -| methods.kt:0:0:0:0 | toString | | methods.kt:2:1:3:1 | topLevelMethod | | methods.kt:5:1:13:1 | | | methods.kt:5:1:13:1 | equals | diff --git a/java/ql/test/kotlin/library-tests/multiple_files/classes.expected b/java/ql/test/kotlin/library-tests/multiple_files/classes.expected index 54a8027e78b..cb27f3012b8 100644 --- a/java/ql/test/kotlin/library-tests/multiple_files/classes.expected +++ b/java/ql/test/kotlin/library-tests/multiple_files/classes.expected @@ -1,8 +1,6 @@ -| file1.kt:0:0:0:0 | Any | | file1.kt:2:1:2:16 | Class1 | -| file2.kt:0:0:0:0 | Any | | file2.kt:2:1:2:16 | Class2 | -| file3.kt:0:0:0:0 | Any | -| file3.kt:0:0:0:0 | MyJvmName | -| file3.kt:0:0:0:0 | Unit | | file3.kt:3:1:3:16 | Class3 | +| file://:0:0:0:0 | Any | +| file://:0:0:0:0 | MyJvmName | +| file://:0:0:0:0 | Unit | diff --git a/java/ql/test/kotlin/library-tests/types/types.expected b/java/ql/test/kotlin/library-tests/types/types.expected index 837c7ae9037..91e3134db54 100644 --- a/java/ql/test/kotlin/library-tests/types/types.expected +++ b/java/ql/test/kotlin/library-tests/types/types.expected @@ -1,3 +1,4 @@ +| file://:0:0:0:0 | Any | Class | | file://:0:0:0:0 | boolean | PrimitiveType | | file://:0:0:0:0 | byte | PrimitiveType | | file://:0:0:0:0 | char | PrimitiveType | @@ -7,5 +8,4 @@ | file://:0:0:0:0 | long | PrimitiveType | | file://:0:0:0:0 | short | PrimitiveType | | file://:0:0:0:0 | string | ??? | -| types.kt:0:0:0:0 | Any | Class | | types.kt:2:1:33:1 | Foo | Class | diff --git a/java/ql/test/kotlin/library-tests/variables/variables.expected b/java/ql/test/kotlin/library-tests/variables/variables.expected index ac48485b579..b2a655e8e2a 100644 --- a/java/ql/test/kotlin/library-tests/variables/variables.expected +++ b/java/ql/test/kotlin/library-tests/variables/variables.expected @@ -1,6 +1,6 @@ -| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | | -| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | | -| variables.kt:2:1:8:1 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | | +| file://:0:0:0:0 | other | file://:0:0:0:0 | Any | file://:0:0:0:0 | | +| file://:0:0:0:0 | other | file://:0:0:0:0 | Any | file://:0:0:0:0 | | +| variables.kt:2:1:8:1 | other | file://:0:0:0:0 | Any | file://:0:0:0:0 | | | variables.kt:3:5:3:21 | prop | file://:0:0:0:0 | int | file://:0:0:0:0 | | | variables.kt:5:20:5:29 | param | file://:0:0:0:0 | int | file://:0:0:0:0 | | | variables.kt:6:9:6:25 | int local | file://:0:0:0:0 | int | variables.kt:6:21:6:25 | ... + ... |