escape ? character in c++ codegen to prevent accidental trigraphs

(using an override of shouldUseUnicodeEscapeForCodePointInDoubleQuotedString in CppTarget)
This commit is contained in:
Felix Nieuwenhuizen 2020-04-19 19:30:56 +02:00
parent ae5efa8a30
commit c134dac863
2 changed files with 14 additions and 1 deletions

View File

@ -282,7 +282,7 @@ public abstract class Target {
return sb.toString();
}
private static boolean shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(int codePoint) {
protected boolean shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(int codePoint) {
// We don't want anyone passing 0x0A (newline) or 0x22
// (double-quote) here because Java treats \\u000A as
// a literal newline and \\u0022 as a literal

View File

@ -47,6 +47,7 @@ public class CppTarget extends Target {
public CppTarget(CodeGenerator gen) {
super(gen, "Cpp");
targetCharValueEscape['?'] = "\\?";
}
public String getVersion() {
@ -69,6 +70,18 @@ public class CppTarget extends Target {
badWords.add("parserRule");
}
@Override
protected boolean shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(int codePoint) {
if (codePoint == '?') {
// in addition to the default escaped code points, also escape ? to prevent trigraphs
// ideally, we would escape ? with \?, but escaping as unicode \u003F works as well
return true;
}
else {
return super.shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(codePoint);
}
}
@Override
public String encodeIntAsCharEscape(int v) {
return "0x" + Integer.toHexString(v) + ", ";