Skip to content

Commit 5adb214

Browse files
committed
Kotlin: format string literals like the Java annotaton extractor
Java's regular strings are formatted as they appear in source, but we don't easily have this information available in Kotlin. During annotation extraction however it guesses a source rendering because the source is not necessarily available. By formatting to match the annotation extractor, we prepare to ensure consistency with a Java database when extracting annotations as seen by Kotlin.
1 parent 7d4ea47 commit 5adb214

30 files changed

Lines changed: 287 additions & 266 deletions

File tree

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,23 @@ open class KotlinFileExtractor(
33953395
extractExprContext(it, locId, callable, enclosingStmt)
33963396
}
33973397

3398+
private fun escapeCharForQuotedLiteral(c: Char) =
3399+
when (c) {
3400+
'\r' -> "\\r"
3401+
'\n' -> "\\n"
3402+
'\t' -> "\\t"
3403+
'\\' -> "\\\\"
3404+
'"' -> "\\\""
3405+
else -> c.toString()
3406+
}
3407+
3408+
// Render a string literal as it might occur in Kotlin source. Note this is a reasonable guess; the real source
3409+
// could use other escape sequences to describe the same String. Importantly, this is the same guess the Java
3410+
// extractor makes regarding string literals occurring within annotations, which we need to coincide with to ensure
3411+
// database consistency.
3412+
private fun toQuotedLiteral(s: String) =
3413+
s.toCharArray().joinToString(separator = "", prefix = "\"", postfix = "\"") { c -> escapeCharForQuotedLiteral(c) }
3414+
33983415
private fun extractExpression(e: IrExpression, callable: Label<out DbCallable>, parent: StmtExprParent) {
33993416
with("expression", e) {
34003417
when(e) {
@@ -3602,7 +3619,7 @@ open class KotlinFileExtractor(
36023619
tw.writeExprs_stringliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx)
36033620
tw.writeExprsKotlinType(id, type.kotlinResult.id)
36043621
extractExprContext(id, locId, callable, exprParent.enclosingStmt)
3605-
tw.writeNamestrings(v.toString(), v.toString(), id)
3622+
tw.writeNamestrings(toQuotedLiteral(v.toString()), v.toString(), id)
36063623
}
36073624
v == null -> {
36083625
extractNull(e.type, tw.getLocation(e), exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ classes.kt:
138138
# 39| 0: [ExprStmt] <Expr>;
139139
# 39| 0: [MethodAccess] f(...)
140140
# 39| -1: [TypeAccess] ClassesKt
141-
# 39| 0: [StringLiteral] init1
141+
# 39| 0: [StringLiteral] "init1"
142142
# 42| 1: [ExprStmt] <Expr>;
143143
# 42| 0: [KtInitializerAssignExpr] ...=...
144144
# 42| 0: [VarAccess] x
145145
# 45| 2: [ExprStmt] <Expr>;
146146
# 45| 0: [MethodAccess] f(...)
147147
# 45| -1: [TypeAccess] ClassesKt
148-
# 45| 0: [StringLiteral] init2
148+
# 45| 0: [StringLiteral] "init2"
149149
# 36| 2: [ExprStmt] <Expr>;
150150
# 36| 0: [MethodAccess] f(...)
151151
# 36| -1: [TypeAccess] ClassesKt
@@ -1119,7 +1119,7 @@ local_anonymous.kt:
11191119
# 40| 1: [BlockStmt] { ... }
11201120
# 42| 0: [LocalVariableDeclStmt] var ...;
11211121
# 42| 1: [LocalVariableDeclExpr] answer
1122-
# 42| 0: [StringLiteral] 42
1122+
# 42| 0: [StringLiteral] "42"
11231123
# 40| 1: [ExprStmt] <Expr>;
11241124
# 40| 0: [ClassInstanceExpr] new (...)
11251125
# 40| -3: [TypeAccess] Interface2

java/ql/test/kotlin/library-tests/collection-literals/PrintAst.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ test.kt:
3939
# 1| 0: [VarAccess] p0
4040
# 0| 1: [ArrayCreationExpr] new String[]
4141
# 0| -2: [ArrayInit] {...}
42-
# 0| 0: [StringLiteral] hello
43-
# 0| 1: [StringLiteral] world
42+
# 0| 0: [StringLiteral] "hello"
43+
# 0| 1: [StringLiteral] "world"
4444
# 0| -1: [TypeAccess] String
4545
# 0| 0: [IntegerLiteral] 2
4646
# 1| 1: [IfStmt] if (...)

java/ql/test/kotlin/library-tests/controlflow/basic/bbStmts.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@
155155
| Test.kt:105:5:109:5 | <Expr>; | 5 | Test.kt:105:9:105:17 | ... (value not-equals) ... |
156156
| Test.kt:105:20:107:5 | { ... } | 0 | Test.kt:105:20:107:5 | { ... } |
157157
| Test.kt:105:20:107:5 | { ... } | 1 | Test.kt:106:9:106:29 | <Expr>; |
158-
| Test.kt:105:20:107:5 | { ... } | 2 | Test.kt:106:18:106:27 | x not null |
158+
| Test.kt:105:20:107:5 | { ... } | 2 | Test.kt:106:18:106:27 | "x not null" |
159159
| Test.kt:105:20:107:5 | { ... } | 3 | Test.kt:106:9:106:29 | println(...) |
160160
| Test.kt:107:16:109:5 | ... -> ... | 0 | Test.kt:107:16:109:5 | ... -> ... |
161161
| Test.kt:107:16:109:5 | ... -> ... | 1 | Test.kt:107:16:107:16 | y |
162162
| Test.kt:107:16:109:5 | ... -> ... | 2 | Test.kt:107:21:107:24 | null |
163163
| Test.kt:107:16:109:5 | ... -> ... | 3 | Test.kt:107:16:107:24 | ... (value not-equals) ... |
164164
| Test.kt:107:27:109:5 | { ... } | 0 | Test.kt:107:27:109:5 | { ... } |
165165
| Test.kt:107:27:109:5 | { ... } | 1 | Test.kt:108:9:108:29 | <Expr>; |
166-
| Test.kt:107:27:109:5 | { ... } | 2 | Test.kt:108:18:108:27 | y not null |
166+
| Test.kt:107:27:109:5 | { ... } | 2 | Test.kt:108:18:108:27 | "y not null" |
167167
| Test.kt:107:27:109:5 | { ... } | 3 | Test.kt:108:9:108:29 | println(...) |
168168
| Test.kt:112:1:116:1 | fn | 0 | Test.kt:112:1:116:1 | fn |
169169
| Test.kt:112:32:116:1 | { ... } | 0 | Test.kt:112:32:116:1 | { ... } |

java/ql/test/kotlin/library-tests/controlflow/basic/getASuccessor.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,20 @@ missingSuccessor
207207
| Test.kt:105:9:107:5 | ... -> ... | WhenBranch | Test.kt:105:9:105:9 | x | VarAccess |
208208
| Test.kt:105:14:105:17 | null | NullLiteral | Test.kt:105:9:105:17 | ... (value not-equals) ... | ValueNEExpr |
209209
| Test.kt:105:20:107:5 | { ... } | BlockStmt | Test.kt:106:9:106:29 | <Expr>; | ExprStmt |
210-
| Test.kt:106:9:106:29 | <Expr>; | ExprStmt | Test.kt:106:18:106:27 | x not null | StringLiteral |
210+
| Test.kt:106:9:106:29 | <Expr>; | ExprStmt | Test.kt:106:18:106:27 | "x not null" | StringLiteral |
211211
| Test.kt:106:9:106:29 | ConsoleKt | TypeAccess | file://:0:0:0:0 | <none> | <none> |
212212
| Test.kt:106:9:106:29 | println(...) | MethodAccess | Test.kt:100:1:110:1 | fn | Method |
213-
| Test.kt:106:18:106:27 | x not null | StringLiteral | Test.kt:106:9:106:29 | println(...) | MethodAccess |
213+
| Test.kt:106:18:106:27 | "x not null" | StringLiteral | Test.kt:106:9:106:29 | println(...) | MethodAccess |
214214
| Test.kt:107:16:107:16 | y | VarAccess | Test.kt:107:21:107:24 | null | NullLiteral |
215215
| Test.kt:107:16:107:24 | ... (value not-equals) ... | ValueNEExpr | Test.kt:100:1:110:1 | fn | Method |
216216
| Test.kt:107:16:107:24 | ... (value not-equals) ... | ValueNEExpr | Test.kt:107:27:109:5 | { ... } | BlockStmt |
217217
| Test.kt:107:16:109:5 | ... -> ... | WhenBranch | Test.kt:107:16:107:16 | y | VarAccess |
218218
| Test.kt:107:21:107:24 | null | NullLiteral | Test.kt:107:16:107:24 | ... (value not-equals) ... | ValueNEExpr |
219219
| Test.kt:107:27:109:5 | { ... } | BlockStmt | Test.kt:108:9:108:29 | <Expr>; | ExprStmt |
220-
| Test.kt:108:9:108:29 | <Expr>; | ExprStmt | Test.kt:108:18:108:27 | y not null | StringLiteral |
220+
| Test.kt:108:9:108:29 | <Expr>; | ExprStmt | Test.kt:108:18:108:27 | "y not null" | StringLiteral |
221221
| Test.kt:108:9:108:29 | ConsoleKt | TypeAccess | file://:0:0:0:0 | <none> | <none> |
222222
| Test.kt:108:9:108:29 | println(...) | MethodAccess | Test.kt:100:1:110:1 | fn | Method |
223-
| Test.kt:108:18:108:27 | y not null | StringLiteral | Test.kt:108:9:108:29 | println(...) | MethodAccess |
223+
| Test.kt:108:18:108:27 | "y not null" | StringLiteral | Test.kt:108:9:108:29 | println(...) | MethodAccess |
224224
| Test.kt:112:1:116:1 | Unit | TypeAccess | file://:0:0:0:0 | <none> | <none> |
225225
| Test.kt:112:1:116:1 | fn | Method | file://:0:0:0:0 | <none> | <none> |
226226
| Test.kt:112:8:112:17 | boolean | TypeAccess | file://:0:0:0:0 | <none> | <none> |

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,19 @@ dc.kt:
145145
# 0| 5: [BlockStmt] { ... }
146146
# 0| 0: [ReturnStmt] return ...
147147
# 0| 0: [StringTemplateExpr] "..."
148-
# 0| 0: [StringLiteral] ProtoMapValue(
149-
# 0| 1: [StringLiteral] bytes=
148+
# 0| 0: [StringLiteral] "ProtoMapValue("
149+
# 0| 1: [StringLiteral] "bytes="
150150
# 0| 2: [MethodAccess] toString(...)
151151
# 0| -1: [TypeAccess] Arrays
152152
# 0| 0: [VarAccess] this.bytes
153153
# 0| -1: [ThisAccess] this
154-
# 0| 3: [StringLiteral] ,
155-
# 0| 4: [StringLiteral] strs=
154+
# 0| 3: [StringLiteral] ", "
155+
# 0| 4: [StringLiteral] "strs="
156156
# 0| 5: [MethodAccess] toString(...)
157157
# 0| -1: [TypeAccess] Arrays
158158
# 0| 0: [VarAccess] this.strs
159159
# 0| -1: [ThisAccess] this
160-
# 0| 6: [StringLiteral] )
160+
# 0| 6: [StringLiteral] ")"
161161
# 1| 8: [Constructor] ProtoMapValue
162162
#-----| 4: (Parameters)
163163
# 1| 0: [Parameter] bytes

java/ql/test/kotlin/library-tests/dataflow/foreach/test.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
| C1.java:10:44:10:46 | "a" | C1.java:12:17:12:20 | ...[...] |
33
| C1.java:10:44:10:46 | "a" | C1.java:15:20:15:23 | ...[...] |
44
| C1.java:10:44:10:46 | "a" | C1.java:19:20:19:20 | s |
5-
| C2.kt:8:32:8:32 | a | C2.kt:9:14:9:14 | l |
6-
| C2.kt:8:32:8:32 | a | C2.kt:10:14:10:17 | ...[...] |
7-
| C2.kt:8:32:8:32 | a | C2.kt:12:18:12:21 | ...[...] |
8-
| C2.kt:8:32:8:32 | a | C2.kt:15:18:15:18 | s |
5+
| C2.kt:8:32:8:32 | "a" | C2.kt:9:14:9:14 | l |
6+
| C2.kt:8:32:8:32 | "a" | C2.kt:10:14:10:17 | ...[...] |
7+
| C2.kt:8:32:8:32 | "a" | C2.kt:12:18:12:21 | ...[...] |
8+
| C2.kt:8:32:8:32 | "a" | C2.kt:15:18:15:18 | s |

java/ql/test/kotlin/library-tests/exprs/PrintAst.expected

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ delegatedProperties.kt:
171171
# 7| 0: [ExprStmt] <Expr>;
172172
# 7| 0: [MethodAccess] println(...)
173173
# 7| -1: [TypeAccess] ConsoleKt
174-
# 7| 0: [StringLiteral] init
174+
# 7| 0: [StringLiteral] "init"
175175
# 8| 1: [ReturnStmt] return ...
176176
# 8| 0: [IntegerLiteral] 5
177177
# 6| -3: [TypeAccess] Function0<Integer>
@@ -2459,10 +2459,10 @@ exprs.kt:
24592459
# 123| 0: [CharacterLiteral] x
24602460
# 124| 106: [LocalVariableDeclStmt] var ...;
24612461
# 124| 1: [LocalVariableDeclExpr] str
2462-
# 124| 0: [StringLiteral] string lit
2462+
# 124| 0: [StringLiteral] "string lit"
24632463
# 125| 107: [LocalVariableDeclStmt] var ...;
24642464
# 125| 1: [LocalVariableDeclExpr] strWithQuote
2465-
# 125| 0: [StringLiteral] string " lit
2465+
# 125| 0: [StringLiteral] "string \" lit"
24662466
# 126| 108: [LocalVariableDeclStmt] var ...;
24672467
# 126| 1: [LocalVariableDeclExpr] b6
24682468
# 126| 0: [InstanceOfExpr] ...instanceof...
@@ -2480,34 +2480,34 @@ exprs.kt:
24802480
# 128| 1: [VarAccess] b7
24812481
# 129| 111: [LocalVariableDeclStmt] var ...;
24822482
# 129| 1: [LocalVariableDeclExpr] str1
2483-
# 129| 0: [StringLiteral] string lit
2483+
# 129| 0: [StringLiteral] "string lit"
24842484
# 130| 112: [LocalVariableDeclStmt] var ...;
24852485
# 130| 1: [LocalVariableDeclExpr] str2
2486-
# 130| 0: [StringLiteral] string lit
2486+
# 130| 0: [StringLiteral] "string lit"
24872487
# 131| 113: [LocalVariableDeclStmt] var ...;
24882488
# 131| 1: [LocalVariableDeclExpr] str3
24892489
# 131| 0: [NullLiteral] null
24902490
# 132| 114: [LocalVariableDeclStmt] var ...;
24912491
# 132| 1: [LocalVariableDeclExpr] str4
24922492
# 132| 0: [StringTemplateExpr] "..."
2493-
# 132| 0: [StringLiteral] foo
2493+
# 132| 0: [StringLiteral] "foo "
24942494
# 132| 1: [VarAccess] str1
2495-
# 132| 2: [StringLiteral] bar
2495+
# 132| 2: [StringLiteral] " bar "
24962496
# 132| 3: [VarAccess] str2
2497-
# 132| 4: [StringLiteral] baz
2497+
# 132| 4: [StringLiteral] " baz"
24982498
# 133| 115: [LocalVariableDeclStmt] var ...;
24992499
# 133| 1: [LocalVariableDeclExpr] str5
25002500
# 133| 0: [StringTemplateExpr] "..."
2501-
# 133| 0: [StringLiteral] foo
2501+
# 133| 0: [StringLiteral] "foo "
25022502
# 133| 1: [AddExpr] ... + ...
25032503
# 133| 0: [VarAccess] str1
25042504
# 133| 1: [VarAccess] str2
2505-
# 133| 2: [StringLiteral] bar
2505+
# 133| 2: [StringLiteral] " bar "
25062506
# 133| 3: [MethodAccess] stringPlus(...)
25072507
# 133| -1: [TypeAccess] Intrinsics
25082508
# 133| 0: [VarAccess] str2
25092509
# 133| 1: [VarAccess] str1
2510-
# 133| 4: [StringLiteral] baz
2510+
# 133| 4: [StringLiteral] " baz"
25112511
# 134| 116: [LocalVariableDeclStmt] var ...;
25122512
# 134| 1: [LocalVariableDeclExpr] str6
25132513
# 134| 0: [AddExpr] ... + ...
@@ -3531,12 +3531,12 @@ exprs.kt:
35313531
# 215| 1: [LocalVariableDeclExpr] d0
35323532
# 215| 0: [MethodAccess] valueOf(...)
35333533
# 215| -1: [TypeAccess] Color
3534-
# 215| 0: [StringLiteral] GREEN
3534+
# 215| 0: [StringLiteral] "GREEN"
35353535
# 216| 8: [LocalVariableDeclStmt] var ...;
35363536
# 216| 1: [LocalVariableDeclExpr] d1
35373537
# 216| 0: [MethodAccess] valueOf(...)
35383538
# 216| -1: [TypeAccess] Color
3539-
# 216| 0: [StringLiteral] GREEN
3539+
# 216| 0: [StringLiteral] "GREEN"
35403540
# 224| 11: [Class] SomeClass1
35413541
# 224| 1: [Constructor] SomeClass1
35423542
# 224| 5: [BlockStmt] { ... }
@@ -4468,7 +4468,7 @@ funcExprs.kt:
44684468
# 36| 0: [TypeAccess] int
44694469
# 36| 5: [BlockStmt] { ... }
44704470
# 36| 0: [ReturnStmt] return ...
4471-
# 36| 0: [StringLiteral]
4471+
# 36| 0: [StringLiteral] ""
44724472
# 36| -3: [TypeAccess] FunctionN<String>
44734473
# 36| 0: [TypeAccess] String
44744474
# 38| 14: [ExprStmt] <Expr>;
@@ -5316,7 +5316,7 @@ funcExprs.kt:
53165316
# 90| 0: [TypeAccess] int
53175317
# 90| 5: [BlockStmt] { ... }
53185318
# 90| 0: [ReturnStmt] return ...
5319-
# 90| 0: [StringLiteral]
5319+
# 90| 0: [StringLiteral] ""
53205320
# 90| -3: [TypeAccess] FunctionN<String>
53215321
# 90| 0: [TypeAccess] String
53225322
# 91| 5: [ExprStmt] <Expr>;
@@ -5407,7 +5407,7 @@ funcExprs.kt:
54075407
# 94| 0: [TypeAccess] int
54085408
# 94| 5: [BlockStmt] { ... }
54095409
# 94| 0: [ReturnStmt] return ...
5410-
# 94| 0: [StringLiteral]
5410+
# 94| 0: [StringLiteral] ""
54115411
# 94| -3: [TypeAccess] Function22<Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,String>
54125412
# 94| 0: [TypeAccess] Integer
54135413
# 94| 1: [TypeAccess] Integer
@@ -5599,7 +5599,7 @@ funcExprs.kt:
55995599
# 70| 0: [TypeAccess] int
56005600
# 70| 5: [BlockStmt] { ... }
56015601
# 70| 0: [ReturnStmt] return ...
5602-
# 70| 0: [StringLiteral]
5602+
# 70| 0: [StringLiteral] ""
56035603
# 73| 4: [Class] Class3
56045604
# 73| 3: [Constructor] Class3
56055605
# 73| 5: [BlockStmt] { ... }
@@ -5625,7 +5625,7 @@ funcExprs.kt:
56255625
# 75| 0: [TypeAccess] Integer
56265626
# 75| 5: [BlockStmt] { ... }
56275627
# 75| 0: [ReturnStmt] return ...
5628-
# 75| 0: [StringLiteral] a
5628+
# 75| 0: [StringLiteral] "a"
56295629
# 75| -3: [TypeAccess] Function1<Generic<Generic<Integer>>,String>
56305630
# 75| 0: [TypeAccess] Generic<Generic<Integer>>
56315631
# 75| 0: [TypeAccess] Generic<Integer>
@@ -6014,7 +6014,7 @@ samConversion.kt:
60146014
# 7| 0: [ReturnStmt] return ...
60156015
# 7| 0: [ValueEQExpr] ... (value equals) ...
60166016
# 7| 0: [ExtensionReceiverAccess] this
6017-
# 7| 1: [StringLiteral]
6017+
# 7| 1: [StringLiteral] ""
60186018
# 7| -3: [TypeAccess] Function2<String,Integer,Boolean>
60196019
# 7| 0: [TypeAccess] String
60206020
# 7| 1: [TypeAccess] Integer
@@ -7058,7 +7058,7 @@ whenExpr.kt:
70587058
# 6| 1: [ThrowStmt] throw ...
70597059
# 6| 0: [ClassInstanceExpr] new Exception(...)
70607060
# 6| -3: [TypeAccess] Exception
7061-
# 6| 0: [StringLiteral] No threes please
7061+
# 6| 0: [StringLiteral] "No threes please"
70627062
# 7| 4: [WhenBranch] ... -> ...
70637063
# 7| 0: [BooleanLiteral] true
70647064
# 7| 1: [ExprStmt] <Expr>;

java/ql/test/kotlin/library-tests/exprs/binop.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
| localFunctionCalls.kt:5:25:5:29 | ... + ... | localFunctionCalls.kt:5:25:5:25 | i | localFunctionCalls.kt:5:29:5:29 | x |
128128
| samConversion.kt:2:33:2:38 | ... % ... | samConversion.kt:2:33:2:34 | it | samConversion.kt:2:38:2:38 | 2 |
129129
| samConversion.kt:2:33:2:43 | ... (value equals) ... | samConversion.kt:2:33:2:38 | ... % ... | samConversion.kt:2:43:2:43 | 0 |
130-
| samConversion.kt:7:36:7:45 | ... (value equals) ... | samConversion.kt:7:36:7:39 | this | samConversion.kt:7:44:7:45 | |
130+
| samConversion.kt:7:36:7:45 | ... (value equals) ... | samConversion.kt:7:36:7:39 | this | samConversion.kt:7:44:7:45 | "" |
131131
| samConversion.kt:10:18:10:22 | ... % ... | samConversion.kt:10:18:10:18 | j | samConversion.kt:10:22:10:22 | 2 |
132132
| samConversion.kt:10:18:10:27 | ... (value equals) ... | samConversion.kt:10:18:10:22 | ... % ... | samConversion.kt:10:27:10:27 | 0 |
133133
| samConversion.kt:12:18:12:22 | ... % ... | samConversion.kt:12:18:12:18 | j | samConversion.kt:12:22:12:22 | 2 |

0 commit comments

Comments
 (0)