Skip to content

Commit 2a0d22e

Browse files
committed
Merge branch 'topic/lkql_jit/fix_builtin_annotations' into 'master'
Escape Java string values when concatenating them in the generated code Closes #463 See merge request eng/libadalang/langkit-query-language!435
2 parents d047f42 + b61dbab commit 2a0d22e

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lkql_jit/builtins_annotations/src/main/java/com/adacore/lkql_jit/annotations/BuiltInProcessor.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,10 @@ private String builtInArguments(TypeElement builtIn, boolean omitSelfParam) {
257257
}
258258
});
259259

260-
names.add("\"" + nameSB + "\"");
261-
defaultVals.add(defaultVal != null ? "\"" + defaultVal + "\"" : "null");
260+
names.add("\"" + escapeString(nameSB.toString()) + "\"");
261+
defaultVals.add(
262+
(defaultVal != null ? "\"" + escapeString(defaultVal) + "\"" : "null")
263+
);
262264
}
263265

264266
return (
@@ -342,9 +344,9 @@ private void processBuiltInFactory(TypeElement builtInFactoryClass) throws IOExc
342344

343345
return (
344346
" new BuiltInFunctionValue(\"" +
345-
fnAnnot.name() +
347+
escapeString(fnAnnot.name()) +
346348
"\", \"" +
347-
fnAnnot.doc() +
349+
escapeString(fnAnnot.doc()) +
348350
"\", " +
349351
builtInArguments(builtInFn, false) +
350352
", BuiltInBody.create(" +
@@ -416,9 +418,9 @@ private void processBuiltInFactory(TypeElement builtInFactoryClass) throws IOExc
416418
"BuiltInMethodFactory." +
417419
(isProperty ? "createAttribute" : "createMethod") +
418420
"(\"" +
419-
name +
421+
escapeString(name) +
420422
"\", \"" +
421-
doc +
423+
escapeString(doc) +
422424
"\"" +
423425
(isProperty ? "" : ", " + builtInArguments(builtInMethod, true)) +
424426
", BuiltInBody.create(" +
@@ -431,7 +433,7 @@ private void processBuiltInFactory(TypeElement builtInFactoryClass) throws IOExc
431433
return (
432434
" Pair.create(new String[] {" +
433435
Arrays.stream(targetTypes)
434-
.map(t -> "\"" + t + "\"")
436+
.map(t -> "\"" + escapeString(t) + "\"")
435437
.collect(Collectors.joining(", ")) +
436438
" }," +
437439
wrapper +
@@ -448,4 +450,15 @@ private void processBuiltInFactory(TypeElement builtInFactoryClass) throws IOExc
448450

449451
builtInPackages.add(Pair.create(packageName, className));
450452
}
453+
454+
/** Return the `input` string with all special characters escaped. */
455+
private static String escapeString(String input) {
456+
return input
457+
.replace("\n", "\\n")
458+
.replace("\r", "\\r")
459+
.replace("\t", "\\t")
460+
.replace("\b", "\\b")
461+
.replace("\f", "\\f")
462+
.replace("\"", "\\\"");
463+
}
451464
}

0 commit comments

Comments
 (0)