3.10.1.5 Preprocessing

Although it is quite possible to conditionalize code without the use of C-style preprocessing, as described in the cases above, it is nevertheless convenient in some cases to use the C approach. Moreover, older Ada compilers have often provided some preprocessing capability, so legacy code may depend on this approach, even though it is not standard.

To accommodate such use, GNAT provides a preprocessor (modeled to a large extent on the various preprocessors that have been used with legacy code on other compilers, to enable easier transition).

You can use the preprocessor used in two different modes. You can use it separately from the compiler to generate a separate output source file, which you then feed to the compiler as a separate step. This is the gnatprep utility, whose use is fully described in Preprocessing with gnatprep.

The preprocessing language allows such constructs as

#if DEBUG or else (PRIORITY > 4) then
   sequence of declarations
#else
   completely different sequence of declarations
#end if;

The values of the symbols DEBUG and PRIORITY can be defined either on the command line or in a separate file.

The other way of running the preprocessor is even closer to the C style and often more convenient. In this approach, the preprocessing is integrated into the compilation process. You pass the compiler the preprocessor input, which includes #if lines etc, and the compiler carries out the preprocessing internally and compiles the resulting output. For more details on this approach, see Integrated Preprocessing.