From c58ca4a6021909edc44baffa2958946902eac1d2 Mon Sep 17 00:00:00 2001 From: Yutaro Sakamoto Date: Wed, 9 Oct 2024 14:55:21 +0900 Subject: [PATCH 1/3] [Feat]: add -fvar-name-hex experimentaly --- cobj/codegen.c | 56 +++++++++++++++++++++++++++++++--------------- cobj/flag-help.def | 3 +++ cobj/flag.def | 3 +++ 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/cobj/codegen.c b/cobj/codegen.c index a22cf98c..57c73b8d 100644 --- a/cobj/codegen.c +++ b/cobj/codegen.c @@ -234,31 +234,51 @@ static void get_java_identifier_helper(struct cb_field *f, char *buf) { } static void strcpy_identifier_cobol_to_java(char *buf, const char *identifier) { - int all_ascii = 1; - unsigned char *p = (unsigned char *)identifier; - for (; *p; ++p) { - unsigned char c = *p; - if (c < 0x0A || 0x80 <= c) { - all_ascii = 0; - break; + if(cb_flag_var_name_hex) { + int all_ascii = 1; + unsigned char *p = (unsigned char *)identifier; + for (; *p; ++p) { + unsigned char c = *p; + if (c < 0x0A || 0x80 <= c) { + all_ascii = 0; + break; + } } - } - if (all_ascii) { - for (; *identifier; ++identifier, ++buf) { - if (*identifier == '-') { - *buf = '_'; - } else { - *buf = *identifier; + if (all_ascii) { + for (; *identifier; ++identifier, ++buf) { + if (*identifier == '-') { + *buf = '_'; + } else { + *buf = *identifier; + } + } + } else { + for (; *identifier; ++identifier) { + sprintf(buf, "%02x", (unsigned char)*identifier); + buf += 2; } } + *buf = '\0'; } else { - for (; *identifier; ++identifier) { - sprintf(buf, "%02x", (unsigned char)*identifier); - buf += 2; + unsigned char *src = (unsigned char *)identifier; + unsigned char *dst = (unsigned char *)buf; + while(*src) { + // ASCII characters + if(*src < 0x80) { + *dst = (*src == '-') ? '_' : *src; + ++dst; + ++src; + // Shift_JIS characters + } else { + dst[0] = src[0]; + dst[1] = src[1]; + dst += 2; + src += 2; + } } + *dst = '\0'; } - *buf = '\0'; } struct cb_label_id_map { diff --git a/cobj/flag-help.def b/cobj/flag-help.def index 5e99e057..2dc068f6 100644 --- a/cobj/flag-help.def +++ b/cobj/flag-help.def @@ -56,5 +56,8 @@ CB_FLAG (cb_flag_functions_all, "functions-all", CB_FLAG (cb_flag_mfcomment, "mfcomment", N_("'*' or '/' in column 1 treated as comment (FIXED only)")) +CB_FLAG (cb_flag_var_name_hex, "var-name-hex", + N_("Use hexdecimal representation for variable names that contains non-ASCII characters")) + //CB_FLAG (cb_flag_no_cobol_comment, "no-cobol-comment", // N_("Do not generate COBOL comments into Java programs")) \ No newline at end of file diff --git a/cobj/flag.def b/cobj/flag.def index f1685a1a..02bf0e61 100644 --- a/cobj/flag.def +++ b/cobj/flag.def @@ -81,5 +81,8 @@ CB_FLAG (cb_flag_serial_variable, "serial-variable", CB_FLAG (cb_flag_short_variable, "short-variable", N_("Use short variable names in Java source code. This feature may cause compilation errors")) +CB_FLAG (cb_flag_var_name_hex, "var-name-hex", + N_("Use hexdecimal representation for variable names that contains non-ASCII characters")) + //CB_FLAG (cb_flag_no_cobol_comment, "no-cobol-comment", // N_("Do not generate COBOL comments into Java programs")) \ No newline at end of file From 44db99c2feb4a5a088649468deaffed0ddfe1a6c Mon Sep 17 00:00:00 2001 From: Yutaro Sakamoto Date: Wed, 9 Oct 2024 16:16:27 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[Fix]:=20convert=20`=EF=BC=8D`=20to=20`?= =?UTF-8?q?=EF=BC=BF`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cobj/codegen.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/cobj/codegen.c b/cobj/codegen.c index 57c73b8d..7262ea0c 100644 --- a/cobj/codegen.c +++ b/cobj/codegen.c @@ -234,7 +234,7 @@ static void get_java_identifier_helper(struct cb_field *f, char *buf) { } static void strcpy_identifier_cobol_to_java(char *buf, const char *identifier) { - if(cb_flag_var_name_hex) { + if (cb_flag_var_name_hex) { int all_ascii = 1; unsigned char *p = (unsigned char *)identifier; for (; *p; ++p) { @@ -263,16 +263,21 @@ static void strcpy_identifier_cobol_to_java(char *buf, const char *identifier) { } else { unsigned char *src = (unsigned char *)identifier; unsigned char *dst = (unsigned char *)buf; - while(*src) { + while (*src) { // ASCII characters - if(*src < 0x80) { + if (*src < 0x80) { *dst = (*src == '-') ? '_' : *src; ++dst; ++src; - // Shift_JIS characters + // Shift_JIS characters } else { - dst[0] = src[0]; - dst[1] = src[1]; + if (src[0] == 0x81 && src[1] == 0x7C) { + dst[0] = 0x81; + dst[1] = 0x51; + } else { + dst[0] = src[0]; + dst[1] = src[1]; + } dst += 2; src += 2; } From 1f125dacdfb72ab7ffb99b619a53679f9d358ba4 Mon Sep 17 00:00:00 2001 From: Yutaro Sakamoto Date: Wed, 9 Oct 2024 17:06:22 +0900 Subject: [PATCH 3/3] [Add]: add a test for -fvar-name-hex --- tests/Makefile.am | 1 + tests/Makefile.in | 1 + tests/command-line-options.at | 1 + .../command-line-options.src/fvar-name-hex.at | 68 +++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 tests/command-line-options.src/fvar-name-hex.at diff --git a/tests/Makefile.am b/tests/Makefile.am index c3934445..3fb827d3 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -163,6 +163,7 @@ command_line_options_DEPENDENCIES = \ command-line-options.src/Wtruncate.at \ command-line-options.src/Wunreachable.at \ command-line-options.src/ftrace-ftraceall.at \ + command-line-options.src/fvar-name-hex.at \ command-line-options.src/fsyntax-only.at \ command-line-options.src/fserial-variable.at \ command-line-options.src/fshort-variable.at \ diff --git a/tests/Makefile.in b/tests/Makefile.in index a84a5a63..7881d2bb 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -704,6 +704,7 @@ command_line_options_DEPENDENCIES = \ command-line-options.src/Wtruncate.at \ command-line-options.src/Wunreachable.at \ command-line-options.src/ftrace-ftraceall.at \ + command-line-options.src/fvar-name-hex.at \ command-line-options.src/fsyntax-only.at \ command-line-options.src/fserial-variable.at \ command-line-options.src/fshort-variable.at \ diff --git a/tests/command-line-options.at b/tests/command-line-options.at index b057fd5b..8ca2d2fc 100644 --- a/tests/command-line-options.at +++ b/tests/command-line-options.at @@ -23,6 +23,7 @@ m4_include([Wterminator.at]) m4_include([Wtruncate.at]) m4_include([Wunreachable.at]) m4_include([ftrace-ftraceall.at]) +m4_include([fvar-name-hex.at]) m4_include([fsyntax-only.at]) m4_include([fserial-variable.at]) m4_include([ffold-copy.at]) diff --git a/tests/command-line-options.src/fvar-name-hex.at b/tests/command-line-options.src/fvar-name-hex.at new file mode 100644 index 00000000..bac09192 --- /dev/null +++ b/tests/command-line-options.src/fvar-name-hex.at @@ -0,0 +1,68 @@ +AT_SETUP([-fvar-name-hex]) + +AT_DATA([prog.cbl], [ IDENTIFICATION DIVISION. + PROGRAM-ID. prog. + + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 新宿 PIC x(5) VALUE "00000". + 01 横浜−1 PIC x(5) VALUE "11111". + 01 甲府−2 PIC x(5) VALUE "22222". + 01 宇都宮1 PIC x(5) VALUE "33333". + 01 仙台2 PIC x(5) VALUE "44444". + PROCEDURE DIVISION. + 東京ラベル. + DISPLAY 新宿. + 神奈川−1ラベル. + DISPLAY 横浜−1. + 山梨−2ラベル. + DISPLAY 甲府−2. + 栃木1ラベル. + DISPLAY 宇都宮1. + 宮城2ラベル. + DISPLAY 仙台2. +]) + + +AT_CHECK([${COMPILE} prog.cbl]) +AT_CHECK([${RUN_MODULE} prog], [0], +[00000 +11111 +22222 +33333 +44444 +]) +AT_CHECK([cat prog.java | grep "新宿" > /dev/null]) +AT_CHECK([cat prog.java | grep "横浜−1" > /dev/null]) +AT_CHECK([cat prog.java | grep "甲府−2" > /dev/null]) +AT_CHECK([cat prog.java | grep "宇都宮1" > /dev/null]) +AT_CHECK([cat prog.java | grep "仙台2" > /dev/null]) + +AT_CHECK([cat prog.java | grep "東京ラベル" > /dev/null]) +AT_CHECK([cat prog.java | grep "神奈川−1ラベル" > /dev/null]) +AT_CHECK([cat prog.java | grep "山梨−2ラベル" > /dev/null]) +AT_CHECK([cat prog.java | grep "栃木1ラベル" > /dev/null]) +AT_CHECK([cat prog.java | grep "宮城2ラベル" > /dev/null]) + +AT_CHECK([${COMPILE} -fvar-name-hex prog.cbl]) +AT_CHECK([${RUN_MODULE} prog], [0], +[00000 +11111 +22222 +33333 +44444 +]) + +AT_CHECK([cat prog.java | grep "90568f68" > /dev/null]) +AT_CHECK([cat prog.java | grep "89a1956c817c8250" > /dev/null]) +AT_CHECK([cat prog.java | grep "8d62957b817c32" > /dev/null]) +AT_CHECK([cat prog.java | grep "894693738b7b8250" > /dev/null]) +AT_CHECK([cat prog.java | grep "90e591e432" > /dev/null]) + +AT_CHECK([cat prog.java | grep "938c8b9e83898378838b" > /dev/null]) +AT_CHECK([cat prog.java | grep "905f93de90ec817c825083898378838b" > /dev/null]) +AT_CHECK([cat prog.java | grep "8e52979c817c3283898378838b" > /dev/null]) +AT_CHECK([cat prog.java | grep "93c896d8825083898378838b" > /dev/null]) +AT_CHECK([cat prog.java | grep "8b7b8fe93283898378838b" > /dev/null]) + +AT_CLEANUP