OSDN Git Service

2011-08-29 Geert Bosch <bosch@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / scos.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                 S C O S                                  --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2009-2011, Free Software Foundation, Inc.         --
10 --                                                                          --
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.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
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.
30
31 with Types; use Types;
32
33 with GNAT.Table;
34
35 package SCOs is
36
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.
41
42    --  Par_SCO is part of the compiler. It scans the parsed source tree and
43    --  populates the internal tables.
44
45    --  Get_SCO reads the text lines in ALI format and populates the internal
46    --  tables with corresponding information.
47
48    --  Put_SCO reads the internal tables and generates text lines in the ALI
49    --  format.
50
51    --------------------
52    -- SCO ALI Format --
53    --------------------
54
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 -gnateS switch is set.
58
59    --  Sloc Ranges
60
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:
64
65    --      line:col-line:col
66
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.
70
71    --  SCO File headers
72
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:
77
78    --      C dependency-number filename
79
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).
84
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).
88
89    --        Note: the filename is redundant in that it could be deduced from
90    --        the corresponding D line, but it is convenient at least for human
91    --        reading of the SCO information, and means that the SCO information
92    --        can stand on its own without needing other parts of the ALI file.
93
94    --  Statements
95
96    --    For the purpose of SCO generation, the notion of statement includes
97    --    simple statements and also the following declaration types:
98
99    --      type_declaration
100    --      subtype_declaration
101    --      object_declaration
102    --      renaming_declaration
103    --      generic_instantiation
104
105    --    and the following regions of the syntax tree:
106
107    --      the part of a case_statement from CASE up to the expression
108    --      the part of a FOR loop iteration scheme from FOR up to the
109    --        loop_parameter_specification
110    --      the part of a WHILE loop up to the condition
111    --      the part of an extended_return_statement from RETURN up to the
112    --        expression (if present) or to the return_subtype_indication (if
113    --        no expression)
114
115    --    and any pragma that occurs at a place where a statement or declaration
116    --    is allowed.
117
118    --  Statement lines
119
120    --    These lines correspond to one or more successive statements (in the
121    --    sense of the above list) which are always executed in sequence (in the
122    --    absence of exceptions or other external interruptions).
123
124    --    Entry points to such sequences are:
125
126    --      the first declaration of any declarative_part
127    --      the first statement of any sequence_of_statements that is not in a
128    --        body or block statement that has a non-empty declarative part
129    --      the first statement after a compound statement
130    --      the first statement after an EXIT, RAISE or GOTO statement
131    --      any statement with a label (the label itself is not part of the
132    --       entry point that is recorded).
133
134    --    Each entry point must appear as the first entry on a CS line.
135    --    The idea is that if any simple statement on a CS line is known to have
136    --    been executed, then all statements that appear before it on the same
137    --    CS line are certain to also have been executed.
138
139    --    The form of a statement line in the ALI file is:
140
141    --      CS *sloc-range [*sloc-range...]
142
143    --    where each sloc-range corresponds to a single statement, and * is
144    --    one of:
145
146    --      t  type declaration
147    --      s  subtype declaration
148    --      o  object declaration
149    --      r  renaming declaration
150    --      i  generic instantiation
151    --      C  CASE statement (from CASE through end of expression)
152    --      E  EXIT statement
153    --      F  FOR loop statement (from FOR through end of iteration scheme)
154    --      I  IF statement (from IF through end of condition)
155    --      p  disabled PRAGMA
156    --      P  PRAGMA
157    --      R  extended RETURN statement
158    --      W  WHILE loop statement (from WHILE through end of condition)
159
160    --      Note: for I and W, condition above is in the RM syntax sense (this
161    --      condition is a decision in SCO terminology).
162
163    --    and is omitted for all other cases
164
165    --    Note: up to 6 entries can appear on a single CS line. If more than 6
166    --    entries appear in one logical statement sequence, continuation lines
167    --    are marked by Cs and appear immediately after the CS line.
168
169    --    Implementation permission: a SCO generator is permitted to emit a
170    --    narrower SLOC range for a statement if the corresponding code
171    --    generation circuitry ensures that all debug information for the code
172    --    implementing the statement will be labeled with SLOCs that fall within
173    --    that narrower range.
174
175    --  Decisions
176
177    --    Note: in the following description, logical operator includes only the
178    --    short-circuited forms and NOT (so can be only NOT, AND THEN, OR ELSE).
179    --    The reason that we can exclude AND/OR/XOR is that we expect SCO's to
180    --    be generated using the restriction No_Direct_Boolean_Operators if we
181    --    are interested in decision coverage, which does not permit the use of
182    --    AND/OR/XOR on boolean operands. These are permitted on modular integer
183    --    types, but such operations do not count as decisions in any case. If
184    --    we are generating SCO's only for simple coverage, then we are not
185    --    interested in decisions in any case.
186
187    --    Note: the reason we include NOT is for informational purposes. The
188    --    presence of NOT does not generate additional coverage obligations,
189    --    but if we know where the NOT's are, the coverage tool can generate
190    --    more accurate diagnostics on uncovered tests.
191
192    --    A top level boolean expression is a boolean expression that is not an
193    --    operand of a logical operator.
194
195    --    Decisions are either simple or complex. A simple decision is a top
196    --    level boolean expression that has only one condition and that occurs
197    --    in the context of a control structure in the source program, including
198    --    WHILE, IF, EXIT WHEN, or immediately within an Assert, Check,
199    --    Pre_Condition or Post_Condition pragma, or as the first argument of a
200    --    dyadic pragma Debug. Note that a top level boolean expression with
201    --    only one condition that occurs in any other context, for example as
202    --    right hand side of an assignment, is not considered to be a (simple)
203    --    decision.
204
205    --    A complex decision is a top level boolean expression that has more
206    --    than one condition. A complex decision may occur in any boolean
207    --    expression context.
208
209    --    So for example, if we have
210
211    --        A, B, C, D : Boolean;
212    --        function F (Arg : Boolean) return Boolean);
213    --        ...
214    --        A and then (B or else F (C and then D))
215
216    --    There are two (complex) decisions here:
217
218    --        1. X and then (Y or else Z)
219
220    --           where X = A, Y = B, and Z = F (C and then D)
221
222    --        2. C and then D
223
224    --    For each decision, a decision line is generated with the form:
225
226    --      C* sloc expression [chaining]
227
228    --    Here * is one of the following characters:
229
230    --      I  decision in IF statement or conditional expression
231    --      E  decision in EXIT WHEN statement
232    --      G  decision in entry guard
233    --      P  decision in pragma Assert/Check/Pre_Condition/Post_Condition
234    --      W  decision in WHILE iteration scheme
235    --      X  decision appearing in some other expression context
236
237    --    For I, E, G, P, W, sloc is the source location of the IF, EXIT,
238    --    ENTRY, PRAGMA or WHILE token, respectively
239
240    --    For X, sloc is omitted
241
242    --    The expression is a prefix polish form indicating the structure of
243    --    the decision, including logical operators and short-circuit forms.
244    --    The following is a grammar showing the structure of expression:
245
246    --      expression ::= term             (if expr is not logical operator)
247    --      expression ::= &sloc term term  (if expr is AND or AND THEN)
248    --      expression ::= |sloc term term  (if expr is OR or OR ELSE)
249    --      expression ::= !sloc term       (if expr is NOT)
250
251    --      In the last three cases, sloc is the source location of the AND, OR,
252    --      or NOT token, respectively.
253
254    --      term ::= element
255    --      term ::= expression
256
257    --      element ::= *sloc-range
258
259    --    where * is one of the following letters:
260
261    --      c  condition
262    --      t  true condition
263    --      f  false condition
264
265    --      t/f are used to mark a condition that has been recognized by the
266    --      compiler as always being true or false. c is the normal case of
267    --      conditions whose value is not known at compile time.
268
269    --    & indicates AND THEN connecting two conditions
270
271    --    | indicates OR ELSE connecting two conditions
272
273    --    ! indicates NOT applied to the expression
274
275    --    Note that complex decisions do NOT include non-short-circuited logical
276    --    operators (AND/XOR/OR). In the context of existing coverage tools the
277    --    No_Direct_Boolean_Operators restriction is assumed, so these operators
278    --    cannot appear in the source in any case.
279
280    --    The SCO line for a decision always occurs after the CS line for the
281    --    enclosing statement. The SCO line for a nested decision always occurs
282    --    after the line for the enclosing decision.
283
284    --    Note that membership tests are considered to be a single simple
285    --    condition, and that is true even if the Ada 2005 set membership
286    --    form is used, e.g. A in (2,7,11.15).
287
288    --    The expression can be followed by chaining indicators of the form
289    --    Tsloc-range or Fsloc-range, where the sloc-range is that of some
290    --    entry on a CS line.
291
292    --    T* is present when the statement with the given sloc range is executed
293    --    if, and only if, the decision evaluates to TRUE.
294
295    --    F* is present when the statement with the given sloc range is executed
296    --    if, and only if, the decision evaluates to FALSE.
297
298    --    For an IF statement or ELSIF part, a T chaining indicator is always
299    --    present, with the sloc range of the first statement in the
300    --    corresponding sequence.
301
302    --    For an ELSE part, the last decision in the IF statement (that of the
303    --    last ELSIF part, if any, or that of the IF statement if there is no
304    --    ELSIF part) has an F chaining indicator with the sloc range of the
305    --    first statement in the sequence of the ELSE part.
306
307    --    For a WHILE loop, a T chaining indicator is always present, with the
308    --    sloc range of the first statement in the loop, but no F chaining
309    --    indicator is ever present.
310
311    --    For an EXIT WHEN statement, an F chaining indicator is present if
312    --    there is an immediately following sequence in the same sequence of
313    --    statements.
314
315    --    In all other cases, chaining indicators are omitted
316
317    --    Implementation permission: a SCO generator is permitted to emit a
318    --    narrower SLOC range for a condition if the corresponding code
319    --    generation circuitry ensures that all debug information for the code
320    --    evaluating the condition will be labeled with SLOCs that fall within
321    --    that narrower range.
322
323    --  Case Expressions
324
325    --    For case statements, we rely on statement coverage to make sure that
326    --    all branches of a case statement are covered, but that does not work
327    --    for case expressions, since the entire expression is contained in a
328    --    single statement. However, for complete coverage we really should be
329    --    able to check that every branch of the case statement is covered, so
330    --    we generate a SCO of the form:
331
332    --      CC sloc-range sloc-range ...
333
334    --    where sloc-range covers the range of the case expression
335
336    --    Note: up to 6 entries can appear on a single CC line. If more than 6
337    --    entries appear in one logical statement sequence, continuation lines
338    --    are marked by Cc and appear immediately after the CC line.
339
340    --  Disabled pragmas
341
342    --    No SCO is generated for disabled pragmas
343
344    ---------------------------------------------------------------------
345    -- Internal table used to store Source Coverage Obligations (SCOs) --
346    ---------------------------------------------------------------------
347
348    type Source_Location is record
349       Line : Logical_Line_Number;
350       Col  : Column_Number;
351    end record;
352
353    No_Source_Location : Source_Location := (No_Line_Number, No_Column_Number);
354
355    type SCO_Table_Entry is record
356       From : Source_Location;
357       To   : Source_Location;
358       C1   : Character;
359       C2   : Character;
360       Last : Boolean;
361
362       Pragma_Sloc : Source_Ptr := No_Location;
363       --  For the statement SCO for a pragma, or for any expression SCO nested
364       --  in a pragma Debug/Assert/PPC, location of PRAGMA token (used for
365       --  control of SCO output, value not recorded in ALI file).
366    end record;
367
368    package SCO_Table is new GNAT.Table (
369      Table_Component_Type => SCO_Table_Entry,
370      Table_Index_Type     => Nat,
371      Table_Low_Bound      => 1,
372      Table_Initial        => 500,
373      Table_Increment      => 300);
374
375    --  The SCO_Table_Entry values appear as follows:
376
377    --    Statements
378    --      C1   = 'S' for entry point, 's' otherwise
379    --      C2   = statement type code to appear on CS line (or ' ' if none)
380    --      From = starting source location
381    --      To   = ending source location
382    --      Last = False for all but the last entry, True for last entry
383
384    --    Note: successive statements (possibly interspersed with entries of
385    --    other kinds, that are ignored for this purpose), starting with one
386    --    labeled with C1 = 'S', up to and including the first one labeled with
387    --    Last = True, indicate the sequence to be output for a sequence of
388    --    statements on a single CS line (possibly followed by Cs continuation
389    --    lines).
390
391    --    Decision (IF/EXIT/WHILE)
392    --      C1   = 'I'/'E'/'W' (for IF/EXIT/WHILE)
393    --      C2   = ' '
394    --      From = IF/EXIT/WHILE token
395    --      To   = No_Source_Location
396    --      Last = unused
397
398    --    Decision (PRAGMA)
399    --      C1   = 'P'
400    --      C2   = ' '
401    --      From = PRAGMA token
402    --      To   = No_Source_Location
403    --      Last = unused
404
405    --      Note: when the parse tree is first scanned, we unconditionally build
406    --      a pragma decision entry for any decision in a pragma (here as always
407    --      in SCO contexts, the only pragmas with decisions are Assert, Check,
408    --      dyadic Debug, Precondition and Postcondition).
409    --
410    --      During analysis, if the pragma is enabled, Set_SCO_Pragma_Enabled
411    --      marks the statement SCO table entry as enaabled (C1 changed from 'p'
412    --      to 'P') to cause the entry to be emitted in Put_SCOs.
413
414    --    Decision (Expression)
415    --      C1   = 'X'
416    --      C2   = ' '
417    --      From = No_Source_Location
418    --      To   = No_Source_Location
419    --      Last = unused
420
421    --    Operator
422    --      C1   = '!', '&', '|'
423    --      C2   = ' '
424    --      From = location of NOT/AND/OR token
425    --      To   = No_Source_Location
426    --      Last = False
427
428    --    Element (condition)
429    --      C1   = ' '
430    --      C2   = 'c', 't', or 'f' (condition/true/false)
431    --      From = starting source location
432    --      To   = ending source location
433    --      Last = False for all but the last entry, True for last entry
434
435    --    Element (chaining indicator)
436    --      C1   = 'H' (cHain)
437    --      C2   = 'T' or 'F' (chaining on decision true/false)
438    --      From = starting source location of chained statement
439    --      To   = ending source location of chained statement
440
441    --    Note: the sequence starting with a decision, and continuing with
442    --    operators and elements up to and including the first one labeled with
443    --    Last = True, indicate the sequence to be output on one decision line.
444
445    ----------------
446    -- Unit Table --
447    ----------------
448
449    --  This table keeps track of the units and the corresponding starting and
450    --  ending indexes (From, To) in the SCO table. Note that entry zero is
451    --  unused, it is for convenience in calling the sort routine. Thus the
452    --  real lower bound for active entries is 1.
453
454    type SCO_Unit_Index is new Int;
455    --  Used to index values in this table. Values start at 1 and are assigned
456    --  sequentially as entries are constructed.
457
458    type SCO_Unit_Table_Entry is record
459       File_Name : String_Ptr;
460       --  Pointer to file name in ALI file
461
462       Dep_Num : Nat;
463       --  Dependency number in ALI file
464
465       From : Nat;
466       --  Starting index in SCO_Table of SCO information for this unit
467
468       To : Nat;
469       --  Ending index in SCO_Table of SCO information for this unit
470    end record;
471
472    package SCO_Unit_Table is new GNAT.Table (
473      Table_Component_Type => SCO_Unit_Table_Entry,
474      Table_Index_Type     => SCO_Unit_Index,
475      Table_Low_Bound      => 0, -- see note above on sorting
476      Table_Initial        => 20,
477      Table_Increment      => 200);
478
479    -----------------
480    -- Subprograms --
481    -----------------
482
483    procedure Initialize;
484    --  Reset tables for a new compilation
485
486    procedure Add_SCO
487      (From        : Source_Location := No_Source_Location;
488       To          : Source_Location := No_Source_Location;
489       C1          : Character       := ' ';
490       C2          : Character       := ' ';
491       Last        : Boolean         := False;
492       Pragma_Sloc : Source_Ptr      := No_Location);
493    --  Adds one entry to SCO table with given field values
494
495 end SCOs;