GNAT Programming System Tutorial:

[Top] | [Contents] | [Index] | [ ? ]

# GNAT Programming System Tutorial

GNAT Programming System Tutorial

Version 1.2.2

Date: 2003/05/27 10:13:08

Copyright (C) 2002-2003, ACT-Europe. This document may be copied, in whole or in part, in any form or by any means, as is or with alterations, provided that (1) alterations are clearly marked as alterations and (2) this copyright notice is included unmodified in any copy.

> 1. Introduction |  |
> 2. Quick overview of the GPS areas |  |
> 3. Editing sources |  |
> 4. Building applications |  |
> 5. Source Navigation |  |
> 6. Search Dialog |  |
> 7. Project Explorer (entities) |  |
> 8. Back to Source Navigation |  |
> 9. Run |  |
> 10. Debug |  |
> 11. Call Graph |  |
> 12. Locations Tree |  |
> 13. Projects |  |
> 14. Epilogue |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 1. Introduction

This document provides a guide through the major capabilities of the GNAT Programming System by working on a code example: sdc, a simple desktop calculator.

It is important to realize that the features that you are about to experiment with are available on multiple platforms, using the same user interface and capabilities, providing a user-friendly environment with a tight integration between the tools.

Start GPS in the directory containing the tutorial files, or if the directory is read-only, copy the ``tutorial'` directory and its subdirectories in a local (writable) area, and start GPS from the ``tutorial'` directory, so that GPS will load the right context.

By default, the tutorial sources can be found under `<prefix>/doc/gps/examples/tutorial`, where `<prefix>` is the prefix directory of the GPS installation.

Alternatively, if you have already started GPS in another directory, you can load the project `sdc.gpr` by using the menu `Project->Open...`

If you are reading this document from GPS itself, we recommend that you select the window displaying this document by clicking on it: the title bar should be displayed in blue; then select the menu item `Window->Floating`: this will move the Help window outside GPS itself, allowing you to easily read this document while following the tutorial steps in the main window of GPS.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 2. Quick overview of the GPS areas

Having launched GPS, you should now have access to a main window composed of several areas:

- a menu bar at the top
- a tool bar under the menu bar
- a project explorer under the tool bar, on the left side
- a working area on the right of the project explorer
- a messages window under the working area
- a status bar at the bottom left
- a progress bar at the bottom right

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 3. Editing sources

In the project explorer, open the `common` directory by clicking on the `[+]` sign on the left of `common`. This will open the directory and display a list of source files located in this directory.

Now, double click on ``sdc.adb'`: this will open a source editor on this file. The source code is syntax-highlighted: keywords, comments, strings and characters have different colors.

As with many other properties, colors are configurable in GPS:

Select the menu `Edit->Preferences`. This will open a preferences dialog window.

Select the `Editor->Fonts & Colors` page by clicking on the cross next to the item `Editor` and then selecting the `Fonts & Colors` item.

As you go over the various lines and labels, you will notice that by holding the mouse over a label, a tooltip pops up displaying on-line help about the selected item.

Change the background color of the `Keywords` by clicking on the last down arrow, at the right of the `Keywords` line.

Choose a color, e.g a light green. When you're done with the color selection, simply click again on the arrow, or anywhere outside the color selection area.

Click on the `Apply` button and look at the effects in the source editor. If you like the new display, click on `OK` to confirm the changes, otherwise clicking on `Cancel` will revert to the previous (black) color.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 4. Building applications

Select the menu `Build->Make->sdc.adb`: this will launch a complete build of the sdc application. Note also that a key binding is associated with this menu item (`F4`).

The build has generated a number of errors in a new window: the Locations tree, displayed in the bottom area. The errors are also highlighted in the corresponding source editor.

GPS has automatically jumped to the first error message (sdc.adb, 28:6 : (style) bad indentation), at the line (28) and column (06) of the error.

Fix the error by hand by inserting a space.

Now you can fix the next error by moving the cursor to the line 30 (press the `down` arrow twice), and by using `Ctrl-Tab` (press first the `Control` key, and then the `Tab` key on your keyboard): this key combination asks the source editor to automatically re-indent the current line.

Note that on some window managers or desktop environments, `Ctrl-Tab` is already defined. If this is the case, you can change this shortcut from the preferences dialog (menu `Edit->Preferences`, `Editor` tab).

You can then fix all the remaining errors by selecting the whole block (from line 28 to line 40) and pressing `Ctrl-Tab`. To select a block, you can either click on the left mouse button and select the area while holding the button, or using the keyboard by pressing the `Shift` key and moving the cursor using the `Up` or `Down` keys.

Press the `F4` key to build again. If you look at the progress bar, you will see that while the build is in progress the state of the compilation is displayed, giving you the current number of files compiled, and the number of remaining files.

This should now report a successful build.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 5. Source Navigation

Now let's try to understand a little bit about how the program is working by looking at the ``sdc.adb'` editor: there's a loop, the main processing is done by the functions Process and Next (at line 30).

Click around line 30, move the mouse over `Process` and let a tooltip appear (`global procedure tokens.process declared at tokens.ads:19`): this gives information about the kind of entity and the location (file and line) of the declaration of this procedure.

Do the same for `Next` (`global procedure tokens.next declared at tokens.ads:15`).

Keeping the mouse over `Next`, display the contextual menu by clicking on the right mouse button, then click on `Goto declaration of Next`: we're now in the package `Tokens`, in file ``tokens.ads'`; but where is this file in the project?

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 6. Search Dialog

Select the menu `Navigate->Find/Replace...`: this will open a search dialog. In the `Search for:` text entry type ``tokens.ads'`. Then select `Project explorer` in the `Look in:` area. The search area provides an easy way to search for text or regular expressions in several contexts including the current file, the project explorer, the help window, ...

Now click on `Find`. The file ``tokens.ads'`, in directory `struct` is highlighted.

Close the search dialog by clicking on the `Close` button.

Note that in this specific case, a simpler way to locate a file in the explorer is to use the contextual menu from the source editor: `Locate in explorer: tokens.ads`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 7. Project Explorer (entities)

Click on the `[+]` sign to open ``tokens.ads'` entities. When you click on a file in the explorer, you get language sensitive information about the file, such as `packages`, `subprograms`, `tasks`, ... for `Ada`.

Open the `subprogram` category, click on `Process`: this will open ``tokens.ads'` and move the cursor on the first line corresponding to the procedure `Process`.

Similarly, click on `Next`, and move your mouse on `Next` in the source editor.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 8. Back to Source Navigation

Using the contextual menu, select `Goto body of Next`; scroll through the procedure `Next`, move the mouse on `Instructions.Read` at line 46 and from the contextual menu, select `Goto body of Read`.

We've now navigated quite a bit through the application source code, which you can verify by clicking on the left arrow in the toolbar, to go back to the previous locations visited.

Repeat the operation until you're back in ``sdc.adb'`. As for the undo/redo capability in the source editor, the `goto previous/next location` is infinite.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 9. Run

It is now time to run the application: select the menu `Build->Run->sdc`, which will open a dialog window. Type `../input.txt` in the text entry: this is the name of a text file that will be passed as argument to the sdc program.

Now click on `OK`: a new window titled `Run: sdc ../input.txt` is created at the bottom of the main window where the sdc application runs and displays an unexpected internal error: this is a good opportunity to use the integrated debugger.

Close the execution window by clicking on the x icon on the top right corner of this window.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 10. Debug

Open the preferences dialog (menu `Edit->Preferences`) and click on the `Debugger` item on the left; set the button `Break on exceptions` to Enabled: this will enable by default a special breakpoint every time an exception is raised. Click on `OK` to confirm your change.

Now select the menu `Debug->Initialize->sdc`: two new windows are created, a data window and a debugger console. You can also look at the various debug menu item and toolbar buttons which are now activated.

Use the menu `Debug->Data->Call Stack`: this opens a new window on the right of the source editors. If you select the contextual menu in the call stack, various pieces of information can be displayed or removed in the call stack. From this contextual menu, add the `Frame Number` info by clicking on it.

Now select the menu `Debug->Run...`. Notice that `../input.txt` has been filled automatically for you since the two menus `Build->Run...` and `Debug->Run...` are synchronized. Click on `OK`: the debugger should stop on an exception (`Constraint_Error` in the file ``stack.adb'`, at line 49).

Go up in the call stack by clicking on the `tokens.process` line (frame number 6).

If you move the mouse over the parameter `T` at line 64, a tooltip is displayed showing the value of `T`. You have probably noticed that tooltips, like menus, are contextual: depending on the current session and on the entity selected, different information is displayed.

Select the contextual menu `Debug->Display T`: this displays in the data window a box containing the different fields of `T`, each clearly separated.

On `T` data display, select the contextual menu `Display->Show Value + Type`: this displays for all fields both their type and value.

Special colors are used in the data display: blue for pointers that can be dereferenced by a double-click (double click on `T.val`); red for fields that have been modified since last step.

In the contextual menu that pops up when you hold the mouse over `T`. select `Debug->View memory at address of T`: a memory view is opened. Use the `up` and `down` arrows on the right to visit memory.

Click in the memory dump, and modify it by typing numbers. Notice the red color for modified values; click on `Undo Changes` to cancel the modifications; click on `Close` to close the memory window.

In the call stack, go back to `stack.push` frame (num 4). Move the mouse over `Last` and let the debugger display its value: 0. From the contextual menu, select `Goto declaration of Last`: this will jump to the line 16 of ``stack.adb'`, where you can see that `Last` is a `Natural`. Now click on the `Goto Previous Location` button in the toolbar: we're now back at line 49 where we can see that for a `Push` procedure, `Last` should be incremented, and not decremented.

Fix the line to `Last := Last + 1;`

Save the file (`Ctrl-S`); End the debug session: menu `Debug->Terminate`; Rebuild (press `F4` key); Rerun (menu `Build->Run->sdc`): the program now completes as expected. Close the execution window.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 11. Call Graph

Now go back to the file ``sdc.adb'`, move the mouse over the procedure sdc at line 8, select the contextual menu `References->Sdc calls`: this will open a new window titled Call graph browser.

Click on the right arrow of `Process` (one of the first items on the right). Also click on the right arrow of `error_msg`.

Select `Orthogonal links` in the contextual menu of the graph to change the way links are displayed in the graph. Click on the canvas, then play with the zoom (`=` and `-` keys).

If you select `Hide links` from `error_msg` contextual menu, this will hide all the links that are related to this item: the link between the callers and callees of `error_msg` are no longer displayed. This can be useful when the graph becomes complex, to hide some parts. If you go back to the contextual menu, you can now select `Show links` to show the links again.

Click on right arrow of `process` (``instructions.ads:12'`, the item at the top right of the graph).

The items can also be moved: move e.g `msg` item around.

Click on left arrow of `msg` to display who is calling `msg`. Notice that `view` (on the left of the graph) calls `msg`.

Click on left arrow of `view`: the arrow disappears, and no new items are created, which means that `view` isn't called by anyone, so we're now going to remove this procedure.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 12. Locations Tree

From view, select the contextual menu `Go to spec`; then from the source editor (file ``stack.ads'`), select the contextual menu `References->Find all references to View`: this highlights the `Locations` tree which now contains all the references for `view`, grouped by files (``stack.ads'` and ``stack.adb'`).

The first location is highlighted automatically: this is the spec of the procedure `View`. Now click in the tree on the `+` sign at the left of ``stack.adb'`: two locations are listed, at line 90 and 97. Click on each of these locations: they correspond to the procedure body.

The `Find all references` capability is another way to list all the uses of an entity, and it confirms that `View` isn't called in our project.

Remove View body by e.g selecting it, and pressing the `Delete` key, then save the file (`Ctrl-S`).

Do the same for the spec, save the file.

Close the ``stack.ads'` and ``stack.adb'` files (menu File->Close, or using the shortcut `Ctrl-W`), as well as the call graph window. Rebuild by pressing the `F4` key.

Let's now see how to create a project corresponding to the sdc project we've used in this tutorial.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 13. Projects

> 13.1 Project Wizard |  |
> 13.2 Project properties |  |
> 13.3 Variable editor |  |
> 13.4 Switch editor |  |
> 13.5 Source dependencies |  |
> 13.6 Project dependencies |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 13.1 Project Wizard

WARNING: while in the project wizard, you will not be able to scroll through the tutorial if you are reading it within GPS. You may want to resize the window now so that the entire section on the wizard is visible, or reopen the tutorial in another browser.

Go to the menu `Project->New...`: this is a standard wizard, with various steps listed on the left area of the window.

Type sdc2 in the project name field. It is possible to create a multi-language project by selecting the C or C++ check box.

Click on `Forward`: we are now on the `VCS page`. VCS stands for Version Control System. GPS has a generic support for VCS which allows it to easily support new systems. Currently GPS supports CVS and ClearCase. Select `CVS`.

Click on `Forward`: this is the source directories selection, used to specify the project's sources. Open ``tutorial'`, click on `struct`, and add it by clicking on the down arrow.

Click on `Forward`: this is the `Build` and `Exec` directory selection, used to store object, ali files, ...

Click on `Browse`, open ``tutorial'`, click on `obj`, click on `OK`.

Click on `Forward`: this is the naming scheme editor. GNAT is very flexible and can use any kind of naming scheme for Ada files. In particular, you can easily set the default file extensions (e.g by using one of the predefined schemes) and you can also specify exceptions that use non standard file names.

Click on `Forward`: this is the main units selection, used mainly for building executables and debugging.

Click on `Add`, open the `common` directory and select `sdc.adb`.

Click on `Forward`: we're now in the switch selector. Select `Recompile if switches changed`.

Click on `Ada` page.

Select `Full errors` and `Overflow checking`. The boxes and the command line (the text entry at the bottom of the page) are fully synchronized, e.g if you click on the command line, and change `-gnatf` to `-gnat`, the `Full errors` check box is unselected; now type `a` to get `-gnata`, and notice that `Enable assertions` is now selected.

Click on `Forward`: voila, we've created a project similar to the one used in this tutorial.

Click on `Cancel` to close the wizard.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 13.2 Project properties

In the project explorer, on the project sdc, use the contextual menu `Edit project properties`. All the properties set in the project wizard can be found here as well. You can also switch between pages by clicking on the tabs.

Once you're done exploring the property pages, click on the `Cancel` button to close the properties window.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 13.3 Variable editor

You have probably noticed the `Build` label located on top of the project explorer. This is a configuration variable. With GPS and the GNAT project facility, you can define as many configuration variables as you want, and modify any project settings (e.g switches, sources, ...) based on the values of configuration variables. These variables can also take any number of different values.

The `Build` variable shows a typical `Debug/Production` configuration where we've set different switches for the two modes.

Click on the button at the left (`Edit variable properties`): this is the variable editor, where values can be added or renamed. Close the variable editor by clicking on the `Cancel` button.

Now, let's take a look at the switches set in the project.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 13.4 Switch editor

Select the menu item `Project->Edit File Switches`: a global switch editor is displayed in the working area.

The editor lists the switches associated with each file in the project and the gray area shows the default (global) switches; you will also notice that ``screen_output.adb'` has specific switches, which are highlighted using a different font.

Switch between `Debug` and `Production` mode in the `Build` combo box: the switches are updated at the same time.

Back to our project, let's now examine the dependencies between sources.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 13.5 Source dependencies

Select ``sdc.adb'` in the explorer and then the contextual menu item `Show dependencies for sdc.adb`: this will open a new graph showing the dependencies between sources of the project.

Click on the right arrow of ``tokens.ads'` to display the files that ``tokens.ads'` depends on. Similarly, click on the right arrow of ``stack.ads'`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 13.6 Project dependencies

Back in the project explorer on the sdc project, select the contextual menu `Add dependency->From project file...`, then open the tutorial directory and click on the `projects` subdirectory. Select the file `prj1.gpr`.

You can see the new dependencies added in the project explorer, as a tree of projects. In particular, project dependencies are duplicated: if you open the `prj1` icon by clicking on the `[+]` sign, and then similarly open the `prj2` icon, you will notice that the project `prj4` is displayed twice: once as a dependency of `prj2`, and once as a dependency of `prj1`.

GPS can also display the graph of dependencies between projects: on sdc project, use the contextual menu `Show projects imported by sdc`: this will open a project hierarchy browser.

On the `sdc.gpr` project, select the contextual menu `Show recursive dependencies for sdc`.

In the browser, you can move the project items, and select them to highlight the dependencies.

Close the project browser by clicking on the `[x]` sign at the top right area of the window, or by typing `Ctrl-W`, or by using the menu `File->Close`.

On prj1 project, select the contextual menu `Remove dependency sdc -> prj1` to remove the dependency added between sdc and prj1.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 14. Epilogue

This terminates our tour of GPS, the GNAT Programming System. We hope this tutorial gave you a good overview of the general capabilities available with GPS. A non exhaustive list of the features not mentioned in this document includes:

- Automatic generation of body files
- Pretty printing
- Visual comparison of files
- Version control
- Flexible multiple document interface

For more information, please look at the User's Guide (gps.html).

[Top] | [Contents] | [Index] | [ ? ]

# Table of Contents

1. Introduction
2. Quick overview of the GPS areas
3. Editing sources
4. Building applications
5. Source Navigation
6. Search Dialog
7. Project Explorer (entities)
8. Back to Source Navigation
9. Run
10. Debug
11. Call Graph
12. Locations Tree
13. Projects

13.1 Project Wizard
13.2 Project properties
13.3 Variable editor
13.4 Switch editor
13.5 Source dependencies
13.6 Project dependencies

14. Epilogue

[Top] | [Contents] | [Index] | [ ? ]

# Short Table of Contents

> 1. Introduction
> 2. Quick overview of the GPS areas
> 3. Editing sources
> 4. Building applications
> 5. Source Navigation
> 6. Search Dialog
> 7. Project Explorer (entities)
> 8. Back to Source Navigation
> 9. Run
> 10. Debug
> 11. Call Graph
> 12. Locations Tree
> 13. Projects
> 14. Epilogue

[Top] | [Contents] | [Index] | [ ? ]

# About this document

This document was generated by Mail Server on June, 15 2003 using texi2html

The buttons in the navigation panels have the following meaning:

Button | Name | Go to | From 1.2.3 go to
--- | --- | --- | ---
[ < ] | Back | previous section in reading order | 1.2.2
[ > ] | Forward | next section in reading order | 1.2.4
[ << ] | FastBack | previous or up-and-previous section | 1.1
[ Up ] | Up | up section | 1.2
[ >> ] | FastForward | next or up-and-next section | 1.3
[Top] | Top | cover (top) of document |
[Contents] | Contents | table of contents |
[Index] | Index | concept index |
[ ? ] | About | this page |

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:

- 1. Section One

- 1.1 Subsection One-One

- ...

- 1.2 Subsection One-Two

- 1.2.1 Subsubsection One-Two-One
- 1.2.2 Subsubsection One-Two-Two
- 1.2.3 Subsubsection One-Two-Three <== Current Position
- 1.2.4 Subsubsection One-Two-Four

- 1.3 Subsection One-Three

- ...

- 1.4 Subsection One-Four

This document was generated by Mail Server on June, 15 2003 using texi2html