Next Previous Contents

7. Package for output of compiler messages

The most of compilers report error messages for incorrect program. Here the package `errors' which serves to output one-pass or multi-pass compiler messages of various modes (errors, warnings, fatal, system errors and appended messages) in Unix style or for traditional listing is suggested. The package also permits adequate error reporting for included files.

The package uses packages `vlobject', `objstack', `position' which use package `allocate'. Therefore package `position' have to be initiated before any work with this package. The interface part of the package is file `errors.h'. The implementation part is file `errors.cpp'. The maximum length of generated error message is suggested to be not greater then `MAX_ERROR_MESSAGE_LENGTH'. The default value (150) of this macro can be redefined with corresponding C++ compiler option `-DMAX_ERROR_MESSAGE_LENGTH=...' during compilation of file `errors.cpp'. The interface contains only one class `errors'. The class has the following members:

Public functions `number_of_errors', `number_of_warnings'

        `unsigned int number_of_errors (void)'
        `unsigned int number_of_warnings (void)'
return values which are number of correspondingly errors and warnings fixed after given class object creation.

Integer variable `maximum_number_of_errors'

has value which is maximum number of errors which will be fixed. If an error is fixed with number equals to `maximum_number_of_errors' then special fatal error `fatal error -- too many errors' with position of given error is fixed instead of the error. And all following errors are ignored. Zero value of the variable means that the special fatal error will never fixed.

Public virtual function `fatal_error_function'

        `void fatal_error_function (void)'
without parameters which will be called after fixing a fatal error. The fatal error function is suggested to do not return the control back. The default fatal error function only calls `exit (1)'.

Public constructor `errors'

        `errors (int immediate_output_flag)'
creates the class `errors' object working in regime depending on parameter value. If the parameter value is nonzero than all fixed messages are output immediately. Otherwise the compiler messages are stored until function `output' are called.

Public destructor ` errors'

        `~errors (void)'
frees all memory allocated during the class `errors' object work.

Public function `output'

        `void output (void)'
sorts (stable sorting) all fixed messages by their positions, outputs ones, and deletes ones. Appended messages will be output after corresponding error or warning. This function should be used only in regime of storing messages.

Public function `error'

        `void error (int fatal_error_flag, position_t
                     position, const char *format, ...)'
fixes error (fatal error if the first parameter value is nonzero) at the position given as the second parameter. If the error is fatal than functions `output' and `*fatal_error_function' are called. The diagnostic messages are formed analogous to output of function `printf'. For example,
           error (1, current_position, "fatal error - no memory");

Public function `warning'

        `void warning (position_t position, const char *format, ...)'
is analogous to the previous but is used to fix a warning.

Public function `append_message'

        `void append_message (position_t position,
                              const char *format, ...)'
When regime of immediately output of fixed message is on this function is analogous to the previous (except for incrementing `number_of_warnings'). In opposite case the appended message will be output with the most recent fixed error or warning independently from value of the first parameter. Of course the previously fixed error or warning must exist.

For example, this function may be used for generation of messages of type

              `<file>:<line>:<position-1>: repeated declaration'
and then
              `<file>:<line>:<position-1>: previous declaration'.
Description of function `output_error_function' contains explanation why decremented position is output.

Public function `system_error'

        `void system_error (int fatal_error_flag,
                            position_t position,
                            const char *format, ...)'
is analogous to function `error' but serves to fix a system error. The current system message without head blanks (given by standard function `strerror') is placed after the message formed by the function parameters. For example, the following call may be used when a file is not opened
             system_error  (1, current_position,
                            "fatal error - %s:", new_file_name);

Public virtual function `output_error_function'

        `void default_output_error_function
              (int appended_message_flag, position_t position,
               const char *message)'
is used to output error message. The function has three parameters -- flag of appended message, message position and message itself.

By default the function is oriented to output in Unix style according to GNU standard. To output a listing the function `output_error_function' should be changed. The default function output message in the following formats:

  MESSAGE                              (NULL file name)
  FILE_NAME:1: MESSAGE                 (zero line number)
  FILE_NAME:LINE_NUMBER: MESSAGE       (zero column number)
After that the function outputs newline. The function also outputs additional messages `in file processed from ...' if given message is not appended message and corresponds to file different from one of previous output error. This message reflects path of the message position (see package `position'), i.e. reflects positions of corresponding include-clauses. Decremented column number is output in order to be in concordance with editor Emacs in which positions start with 0.

Next Previous Contents