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

Skip to content

Commit 14a1056

Browse files
committed
Kotlin: Fix File locations, and fromSource/hasSourceLocation for Kotlin code
1 parent b9359bd commit 14a1056

6 files changed

Lines changed: 10 additions & 5 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
166166
}
167167

168168
fun extractFileContents(id: Label<DbFile>) {
169+
val locId = tw.getWholeFileLocation()
169170
val pkg = file.fqName.asString()
170171
val pkgId = extractPackage(pkg)
172+
tw.writeHasLocation(id, locId)
171173
tw.writeCupackage(id, pkgId)
172174
file.declarations.map { extractDeclaration(it) }
173175
CommentExtractor(this).extract()
@@ -198,7 +200,7 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
198200
val qualClassName = if (pkg.isEmpty()) jvmName else "$pkg.$jvmName"
199201
val label = "@\"class;$qualClassName\""
200202
val id: Label<DbClass> = tw.getLabelFor(label)
201-
val locId = tw.getLocation(-1, -1) // TODO: This should be the whole file
203+
val locId = tw.getWholeFileLocation()
202204
val pkgId = extractPackage(pkg)
203205
tw.writeClasses(id, jvmName, pkgId, id)
204206
tw.writeHasLocation(id, locId)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class FileTrapWriter (
7777
fun getLocation(e: IrElement): Label<DbLocation> {
7878
return getLocation(e.startOffset, e.endOffset)
7979
}
80+
fun getWholeFileLocation(): Label<DbLocation> {
81+
return getLocation(fileId, 0, 0, 0, 0)
82+
}
8083
fun getLocation(startOffset: Int, endOffset: Int): Label<DbLocation> {
8184
// If the compiler doesn't have a location, then start and end are both -1
8285
val unknownLoc = startOffset == -1 && endOffset == -1

java/ql/lib/semmle/code/Location.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class Location extends @location {
192192
}
193193

194194
private predicate hasSourceLocation(Top l, Location loc, File f) {
195-
hasLocation(l, loc) and f = loc.getFile() and f.getExtension() = "java"
195+
hasLocation(l, loc) and f = loc.getFile() and f.getExtension() = ["java", "kt"]
196196
}
197197

198198
cached

java/ql/lib/semmle/code/java/Element.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Element extends @element, Top {
3434
* Elements pertaining to source files may include generated elements
3535
* not visible in source code, such as implicit default constructors.
3636
*/
37-
predicate fromSource() { this.getCompilationUnit().getExtension() = "java" }
37+
predicate fromSource() { this.getCompilationUnit().getExtension() = ["java", "kt"] }
3838

3939
/** Gets the compilation unit that this element belongs to. */
4040
CompilationUnit getCompilationUnit() { result = this.getFile() }

java/ql/test/kotlin/library-tests/classes/classes.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| classes.kt:0:0:0:0 | ClassesKt | ClassesKt |
12
| classes.kt:2:1:2:18 | ClassOne | ClassOne |
23
| classes.kt:4:1:6:1 | ClassTwo | ClassTwo |
34
| classes.kt:8:1:10:1 | ClassThree | ClassThree |
@@ -7,7 +8,6 @@
78
| classes.kt:34:1:47:1 | ClassSeven | ClassSeven |
89
| file://:0:0:0:0 | Any | kotlin.Any |
910
| file://:0:0:0:0 | Boolean | kotlin.Boolean |
10-
| file://:0:0:0:0 | ClassesKt | ClassesKt |
1111
| file://:0:0:0:0 | Int | kotlin.Int |
1212
| file://:0:0:0:0 | String | java.lang.String |
1313
| file://:0:0:0:0 | String | kotlin.String |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
| file1.kt:2:1:2:16 | Class1 | Class1 |
22
| file2.kt:2:1:2:16 | Class2 | Class2 |
3+
| file3.kt:0:0:0:0 | MyJvmName | MyJvmName |
34
| file3.kt:3:1:3:16 | Class3 | Class3 |
45
| file://:0:0:0:0 | Any | kotlin.Any |
56
| file://:0:0:0:0 | Boolean | kotlin.Boolean |
67
| file://:0:0:0:0 | Int | kotlin.Int |
7-
| file://:0:0:0:0 | MyJvmName | MyJvmName |
88
| file://:0:0:0:0 | String | java.lang.String |
99
| file://:0:0:0:0 | String | kotlin.String |
1010
| file://:0:0:0:0 | Unit | kotlin.Unit |

0 commit comments

Comments
 (0)