Warnings, errors and -Wall
A COBOL compiler is not quiet, but it separates two things: a warning says the program is valid and probably not what the writer meant, an error says the program cannot be built at all. Both print the same way, with the file, the line, a copy of the source and sometimes a note that points at the declaration the message is about.
This lesson reads four warnings and one error from a real build, shows which of them appear only because -Wall is on, and ends with the two flags that turn the whole subject into a gate: -Werror and -fsyntax-only.
Warnings that are always on
The first program gives a two-digit field a three-digit value. The compiler keeps the two low-order digits and says so; the build succeeds, and without -Wall the message is printed too, because this group is on by default.
IDENTIFICATION DIVISION.
PROGRAM-ID. SMALL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SMALL PIC 9(2) VALUE 100.
PROCEDURE DIVISION.
DISPLAY SMALL.
STOP RUN.
cobc -x -free -Wall -o small small.cob
./small
small.cob:6: warning: value size exceeds data size [-Wothers]
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 SMALL PIC 9(2) VALUE 100.
7 |
8 | PROCEDURE DIVISION.
00
The output is 00, not an error and not 100: VALUE 100 into a two-digit field keeps the digits it can hold. The warning is the only thing that says a digit was lost, which is why the course builds everything with warnings visible.
The second default warning is about punctuation. A program whose last statement has no period still ends the sentence at the end of the file, and the compiler accepts it with a dialect warning.
IDENTIFICATION DIVISION.
PROGRAM-ID. PERIOD.
PROCEDURE DIVISION.
DISPLAY "hi".
STOP RUN
cobc -x -free -Wall -o period period.cob
./period
period.cob:7: warning: optional period used [-Wdialect]
5 | DISPLAY "hi".
6 | STOP RUN
7 > <EOF>
hi
What -Wall adds
-Wall adds the groups the compiler keeps for code that is valid but suspicious. The program below moves a six-character literal into a three-character field. Without -Wall the build is silent; with it, the compiler reports the truncation and then points at both the literal and the declaration.
IDENTIFICATION DIVISION.
PROGRAM-ID. TRUNC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NAME-FIELD PIC X(3).
01 NUM-FIELD PIC 9(3).
PROCEDURE DIVISION.
MOVE "ABCDEF" TO NAME-FIELD.
MOVE 123 TO NAME-FIELD.
DISPLAY NAME-FIELD.
DISPLAY NUM-FIELD.
STOP RUN.
cobc -x -free -Wall -o trunc trunc.cob
./trunc
trunc.cob:10: warning: value size exceeds data size [-Wtruncate]
8 |
9 | PROCEDURE DIVISION.
10 > MOVE "ABCDEF" TO NAME-FIELD.
11 | MOVE 123 TO NAME-FIELD.
12 | DISPLAY NAME-FIELD.
trunc.cob:10: note: value size is 6 [-Wtruncate]
trunc.cob:6: note: 'NAME-FIELD' defined here as PIC X(3) [-Wtruncate]
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 NAME-FIELD PIC X(3).
7 | 01 NUM-FIELD PIC 9(3).
8 |
123
000
Read the notes in order. The first note gives the length of the literal, the second names the line the field was declared on, and the snippet after it marks that line with a > in the middle column. The run then shows what the first MOVE did: it kept ABC, and the second MOVE overwrote it with 123. NUM-FIELD was never set, so it holds zeros.
An error, not a warning
A warning leaves a program that runs; an error does not. The program below performs a paragraph that does not exist, which is the mistake a reader makes by renaming a paragraph and forgetting a PERFORM.
IDENTIFICATION DIVISION.
PROGRAM-ID. MISSING.
PROCEDURE DIVISION.
PERFORM REPORT-TOTAL.
STOP RUN.
cobc -x -free -Wall -o missing missing.cob
missing.cob:5: error: 'REPORT-TOTAL' is not defined
3 |
4 | PROCEDURE DIVISION.
5 > PERFORM REPORT-TOTAL.
6 | STOP RUN.
7 | <EOF>
The status of that build is 1 and no output file is written; the shell sees the failure and a Makefile stops. The message names the paragraph in quotes, which is how every cobc diagnostic names the word it could not resolve.
How to read a diagnostic
Every message has the same four parts, and reading them in order is faster than reading the text:
- file:line: the place, from the first character. The line number is the COBOL line, not a line of the generated C.
- warning: or error:, then the message, then the group in brackets. The group is what -Wno-<group> switches off.
- The snippet: three lines around the position, with > on the line itself.
- Any note: lines after it, each with its own position, which point at the other half of the problem.
The snippet is not the source as you wrote it: it is the compiler's copy, with the line numbers it uses. The lesson on the -t listing shows the file to open when the mapping to the generated C matters.
Turning a warning into an error
-Werror keeps every warning and makes the build fail on the first one. The group name changes shape in the message, from [-Wothers] to [-Werror=others], and the word warning becomes error: the diagnostic is the same, the status is not.
cobc -x -free -Wall -Werror -o werror small.cob
echo "status: $?"
small.cob:6: error: value size exceeds data size [-Werror=others]
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 SMALL PIC 9(2) VALUE 100.
7 |
8 | PROCEDURE DIVISION.
status: 1
A project decides once whether it builds with -Werror. The course does not: a lesson that teaches a warning has to be able to show it.
The gate before a commit
-fsyntax-only runs the whole front end, warnings included, and writes nothing. With -Wall -Werror it becomes a check that a folder of sources is clean, and it is what this site runs over every program printed on a lesson page.
cobc -fsyntax-only -free -Wall small.cob
echo "status: $?"
small.cob:6: warning: value size exceeds data size [-Wothers]
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 SMALL PIC 9(2) VALUE 100.
7 |
8 | PROCEDURE DIVISION.
status: 0
The status is 0 because the check found no error, and the warning is still printed: a gate that wants silence adds -Werror as well.
Exercises
- Build small.cob without -Wall and confirm that the warning is still printed. Which of the four warnings on this page are in a group that -Wall controls?
- Switch the -Wtruncate warning off with -Wno-truncate and build trunc.cob again. What is printed, and what does the program still do?
- Add a paragraph named REPORT-TOTAL to missing.cob that displays one line, and build it. Which line of output appears first?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the difference between a warning and an error in a cobc build?
Answer
A warning leaves a program that builds, an error does not
A warning describes a program that is valid and probably wrong; an error stops the build and no output file is written.
-
What does
-Wallchange?Answer
It adds warning groups that are off by default
-Wall enables the extra groups, such as -Wtruncate; -Werror is the flag that turns warnings into failures.
-
Where does the line number in a cobc diagnostic point?
Answer
The line of the COBOL source
The position is the COBOL source line, and the snippet printed under it is that line and its neighbours.
-
What does a
note:line after a warning add?Answer
The other half of the story, such as where a field was declared
Notes point at the declaration or the other operand, each with its own position, so the reader sees both ends of the problem.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 D PIC 9(1) VALUE 12. PROCEDURE DIVISION. DISPLAY D. STOP RUN.Answer
2
The literal is truncated to the one digit the field holds, and the digit kept is the low-order one.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. PROCEDURE DIVISION. PERFORM REPORT-TOTAL. STOP RUN.Answer
error: 'REPORT-TOTAL' is not defined
PERFORM needs a paragraph or section of that name, and cobc reports the name it cannot find.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAME-FIELD PIC X(3). PROCEDURE DIVISION. MOVE "ABCDEF" TO NAME-FIELD. DISPLAY NAME-FIELD. STOP RUN.Answer
ABC
MOVE into a shorter alphanumeric field keeps the leftmost characters, and DISPLAY prints exactly three.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL PIC 9(4). PROCEDURE DIVISION. DISPLAY TTOTAL. STOP RUN.Answer
error: 'TTOTAL' is not defined
TTOTAL was never declared, and a name used before it is declared is an error, not a warning.
-
What does
-Werror=othersmean in a diagnostic line?Answer
The warning group others was reported as an error
With -Werror a warning of that group stops the build, and the bracket shows the group under its -Werror= name.
-
Which build prints nothing at all for a program whose only flaw is a truncated literal?
Answer
cobc -x -free -Wno-truncate -Wall prog.cob
-Wno-truncate switches that group off even after -Wall, so the truncation goes unreported.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev