Skip to content

Conversation

@kio-watanabe
Copy link
Contributor

Overview

  • Improved readability in the generated Java code by adding line breaks for each & delimiter when string literals are concatenated with & in the COBOL program.
  • Background: A fix was made in Open COBOL ESQL4J to format SQL statements as shown below. This change was implemented to preserve the SQL statement formatting during conversion using opensource COBOL4J.
    OCESQL 01 SQ0002.
    OCESQL 02 FILLER PIC X(184) VALUE
    OCESQL " CREATE TABLE EMP"
    OCESQL & " ("
    OCESQL & " EMP_NO NUMERIC(4,0) NOT NULL,"
    OCESQL & " EMP_NAME CHAR(20),"
    OCESQL & " EMP_SALARY NUMERIC(4,0),"
    OCESQL & " CONSTRAINT IEMP_0 PRIMARY KEY (EMP_NO)"
    OCESQL & " )".
    OCESQL 02 FILLER PIC X(1) VALUE X"00".
  • Added tests to verify the output of &-concatenated strings in the generated Java code.

Details

Improvement of Generated Java Code for &-Concatenated String Literals

  • Code conversion example:
  01 X-CONCAT PIC X(25) VALUE "abcde"
  & "fghij"
  & "klmno"
  & "pqrst"
  & "uvwxy".
  public static final byte[] str_0_abcdefghijklmnopqrstuvwxy = CobolUtil.stringToBytes(
  "abcde" + 
  "fghij" + 
  "klmno" + 
  "pqrst" + 
  "uvwxy"
  );
  • Struct modifications
    • Added two variables to struct cb_literal_segment:
      • size_t *segment_sizes: Stores the length of each segment in the &-concatenated string. For example, for "abcde" & "fghijkl" & "mno", it holds {5, 7, 3}.
      • size_t segment_count: Stores the number of segments in the &-concatenated string. For example, for "ab" & "cd" & "ef", it holds 3.
    • Added size_t *segment_sizes to struct string_literal_cache. Its purpose is the same as in struct cb_literal_segment.
  • Modifications to tree.c
    • Modified the cb_tree cb_concat_literals function:
      • Changed the part that currently calls cb_build_alphanumeric_literal to call the newly added functions cb_build_concat_alphanumeric_literal or cb_build_concat_national_literal instead.
    • Added cb_tree cb_build_concat_alphanumeric_literal and cb_tree cb_build_concat_national_literal functions:
      • These functions are wrappers to call the newly added build_concat_literal function.
    • Added cb_literal *build_concat_literal function:
      • This function extends the existing build_literal function by adding logic to populate segment_sizes (with segment lengths) and segment_count (with the number of segments).
  • Modifications to codegen.c
    • Added global variables size_t *sgmt_sizes and size_t sgmt_count:
      • Used within the static void joutput_initialize_one function to temporarily store segment_sizes and segment_count.
    • Modified the static void joutput_string function:
      • It references the values stored in the global variables size_t *sgmt_sizes and size_t sgmt_count and stores them in new_literal_cache->segment_sizes. This new_literal_cache->segment_sizes is then passed as an argument to the static void joutput_string_write function.
    • Modified the static void joutput_string_write function:
      • Added size_t *tmp_sgmt_sizes as an argument.
      • If the value of size_t *tmp_sgmt_sizes is not NULL, it outputs line breaks in the Java code according to the string lengths stored in size_t *tmp_sgmt_sizes.

Test Addition

  • Added convert-string-concat.at to misc.src
    • Tests the Java code output for variables defined with PIC X and PIC N that have &-concatenated strings in their VALUE clause.

Operation Check with Open COBOL ESQL4J Integration

@kio-watanabe kio-watanabe requested a review from Copilot October 20, 2025 09:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Improves Java code output formatting for COBOL string literals concatenated with '&' by preserving the original segment structure and adding appropriate line breaks in the generated code. This change enhances readability when SQL statements and other multi-line strings are converted from COBOL to Java.

  • Modified literal handling structures to track segment sizes and counts for concatenated strings
  • Updated code generation to output each segment on a separate line with proper indentation
  • Added comprehensive tests to verify the formatting improvements

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/misc.src/convert-string-concat.at New test file verifying Java output formatting for '&'-concatenated strings
tests/misc.at Includes the new test file in the test suite
tests/Makefile.in Updates build configuration to include the new test
tests/Makefile.am Updates automake configuration to include the new test
cobj/tree.h Adds segment tracking fields to cb_literal struct and new function declarations
cobj/tree.c Implements segment tracking logic and new concatenation functions
cobj/codegen.c Updates Java code generation to format concatenated strings with line breaks

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@kio-watanabe
Copy link
Contributor Author

@yutaro-sakamoto
I have addressed the points you raised.

@yutaro-sakamoto yutaro-sakamoto merged commit 7ea264b into opensourcecobol:develop Oct 24, 2025
145 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants