1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This package defines tables used to store Source Coverage Obligations. It
27 -- is used by Par_SCO to build the SCO information before writing it out to
28 -- the ALI file, and by Get_SCO/Put_SCO to read and write the text form that
29 -- is used in the ALI file.
31 with Types; use Types;
37 -- SCO information can exist in one of two forms. In the ALI file, it is
38 -- represented using a text format that is described in this specification.
39 -- Internally it is stored using two tables SCO_Table and SCO_Unit_Table,
40 -- which are also defined in this unit.
42 -- Par_SCO is part of the compiler. It scans the parsed source tree and
43 -- populates the internal tables.
45 -- Get_SCO reads the text lines in ALI format and populates the internal
46 -- tables with corresponding information.
48 -- Put_SCO reads the internal tables and generates text lines in the ALI
55 -- Source coverage obligations are generated on a unit-by-unit basis in the
56 -- ALI file, using lines that start with the identifying character C. These
57 -- lines are generated if the -gnatC switch is set.
61 -- In several places in the SCO lines, Sloc ranges appear. These are used
62 -- to indicate the first and last Sloc of some construct in the tree and
63 -- they have the form:
67 -- Note that SCO's are generated only for generic templates, not for
68 -- generic instances (since only the first are part of the source). So
69 -- we don't need generic instantiation stuff in these line:col items.
73 -- The SCO information follows the cross-reference information, so it
74 -- need not be read by tools like gnatbind, gnatmake etc. The SCO output
75 -- is divided into sections, one section for each unit for which SCO's
76 -- are generated. A SCO section has a header of the form:
78 -- C dependency-number filename
80 -- This header precedes SCO information for the unit identified by
81 -- dependency number and file name. The dependency number is the
82 -- index into the generated D lines and is ones origin (i.e. 2 =
83 -- reference to second generated D line).
85 -- Note that the filename here will reflect the original name if
86 -- a Source_Reference pragma was encountered (since all line number
87 -- references will be with respect to the original file).
91 -- For the purpose of SCO generation, the notion of statement includes
92 -- simple statements and also the following declaration types:
95 -- subtype_declaration
97 -- renaming_declaration
98 -- generic_instantiation
102 -- These lines correspond to a sequence of one or more statements which
103 -- are always exeecuted in sequence, The first statement may be an entry
104 -- point (e.g. statement after a label), and the last statement may be
105 -- an exit point (e.g. an exit statement), but no other entry or exit
106 -- points may occur within the sequence of statements. The idea is that
107 -- the sequence can be treated as a single unit from a coverage point of
108 -- view, if any of the code for the statement sequence is executed, this
109 -- corresponds to coverage of the entire statement sequence. The form of
110 -- a statement line in the ALI file is:
116 -- An exit point is a statement that causes transfer of control. Examples
117 -- are exit statements, raise statements and return statements. The form
118 -- of an exit point in the ALI file is:
124 -- Decisions represent the most significant section of the SCO lines
126 -- Note: in the following description, logical operator includes the
127 -- short circuited forms (so can be any of AND, OR, XOR, NOT, AND THEN,
130 -- Decisions are either simple or complex. A simple decision is a boolean
131 -- expresssion that occurs in the context of a control structure in the
132 -- source program, including WHILE, IF, EXIT WHEN. Note that a boolean
133 -- expression in any other context, for example, on the right side of an
134 -- assignment, is not considered to be a decision.
136 -- A complex decision is an occurrence of a logical operator which is not
137 -- itself an operand of some other logical operator. If any operand of
138 -- the logical operator is itself a logical operator, this is not a
139 -- separate decision, it is part of the same decision.
141 -- So for example, if we have
143 -- A, B, C, D : Boolean;
144 -- function F (Arg : Boolean) return Boolean);
146 -- A and then (B or else F (C and then D))
148 -- There are two (complex) decisions here:
150 -- 1. X and then (Y or else Z)
152 -- where X = A, Y = B, and Z = F (C and then D)
156 -- For each decision, a decision line is generated with the form:
160 -- Here * is one of the following characters:
162 -- I decision in IF statement or conditional expression
163 -- E decision in EXIT WHEN statement
164 -- W decision in WHILE iteration scheme
165 -- X decision appearing in some other expression context
167 -- The expression is a prefix polish form indicating the structure of
168 -- the decision, including logical operators and short circuit forms.
169 -- The following is a grammar showing the structure of expression:
171 -- expression ::= term (if expr is not logical operator)
172 -- expression ::= & term term (if expr is AND or AND THEN)
173 -- expression ::= | term term (if expr is OR or OR ELSE)
174 -- expression ::= ^ term term (if expr is XOR)
175 -- expression ::= !term (if expr is NOT)
178 -- term ::= expression
180 -- element ::= outcome sloc-range
182 -- outcome is one of the following letters:
188 -- where t/f are used to mark a condition that has been recognized by
189 -- the compiler as always being true or false.
191 -- & indicates either AND or AND THEN connecting two conditions. In the
192 -- context of couverture we only permit AND THEN in the source in any
193 -- case, so & can always be understood to be AND THEN.
195 -- | indicates either OR or OR ELSE connection two conditions. In the
196 -- context of couverture we only permit OR ELSE in the source in any
197 -- case, so | can always be understood to be OR ELSE.
199 -- ^ indicates XOR connecting two conditions. In the context of
200 -- couverture, we do not permit XOR, so this will never appear.
202 -- ! indicates NOT applied to the expression.
204 ---------------------------------------------------------------------
205 -- Internal table used to store Source Coverage Obligations (SCOs) --
206 ---------------------------------------------------------------------
208 type Source_Location is record
209 Line : Logical_Line_Number;
213 No_Source_Location : Source_Location := (No_Line_Number, No_Column_Number);
215 type SCO_Table_Entry is record
216 From : Source_Location;
217 To : Source_Location;
223 package SCO_Table is new GNAT.Table (
224 Table_Component_Type => SCO_Table_Entry,
225 Table_Index_Type => Nat,
226 Table_Low_Bound => 1,
227 Table_Initial => 500,
228 Table_Increment => 300);
230 -- The SCO_Table_Entry values appear as follows:
235 -- From = starting source location
236 -- To = ending source location
242 -- From = starting source location
243 -- To = ending source location
247 -- C1 = 'I', 'E', 'W', 'X' (if/exit/while/expression)
248 -- C2 = 'c', 't', or 'f'
249 -- From = starting source location
250 -- To = ending source location
254 -- C1 = 'I', 'E', 'W', 'X' (if/exit/while/expression)
256 -- From = No_Location
261 -- C1 = '!', '^', '&', '|'
263 -- From = No_Location
269 -- C2 = 'c', 't', or 'f' (condition/true/false)
270 -- From = starting source location
271 -- To = ending source location
272 -- Last = False for all but the last entry, True for last entry
274 -- Note: the sequence starting with a decision, and continuing with
275 -- operators and elements up to and including the first one labeled with
276 -- Last=True, indicate the sequence to be output for a complex decision
277 -- on a single CD decision line.
283 -- This table keeps track of the units and the corresponding starting and
284 -- ending indexes (From, To) in the SCO table. Note that entry zero is
285 -- unused, it is for convenience in calling the sort routine. Thus the
286 -- real lower bound for active entries is 1.
288 type SCO_Unit_Index is new Int;
289 -- Used to index values in this table. Values start at 1 and are assigned
290 -- sequentially as entries are constructed.
292 type SCO_Unit_Table_Entry is record
293 File_Name : String_Ptr;
294 -- Pointer to file name in ALI file
297 -- Dependency number in ALI file
300 -- Starting index in SCO_Table of SCO information for this unit
303 -- Ending index in SCO_Table of SCO information for this unit
306 package SCO_Unit_Table is new GNAT.Table (
307 Table_Component_Type => SCO_Unit_Table_Entry,
308 Table_Index_Type => SCO_Unit_Index,
309 Table_Low_Bound => 0, -- see note above on sorting
311 Table_Increment => 200);
317 procedure Initialize;
318 -- Reset tables for a new compilation
321 (From : Source_Location := No_Source_Location;
322 To : Source_Location := No_Source_Location;
323 C1 : Character := ' ';
324 C2 : Character := ' ';
325 Last : Boolean := False);
326 -- Adds one entry to SCO table with given field values