Previous: No dead predicate warnings, Up: Pragmas   [Contents]


19.6 Source file name

The ‘source_file’ pragma and ‘#line’ directives provide support for preprocessors and other tools that generate Mercury code. The tool can insert these directives into the generated Mercury code to allow the Mercury compiler to report diagnostics (error and warning messages) at the original source code location, rather than at the location in the automatically generated Mercury code.

A ‘source_file’ pragma is a declaration of the form

:- pragma source_file(Name).

where Name is a string that specifies the name of the source file.

For example, if a preprocessor generated a file foo.m based on an input file foo.m.in, and it copied lines 20, 30, and 31 from foo.m.in, the following directives would ensure that any error or warnings for those lines copied from foo.m were reported at their original source locations in foo.m.in.

:- module foo.
:- pragma source_file("foo.m.in").
#20
% this line comes from line 20 of foo.m
#30
% this line comes from line 30 of foo.m
% this line comes from line 31 of foo.m
:- pragma source_file("foo.m").
#10
% this automatically generated line is line 10 of foo.m

Note that if a generated file contains some text which is copied from a source file, and some which is automatically generated, it is a good idea to use ‘pragma source_file’ and ‘#line’ directives to reset the source file name and line number to point back to the generated file for the automatically generated text, as in the above example.