Error reporting
SPL code generation APIs provide four different families
of functions that are related to error reporting. These functions
are the print, warn, error,
and exit family of functions that are supplied by
the SPL::CodeGen module.
The print function takes a format string and zero
or more arguments that are used to customize the format string. It
prints to the standard output. For details of the format string and
the rules that govern substitutions within the format string, refer
to the documentation of the built-in Perl function printf.
The following is a sample use:
SPL::CodeGen::print("The cost is %6.2f", 499);
There are three other variants of print.println is
similar to print, but adds a line at the end. printVerbose is
similar to print but only prints if the compiler
is launched with the verbose option. printlnVerbose is
the verbose variation that appends a new line at the end.
The warn function is used to print warnings and
similar to print it has the ln and Verbose variants.
However, it prints to the standard error rather than the standard
output. It also uses the standard SPL compiler output formatting for
warnings and errors. The error function is similar
to warn, but is used for errors. It has the ln variant,
but not the Verbose one, since errors are not maskable
based on the verbosity setting. If the error function
is called at least once during the execution of a code generator,
then the exit code for that execution indicates that there were failures. The exit function
is similar to error but it exits the code generator
right away after it prints an error message, and the exit code will
indicate that there were failures. It also has an ln variant.
The built-in Perl function exit must not be used
for exiting a code generator, but instead SPL::CodeGen::exit is
used.
The warn, error, and exit functions
take similar arguments as the print function, but
they also support an optional last argument of type SourceLocation.
This argument represents the SPL source location of the error or warning. SourceLocation objects
are available through the getSourceLocation methods
that are provided by various objects accessible from the $model variable
that represents the operator instance model. The following is an example
use:
<%
my $inputPort = $model->getInputPortAt(0);
my $outputPort = $model->getOutputPortAt(0);
my $inTupleType = $inputPort->getCppTupleType();
my $outTupleType = $outputPort->getCppTupleType();
if($inTupleType ne $outTupleType) {
SPL::CodeGen::exitln("Schema mismatch between input and output ports. " .
"The types are %s and %s, respectively.", $inputPort->getSPLTupleType(),
$outputPort->getSPLTupleType(),
$outputPort->getSourceLocation());
}
%>
And the resulting output:
a.spl:9:5: ERROR: Schema mismatch between input and output ports. The types are
tuple<int a> and tuple<int b>.