Follow

Analyze COBOL program/s for dead/unreachable code

COBOL DEAD/UNREACHABLE CODE ANALYSIS

Beginning rev 18.4.9 Elastic COBOL allows the developer to perform code analysis and identify dead code as well as unreachable code. In COBOL we term dead code as procedures (Sections or Paragraphs) that have been defined but not invoked and unreachable code as code within a paragraph that follows a verb which redirects logic elsewhere such as GO TO, EXIT PROGRAM etc. Refer to the following program:

IDENTIFICATION DIVISION.
PROGRAM-ID. xxx.
PROCEDURE DIVISION.
main-para.
PERFORM PARA1
STOP RUN.

PARA1.
display "PARA1".
GO TO PARA2.
DISPLAY "UNREACHABLE CODE".
PERFORM PARA3.

PARA2.
display "PARA2".
EXIT PROGRAM.

PARA3.
display "PARA3".

PARA4.
display "PARA4".

In the above program, PARA3 and PARA4 is dead code since they will never be invoked. In PARA1 statements following the GO TO are unreachable code. 

The compiler marks code as unreachable or dead, only if the logic flow does not involve conditional verbs. For example consider the code below:

IF WITCH=WET
   GO TO KANSAS
END-IF
DISPLAY "TOTO! WE ARE HOME"

In the above case the DISPLAY statement would be considered as used code. Simply put when conditional logic is encountered the compiler always assumes all paths as invoked.

To compile your program for dead code analysis specify the compiler directive as follows:

-out:analyzedeadcode 0|1|2         where 0=Comment generated code

                                                            1=Remove generated code

                                                            2=Retain generated code

The param value refers to the state of the output java generated code. For the above sample program PARA3 output java code with a compiler directive of -out:analyzedeadcode 0  would be

public final int para3_OF_defaultsection_0()
{
CobolException.runtimeError("Dead code usage:para3_OF_defaultsection_0");
/*
boolean _sizeError=false;
_sizeError=!_context.system().display(_context,(String)"PARA3");
return 0; // fall through
*/ return 0;
} // End Paragraph

whereas a compiler directive of  -out:analyzedeadcode 1 would result in the following output:

public final int para3_OF_defaultsection_0()
{
CobolException.runtimeError("Dead code usage:para3_OF_defaultsection_0");
return 0;
} // End Paragraph

Note how in both cases the compiler generates a CobolException statement into the dead code to warn the user about zombie outbreaks.

Using param value of 2 would generate the output code as default.

In all 3 cases the compiler will generate warnings for dead code as well as unreachable code.

When dealing with graphical programs that have GUI screens with event procedures, all such procedures are considered active. 

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk