Tooling: cobc and g++

A compiler is a program with a flag set, and the flag set is most of what a team means by tooling. This page runs the same three gates through both compilers: a syntax check, a warning build that fails on warnings, and a runtime check on a bug that compiles cleanly. Two of the three have a direct equivalent. The third does not, and the difference is worth knowing before a project plans its testing.

The flags side by side

purpose cobc g++ compile and link -x (default) compile to object -c -c module or library -m -shared syntax check only -fsyntax-only -fsyntax-only warnings -Wall, -Wextra -Wall, -Wextra warnings as errors -Werror, -Werror=truncate -Werror optimise -O2 -O2 debug information -g -g runtime checks -debug (subscript, CALL) -fsanitize=address,undefined listing or assembly -t (a listing) -S (assembly) language standard -std=cobol85 / cobol2014 -std=c++23

The names are close enough that a build script reads the same on both sides, with two exceptions: C++ links by default and COBOL needs -x, and the runtime checks live in different places. The rest of this page is those two gates.

The first gate: syntax only

Both compilers can parse a file without producing anything, which is what a pre-commit hook wants:

cobc -fsyntax-only -free -Wall maint.cob
echo $?
g++ -std=c++23 -fsyntax-only -Wall -Wextra maint.cpp
echo $?

0
0

No output and status zero: the two files of the previous page are valid. cobc --help describes the option as "syntax error checking only; don\'t emit any output", and it is the cheapest gate a COBOL build can have.

The second gate: warnings and -Werror

A COBOL program with a literal that does not fit its field, which is the mistake that cuts a surname in a report:

IDENTIFICATION DIVISION. PROGRAM-ID. WARN. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NAME PIC X(20) VALUE "Ada Lovelace". PROCEDURE DIVISION. MOVE "Katherine Johnson-Longname" TO WS-NAME DISPLAY "name [" WS-NAME "]" STOP RUN.

cobc -x -free -Wall -O2 -o warn warn.cob
./warn

warn.cob:9: warning: value size exceeds data size [-Wtruncate]
   7 |
   8 | PROCEDURE DIVISION.
  9 > MOVE "Katherine Johnson-Longname" TO WS-NAME
   10 | DISPLAY "name [" WS-NAME "]"
   11 | STOP RUN.
name [Katherine Johnson-Lo]

The warning names the rule and the line, and the program runs with the name cut. To make the build stop, the same command with -Werror:

cobc -x -free -Wall -Werror -O2 -o warn_error warn.cob
echo $?

warn.cob:9: error: value size exceeds data size [-Werror=truncate]
   7 |
   8 | PROCEDURE DIVISION.
  9 > MOVE "Katherine Johnson-Longname" TO WS-NAME
   10 | DISPLAY "name [" WS-NAME "]"
   11 | STOP RUN.
1

C++ does the same with a different warning, an unused variable:

#include <cstdio> int main() { int unused = 0; std::printf("hello\n"); return 0; }

g++ -std=c++23 -Wall -Wextra -Werror -o /dev/null warn.cpp
echo $?

warn.cpp: In function 'int main()':
warn.cpp:3:9: error: unused variable 'unused' [-Werror=unused-variable]
    3 |     int unused = 0;
       |         ^~~~~~
1

Both gates work the same way and both are worth having in CI. The difference is the size of the warning set: g++ with -Wall -Wextra knows several hundred diagnostics, cobc knows a few dozen, and the COBOL ones are about the things COBOL can get wrong: truncation, size, a missing period, an unreachable paragraph. That is a smaller net and a more uniform language to throw it over.

The sanitizers

The third gate is the one with no COBOL equivalent: a build that checks memory at run time. A C++ program that writes past the end of an array compiles with no warning at the default level and crashes somewhere else later; with AddressSanitizer it stops at the write:

#include <cstdio> int main() { int* values = new int[4]; for (int i = 0; i < 40; ++i) { values[i] = i; } std::printf("last %d\n", values[39]); delete[] values; return 0; }

g++ -std=c++23 -O0 -g -fsanitize=address -o asan2 asan2.cpp
./asan2
echo $?

=================================================================
==536==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7a2689900020 at pc 0x5730aaeab270 bp 0x7ffc3ae8a0e0 sp 0x7ffc3ae8a0d0
WRITE of size 4 at 0x7a2689900020 thread T0
    #0 0x5730aaeab26f in main /tmp/b/tool/asan2.cpp:5
    #1 0x7e068a3f4153 in libc_start_main_stage2 src/env/__libc_start_main.c:95

0x7a2689900020 is located 0 bytes after 16-byte region [0x7a2689900010,0x7a2689900020)
allocated by thread T0 here:
    1

UndefinedBehaviorSanitizer covers the arithmetic and the null pointer:

#include <cstdio> int main() { int big = 2147483647; big += 1; std::printf("big %d\n", big); int* p = nullptr; if (big < 0) { std::printf("%d\n", *p); } return 0; }

g++ -std=c++23 -O0 -g -fsanitize=undefined -o ub ub.cpp
./ub
echo $?

ub.cpp:4:9: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
big -2147483648
ub.cpp:8:20: runtime error: load of null pointer of type 'int'
Segmentation fault (core dumped)
139

COBOL has nothing of this kind, and it does not need the memory half: subscripts, reference modification and record lengths are checked by cobc -debug, which is a runtime check of the language, and the lesson on -debug shows it stopping a program at the line that wrote past a table. What COBOL does not have is a checker for the arithmetic: an ADD that overflows a field runs ON SIZE ERROR only if the program wrote that clause, and a program that did not gets a truncated number and no message. The COBOL answer is the clause in the source; the C++ answer is a build flag.

gdb on a crash

Both languages end up in gdb with a binary built with -g. The C++ crash of the previous section, in batch mode:

gdb -batch -ex 'set disable-randomization off' -ex run -ex 'bt 3' ./ub

ub.cpp:4:9: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
big -2147483648
ub.cpp:8:20: runtime error: load of null pointer of type 'int'

Program received signal SIGSEGV, Segmentation fault.
0x00005cbf38f10231 in main () at ub.cpp:8
8        std::printf("%d\n", *p);
#0  0x00005cbf38f10231 in main () at ub.cpp:8

The COBOL side of the same tool is the cobcrun and gdb lesson: a breakpoint on a COBOL line, a backtrace through the generated C, and the same warning that gdb in a container prints. A team that knows gdb knows both, which is one tool fewer to learn than the flag tables suggest.

What one toolchain has and the other does not

CheckCOBOLC++
memory and bounds-debug: subscript, CALL, record lengthASan, UBSan, valgrind
arithmetic overflowON SIZE ERROR in the sourceUBSan, and -ftrapv
data racesno threads in the languageThreadSanitizer
coveragegcov over the generated Cgcov, llvm-cov
test frameworknone in the imagenone in the language

Two of the five rows are equal, one favours COBOL and two favour C++. The COBOL advantage is that the checks are part of the language and the subscripts are checked rather than likely; the C++ advantage is that its tools find problems the language does not have a notion of. A project that moves from one to the other should budget for the row it loses, and the testing group is what this image can do about the COBOL side.

The next page is the closing one: what all of these pages measured, and where each language is the right choice.

The COBOL was built with cobc (GnuCOBOL) 3.2.0 and the C++ with g++ (Alpine 15.2.0) 15.2.0, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container with nproc 4. The sanitizer builds used -O0 -g, the others -O2.

Article author: Arthur Isaev