Go to the previous, next section.

Error Handling

The SUIF library includes a number of functions for error handling. These functions handle three kinds of problems: errors, warnings, and assertions. Assertions are tests for things that should never occur in a correct program. Any problem that could potentially occur in a correct program due to bad input or other external conditions should be treated as an error rather than an assertion failure. Warnings are used to report potentially troublesome conditions that are not serious enough to cause a fatal error.

Two macros are available to implement assertions. The assert macro takes a single expression as an argument. If the expression evaluates to zero, a message is printed showing the current line number, file name, and the expression that failed, and then the abort function is called to terminate the program. The assert_msg macro is similar except that it also prints a user-defined message if the assertion fails. Besides the expression to be tested, assert_msg takes an argument that contains the message to be printed. This second argument must hold the printf format string and the arguments for the call to printf, separated by commas. To keep the commas from splitting up the second argument, it must be surrounded by parentheses. For example:

assert_msg(sym->parent(), ("no parent for %s", sym->name()));

Errors may be reported using the error_line function. The return_code parameter specifies the method of terminating the program. If the return code is negative, the abort function is called. If it is greater than zero, the exit function is called with the specified return code. If the return code is zero, the error message will be printed but the program will not terminate. The the_node parameter is optional and may be set to NULL. If used, it points to the AST node closest to the point where the error was detected, and the library tries to use it to find and print the corresponding source line number. Finally, the fmt parameter specifies the printf format string used to print the error message and any additional parameters are passed on to printf. The verror_line function is identical to error_line except that it passes the variable arguments for printf using a varargs-style list. This is just like the standard vprintf function.

The warning_line and vwarning_line functions are just like the corresponding error functions, except that they print warning messages and do not terminate the program. Consequently, they do not have return_code parameters.

Go to the previous, next section.