# Finding and editing dead code with Coco

Finding and editing dead code with Coco

As part of the coverage report, Coco generates a list of lines with dead code. One would often like to remove these lines, but when they are spread over a large project, it requires some effort to navigate the editor to them. This can be made easier with a lesser-known feature of the cmreport program, which is part of Coco. With cmreport, it is possible to edit these lines one by one in a text editor or an IDE. The same idea can be used for other tasks.

The idea behind it is based on the fact that many editors and IDEs can read the output of the compiler – especially the error messages and warnings – and jump to the places in the code which they refer to. We can use this feature and let cmreport generate output that looks like compiler error messages and points to the lines with dead code.

To do this we use the text output format of cmreport. Mostly one uses cmreport to generate HTML reports, but there is also a line-oriented plain text format that is ideally suited to generate imitated compiler error messages.

### A script to generate error messages in the Unix format

The format for error messages differs between Unix-like systems and Windows. We will start with the Unix format. In Unix, compiler error messages look like /path/to/file.cpp:55:12: Some error message, where the first number is a line number and the second one the column number at which the error occurs.

The following shell script creates a list of dead code locations in the same format:

#! /bin/bash
#
# Usage: deadcode.sh file.csmes

COCODIR=/opt/SquishCoco/bin
${COCODIR}/cmreport -m "$1" --text= --format-dead-code='%f:%l:%c: %m'


It requires that Coco is installed in the standard location, at /opt/SquishCoco. If it is not, the variable COCODIR must be changed. (And do not forget to make the script executable with « chmod +x deadcode.sh« !)

The script has one parameter, which is a .csmes file. cmreport reads this file, extracts lines with dead code in it and writes them to the standard output. To do this, the following options are used:

• -m specifies the .csmes file to read. Here it is the first parameter of the script, which is enclosed in quotation marks to handle file paths with spaces correctly.
• –text specifies that the output format is text. There is no output file given after the « = » sign, which means that the output of cmreport appears on the standard output stream.
• –format-dead-code specifies the output format for lines with dead code. There are no other format specification flags given, therefore cmreport will only report dead code lines. The format string contains the following escape sequences:
• %f stands for the full path to the file,
• %l is the line number of the line,
• %c the column number, and finally
• %e is a short description of the line. In case of dead code it starts with the words « dead code:  » and then continues with the content of the line.

Together these flags let cmreport produce an output in the same format as a C/C++ compiler.

### Use with the parser example

The parser example in the tutorial that comes with Coco contains a few lines of dead code. We can therefore use our script to find them.

First we need to generate an instrumented version of the parser, to get a .csmes file, in our case parser.csmes. Running the parser is not necessary, it is enough to compile it with « ./instrumented make -f gmake.mak distclean parser« .

We will use is the Emacs editor to edit the source code. Emacs has a compile command, which usually is used to run compilation commands like make. We need to run our deadcode.sh script instead, in the way that is displayed at the right. The way it is displayed here, the deadcode.sh script must be located in the same directory as the file parser.csmes, and the compile command must be run from a file that is in the same directory as there files. If that does not work in your case, try to use full paths for the script and he .csmes file.

The result is shown at the right. At the bottom one sees the output of the script showing that the file parser.cpp contains five lines of dead code. Emacs has already opened the file automatically and placed the cursor at the beginning of the first line with dead code.

### A script for Windows

Under windows, error messages have a different format. They look like C:\path\to\file.cpp(55): Some error message, with the line number in braces. This needs different command line parameters for cmreport and we also should better write the script that generates the fictitious error messages as a Windows batch file.

The result is shown below; the most important difference to the Unix version is due to the fact that the percent sign plays a special role in the batch file syntax; to get the right output we need to use double percent signs in the format string. We also need to be aware that Coco may also be installed in the directory C:\Program Files (x86)\squishcoco; if that is the case, the variable COCODIR needs to be changed.

@echo off
REM Usage: deadcode.cmd file.csmes

set "COCODIR=C:\Program Files\squishcoco"
"%COCODIR%\cmreport" -m %1 --text= --format-dead-code="%%f(%%l): %%m"

### Use with Visual Studio

Users who prefer Emacs can use the script in the same way as on Unix, but what do those who prefer Visual Studio? One possibility here is to use the « External Tools » feature of Visual Studio.

To create an external tool that generates a list of dead code lines, one needs to select the entry « External tools… » in the « Tools » menu. A dialog window appears, in which a new entry for « Dead code » must be entered as shown in the image at the right. In the « Commands » and « Arguments » fields, full path names are needed; for « Arguments », the variable \$(TargetDir) often evaluates to an useful path prefix for the .csmes file.

Among the checkboxes, the most important one is that for « Use Output window », which directs the script output to the compiler output window of Visual Studio: Clicking on error messages in this window directs the editor to the place in the code to which they refer.

The only disadvantage of this method is that it has the .csmes file built in, which could be annoying when the project changes. As a remedy, the checkbox « Prompt for arguments » is here also selected: It allows to enter a different .csmes file, if necessary.