Gem #156: Listing Control in GNAT

by Robert Dewar —AdaCore

Let's get started...

The default output from the compiler just includes the error messages, along with any warnings that are enabled by default.

For example:

	f.adb:3:04: warning: "return" statement missing following this statement
        f.adb:3:04: warning: Program_Error may be raised at run time
        f.adb:4:14: warning: value not in range of type "Standard.Natural"
        f.adb:4:14: warning: "Constraint_Error" will be raised at run time
        f.adb:6:16: division by zero
        f.adb:6:16: static expression fails Constraint_Check

These messages show the exact location of messages, and if you edit the file you can find out exactly where each message is issued. But there are many switches that can be used to modify the output. To see better where each message is issued, without generating too much output, you can use -gnatv:

     3.    if A > B then
           |
        >>> warning: "return" statement missing following this statement
        >>> warning: Program_Error may be raised at run time
     4.       return -1;
                     |
        >>> warning: value not in range of type "Standard.Natural"
        >>> warning: "Constraint_Error" will be raised at run time
     6.       return 5 / 0;
                       |
        >>> division by zero
        >>> static expression fails Constraint_Check

And if you use -gnatl, you can get a full listing with line numbers and all the messages:

Compiling: f.adb (source file time stamp: 2013-12-28 18:26:22)

     1. function F (A, B : Natural) return Natural is
     2. begin
     3.    if A > B then
           |
        >>> warning: "return" statement missing following this statement
        >>> warning: Program_Error may be raised at run time
     4.       return -1;
                     |
        >>> warning: value not in range of type "Standard.Natural"
        >>> warning: "Constraint_Error" will be raised at run time
     5.    elsif B = 0 then
     6.       return 5 / 0;
                       |
        >>> division by zero
        >>> static expression fails Constraint_Check
     7.    end if;
     8. end F;

Note that in the above output, the source-file time stamp may be annoying if, for example, you are filing regression test output, but it can be suppressed using the switch -gnatd7. Also -gnatl takes an optional parameter (e.g., -gnatl=f.lst) that allows this output to be written to a designated file.

In the above output, we have messages that extend over two lines. The switch -gnatjnn, where nn is a decimal integer, provides a nice way of outputting such messages. The nn value is the maximum line length, so, for example, if we would like to limit the output message length to 68 characters, we can use the switches -gnatl and -gnatj68:

     1. function F (A, B : Natural) return Natural is
     2. begin
     3.    if A > B then
           |
        >>> warning: "return" statement missing following this
            statement, Program_Error may be raised at run time
     4.       return -1;
                     |
        >>> warning: value not in range of type "Standard.Natural",
            "Constraint_Error" will be raised at run time
     5.    elsif B = 0 then
     6.       return 5 / 0;
                       |
        >>> division by zero, static expression fails
            Constraint_Check
     7.    end if;
     8. end f;

The -gnatj switch is a pretty recent addition, and many people are not aware of it, but it is definitely nice in many situations.

In addition to basic source output control, there are various auxiliary outputs that are useful. Of particular interest is -gnatR, causing the compiler to print representation information, including sizes and alignments, which can be very useful for diagnosing problems in interfacing to external systems and hardware.


About the Author

Dr. Robert Dewar is co-founder, President and CEO of AdaCore and Emeritus Professor of Computer Science at New York University. With a focus on programming language design and implementation, Dr. Dewar has been a major contributor to Ada throughout its evolution and is a principal architect of AdaCore’s GNAT Ada technology. He has co-authored compilers for SPITBOL (SNOBOL), Realia COBOL for the PC (now marketed by Computer Associates), and Alsys Ada, and has also written several real-time operating systems, for Honeywell Inc. Dr. Dewar has delivered papers and presentations on programming language issues and safety certification and, as an expert on computers and the law, he is frequently invited to conferences to speak on Open Source software, licensing issues, and related topics.