File: metrics-output.adb

package info (click to toggle)
asis 2018-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,528 kB
  • sloc: ada: 155,205; makefile: 297; sh: 67; xml: 48; csh: 10
file content (1291 lines) | stat: -rw-r--r-- 44,083 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
------------------------------------------------------------------------------
--                                                                          --
--                      GNAT METRICS TOOLS COMPONENTS                       --
--                                                                          --
--                       M E T R I C S . O U T P U T                        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                    Copyright (C) 2003-2017, AdaCore                      --
--                                                                          --
-- GNATMETRIC  is  free software; you can  redistribute it and/or modify it --
-- under terms of the  GNU  General Public License as published by the Free --
-- Software  Foundation;  either version 3, or (at your option)  any  later --
-- version. GNATMETRIC  is  distributed in the hope that it will be useful, --
-- but  WITHOUT  ANY  WARRANTY;   without  even  the  implied  warranty  of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details.  You should have received a copy of the --
-- GNU General Public License distributed with GNAT; see file  COPYING3. If --
-- not,  go  to  http://www.gnu.org/licenses  for  a  complete  copy of the --
-- license.                                                                 --
--                                                                          --
-- GNAT Metrics Toolset is maintained by AdaCore (http://www.adacore.com).  --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Characters.Handling;     use Ada.Characters.Handling;
with Ada.Strings.Fixed;           use Ada.Strings.Fixed;
with Ada.Text_IO;                 use Ada.Text_IO;

with GNAT.Directory_Operations;   use GNAT.Directory_Operations;
with GNAT.OS_Lib;                 use GNAT.OS_Lib;

with Asis.Compilation_Units;      use Asis.Compilation_Units;
with Asis.Declarations;           use Asis.Declarations;
with Asis.Elements;               use Asis.Elements;
with Asis.Extensions.Flat_Kinds;  use Asis.Extensions.Flat_Kinds;
with Asis.Text;                   use Asis.Text;

with ASIS_UL.Common;              use ASIS_UL.Common;
with ASIS_UL.Debug;               use ASIS_UL.Debug;
with ASIS_UL.Environment;
with ASIS_UL.Options;
with ASIS_UL.Output;              use ASIS_UL.Output;

with METRICS.ASIS_Utilities;      use METRICS.ASIS_Utilities;
with METRICS.Common;              use METRICS.Common;
with METRICS.Coupling;            use METRICS.Coupling;
with METRICS.Metric_Definitions;  use METRICS.Metric_Definitions;
with METRICS.Options;             use METRICS.Options;
with METRICS.Source_Table;        use METRICS.Source_Table;

package body METRICS.Output is

   -----------------------
   -- Local subprograms --
   -----------------------

   procedure Report_No_EOL (Message : String; Depth : Natural := 0);
   --  Send Message into output file. The message is not appended with
   --  platform-specific EOL. Depth is used to send the indentation if needed.
   --  This procedure should not be used for generating XML output

   procedure Set_Global_Metrics_Output;
   --  Creates or opens, if needed (depending on gnatmetric options), and sets
   --  the output file for outputting the global metrics results
   --
   --  Does nothing for XML output

   ----------------
   -- Brief_Help --
   ----------------

   procedure Brief_Help is
   begin
      pragma Style_Checks ("M200"); -- Allow long lines

      Info ("usage: gnatmetric [options] {filename} {-files filename} [-cargs gcc_switches]");
      Info (" options:");
      Info (" --version - Display version and exit");
      Info (" --help    - Display usage and exit");
      Info ("");
      Info (" -Pproject         - Use project file project. Only one such switch can be used");
      Info (" -U                - process all sources of the argument project");
      Info (" -U main           - process the closure of units rooted at unit main");
      Info (" -Xname=value      - specify an external reference for argument project file");
      Info (" --subdirs=dir     - specify subdirectory to place the result files into");
      Info (" --no_objects_dir  - place results into current dir instead of project dir");
      Info (" -eL               - follow all symbolic links when processing project files");
      Info (" --ignore=filename - do not process sources listed in filename");

      Info ("");
      Info (" -a    - process sources from RTL");
      if False then -- Disable this for now
         Info (" --incremental -- incremental processing on a per-file basis");
      end if;
      Info (" -jn   - n is the maximal number of processes");
      Info (" -v    - verbose mode");
      Info (" -q    - quiet mode");
      Info ("");

      Info (" -nolocal - do not compute detailed metrics for local program units");
      Info ("");

      Info ("Options to control metrics to compute. An option --<metric> turns computing");
      Info ("the metric ON, the corresponding --no-<metric> option turns computing the");
      Info ("metric OFF. If no metric option is specified, all the metrics are computed");
      Info ("and reported. If at least one positive option is  specified, only explicitly");
      Info ("selected metrics are computed.");
      Info ("");

      Info ("Complexity metrics:");
      Info ("  --complexity-all        - all complexity metrics");
      Info ("  --complexity-cyclomatic - McCabe Cyclomatic Complexity");
      Info ("  --complexity-essential  - Essential Complexity");
      Info ("  --complexity-average    - average McCabe Cyclomatic Complexity of a body");
      Info ("  --loop-nesting          - maximal loop nesting level");
      Info ("  --no-static-loop        - do not count static loops for cyclomatic complexity");
      Info ("  -ne                     - do not consider exit statements as gotos when");
      Info ("                            computing Essential Complexity");
      Info ("  --extra-exit-points     - extra exit points in subprograms");
      Info ("");

      Info ("Line metrics:");
      Info ("  --lines-all         - all line metrics");
      Info ("  --lines             - number of all lines");
      Info ("  --lines-code        - number of code lines");
      Info ("  --lines-comment     - number of comment lines");
      Info ("  --lines-eol-comment - number of code lines also containing comments");
      Info ("  --lines-ratio       - comment/code lines percentage");
      Info ("  --lines-blank       - number of blank lines");
      Info ("  --lines-average     - average number of code lines in a body");
      Info ("");

      Info (" Syntax element metrics:");
      Info ("  --syntax-all         - all syntax element metrics");
      Info ("  --declarations       - total number of declarations");
      Info ("  --statements         - total number of statements");
      Info ("  --public-subprograms - number of public subprograms in a compilation unit");
      Info ("  --all-subprograms    - number of subprograms in a compilation unit");
      Info ("  --public-types       - number of public types in a compilation unit");
      Info ("  --all-types          - number of types in a compilation unit");
      Info ("  --unit-nesting       - maximal unit nesting level");
      Info ("  --construct-nesting  - maximal construct nesting level");
      Info ("  --param-number       - number of subprogram parameters");
      Info ("");

      Info (" Coupling metrics. By default they are disabled, options below enable all or");
      Info (" specific coupling metrics, there is no  option to disable coupling metrics");
      Info ("  --coupling-all           - all coupling metrics");
      Info ("  --tagged-coupling-out    - tagged (class) fan-out coupling");
      Info ("  --tagged-coupling-in     - tagged (class) fan-in coupling");
      Info ("  --hierarchy-coupling-out - hierarchy (category) fan-out coupling");
      Info ("  --hierarchy-coupling-in  - hierarchy (category) fan-in coupling");
      Info ("  --unit-coupling-out      - unit fan-out coupling");
      Info ("  --unit-coupling-in       - unit fan-in coupling");
      Info ("  --control-coupling-out   - control fan-out coupling");
      Info ("  --control-coupling-in    - control fan-in coupling");
      Info ("");

      Info (" output file control:");
      Info ("  -d=dirname     - put files with detailed metrics into 'dirname'");
      Info ("  -x             - generate XML output");
      Info ("  -xs            - generate XML output and corresponding schema file");
      Info ("  -nt            - do not generate output in text form, implies '-x'");
      Info ("  -o file-suffix - suffix for the file to put detailed metrics for");
      Info ("                   a source file into (file suffix should follow OS");
      Info ("                   file name conventions and contain '.' or '$' character)");
      Info ("  -og filename   - name of the file to put global metrics info into");
      Info ("                   (if not set, this info is sent to Stdout),");
      Info ("                   ignored if -nt is used");
      Info ("  -ox filename   - name of the file to put XML output into, implies '-x'");
      Info ("  -sfn           - use short source file name in output");
      Info ("");
      Info (" filename        - name of Ada source file for which metrics");
      Info ("                   should be computed (wildcards are allowed)");
      Info (" -files filename - name of the text file containing a list of Ada");
      Info ("                   source files for which metrics should be computed");

      Info (" gcc_switches    - switches to be passed to gcc called by " &
            ASIS_UL.Common.Tool_Name.all);

      pragma Style_Checks ("M79");
   end Brief_Help;

   ---------------
   -- Close_Tag --
   ---------------

   procedure Close_Tag (Tag_Name : String; Depth : Natural := 0) is
   begin
      Report_XML ("</" & Tag_Name & '>', Depth => Depth);
   end Close_Tag;

   ---------------------
   -- Generate_Header --
   ---------------------

   procedure Generate_Header (SF : SF_Id; CU : Compilation_Unit) is
   begin

      if Generate_Text_Output then
         Report ("Metrics computed for " &  Source_Name_For_Output (SF));
         Report ("containing " & CU_Profile (CU));
      end if;

      if Generate_XML_Output then
         Report_XML
           ("<file name=" & '"' &
             Source_Name_For_Output (SF) & """>", Depth => 1);
      end if;

   end Generate_Header;

   --------------------------
   -- Generate_Line_Output --
   --------------------------

   procedure Generate_Line_Output (SF : SF_Id) is
      Tmp : Metric_Count;
   begin

      if Line_Metrics_Set then
         Report ("");
         Report ("=== Code line metrics ===");
      else
         return;
      end if;

      if Source_Status (SF) = Error_Detected then
         Report ("WARNING! errors are detected when processing the file");
         Report ("         COMPUTED LINE METRIC VALUES ARE NOT SAFE");

      else
         Global_Statistics.Computed_Line_Metrics :=
           Global_Statistics.Computed_Line_Metrics + 1;
      end if;

      if Compute_All_Lines then
         Report_No_EOL ("  all lines           :");
         Tmp := Get_All_Lines (SF);

         if Computed (Tmp) then
            Report (Tmp'Img);

            Output_XML_Metric ("all_lines", Tmp, Depth => 2);

            Global_Statistics.Line_Metrics.All_Lines :=
              Global_Statistics.Line_Metrics.All_Lines + Tmp;
         else
            Report (" unknown");
         end if;

      end if;

      if Compute_Code_Lines
        or else
         Compute_Comment_Code_Ratio
      then

         Tmp := Get_Code_Lines (SF);

         Global_Statistics.Line_Metrics.Code_Lines :=
           Global_Statistics.Line_Metrics.Code_Lines + Tmp;

         if Compute_Code_Lines then
            Report_No_EOL ("  code lines          :");
            Report (Tmp'Img);

            Output_XML_Metric ("code_lines", Tmp, Depth => 2);
         end if;

      end if;

      if Compute_Comment_Lines
        or else
         Compute_Comment_Code_Ratio
      then
         Tmp := Get_Comment_Lines (SF);

         Global_Statistics.Line_Metrics.Comment_Lines :=
           Global_Statistics.Line_Metrics.Comment_Lines + Tmp;

         if Compute_Comment_Lines then
            Report_No_EOL ("  comment lines       :");
            Report (Tmp'Img);

            Output_XML_Metric ("comment_lines", Tmp, Depth => 2);
         end if;

      end if;

      if Compute_EOL_Comments
        or else
         Compute_Comment_Code_Ratio
      then
         Tmp := Get_EOL_Comments (SF);

         Global_Statistics.Line_Metrics.EOL_Comments :=
           Global_Statistics.Line_Metrics.EOL_Comments + Tmp;

         if Compute_EOL_Comments then
            Report_No_EOL ("  end-of-line comments:");
            Report (Tmp'Img);

            Output_XML_Metric ("eol_comments", Tmp, Depth => 2);
         end if;

      end if;

      if Compute_Comment_Code_Ratio then
         Report_No_EOL ("  comment percentage  :");

         if Computed (Get_EOL_Comments (SF))
           and then
            Computed (Get_Comment_Lines (SF))
           and then
            Computed (Get_Code_Lines (SF))
         then

            Comment_Code_Ratio :=
                (Float (Get_Comment_Lines (SF)) +
                   Float (Get_EOL_Comments (SF)))
              /
                (Float (Get_Comment_Lines (SF)) +
                   Float (Get_Code_Lines (SF)))
              *
                 100.0;

            Comment_Code_Ratio_To_Print :=
              Comment_Code_Percentage (Comment_Code_Ratio);

            Report (Comment_Code_Ratio_To_Print'Img);

            Output_XML_Metric
              ("comment_percentage",
               Comment_Code_Ratio_To_Print'Img,
               Depth => 2);

         else
            Report (" unknown");
         end if;

      end if;

      if Compute_Blank_Lines then
         Report_No_EOL ("  blank lines         :");
         Tmp := Get_Blank_Lines (SF);

         if Computed (Tmp) then
            Report (Tmp'Img);

            Output_XML_Metric ("blank_lines", Tmp, Depth => 2);

            Global_Statistics.Line_Metrics.Blank_Lines :=
              Global_Statistics.Line_Metrics.Blank_Lines + Tmp;
         else
            Report (" unknown");
         end if;

      end if;

   end Generate_Line_Output;

   --------------
   -- Open_Tag --
   --------------

   procedure Open_Tag (Tag_Name : String) is
   begin
      --  ??? Indentation!!!
      Report_XML ('<' & Tag_Name & '>');
   end Open_Tag;

   -----------------------
   -- Output_XML_Metric --
   -----------------------

   procedure Output_XML_Metric
     (Metric : String;
      Value  : Metric_Count;
      Depth  : Natural := 0)
   is
      Value_Image : constant String := Value'Img;
   begin
      Report_XML
        ("<metric name=" &
         '"' & Metric & '"' &
         '>' & Value_Image (2 .. Value_Image'Last) & "</metric>",
         Depth => Depth);
   end Output_XML_Metric;

   procedure Output_XML_Metric
     (Metric : String;
      Value  : String;
      Depth  : Natural := 0)
   is
   begin
      Report_XML
        ("<metric name=" &
         '"' & Metric & '"' &
         '>' & Trim (Value, Ada.Strings.Both) & "</metric>",
         Depth => Depth);
   end Output_XML_Metric;

   ----------------------------
   -- Print_Gnatmetric_Usage --
   ----------------------------

   procedure Print_Gnatmetric_Usage is
   begin
      Set_Error (Standard_Output);
      Brief_Help;

      New_Line;
      New_Line;
      Put_Line ("Report bugs to report@adacore.com");
   end Print_Gnatmetric_Usage;

   ------------
   -- Report --
   ------------

   procedure Report
     (Message : String;
      Depth   :   Natural := 0)
   is
      Indent : constant String := Depth * Indent_String;
   begin

      if Generate_Text_Output then
         Put      (Indent);
         Put_Line (Message);
      end if;

   end Report;

   ----------------
   -- Report_XML --
   ----------------

   procedure Report_XML
     (Message : String;
      Depth   :   Natural := 0)
   is
      Indent : constant String := Depth * Indent_String;
   begin

      if Generate_XML_Output then
         Put      (XML_Out_File, Indent);
         Put_Line (XML_Out_File, Message);
      end if;

   end Report_XML;

   ------------------------------
   -- Report_Global_Statistics --
   ------------------------------

   procedure Report_Global_Statistics is
      LSLOC : Metric_Count := 0;
      --  Used to compute the logical SLOC (all declarations + all statement)

      ITD : Public_Types_Details
        renames Global_Statistics.Public_Types_Detailed;
   begin
      --  In incremental mode, we shouldn't print anything, because it's not a
      --  complete "summary".

      if ASIS_UL.Options.Incremental_Mode or ASIS_UL.Options.Mimic_gcc then
         return;
      end if;

      if Compute_Comment_Code_Ratio then

         if Global_Statistics.Computed_Line_Metrics > 0 then
            Comment_Code_Ratio :=
                (Float (Global_Statistics.Line_Metrics.Comment_Lines) +
                   Float (Global_Statistics.Line_Metrics.EOL_Comments))
              /
                (Float (Global_Statistics.Line_Metrics.Comment_Lines) +
                   Float (Global_Statistics.Line_Metrics.Code_Lines))
              *
                 100.0;
         end if;

         Comment_Code_Ratio_To_Print :=
           Comment_Code_Percentage (Comment_Code_Ratio);
      end if;

      if Compute_Average_Lines_In_Bodies then

         if Num_Of_Processes_Bodies > 0 then
            Average_Lines_In_Process_Bodies :=
              Float (Lines_In_Process_Bodies) /
                Float (Num_Of_Processes_Bodies);
         else
            Average_Lines_In_Process_Bodies := 0.0;
         end if;

         Average_Lines_In_Process_Bodies_To_Print :=
           Real_Val_To_Print (Average_Lines_In_Process_Bodies);
      end if;

      if Compute_Average_Complexity
        and then
         Units_Compute_Average_Complexity_For >  0
      then
         Average_Cyclomatic_Complexity :=
           Float (Total_Cyclomatic_Complexity) /
           Float (Units_Compute_Average_Complexity_For);

         Average_Cyclomatic_Complexity_To_Print :=
          Real_Val_To_Print (Average_Cyclomatic_Complexity);
      end if;

      if Coupling_Metrics_Set then

         if Print_Coupling_Unit_Table then
            Print_Unit_Table;
         end if;

         Compute_Coupling_Metrics;
      end if;

      if Generate_Text_Output then

         Set_Global_Metrics_Output;

         if Line_Metrics_Set then

            Report ("Line metrics summed over" &
                    Global_Statistics.Computed_Line_Metrics'Img & " units");

            if Compute_All_Lines then
               Report ("  all lines            :" &
                       Global_Statistics.Line_Metrics.All_Lines'Img);
            end if;

            if Compute_Code_Lines then
               Report ("  code lines           :" &
                       Global_Statistics.Line_Metrics.Code_Lines'Img);
            end if;

            if Compute_Comment_Lines then
               Report ("  comment lines        :" &
                       Global_Statistics.Line_Metrics.Comment_Lines'Img);
            end if;

            if Compute_EOL_Comments then
               Report ("  end-of-line comments :" &
                       Global_Statistics.Line_Metrics.EOL_Comments'Img);
            end if;

            if Compute_Comment_Code_Ratio then
               Report ("  comment percentage   :" &
                       Comment_Code_Ratio_To_Print'Img);
            end if;

            if Compute_Blank_Lines then
               Report ("  blank lines          :" &
                       Global_Statistics.Line_Metrics.Blank_Lines 'Img);
            end if;

         end if;

         if Compute_Average_Lines_In_Bodies then

            if Line_Metrics_Set then
               Report ("");
            end if;

            Report ("Average lines in body:" &
                    Average_Lines_In_Process_Bodies_To_Print'Img);
         end if;

         if Global_Element_Metrics_Set then

            if Line_Metrics_Set or else Compute_Average_Lines_In_Bodies then
               Report ("");
            end if;

            Report ("Element metrics summed over" &
                    Global_Statistics.Computed_Element_Metrics'Img & " units");

            if Compute_All_Statements then
               Report ("  all statements      :" &
                       Global_Statistics.Syntax_Metrics.All_Statements'Img);
            end if;

            if Compute_All_Declarations then
               Report ("  all declarations    :" &
                       Global_Statistics.Syntax_Metrics.All_Declarations'Img);
            end if;

            if Compute_All_Declarations and then Compute_All_Statements then

               LSLOC :=
                 Global_Statistics.Syntax_Metrics.All_Statements +
                 Global_Statistics.Syntax_Metrics.All_Declarations;

               Report ("  logical SLOC        :" & LSLOC'Img);

            end if;

            if Compute_Public_Types
              and then
               Global_Statistics.Public_Types          >  0
            then
               Report ("");
               Report (Global_Statistics.Public_Types'Img &
                       " public types in" &
                       Global_Statistics.Computed_Public_Types'Img &
                       " units");

               if Details_Present (ITD) then

                  Report (" including");

                  if ITD.Abstract_Types > 0 then
                     Report
                       (ITD.Abstract_Types'Img & " abstract types",
                        Depth => 1);
                  end if;

                  if ITD.Tagged_Types > 0 then
                     Report (ITD.Tagged_Types'Img & " tagged types",
                             Depth => 1);
                  end if;

                  if ITD.Private_Types > 0 then
                     Report
                       (ITD.Private_Types'Img & " private types", Depth => 1);
                  end if;

                  if ITD.Task_Types > 0 then
                     Report (ITD.Task_Types'Img & " task types", Depth => 1);
                  end if;

                  if ITD.Protected_Types > 0 then
                     Report
                       (ITD.Protected_Types'Img & " protected types",
                        Depth => 1);
                  end if;

               end if;

            end if;

            if Compute_All_Types
              and then
               Global_Statistics.All_Types          >  0
            then
               Report ("");
               Report (Global_Statistics.All_Types'Img &
                       " type declarations in" &
                       Global_Statistics.Computed_All_Types'Img &
                       " units");
            end if;

            if Compute_Public_Subprograms
              and then
               Global_Statistics.Public_Subprograms          >  0
            then
               Report ("");
               Report (Global_Statistics.Public_Subprograms'Img &
                       " public subprograms in" &
                       Global_Statistics.Computed_Public_Subprograms'Img &
                       " units");
            end if;

            if Compute_All_Subprograms
              and then
               Global_Statistics.All_Subprograms          >  0
            then
               Report ("");
               Report (Global_Statistics.All_Subprograms'Img &
                       " subprogram bodies in" &
                       Global_Statistics.Computed_All_Subprograms'Img &
                       " units");
            end if;

         end if;

         if Compute_Average_Complexity
           and then
            Units_Compute_Average_Complexity_For >  0
         then
            if Line_Metrics_Set
              or else
               Compute_Average_Lines_In_Bodies
              or else
               Element_Metrics_Set
            then
               Report ("");
            end if;

            Report ("Average cyclomatic complexity:" &
                    Average_Cyclomatic_Complexity_To_Print'Img);
         end if;

         if Coupling_Metrics_Set then
            Report_Coupling_Metrics
              (Text => True,
               XML  => False);
         end if;

      end if;

      if Generate_XML_Output then

         --  XML output. Here we output all the computed metric values, even
         --  if we have only one unit or if this value is zero.

         --  ??? Indentation!!!

         if Compute_All_Lines then
            Output_XML_Metric
              ("all_lines",
               Global_Statistics.Line_Metrics.All_Lines,
               Depth => 1);
         end if;

         if Compute_Code_Lines then
            Output_XML_Metric
              ("code_lines",
               Global_Statistics.Line_Metrics.Code_Lines,
               Depth => 1);
         end if;

         if Compute_Comment_Lines then
            Output_XML_Metric
              ("comment_lines",
               Global_Statistics.Line_Metrics.Comment_Lines,
               Depth => 1);
         end if;

         if Compute_EOL_Comments then
            Output_XML_Metric
              ("eol_comments",
               Global_Statistics.Line_Metrics.EOL_Comments,
               Depth => 1);
         end if;

         if Compute_Comment_Code_Ratio then
            Output_XML_Metric
              ("comment_percentage",
               Comment_Code_Ratio_To_Print'Img,
               Depth => 1);
         end if;

         if Compute_Blank_Lines then
            Output_XML_Metric
              ("blank_lines",
               Global_Statistics.Line_Metrics.Blank_Lines,
               Depth => 1);
         end if;

         if Compute_Average_Lines_In_Bodies then
            Output_XML_Metric
              ("average_lines_in_bodies",
               Average_Lines_In_Process_Bodies_To_Print'Img,
               Depth => 1);
         end if;

         --  ??? Interface type details

         if Compute_Public_Types then
            Output_XML_Metric
             ("public_types",
              Global_Statistics.Public_Types,
               Depth => 1);
         end if;

         if Compute_All_Types then
            Output_XML_Metric
              ("all_types",
               Global_Statistics.All_Types,
               Depth => 1);
         end if;

         if Compute_Public_Subprograms then
            Output_XML_Metric
              ("public_subprograms",
               Global_Statistics.Public_Subprograms,
               Depth => 1);
         end if;

         if Compute_All_Subprograms then
            Output_XML_Metric
              ("all_subprograms",
               Global_Statistics.All_Subprograms,
               Depth => 1);
         end if;

         if Compute_All_Statements then
            Output_XML_Metric
              ("all_stmts",
               Global_Statistics.Syntax_Metrics.All_Statements,
               Depth => 1);
         end if;

         if Compute_All_Declarations then
            Output_XML_Metric
              ("all_dcls",
               Global_Statistics.Syntax_Metrics.All_Declarations,
               Depth => 1);
         end if;

         if Compute_All_Declarations and then Compute_All_Statements then
            LSLOC :=
              Global_Statistics.Syntax_Metrics.All_Statements +
              Global_Statistics.Syntax_Metrics.All_Declarations;

            Output_XML_Metric ("lsloc", LSLOC, Depth => 1);
         end if;

         if Compute_Average_Complexity
           and then
            Units_Compute_Average_Complexity_For >  0
         then
            Output_XML_Metric
              ("average_complexity",
               Average_Cyclomatic_Complexity_To_Print'Img,
               Depth => 1);
         end if;

         if Coupling_Metrics_Set then
            Report_Coupling_Metrics
              (Text => False,
               XML  => True);
         end if;

         Close_Tag ("global");

      end if;

      if Generate_Text_Output then
         Set_Output (Standard_Output);
      end if;

   end Report_Global_Statistics;

   -------------------
   -- Report_No_EOL --
   -------------------

   procedure Report_No_EOL (Message : String; Depth : Natural := 0) is
      Indent : constant String := Depth * Indent_String;
   begin

      if Generate_Text_Output then
         Put (Current_Output, Indent);
         Put (Current_Output, Message);
      end if;

   end Report_No_EOL;

   -------------------------
   -- Report_Program_Unit --
   -------------------------

   procedure Report_Program_Unit
     (El           : Element;
      Depth        : Natural;
      Library_Item : Boolean := False)
   is
      Arg_Kind  : constant Flat_Element_Kinds := Flat_Element_Kind (El);
      Indent    : constant String             := Depth * Indent_String;
      Def_Name  : constant Element            := Names (El) (1);
      Name_Str  : constant String             :=
        To_String (Defining_Name_Image (Def_Name));
      Unit_Span : constant Span               := Element_Span (El);

      First_Line_Str : constant String := Unit_Span.First_Line'Img;
      First_Col_Str  : constant String := Unit_Span.First_Column'Img;

      function XML_Name_Str return String;
      --  Generates XML name for a defining operator symbol by replacing
      --  '"', '<', '>', '&' with the appropriate strings. For a defining
      --  identifier or defining A_Defining_Expanded_Name returns Name_Str

      function Program_Unit_Kind return String;
      --  Returns the string describing the kind of a program unit

      function Program_Unit_Kind return String is
      begin
         case Arg_Kind is

            --  Specs:

            when A_Task_Type_Declaration =>
               return ("task type");

            when  A_Protected_Type_Declaration =>
               return ("protected type");

            when A_Single_Task_Declaration =>
               return ("task object");

            when A_Single_Protected_Declaration =>
               return ("protected object");

            when A_Procedure_Declaration =>
               return ("procedure");

            when A_Function_Declaration =>
               return ("function");

            when A_Package_Declaration =>
               return ("package");

            when A_Package_Renaming_Declaration =>
               return ("package renaming");

            when A_Procedure_Renaming_Declaration =>
               return ("procedure renaming");

            when A_Function_Renaming_Declaration =>
               return ("function renaming");

            when A_Generic_Package_Renaming_Declaration =>
               return ("generic package renaming");

            when A_Generic_Procedure_Renaming_Declaration =>
               return ("generic procedure renaming");

            when A_Generic_Function_Renaming_Declaration =>
               return ("generic function renaming");

            when A_Generic_Procedure_Declaration =>
               return ("generic procedure");

            when A_Generic_Function_Declaration =>
               return ("generic function");

            when A_Generic_Package_Declaration =>
               return ("generic package");

            when A_Package_Instantiation =>
               return ("package instantiation");

            when A_Procedure_Instantiation =>
               return ("procedure instantiation");

            when A_Function_Instantiation =>
               return ("function instantiation");

            when An_Expression_Function_Declaration =>
               return ("expression function");
            when A_Null_Procedure_Declaration =>
               return ("null procedure");

            --  Bodies:

            when A_Task_Body_Declaration =>
               return ("task body");

            when A_Protected_Body_Declaration =>
               return ("protected body");

            when An_Entry_Body_Declaration =>
               return ("entry body");

            when A_Package_Body_Declaration =>
               return ("package body");

            when A_Procedure_Body_Declaration =>
               return ("procedure body");

            when A_Function_Body_Declaration  =>
               return ("function body");

            when A_Procedure_Body_Stub =>
               return ("procedure body stub");

            when A_Function_Body_Stub =>
               return ("function body stub");

            when A_Package_Body_Stub =>
               return ("package body stub");

            when A_Task_Body_Stub =>
               return ("task body stub");

            when A_Protected_Body_Stub =>
               return ("protected body stub");

            when others =>
               pragma Assert (False, "Program_Unit_Kind: " & Arg_Kind'Img);
               return (Arg_Kind'Img);
         end case;
      end Program_Unit_Kind;

      function XML_Name_Str return String is
      begin

         if Name_Str = """<""" then
            return "&quot;&lt;&quot;";

         elsif Name_Str = """<=""" then
            return "&quot;&lt;=&quot;";

         elsif Name_Str = """>""" then
            return "&quot;&gt;&quot;";

         elsif Name_Str = """>=""" then
            return "&quot;&gt;=&quot;";

         elsif Name_Str = """&""" then
            return "&quot;&amp;&quot;";

         elsif Defining_Name_Kind (Def_Name) = A_Defining_Operator_Symbol then
            return "&quot;" &
                   Name_Str (Name_Str'First + 1 .. Name_Str'Last - 1) &
                   "&quot;";

         else
            return Name_Str;
         end if;
      end XML_Name_Str;

   begin
      Report_No_EOL (Indent);
      Report_No_EOL (Name_Str);
      Report_No_EOL (" (");

      Report_No_EOL (Program_Unit_Kind);

      if Library_Item then

         if Unit_Kind (Enclosing_Compilation_Unit (El)) in A_Subunit then
            Report_No_EOL (" - subunit");
         else
            Report_No_EOL (" - library item");
         end if;

      end if;

      Report_No_EOL (" at lines ");
      Report_No_EOL (Unit_Span.First_Line'Img);
      Report_No_EOL (":");
      Report_No_EOL (Unit_Span.Last_Line'Img);
      Report        (")");

      Report_XML
        ("<unit name=""" & XML_Name_Str                           &
         """ kind=""" & Program_Unit_Kind                         &
         """ line=""" & First_Line_Str (2 .. First_Line_Str'Last) &
         """ col="""  & First_Col_Str  (2 .. First_Col_Str'Last)  & """>",
         Depth => Depth + 2);

   end Report_Program_Unit;

   -------------------------------
   -- Set_Global_Metrics_Output --
   -------------------------------

   procedure Set_Global_Metrics_Output is
   begin

      if Global_File_Name /= null then
         pragma Assert
           (Get_Current_Dir = ASIS_UL.Environment.Tool_Temp_Dir.all &
              Directory_Separator);
         --  If we are here, we for sure are in our temporary directory
         Change_Dir (ASIS_UL.Environment.Tool_Current_Dir.all);

         begin
            if GNAT.OS_Lib.Is_Regular_File (Global_File_Name.all) then
               Open (Global_Out_File, Out_File, Global_File_Name.all);
            else
               Create (Global_Out_File, Out_File, Global_File_Name.all);
            end if;

         exception
            when others =>
               Error ("gnatmetric: can not open " & Global_File_Name.all);
               Error ("check the file name");
         end;

         Set_Output (Global_Out_File);
      else
         Set_Output (Standard_Output);
      end if;

      Set_Error  (Standard_Error);
      Global_Output := Current_Output;
   end Set_Global_Metrics_Output;

   -------------------------
   -- Set_Source_Out_File --
   -------------------------

   procedure Set_Source_Out_File (SF : SF_Id) is
      S : constant String := Source_Out_File (SF);
   begin

      if not Generate_Text_Output then
         return;
      end if;

      if GNAT.OS_Lib.Is_Regular_File (S) then
         Open (Source_Output_File, Out_File, S);
      else
         Create (Source_Output_File, Out_File, S);
      end if;

      Set_Output (Source_Output_File);

      if ASIS_UL.Options.Mimic_gcc then
         if ASIS_UL.Options.Verbose_Mode then
            Put_Line (Standard_Output, "creating " & S);
         elsif Debug_Flag_V then
            Put_Line (Standard_Output, "creating " & File_Name (S));
         end if;
      end if;

   exception
      when others =>
         Error ("gnatmetric: can not write in " & S);
         raise Fatal_Error;
   end Set_Source_Out_File;

   ----------------------
   -- Set_XML_Out_File --
   ----------------------

   procedure Set_XML_Out_File is
   begin

      if Generate_XML_Output then

         begin

            if GNAT.OS_Lib.Is_Regular_File (XML_File_Name.all) then
               Open (XML_Out_File, Out_File, XML_File_Name.all);
            else
               Create (XML_Out_File, Out_File, XML_File_Name.all);
            end if;

         exception
            when others =>
               Generate_XML_Output := False;
               Error ("gnatmetric: can not open " & XML_File_Name.all);
               Error ("check the file name");

               if not Generate_Text_Output then
                  --  No need to do anything - we have no file to output the
                  --  result
                  raise Fatal_Error;
               end if;

         end;

         if Generate_XML_Schema then

            begin

               if GNAT.OS_Lib.Is_Regular_File (XSD_File_Name.all) then
                  Open (XSD_Out_File, Out_File, XSD_File_Name.all);
               else
                  Create (XSD_Out_File, Out_File, XSD_File_Name.all);
               end if;

            exception
               when others =>
                  Generate_XML_Output := False;
                  Error ("gnatmetric: can not open schema file for " &
                         XML_File_Name.all);
                  Error ("check the file name");

                  Generate_XML_Schema := False;

            end;

         end if;

         Report_XML ("<?xml version=""1.0""?>");

         if Generate_XML_Schema then
            Report_XML
              ("<global xmlns:xsi=" &
               """http://www.w3.org/2001/XMLSchema-instance"" " &
               "xsi:noNamespaceSchemaLocation="""               &
               XSD_File_Name.all & """>");
         else
            Open_Tag ("global");
         end if;

      end if;

   end Set_XML_Out_File;

   ----------------------------
   -- Source_Name_For_Output --
   ----------------------------

   function Source_Name_For_Output (SF : SF_Id) return String is
   begin
      if Short_SFN_In_Output then
         return Short_Source_Name (SF);
      else
         return Source_Name (SF);
      end if;
   end Source_Name_For_Output;

   ---------------------
   -- Source_Out_File --
   ---------------------

   function Source_Out_File (SF : SF_Id) return String is
      Dir_From_Prj : constant String := Get_Result_Dir (SF);
   begin
      if Output_Dir.all = "" then
         if Dir_From_Prj = "" then
            return Source_Name (SF) & Out_Suffix.all;
         else
            return Dir_From_Prj & Short_Source_Name (SF) & Out_Suffix.all;
         end if;
      else
         return Output_Dir.all & Directory_Separator &
                Short_Source_Name (SF) & Out_Suffix.all;
      end if;
   end Source_Out_File;

   ----------------------
   -- Write_XML_Schema --
   ----------------------

   procedure Write_XML_Schema is
   begin
      Set_Output (XSD_Out_File);
      pragma Style_Checks (Off);

      Put_Line ("<?xml version=""1.0"" encoding=""UTF-8""?>");
      Put_Line ("<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">");
      Put_Line ("        <xs:element name=""global"">");
      Put_Line ("                <xs:complexType>");
      Put_Line ("                        <xs:sequence>");
      Put_Line ("                                <xs:element ref=""file"" minOccurs=""0"" maxOccurs=""unbounded""/>");
      Put_Line ("                                <xs:element ref=""metric"" minOccurs=""0"" maxOccurs=""unbounded""/>");
      Put_Line ("                                <xs:element ref=""coupling"" minOccurs=""0"" maxOccurs=""1""/>");
      Put_Line ("                        </xs:sequence>");
      Put_Line ("                </xs:complexType>");
      Put_Line ("        </xs:element>");
      Put_Line ("        <xs:element name=""file"">");
      Put_Line ("                <xs:complexType>");
      Put_Line ("                        <xs:sequence>");
      Put_Line ("                                <xs:element ref=""metric"" minOccurs=""0"" maxOccurs=""unbounded""/>");
      Put_Line ("                                <xs:element ref=""unit"" minOccurs=""0"" maxOccurs=""unbounded""/>");
      Put_Line ("                        </xs:sequence>");
      Put_Line ("                        <xs:attribute name=""name"" use=""required"" type=""xs:string""/>");
      Put_Line ("                </xs:complexType>");
      Put_Line ("        </xs:element>");
      Put_Line ("        <xs:element name=""unit"">");
      Put_Line ("                <xs:complexType>");
      Put_Line ("                        <xs:sequence>");
      Put_Line ("                                <xs:element ref=""metric"" minOccurs=""0"" maxOccurs=""unbounded""/>");
      Put_Line ("                                <xs:element ref=""unit"" minOccurs=""0"" maxOccurs=""unbounded""/>");
      Put_Line ("                        </xs:sequence>");
      Put_Line ("                        <xs:attribute name=""name"" use=""required"" type=""xs:string""/>");
      Put_Line ("                        <xs:attribute name=""line"" use=""required"" type=""xs:decimal""/>");
      Put_Line ("                        <xs:attribute name=""kind"" type=""xs:string""/>");
      Put_Line ("                        <xs:attribute name=""col"" use=""required"" type=""xs:byte""/>");
      Put_Line ("                </xs:complexType>");
      Put_Line ("        </xs:element>");
      Put_Line ("        <xs:element name=""metric"">");
      Put_Line ("                <xs:complexType>");
      Put_Line ("                        <xs:simpleContent>");
      Put_Line ("                                <xs:extension base=""xs:decimal"">");
      Put_Line ("                                        <xs:attribute name=""name"" use=""required"" type=""xs:string""/>");
      Put_Line ("                                </xs:extension>");
      Put_Line ("                        </xs:simpleContent>");
      Put_Line ("                </xs:complexType>");
      Put_Line ("        </xs:element>");
      Put_Line ("        <xs:element name=""coupling"">");
      Put_Line ("                <xs:complexType>");
      Put_Line ("                        <xs:sequence>");
      Put_Line ("                                <xs:element ref=""file"" minOccurs=""0"" maxOccurs=""unbounded""/>");
      Put_Line ("                        </xs:sequence>");
      Put_Line ("                </xs:complexType>");
      Put_Line ("        </xs:element>");
      Put_Line ("</xs:schema>");

      pragma Style_Checks (On);
      Set_Output (Standard_Output);

      Close (XSD_Out_File);
   end Write_XML_Schema;

end METRICS.Output;