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

Skip to content

Commit 8330a40

Browse files
committed
Kotlin: Add warning location to warnings
This also tweaks how the "too many warnings" logic works
1 parent 41d4c21 commit 8330a40

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

  • java/kotlin-extractor/src/main/kotlin/utils

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ open class Logger(val logCounter: LogCounter, open val tw: TrapWriter) {
3333
return "[${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} K]"
3434
}
3535

36+
private fun getWarningLocation(): String? {
37+
val st = Exception().stackTrace
38+
for(x in st) {
39+
when(x.className) {
40+
"com.github.codeql.Logger",
41+
"com.github.codeql.FileLogger" -> {}
42+
else -> {
43+
return x.toString()
44+
}
45+
}
46+
}
47+
return null
48+
}
49+
3650
fun flush() {
3751
tw.flush()
3852
System.out.flush()
@@ -53,26 +67,26 @@ open class Logger(val logCounter: LogCounter, open val tw: TrapWriter) {
5367
fun trace(msg: String, exn: Exception) {
5468
trace(msg + " // " + exn)
5569
}
56-
fun warn(severity: Severity, msg: String, locationString: String? = null, locationId: Label<DbLocation> = tw.unknownLocation, stackIndex: Int = 2) {
57-
val st = Exception().stackTrace
70+
fun warn(severity: Severity, msg: String, locationString: String? = null, locationId: Label<DbLocation> = tw.unknownLocation) {
71+
val warningLoc = getWarningLocation()
72+
val warningLocStr = if(warningLoc == null) "<unknown location>" else warningLoc
5873
val suffix =
59-
if(st.size < stackIndex + 1) {
74+
if(warningLoc == null) {
6075
" Missing caller information.\n"
6176
} else {
62-
val caller = st[stackIndex].toString()
63-
val count = logCounter.warningCounts.getOrDefault(caller, 0) + 1
64-
logCounter.warningCounts[caller] = count
77+
val count = logCounter.warningCounts.getOrDefault(warningLoc, 0) + 1
78+
logCounter.warningCounts[warningLoc] = count
6579
when {
6680
logCounter.warningLimit <= 0 -> ""
67-
count == logCounter.warningLimit -> " Limit reached for warnings from $caller.\n"
81+
count == logCounter.warningLimit -> " Limit reached for warnings from $warningLoc.\n"
6882
count > logCounter.warningLimit -> return
6983
else -> ""
7084
}
7185
}
7286
val ts = timestamp()
7387
tw.writeDiagnostics(StarLabel(), severity.sev, "", msg, "$ts $msg\n$suffix", locationId)
7488
val locStr = if (locationString == null) "" else "At " + locationString + ": "
75-
print("$ts Warning: $locStr$msg\n$suffix")
89+
print("$ts Warning($warningLocStr): $locStr$msg\n$suffix")
7690
}
7791
fun warn(msg: String, exn: Exception) {
7892
warn(Severity.Warn, msg + " // " + exn)
@@ -102,9 +116,9 @@ class FileLogger(logCounter: LogCounter, override val tw: FileTrapWriter): Logge
102116
return "[${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} K]"
103117
}
104118

105-
fun warnElement(severity: Severity, msg: String, element: IrElement, stackIndex: Int = 3) {
119+
fun warnElement(severity: Severity, msg: String, element: IrElement) {
106120
val locationString = tw.getLocationString(element)
107121
val locationId = tw.getLocation(element)
108-
warn(severity, msg, locationString, locationId, stackIndex)
122+
warn(severity, msg, locationString, locationId)
109123
}
110124
}

0 commit comments

Comments
 (0)