Warning groups with -W

Every page of this course builds with -Wall, and this one shows what the flag does to a program that deserves it. The three flags of the subject are a group to turn warnings on, a group to turn more on, and one to make them fatal.

-Wall on one program

The program has two mistakes: a three-digit literal in a two-digit field, and a MOVE from a field into a REDEFINES of itself, which overlaps:

IDENTIFICATION DIVISION. PROGRAM-ID. W1. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-SMALL PIC 9(2) VALUE 100. 01 WS-SRC PIC X(5) VALUE "ABCDE". 01 WS-OVL REDEFINES WS-SRC PIC X(3). PROCEDURE DIVISION. MOVE WS-SRC TO WS-OVL. DISPLAY WS-SMALL. DISPLAY WS-SRC. STOP RUN.

cobc -x -free -Wall -o w1 w.cob
./w1

w.cob:5: warning: value size exceeds data size [-Wothers]
3 | DATA DIVISION.
4 | WORKING-STORAGE SECTION.
5 > 01 WS-SMALL PIC 9(2) VALUE 100.
6 | 01 WS-SRC PIC X(5) VALUE "ABCDE".
7 | 01 WS-OVL REDEFINES WS-SRC PIC X(3).
w.cob:9: warning: overlapping MOVE may produce unpredictable results [-Woverlap]
7 | 01 WS-OVL REDEFINES WS-SRC PIC X(3).
8 | PROCEDURE DIVISION.
9 > MOVE WS-SRC TO WS-OVL.
10 | DISPLAY WS-SMALL.
11 | DISPLAY WS-SRC.
00
ABCDE

Two warnings, and the run shows that both were right: the two-digit field holds 00, the low two digits of 100, and WS-SRC still holds its five letters because the overlapping move copied three characters onto the start of itself.

The bracketed group after each message is the one to turn off: [-Wothers] and [-Woverlap]. That word is the whole interface of the warning system.

-Wextra and the groups it adds

-Wextra is -Wall plus the groups the compiler does not set by default. The same program gets five warnings:

cobc -x -free -Wextra -o w1 w.cob

w.cob:5: warning: value size exceeds data size [-Wothers]
w.cob:9: warning: overlapping MOVE may produce unpredictable results [-Woverlap]
w.cob:9: warning: sending field larger than receiving field [-Wpossible-truncate]
w.cob:10: warning: DISPLAY statement not terminated by END-DISPLAY [-Wterminator]
w.cob:11: warning: DISPLAY statement not terminated by END-DISPLAY [-Wterminator]

Two of the three new ones are about the same move: the field it sends is larger than the field it fills, which -Wpossible-truncate reports because the sizes are known. The other two are about the missing END-DISPLAY terminators, which -Wterminator wants on every statement that has a scope terminator and does not use it.

Five warnings for four lines of procedure division is not a reason to avoid -Wextra. It is a reason to build the code the course writes with -Wall and to run -Wextra once on an old program that is about to be changed, which is where the warnings are worth their noise.

-Werror

-Werror makes the warnings errors. The messages keep their text and change their word and their group, and the build stops with status 1:

cobc -x -free -Wall -Werror -o w2 w.cob
echo $?

w.cob:5: error: value size exceeds data size [-Werror=others]
w.cob:9: error: overlapping MOVE may produce unpredictable results [-Werror=overlap]
1

That is the gate of a shop that does not want to see the same warning for the tenth year: a program either builds clean or it does not build. The cost is a build that breaks on a new compiler for a warning that was always there, which is a decision for the shop and not for the language.

Turning one group off

-Wno- and the name of a group takes that one out of the set. The counts on this program, with -Wextra:

cobc -x -free -Wextra -o w1 w.cob 2>&1 | grep -c "warning:"
cobc -x -free -Wextra -Wno-overlap -o w1 w.cob 2>&1 | grep -c "warning:"
cobc -x -free -Wextra -Wno-possible-truncate -o w1 w.cob 2>&1 | grep -c "warning:"

5
4
4

One group off, one warning fewer. A shop that has decided an overlapping move is deliberate writes -Wno-overlap in its build file and keeps the other four, which is the point of groups rather than one switch.

The groups of this compiler

The list comes from cobc --help. The ones a COBOL program meets most often are these:

GroupWhat it reportsIn -Wall
-Wtruncatea constant that does not fit its fieldyes
-Woverlapa MOVE whose fields overlapyes
-Wredefinitiona data item defined twice in one scopeyes
-Wtypinga type mismatch in a MOVE or a comparisonyes
-WparenthesesAND inside OR without parenthesesyes
-Wpossible-truncatea move that may truncate at run timeno
-Wpossible-overlapa move that may overlap at run timeno
-Wstrict-typinga type mismatch even when the sizes matchno
-Wterminatora statement without its scope terminatorno
-Wobsoletea feature the standard calls obsoleteno

The three groups the help marks as always active are the ones a build cannot turn off: -Wunfinished, -Wpending and -Wignored-error. They are the words the OO lessons showed: OBJECTS is not implemented came from -Wpending and no flag silences it.

The next lesson puts the flags into a build file, which is where they belong once a program has more than one file.

Exercises

  1. Build the program with -Wall -Wno-overlap and count the warnings.
  2. Add the missing END-DISPLAY terminators and build with -Wextra again. How many warnings are left?
  3. Write a condition with AND inside OR and no parentheses and build with -Wall.
  4. Put -Werror in a shell script that builds the program of this page and stops on the first failure.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which flag turns on most of the warnings of cobc?

    Answer

    -Wall

    The page built the program with -Wall and got two of its five warnings.

  2. What does -Wextra add to -Wall?

    Answer

    The groups the compiler does not set by default, such as -Wpossible-truncate

    Three more warnings appeared, one of them from -Wpossible-truncate.

  3. What does -Werror do?

    Answer

    It makes every warning an error and the build fails with status 1

    The messages changed to -Werror=others and the build stopped with 1.

  4. How is one warning group turned off?

    Answer

    -Wno- and the name of the group

    -Wno-overlap took the count from five to four.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-S PIC 9(2) VALUE 99. PROCEDURE DIVISION. DISPLAY W-S. STOP RUN.

    Answer

    99

    The two digits of the field are both in use.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-S PIC X(5) VALUE "ABCDE". 01 W-O REDEFINES W-S PIC X(3). PROCEDURE DIVISION. MOVE W-S TO W-O. DISPLAY W-S. STOP RUN.

    Answer

    ABCDE

    The overlapping move copies the first three characters onto the start of the same field.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9(3) VALUE 0. 01 W-I PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM VARYING W-I FROM 1 BY 1 UNTIL W-I IS GREATER THAN 2 ADD W-I TO W-N END-PERFORM. DISPLAY W-N. STOP RUN.

    Answer

    003

    One and two add to three.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(2) VALUE 12. 01 W-B PIC 9(2) VALUE 30. 01 W-T PIC 9(3). PROCEDURE DIVISION. ADD W-A W-B GIVING W-T DISPLAY W-T. STOP RUN.

    Answer

    042

    Twelve and thirty add to forty-two.

  9. Which groups can a build not turn off?

    Answer

    -Wunfinished, -Wpending and -Wignored-error

    The help marks those three as always active.

  10. What is the cost of -Werror in a shop?

    Answer

    A new compiler can break a build over a warning that was always there

    Every warning becomes a failure, old ones included.

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

Banner image