9.3. Style Checking

The command “Check Style” allows you to analyze Ada source files using several different coding style rules. The command is provided in the Ada & Analyze menus on the menubar and in the contextual menus of the editor and the GNAT Project Explorer.

Note that in addition to analyzing individual files, you can analyze all the files in an entire project by choosing the command from the contextual menu when the project node in the GNAT Project Explorer is selected. This approach is illustrated below:

invoking style checks on project

9.3.1. Controls

The actual analysis is provided by the external tool “gnatcheck”. See the GNAT User’s Guide for the details of using the tool.

The specific switches applied are controlled by means of a “rules” file. The name and location of this file can be specified in the package Check in the GNAT project file. The various switches may be specified there as well.

To specify the rules files, within the package Check specify the Default_Switches attribute for language “Ada”. In that attribute, you must specify both the “-rules” switch as well as the name of the rules file itself using the “-from=” switch. For example, in the following GNAT project file fragment we specify the switch file name “ms1553.rules”:

specifying package check switches

If you don’t specify a rules file in the project file, the command will open a dialog box to prompt you for the file name and location:

no rules file dialog

In addition to setting the individual rules switches manually in the project file, you can also set the rules switches via a multipage graphical editor. The editor will be opened whenever you double-click on a file with the extension “rules”, such as “ada.rules” or any similarly named file. To create a new rules file, just use the new-file wizard to create a file with an extension of ”.rules” and the editor will open for it automatically.

In the following, the file “ms1553.rules” has been opened:

rules file editor 1

Note the tabs at the bottom. The rules are separated into logical sections, based in part on their point of origin (e.g., the Ada 95 Quality and Style Guide, or the GNAT style warnings provided by the compiler). Each tabbed page contains options to be enabled or disabled by check-boxes, except for the last page on the far right. That page, labeled with the file name itself, contains the file in textual form.

9.3.2. Results

The results of the analysis appear in a text file. By default the file is named “gnatcheck.out”, but you can specify the name in the GNAT project file.

sample check result file