How-To#
Debugging Methods#
Using pragmas
function Create(info: Object_Info) return Object is
part1: --...
pragma Assert(Is_Valid(part1), "Pragmas can be placed in declarative contexts");
pragma Debug(Put_Line(info'Image)); -- The `Debug` pragma is a GNAT extension.
part2: --...
begin
if Config.Debug then -- Also see the "Conditional Compilation" section
-- Debugging stuff goes here
end if;
end Create;
Conditional Compilation#
Boolean Constants#
if Config.Debug then
Put_Line("Extra debugging information/checks");
end if;
package Config
Debug: constant Boolean := True;
end Config;
Separate Subprograms#
The Do_Something
may then be defined in, for example, my_package-do_something-2022.adb
and
my_package-do_something-2005.adb
to allow for both Ada2022 and Ada2005 implementations of the same subprogram.
procedure Do_Something is separate;
for Body("My_Package.Do_Something") use "my_package-do_something-2005.ada";
This method works in every situation, such as having separate Windows/Linux/MacOS/etc implementations, but it will be
very verbose and may duplicate a lot of code if there are only a few subprograms that need to be swapped out.
Preprocessing#
GNAT provides a C-style preprocessor to allow for conditional compilation. For small sections of code it will more easily allow for code reuse without copying and removes the need for separate files. See the GNATPrep page for usage.