asis_2019.0.0_3ca32fa2/tools/gnatcheck/gnatcheck-diagnoses.adb

   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
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
------------------------------------------------------------------------------
--                                                                          --
--                          GNATCHECK COMPONENTS                            --
--                                                                          --
--                  G N A T C H E C K . D I A G N O S E S                   --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2005-2018, AdaCore                     --
--                                                                          --
-- GNATCHECK  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.  GNATCHECK  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.                                                                 --
--                                                                          --
-- GNATCHECK is maintained by AdaCore (http://www.adacore.com).             --
--                                                                          --
------------------------------------------------------------------------------

pragma Ada_2012;

with Ada.Calendar;               use Ada.Calendar;
with Ada.Characters.Handling;    use Ada.Characters.Handling;
with Ada.Command_Line;
with Ada.Containers.Ordered_Sets;
with Ada.Exceptions;
with Ada.Strings;                use Ada.Strings;
with Ada.Strings.Fixed;          use Ada.Strings.Fixed;
with Ada.Text_IO;                use Ada.Text_IO;

with GNAT.Directory_Operations;

with Gnatvsn;                    use Gnatvsn;

with Asis.Elements;              use Asis.Elements;
with Asis.Expressions;           use Asis.Expressions;
with Asis.Extensions.Strings;    use Asis.Extensions.Strings;
with Asis.Text;                  use Asis.Text;

with ASIS_UL.Common;             use ASIS_UL.Common;
with ASIS_UL.Compiler_Options;
with ASIS_UL.Debug;
with ASIS_UL.Misc;               use ASIS_UL.Misc;
with ASIS_UL.Options;            use ASIS_UL.Options;
with ASIS_UL.Output;             use ASIS_UL.Output;
with ASIS_UL.Projects.Aggregate; use ASIS_UL.Projects.Aggregate;

with Gnatcheck.Compiler;         use Gnatcheck.Compiler;
with Gnatcheck.Options;          use Gnatcheck.Options;
with Gnatcheck.Rules;            use Gnatcheck.Rules;
with Gnatcheck.Rules.Rule_Table; use Gnatcheck.Rules.Rule_Table;

package body Gnatcheck.Diagnoses is

   -----------------------
   -- Diagnoses storage --
   -----------------------

   type Diag_Message is record
      Text               : String_Access;
      Justification      : String_Access;
      Diagnosis_Kind     : Diagnosis_Kinds;
      Rule               : Rule_Id;
      SF                 : SF_Id;
      Num                : Positive;
      --  Is needed to order properly messages corresponding to the same SLOC.
   end record;

   function Diag_Is_Less_Than (L, R : Diag_Message) return Boolean;
   function Diag_Is_Less_Than_Old (L, R : Diag_Message) return Boolean;
   pragma Unreferenced (Diag_Is_Less_Than_Old);
   --  If L or R is null, raises Constraint_Error. Otherwise compares SLOC
   --  parts of L and R (file names are converted to lower case (is it correct?
   --  what about case sensitive file systems???). If SLOC parts do not contain
   --  instantiation chains, then they are compared. If both SLOCs contain
   --  instantiation chains, first the SLOCs of (the last) instantiations (that
   --  is, the rightmost SLOCs in the chains) are compared, and if they are
   --  equal, the SLOCs in the templates (the leftmost SLOCs in the chains) are
   --  compared. If one SLOC contains an instantiation chain, but another one
   --  does not, then at the first step the SLOC of the last instantiation is
   --  compared with another SLOC, and in case if they are equal, the SLOC
   --  without instantiation chain is considered as being less than the SLOC
   --  with the chain. All SLOCs are compared lexicographically.
   --
   --  If comparing SLOCs based on the rules given above results in the fact
   --  that these SLOCs are equal, Num parts are compared.
   --
   --  !!! Note, that this function is not a correct "<" relation in respect to
   --  what is needed by Ordered Sets container! Suppose we have two diagnostic
   --  messages generated for a source:
   --
   --    foo.ads:100:10: something is bad    - D1
   --    foo.ads:100:10: because of this     - D2
   --
   --  Suppose that they are stored in the diagnoses container. For these
   --  messages we have Diag_Is_Less_Than (D1, D2) is TRUE because of Num parts
   --  comparison. Suppose we have got them again (when compiling another
   --  source that depends on foo.ads). We will get another Diag_Message object
   --  for the first message (let's call it D3), and it will have Num part that
   --  will be greater than D2.Num. So if we
   --  compare Diag_Is_Less_Than (D3, D2), the result will be FALSE, but D1 and
   --  D3 will be representing the same diagnosis. The bad consequence of this
   --  is that we may get duplications of a diagnosis when using
   --  Error_Messages_Storage.Insert. See the body of Store_Diagnosis for the
   --  workaround we are using to avoid this.
   --
   --  Diag_Is_Less_Than_Old assumes the old format of SLOC for expanded
   --  generic (with [])
   --
   --  ??? Some better approach needed!!!

   function Diag_Is_Equal (L, R : Diag_Message) return Boolean;
   --  If L or R is null, raises Constraint_Error. Otherwise returns
   --  "="(L.Text.all, R.Text.all), where "=" is predefined equality for
   --  String.

   package Error_Messages_Storage is new Ada.Containers.Ordered_Sets
     (Element_Type => Diag_Message,
      "<"          => Diag_Is_Less_Than,
      "="          => Diag_Is_Equal);

   All_Error_Messages : Error_Messages_Storage.Set;

   Unused_Position : Error_Messages_Storage.Cursor;
   Unused_Inserted : Boolean;

   -------------------------------------------
   --  Local routines for diagnoses storage --
   -------------------------------------------

   procedure Compute_Statisctics;
   --  Computes the number of violations and diagnoses of different kinds.
   --  Results are stored in the corresponding counters in the package spec.
   --  Also computes file statistics and stores it in the following counters.
   --  (Should we move all the counters in the body?)
   Checked_Sources                  : Natural := 0;
   Unverified_Sources               : Natural := 0;
   Fully_Compliant_Sources          : Natural := 0;
   Sources_With_Violations          : Natural := 0;
   Sources_With_Exempted_Violations : Natural := 0;
   Ignored_Sources                  : Natural := 0;

   procedure Diag_Srorage_Debug_Image;
   --  Prints out the debug image of the debug storage.

   function Next_Message_Num return Positive;
   --  Returns next value for the Num field of diagnostic message

   -------------------------------------------
   --  Local routines for report generation --
   -------------------------------------------

   Source_List_File_Name_Str : constant String := "gnatcheck-source-list.out";
   function Source_List_File_Name return String;
   --  Name of the file with all the argument file names created by
   --  gnatcheck if a used-provided file cannot be used for this.

   Ignored_Source_List_File_Name_Str : constant String :=
     "gnatcheck-ignored-source-list.out";
   function Ignored_Source_List_File_Name return String;
   --  Name of the file with the argument file names that has been ignored by
   --  the gnatcheck run.

   Rule_List_File_Name_Str : constant String := "gnatcheck-rule-list.out";
   function Rule_List_File_Name return String;
   --  Name of the file with all the active rules with their actual parameters
   --  created by gnatcheck if a used-provided coding standard file cannot be
   --  used for this.

   Number : String_Access;
   --  Used when processing individual projects as a part of aggregate project
   --  processing. Represents a numeric index (prepended by "_") that is
   --  taken from the report file name.

   function All_Sources_In_One_File return Boolean;
   --  Checks if all the argument sources are listed in a single user-provided
   --  file. In case if gnatcheck is called from the GNAT driver analyses
   --  the original call sequence for the GNAT driver.

   procedure Copy_User_Info;
   --  Copies into the report file the text from user-provided file.

   procedure Print_Active_Rules_File;
   --  Prints the reference to the (actual argument or artificially created)
   --  file that contains the list of all the rules that are active for the
   --  given gnatcheck run.

   procedure Print_Argument_Files_Summary;
   --  Prints the total numbers of: all the argument files, non-compilable
   --  files, files with no violations, files with violations, files with
   --  exempted violations only.

   Diagnoses_To_Print : array (Rule_Violation .. Compiler_Error) of Boolean :=
     (others => False);
   --  Specifies which diagnoses should be printed out by the following
   --  procedure

   Print_Exempted_Violations : Boolean;
   --  Flag specifying if exempted or non-exempted violations should be
   --  printed. Has its effect only if Diagnoses_To_Print (Rule_Violation) is
   --  True.

   procedure Print_Diagnoses;
   --  Iterates through all the diagnoses and prints into the report file those
   --  of them, for which Diagnoses_To_Print is True (and the value of
   --  Print_Exempted_Violations either corresponds to the diagnosis or is not
   --  applicable for the diagnosis kind).

   procedure Print_Failure_Info;
   pragma Unreferenced (Print_Failure_Info);
   --  Prints info about non-fatal failures detected during gnatcheck run.

   procedure Print_File_List_File;
   --  Prints the reference to the (actual argument or artificially created)
   --  file that contains the list of all the files passed to gnatcheck

   procedure Print_Ignored_File_List_File;
   --  Prints the reference to the artificially created file that contains the
   --  list of files passed to gnatcheck that have not been processed because
   --  '--ignore=...' option. Note that it can be different from the list
   --  provided by '--ignore=...' option - this list contains only the existing
   --  files that have been passed as tool argument sources.

   procedure Print_Gnatcheck_Command_Line (XML : Boolean := False);
   --  Prints the gnatcheck command line. In case if gnatcheck has been
   --  called from the GNAT driver, prints the call to the GNAT driver, but not
   --  the gnatcheck call generated by the GNAT driver. If XML is ON, prints
   --  the output into XML output file, otherwise - in the text output file.

   procedure Print_Out_Diagnoses;
   --  Duplicates diagnoses about non-exempted rule violations, exemption
   --  warnings and compiler error messages into Stderr. Up to Max_Diagnoses
   --  diagnoses are reported. If Max_Diagnoses equal to 0, all the diagnoses
   --  of these kinds are reported.

   procedure Print_Runtime (XML : Boolean := False);
   --  Prints the runtime version used for gnatcheck call. It is either the
   --  parameter of --RTS option used for (actual) gnatcheck call or the
   --  "<default>" string if --RTS parameter is not specified. If XML is ON,
   --  prints the output into XML output file, otherwise - in the text output
   --  file.

   procedure Print_Violation_Summary;
   --  Prints the total numbers of: non-exempted)violations, exempted
   --  violations, exemption warnings and compiler errors.

   procedure XML_Report_Diagnosis
     (Diag         : Diag_Message;
      Short_Report : Boolean);
   --  Prints into XML report file the information from the diagnosis. The
   --  boolean parameter is used to define the needed indentation level

   function Select_Line   (Diag : String_Access) return Positive;
   function Select_Column (Diag : String_Access) return Positive;
   --  Assuming that Diag is a diagnosis, gets the line/column number from it.
   --  The result is undefined if Diag does not have a syntax of the diagnosis.
   --  At the moment this function does not work properly if Diag contains SLOC
   --  pointing into expanded instantiation.

   ----------------------------------------------------------------------
   --  Data structures and local routines for rule exemption mechanism --
   ----------------------------------------------------------------------

   type Exemption_Kinds is
     (Not_An_Exemption,
      Exempt_On,
      Exempt_Off);

   function Get_Exemption_Kind (Image : Wide_String) return Exemption_Kinds;
   --  Returns Exemption_Kinds value represented by Image. Returns
   --  Not_An_Exemption if Image does not represent a valid exemption kind.

   type Exemption_Info is record
      Line_Start : Natural;
      Col_Start  : Natural;
      --  Location of exemption pragma that turns exemption ON

      Line_End : Natural;
      Col_End  : Natural;
      --  End of the exemption section

      Justification : String_Access;

      Detected : Natural;
      --  Number of the diagnoses generated for exempted rule
   end record;

   type Exemption_Sections_Array is array (Rule_Id range <>) of Exemption_Info;
   type Exemption_Sections_Array_Access is access Exemption_Sections_Array;
   Exemption_Sections : Exemption_Sections_Array_Access;
   --  Storage for currently processed exemption sections. Should have a
   --  separate entry for each rule. (We cannot allocate it statically because
   --  of elaboration problems - we do not know how many rules we have unlit
   --  all of them are registered).

   function Is_Exempted (Rule : Rule_Id) return Boolean;
   --  Checks if Rule is in exempted state. Assumes Present (Rule).

   procedure Process_Postponed_Exemptions;
   --  ??? To be fully specified and implemented.
   --  The idea is to iterate through the stored diagnoses and to try to apply
   --  postponed exemption to global rules and diagnoses generated for expanded
   --  generics, and for compiler checks

   procedure Turn_Off_Exemption
     (Rule         : Rule_Id;
      Closing_Span : Asis.Text.Span;
      SF           : SF_Id);
   --  Cleans up the stored exemption section for the argument Rule, for some
   --  rules (global rules, rules checked of expanded instantiations, compiler
   --  checks) the exemption section is stored in some rule-specific data
   --  structure before being cleaning up.

   -----------------------------
   -- Parametric exemptions --
   -----------------------------

   Rule_Exemption_Parameters : Exemption_Parameters.Set;
   --  Temporary place for collecting rule parameters in case if exemption
   --  specifies parameters for a rule.

   type Parametrized_Exemption_Info is record
      Exempt_Info : Exemption_Info;
      Rule        : Rule_Id;
      SF          : SF_Id;
      Params      : String_List_Access;
   end record;

   type Param_Ex_Info_Key is record
      SF         : SF_Id;
      Line_Start : Natural;
      Col_Start  : Natural;
   end record;

   function Key
     (Element : Parametrized_Exemption_Info)
      return    Param_Ex_Info_Key is
        (SF         => Element.SF,
         Line_Start => Element.Exempt_Info.Line_Start,
         Col_Start  => Element.Exempt_Info.Col_Start);

   function "<" (L, R : Param_Ex_Info_Key) return Boolean is
     (L.SF < R.SF
     or else
      (L.SF = R.SF
       and then
       ((L.Line_Start < R.Line_Start)
        or else
        (L.Line_Start = R.Line_Start and then
         L.Col_Start < R.Col_Start))));

   function Is_Equal (L, R : String_List_Access) return Boolean;
   --  Checks if L and R have the same content.

   function Is_Equal
     (L    : Exemption_Parameters.Set;
      R    : String_List_Access)
      return Boolean;
   --  Checks that L and R are both non-empty/non-null and have the same
   --  content.

   function Belongs
      (L    : String;
       R    : String_List_Access)
       return Boolean;
   --  Checks if R is non-null and L belongs to R

   function To_Pars_List (Pars : Exemption_Parameters.Set) return
      String_List_Access;
   --  Converts (non-empty! the caller is responsible for that!) set of rule
   --  exemption parameters into string list.

   function Params_Img
     (Params : String_List_Access;
      Rule   : Rule_Id)
      return   String;
   --  Returns the string image of rule exemption parameters that are supposed
   --  to be stored in Params list (and Params is supposed to be not null) in
   --  the format suitable for including in a diagnosis. Rule parameter is
   --  needed to decide if we should fold the parameter in proper case (for
   --  sure we should not do this for Warnings rule)

   function "=" (L, R : Parametrized_Exemption_Info) return Boolean is
     (L.SF = R.SF                                         and then
      Is_Equal (L.Params, R.Params)                       and then
      L.Exempt_Info.Line_Start = R.Exempt_Info.Line_Start and then
      L.Exempt_Info.Col_Start  = R.Exempt_Info.Col_Start);

   function "<" (L, R : Parametrized_Exemption_Info) return Boolean is
     (L.SF < R.SF
     or else
      (L.SF = R.SF
       and then
       ((L.Exempt_Info.Line_Start < R.Exempt_Info.Line_Start)
        or else
        (L.Exempt_Info.Line_Start = R.Exempt_Info.Line_Start and then
         L.Exempt_Info.Col_Start < R.Exempt_Info.Col_Start))));

   package Parametrized_Exemption_Sections is new
     Ada.Containers.Ordered_Sets (Parametrized_Exemption_Info);

   package Exem_Section_Keys is new
     Parametrized_Exemption_Sections.Generic_Keys
       (Key_Type => Param_Ex_Info_Key,
        Key      => Key);
   --  ??? Should be documented!!!

   type Rule_Param_Exempt_Sections_Type is array (Rule_Id range <>) of
     Parametrized_Exemption_Sections.Set;

   type Rule_Param_Exempt_Sections_Access is access
     Rule_Param_Exempt_Sections_Type;

   Rule_Param_Exempt_Sections : Rule_Param_Exempt_Sections_Access;
   --  Stores parametric exemption sections for a source being processed

   procedure Set_Rule_Exempt_Pars
     (Rule : Rule_Id;
      Pars : String;
      SF   : SF_Id;
      SLOC : String);
   --  Assuming that Rule is a rule that allows parametric expressions,
   --  and Pars is a part of a third argument of a rule exemption pragma that
   --  contains rule parameters, parses Pars string and checks if each of the
   --  specified parameters indeed can be used as rule exemption parameter.
   --  If all the parameters can be used in rule exemption, all of them are
   --  placed into the value of Rule_Exemption_Parameters set variable. If
   --  some parameter is wrong, and error message is generated and
   --  Rule_Exemption_Parameters sets to empty set. Note that if immediately
   --  after a call to this subprogram Rule_Exemption_Parameters is empty, then
   --  the corresponding exemption pragma is incorrect.
   --  Parameters SF and SLOC are needed to form an error message - this
   --  procedure does not have any ASIS Element to compute it from. They should
   --  correspond to a pragma parameter denoting a rule (and parameters)

   function Allows_Parametrized_Exemption (Rule : Rule_Id) return Boolean;
   --  Checks if Rule allows fine-tuned exemption (with specifying parameters
   --  for that it can be exempted). Assumes Present (Rule).

   function Allowed_As_Exemption_Parameter
     (Rule  : Rule_Id;
      Param : String)
      return  Boolean;
   --  Checks if Param is allowed as a rule parameter in rule exemption pragma.
   --  Assumes that Param has already folded to lower case. Always returns
   --  False if Allows_Prametrized_Exemption (Rule) is False.

   procedure Is_Exempted_With_Pars
     (Rule        :     Rule_Id;
      Exempted_At : out Parametrized_Exemption_Sections.Cursor);
   --  Checks if Rule is already exempted with the set of parameters that are
   --  stored in the global Rule_Exemption_Parameters variable. If it does then
   --  Exempted_At is set to the set cursor pointing to the corresponding
   --  exemption section in Rule_Param_Exempt_Sections, otherwise Exempted_At
   --  is set to No_Element. The check is made for the parametric exemption
   --  sections that are currently opened in the source being traversed and
   --  stored in Rule_Param_Exempt_Sections (see below). This procedure is
   --  supposed to use only in Process_Exemption_Pragma, it depends on the
   --  specific calling context created when analyzing a rule exemption pragma.

   procedure Same_Parameter_Exempted
     (Rule        :     Rule_Id;
      Exempted_At : out Parametrized_Exemption_Sections.Cursor;
      Par         : out String_Access);
   --  Is similar to the previous procedure, but it checks that there is at
   --  least one parameter in Rule_Exemption_Parameters that has already been
   --  used in definition of some opened parametric exception section for
   --  this rule:
   --
   --     Exempt_On, Rule:Par1, Par3
   --     ...
   --     Exempt_On, Rule:Par1, Par2
   --
   --  If the checks succeeds, Par is set to point to this parameter. The
   --  caller is responsible for freeing the memory occupied by Par.

   function Get_Param_Justification
     (Rule                       : Rule_Id;
      Diag                       : String;
      SF                         : SF_Id;
      Line                       : Natural := 0;
      Col                        : Natural := 0;
      Check_Postponed_Exemptions : Boolean := False)
      return                       String_Access;
   --  Diag is supposed to be a diagnostic message (with all the parameters
   --  and variants resolved) generated for Rule (and Rule should allow
   --  parametric exemptions). It checks if Text corresponds to some
   --  parametric section for this Rule. If Check_Postponed_Exemptions is ON,
   --  the check is made for stored for postponed analysis parametric
   --  exemption sections, otherwise the check is considered as performed for
   --  the currently analyzed source and information about parametric exemption
   --  sections from Rule_Param_Exempt_Sections is used.
   --  If Diag fits into an exemption section, the result is the pointer to the
   --  corresponding justification and the diagnostic counter for this section
   --  is increased as a side effect of the function call. Otherwise the result
   --  is null.
   --  Parameters Line and Col are used only if Check_Postponed_Exemptions,
   --  they should be set to the line and column numbers from the diagnosis.
   --  If we analyze the diagnosis against postponed exemptions, we have the
   --  full set of exemption sections for SF, so Line and Col are used to
   --  filter out sections the diagnosis does not get into, and they
   --  allow to break iterating through sections when we have visited all that
   --  start before the diagnosis.
   --
   --  If Check_Postponed_Exemptions is OFF, the set of exemption sections
   --  stored in Rule_Param_Exempt_Sections for Rule contains all those and
   --  only those sections that covers the SLOC of diagnosis, so we do not need
   --  Line and Col from the text of the diagnosis.

   function Rule_Parameter (Diag : String; Rule : Rule_Id) return String;
   --  Provided that Rule allows parametric exemptions, and  Diag is a
   --  diagnostic message corresponding to this rule, defines the rule
   --  exemption parameter Diag corresponds to.

   function Get_Exem_Section
     (Exem_Sections              : Parametrized_Exemption_Sections.Set;
      Param                      : String;
      Line                       : Natural := 0;
      Col                        : Natural := 0;
      Check_Postponed_Exemptions : Boolean := False)
      return          Parametrized_Exemption_Sections.Cursor;
   --  Tries to locate in Exem_Sections the section that exempts the rule this
   --  Param is supposed to corresponds to (choosing the right set of exemption
   --  sections should be done outside the call) with Param. Returns
   --  No_Element if the attempt fails.
   --  If Check_Postponed_Exemptions is ON, the function uses Line and Col to
   --  optimize iteration through Exem_Sections (see comment to
   --  Get_Param_Justification

   procedure Icrease_Diag_Counter
     (Exem_Sections : in out Parametrized_Exemption_Sections.Set;
      Section       :        Parametrized_Exemption_Sections.Cursor);
   --  Adds 1 to the counter of detected violations for the exemption sections
   --  pointed by Section in Exem_Sections.

   procedure Turn_Off_Parametrized_Exemption
     (Rule         :        Rule_Id;
      Exempted_At  : in out Parametrized_Exemption_Sections.Cursor;
      Closing_Span :        Asis.Text.Span;
      SF           :        SF_Id);
   --  Cleans up the stored exemption section for the argument Rule, for some
   --  rules (global rules, rules checked of expanded instantiations, compiler
   --  checks) the exemption section is stored in some rule-specific data
   --  structure before being cleaning up.

   --------------------------------------
   --  Exemptions for postponed checks --
   --------------------------------------

   type Postponed_Rule_Exemption_Info;
   type  Postponed_Rule_Exemption_Info_Access is access
     Postponed_Rule_Exemption_Info;

   type Postponed_Rule_Exemption_Info is record
      Exemption_Section      : Exemption_Info;
      SF                     : SF_Id;
      Next_Exemption_Section : Postponed_Rule_Exemption_Info_Access;
   end record;

   type Postponed_Check_Exemption_Sections_Array is array (SF_Id range <>) of
     Postponed_Rule_Exemption_Info_Access;

   type Postponed_Check_Exemption_Sections_Array_Access is access
     Postponed_Check_Exemption_Sections_Array;

   type Postponed_Exemption_Sections_Array is array (Rule_Id range <>) of
     Postponed_Check_Exemption_Sections_Array_Access;

   type Postponed_Exemption_Sections_Array_Access is access
     Postponed_Exemption_Sections_Array;

   Postponed_Exemption_Sections : Postponed_Exemption_Sections_Array_Access;
--   array (Compiler_Checks) of  --  ????
--     Postponed_Check_Exemption_Sections_Array_Access;
   --  For each argument source, stores all the compiler check exemption
   --  sections found in this source. These sections are stored as they are
   --  processed - that is, in alphabetical order. Sections for different kinds
   --  of compiler checks are stored separately.

   type Current_Postponed_Exemption_Sections_Array is array
     (Rule_Id range <>) of Postponed_Rule_Exemption_Info_Access;

   type Current_Postponed_Exemption_Sections_Array_Access is access
     Current_Postponed_Exemption_Sections_Array;

   Current_Postponed_Exemption_Sections :
     Current_Postponed_Exemption_Sections_Array_Access;
--   array (Compiler_Checks) of  --  ????
--     Postponed_Rule_Exemption_Info_Access;
   --  Point to the last exemption section for the corresponding postponed
   --  check for currently processed file when storing exemption sections, or
   --  to the last traversed section when mapping diagnoses on exemption
   --  sections. Equals to null if no such section has been encountered yet.
   --  When storing exemption sections, are reset by ???, are modified by
   --  Turn_Off_Exemption procedure. When mapping diagnoses onto exemption
   --  sections, ???

   function Needs_Postponed_Exemption_Processing
     (Rule : Rule_Id)
      return Boolean;
   --  Checks if the exemption processing for the argument rule should be
   --  postponed till all the sources are processed. For the moment returns
   --  True for compiler checks, global rules and rules that are checked on
   --  expanded generics

   procedure Add_Exempted_Violation (For_Check : Rule_Id);
   --  Adds 1 to the detection counter for the currently accessed section for
   --  the argument check

   function Get_Justification (For_Check : Rule_Id) return String_Access;
   --  Gets justification from the currently accessed section for the argument
   --  check

   function Get_Original_SF (Diag : Diag_Message) return SF_Id;
   --  In case if the diagnosis is formulated for expanded instantiation,
   --  returns the ID if the file containing the template code (or No_SF_Id if
   --  this file is not a gnatcheck argument), otherwise returns the source
   --  file ID stored in the diagnosis

   procedure Map_On_Postponed_Check_Exemption      --  ???
     (In_File     :     SF_Id;
      For_Check   :     Rule_Id;
      For_Line    :     Positive;
      Is_Exempted : out Boolean);
   --  This procedure first sets the appropriate current postponed rule check
   --  section for the argument check and then checks if For_Line parameter
   --  gets into the corresponding exemption section and sets Is_Exempted
   --  accordingly. Setting the appropriate rule check sections means
   --  the following. If source file in the current section is not equal to
   --  In_File, the first section for In_File is set as current exemption
   --  section. Then sections for the given file are traversed until the
   --  section that either contains For_Line is found, or the section that
   --  starts after For_Line is reached, or all the section chain is exceeded
   --  (in this case the last section is set as current). If there is no
   --  exemption section In_File, then Is_Exempted is set to False

   --  Parsed_File : SF_Id := No_SF_Id;
   --  The file that is currently parsed and that contains Annotate pragmas
   --  being processed.

   ---------------------------------------------------
   --  Parametric exemptions for postponed checks --
   ---------------------------------------------------

   type Per_Source_Postponed_Param_Exemp is array (SF_Id range <>)
     of Parametrized_Exemption_Sections.Set;

   type Per_Source_Postponed_Param_Exemp_Access is access
     Per_Source_Postponed_Param_Exemp;

   type Per_Rule_Postponed_Param_Exemp is array (Rule_Id range <>) of
     Per_Source_Postponed_Param_Exemp_Access;

   type Per_Rule_Postponed_Param_Exemp_Access is access
     Per_Rule_Postponed_Param_Exemp;

   Postponed_Param_Exempt_Sections : Per_Rule_Postponed_Param_Exemp_Access;
   --  Here we collect all the parametric exemption sections for postponed
   --  checks.

   ---------------------------------------------
   --  Debug routines for exemption mechanism --
   ---------------------------------------------

   procedure Parametrized_Exem_Section_Debug_Image
     (S : Parametrized_Exemption_Sections.Cursor);
   --  Prints out the debug image of the information stored for a parametric
   --  exemption section.

   procedure Rule_Parametrized_Exem_Sections_Debug_Image (Rule : Rule_Id);
   pragma Unreferenced (Rule_Parametrized_Exem_Sections_Debug_Image);
   --  Prints out the debug image of the current state of the information about
   --  parametric exemption sections stored for Rule in
   --  Rule_Param_Exempt_Sections

   procedure Postponed_Exemptions_Debug_Image;
   --  Prints out the debug image of the exemption section storage for the
   --  rules that require processing of exemptions after processing all the
   --  sources (compiler checks, global rules, what else?)

   ----------------------------
   -- Add_Exempted_Violation --
   ----------------------------

   procedure Add_Exempted_Violation (For_Check : Rule_Id) is
   begin
      Current_Postponed_Exemption_Sections (For_Check).Exemption_Section.
        Detected :=
          Current_Postponed_Exemption_Sections (For_Check).Exemption_Section.
            Detected + 1;
   end Add_Exempted_Violation;

   -----------------------------
   -- All_Sources_In_One_File --
   -----------------------------

   function All_Sources_In_One_File return Boolean is
      Result : Boolean;
   begin
      Result := not Individual_Files_Specified
              and then
                Arg_Source_File_Name /= "";

      if Result
        and then
         Getenv ("GNAT_DRIVER_COMMAND_LINE") /= null
      then
         --  Analyze GNAT driver call
         declare
            GNAT_Driver_Call : constant String :=
              Getenv ("GNAT_DRIVER_COMMAND_LINE").all;

            Word_Start : Natural := GNAT_Driver_Call'First;
            Word_End   : Natural := GNAT_Driver_Call'First;

            First_Idx :           Natural := GNAT_Driver_Call'First;
            Last_Idx  : constant Natural := GNAT_Driver_Call'Last;

            Num_Of_Arg_Files : Natural := 0;

            procedure Set_Word;
            --  Select Word_Start and Word_End to point to the first word in
            --  GNAT_Driver_Call (First_Idx .. Last_Idx), and then set
            --  First_Idx to Word_End + 1. If there is no word any more in
            --  GNAT_Driver_Call (First_Idx .. Last_Idx), set Word_Start to
            --  Last_Idx + 1

            procedure Set_Word is
            begin
               Word_Start := Last_Idx + 1;

               for J in First_Idx .. Last_Idx loop
                  if GNAT_Driver_Call (J) /= ' ' then
                     Word_Start := J;
                     exit;
                  end if;
               end loop;

               if Word_Start <= Last_Idx then
                  Word_End := Last_Idx;

                  for J in Word_Start .. Last_Idx loop
                     if GNAT_Driver_Call (J) = ' ' then
                        Word_Start := J - 1;
                        exit;
                     end if;
                  end loop;

                  First_Idx := Word_End + 1;
               end if;

            end Set_Word;

         begin
            --  Just in case:
            if GNAT_Driver_Call = "" then
               goto Result_Detected;
            end if;

            --  First, case when -U option is given:
            if Index (GNAT_Driver_Call, "-U") /= 0 then
               Result := False;
               goto Result_Detected;
            end if;

            --  Here we have to parse the string.
            --  Skip the call sequence up to the space after 'check' - there is
            --  nothing interesting in it

            First_Idx :=
              Index (Ada.Characters.Handling.To_Lower (GNAT_Driver_Call),
                    "check");
            First_Idx := First_Idx + 5;

            Set_Word;

            while Word_Start <= Last_Idx loop
               if GNAT_Driver_Call (Word_Start) = '-' then
                  case GNAT_Driver_Call (Word_Start + 1) is
                     when 'a' |
                          'd' |
                          'h' |
                          'm' |
                          'q' |
                          't' |
                          'v' |
                          's' |
                          'l' |
                          '-' =>
                        --  Just skip the option, either gnatcheck's or
                        --  GNAT driver's
                        null;
                     when 'c' | -- 'cargs'
                          'r' => -- 'rules'
                        --  No information about argument sources in the rest
                        --  of the call sequence
                        exit;
                     when 'P' |
                          'o' =>
                        --  Project file: we can have either '-P prj' or -Pprj
                        --  Specification of the output file: we may have
                        --  either '-o bla' or -o=foo
                        if Word_Start + 1 = Word_End then
                           Set_Word;
                        else
                           null;
                        end if;

                     when 'f' =>
                        --  Specification of the argument file list: we may
                        --  have either -files=foo or '-files bar'

                        Num_Of_Arg_Files := Num_Of_Arg_Files + 1;

                        if Num_Of_Arg_Files > 1 then
                           Result := False;
                           goto Result_Detected;
                        end if;

                        if Word_Start + 5 = Word_End then
                           Set_Word;
                        end if;

                     when others =>
                        Error ("bug in command line analysis");
                        Error ("report a problem to report@adacore.com");
                        raise Fatal_Error;
                  end case;
               else
                  --  Definitely an explicitly specified file name, so
                  Result := False;
                  goto Result_Detected;
               end if;

               Set_Word;
            end loop;

            if Result and then Num_Of_Arg_Files = 0 then
               --  Call to the GNAT driver with no argument files specified,
               --  all the project files should be processed
               Result := False;
            end if;
         end;
      end if;

      <<Result_Detected>> return Result;
   end All_Sources_In_One_File;

   ------------------------------------
   -- Allowed_As_Exemption_Parameter --
   ------------------------------------

   function Allowed_As_Exemption_Parameter
     (Rule  : Rule_Id;
      Param : String)
      return  Boolean
   is
      Result : Boolean := False;
   begin
      if not Allows_Parametrized_Exemption (Rule) then
         return False;
      end if;

      case Rule is
         when Restrictions_Id =>
            Result := Is_Restriction_Exemption_Par (Param);
         when Style_Checks_Id =>
            null;
         when Warnings_Id =>
            Result := Is_Warning_Exemption_Par (Param);
         when others =>
            Result :=
              Allowed_As_Exemption_Parameter
                (All_Rules.Table (Rule).all,
                 Param);
      end case;

      return Result;
   end Allowed_As_Exemption_Parameter;

   -----------------------------------
   -- Allows_Parametrized_Exemption --
   -----------------------------------

   function Allows_Parametrized_Exemption (Rule : Rule_Id) return Boolean is
      Result : Boolean := False;
   begin
      case Rule is
         when Restrictions_Id |
              Warnings_Id     =>
            Result := True;
         when Style_Checks_Id =>
            null;
         when others =>
            Result :=
              Allows_Parametrized_Exemption (All_Rules.Table (Rule).all);
      end case;

      return Result;
   end Allows_Parametrized_Exemption;

   function Belongs
      (L    : String;
       R    : String_List_Access)
       return Boolean
   is
      Result : Boolean := False;
   begin
      if R /= null then

         for J in R'Range loop
            if L = R (J).all then
               Result := True;
               exit;
            elsif R (J).all > L then
               exit;
            end if;
         end loop;

      end if;

      return Result;
   end Belongs;

   ------------------------------------
   -- Check_Unclosed_Rule_Exemptions --
   ------------------------------------

   procedure Check_Unclosed_Rule_Exemptions
     (SF   : SF_Id;
      Unit : Asis.Element)
   is
      Comp_Span : constant Span := Compilation_Span (Unit);
      use Parametrized_Exemption_Sections;

      Next_Section : Cursor;
   begin

      --  Non-parametric exemptions:

      for Rule in Exemption_Sections'Range loop
         if Is_Exempted (Rule) then
            Store_Diagnosis
              (Text               =>
                 Short_Source_Name (SF)                       & ':'    &
                 Image (Exemption_Sections (Rule).Line_Start) & ':'    &
                 Image (Exemption_Sections (Rule).Col_Start)  & ": "   &
                 "No matching 'exempt_OFF' annotation "       &
                 "for rule " & Rule_Name (Rule),
               Diagnosis_Kind     => Exemption_Warning,
               SF                 => SF);

            Turn_Off_Exemption
              (Rule         => Rule,
               Closing_Span => Comp_Span,
               SF           => SF);
         end if;
      end loop;

      --  Parametric exemptions:

      for Rule in Rule_Param_Exempt_Sections'Range loop

         if Allows_Parametrized_Exemption (Rule)
           and then
            not Is_Empty (Rule_Param_Exempt_Sections (Rule))
         then
            --  We cannot use set iterator here - we need to use Rule and SF
            --  into processing routine

            Next_Section := First (Rule_Param_Exempt_Sections (Rule));

            while Has_Element (Next_Section) loop

               Store_Diagnosis
                 (Text               =>
                    Short_Source_Name (SF) & ':'                    &
                    Image (Parametrized_Exemption_Sections.Element
                      (Next_Section).Exempt_Info.Line_Start) & ':'  &
                    Image (Parametrized_Exemption_Sections.Element
                      (Next_Section).Exempt_Info.Col_Start)  & ": " &
                    "No matching 'exempt_OFF' annotation "          &
                    "for rule " & Rule_Name (Rule),
                  Diagnosis_Kind     => Exemption_Warning,
                  SF                 => SF);

               Turn_Off_Parametrized_Exemption
                 (Rule, Next_Section, Comp_Span, SF);

               Next_Section := First (Rule_Param_Exempt_Sections (Rule));
            end loop;

         end if;

      end loop;

   end Check_Unclosed_Rule_Exemptions;

   -------------------------
   -- Compute_Statisctics --
   -------------------------

   procedure Compute_Statisctics is
      type Violations_Detected is record
         Exempted_Violations_Detected     : Boolean := False;
         Non_Exempted_Violations_Detected : Boolean := False;
      end record;

      File_Counter : array (First_SF_Id .. Last_Argument_Source) of
        Violations_Detected := (others => (False, False));

      procedure Count_Diagnoses (Position : Error_Messages_Storage.Cursor);

      procedure Count_Diagnoses (Position : Error_Messages_Storage.Cursor) is
         SF : constant SF_Id := Error_Messages_Storage.Element (Position).SF;
      begin

         if not Is_Argument_Source (SF) then
            --  All the statistics is collected for argument files only!
            return;
         end if;

         case Error_Messages_Storage.Element (Position).Diagnosis_Kind is
            when Not_A_Diagnosis =>
               pragma Assert (False);
               null;
            when Rule_Violation =>
               if Error_Messages_Storage.Element (Position).Justification =
                  null
               then
                  Detected_Non_Exempted_Violations :=
                    Detected_Non_Exempted_Violations + 1;
                  File_Counter (SF).Non_Exempted_Violations_Detected := True;
               else
                  Detected_Exempted_Violations :=
                    Detected_Exempted_Violations + 1;
                  File_Counter (SF).Exempted_Violations_Detected := True;
               end if;
            when Exemption_Warning =>
               Detected_Exemption_Warning := Detected_Exemption_Warning + 1;
            when Compiler_Error =>
               Detected_Compiler_Error := Detected_Compiler_Error + 1;
         end case;
      end Count_Diagnoses;
   begin
      Error_Messages_Storage.Iterate
        (Container => All_Error_Messages,
         Process   => Count_Diagnoses'Access);

      for SF in First_SF_Id .. Last_Argument_Source loop
         if Source_Status (SF) in
              Not_A_Legal_Source |
              Not_A_Legal_Source_Needs_Listing_Processing |
              Error_Detected
         then
            Unverified_Sources := Unverified_Sources + 1;
         else
            Checked_Sources := Checked_Sources + 1;

            if File_Counter (SF).Non_Exempted_Violations_Detected then
               Sources_With_Violations := Sources_With_Violations + 1;
            elsif File_Counter (SF).Exempted_Violations_Detected then
               Sources_With_Exempted_Violations :=
                 Sources_With_Exempted_Violations + 1;
            elsif Source_Status (SF) = Processed then
               Fully_Compliant_Sources := Fully_Compliant_Sources + 1;
            end if;
         end if;

      end loop;

   end Compute_Statisctics;

   --------------------
   -- Copy_User_Info --
   --------------------

   procedure Copy_User_Info is
      Max_Line_Len : constant Positive := 1024;
      Line_Buf     :          String (1 .. Max_Line_Len);
      Line_Len     :          Natural;
      User_File    :          Ada.Text_IO.File_Type;
   begin
      --  Very simple-minded implementation...

      Open (File => User_File,
            Mode => In_File,
            Name => User_Info_File_Full_Path.all);

      Get_Line (File => User_File,
                Item => Line_Buf,
                Last => Line_Len);

      while not End_Of_File (User_File) loop
         if Text_Report_ON then
            Report (Line_Buf (1 .. Line_Len));
         end if;

         if XML_Report_ON then
            XML_Report (Line_Buf (1 .. Line_Len), 1);
         end if;

         Get_Line (File => User_File,
                   Item => Line_Buf,
                   Last => Line_Len);
      end loop;

      Close (User_File);
   exception
      when E : others =>

         Report_EOL;
         Report ("cannot successfully copy information " &
                 "from " & User_Info_File.all);

         if Is_Open (User_File) then
            Close (User_File);
         end if;

         Error ("cannot copy information from " & User_Info_File.all &
                " into report file");

         Error_No_Tool_Name (Ada.Exceptions.Exception_Information (E));
   end Copy_User_Info;

   -------------------
   -- Diag_Is_Equal --
   -------------------

   function Diag_Is_Equal (L, R : Diag_Message) return Boolean is
   begin
      return L.Text.all = R.Text.all;
   end Diag_Is_Equal;

   -----------------------
   -- Diag_Is_Less_Than --
   -----------------------

   function Diag_Is_Less_Than (L, R : Diag_Message) return Boolean is
      L_First : constant Natural := L.Text'First;
      R_First : constant Natural := R.Text'First;

      L_SLOC_Start : Natural;
      R_SLOC_Start : Natural;

      L_SLOC_End : Natural := Index (L.Text.all, ": ") - 1;
      R_SLOC_End : Natural := Index (R.Text.all, ": ") - 1;

      L_Has_Chain : Boolean := False;
      R_Has_Chain : Boolean := False;
      Inst_Idx    : Natural;
      Tmp_Idx     : Natural;

      Result : Boolean;
   begin
      if Diag_Is_Equal (L, R) then
         return False;
      end if;

      --  For computing L_Has_Chain and R_Has_Chain we have to check not only
      --  the presence of Instance_SLOC_Txt in the diagnosis, but also to
      --  verify that it is used as a SLOC of the diagnoses, but not as some
      --  part of diagnoses text (some refinement etc.)
      Inst_Idx := Index (L.Text.all, Instance_SLOC_Txt);

      if Inst_Idx > 0 then
         Tmp_Idx := Index (L.Text.all, "(");

         L_Has_Chain := Tmp_Idx = 0 or else Inst_Idx < Tmp_Idx;
      end if;

      Inst_Idx := Index (R.Text.all, Instance_SLOC_Txt);

      if Inst_Idx > 0 then
         Tmp_Idx := Index (R.Text.all, "(");

         R_Has_Chain := Tmp_Idx = 0 or else Inst_Idx < Tmp_Idx;
      end if;

      if L_Has_Chain then
         L_SLOC_Start :=
           Index (L.Text (L_First .. L_SLOC_End), " ", Going => Backward) + 1;
      else
         L_SLOC_Start := L_First;
      end if;

      if R_Has_Chain then
         R_SLOC_Start :=
           Index (R.Text (R_First .. R_SLOC_End), " ", Going => Backward) + 1;
      else
         R_SLOC_Start := R_First;
      end if;

      Result := SLOC_Less_Than (L.Text (L_SLOC_Start .. L_SLOC_End),
                                R.Text (R_SLOC_Start .. R_SLOC_End));

      if L.Text (L_SLOC_Start .. L_SLOC_End) /=
         R.Text (R_SLOC_Start .. R_SLOC_End)
      then
         return Result;
      else
         --  If we are here, SLOCs are the same
         if L_Has_Chain and then not R_Has_Chain then
            return False;
         elsif not L_Has_Chain and then R_Has_Chain then
            return True;
         end if;
      end if;

      --  If we are here then either both SLOC have chains or none of them has
      --  a chain. If we have chains, we have to compare SLOCs in templates:

      if L_Has_Chain then
         pragma Assert (R_Has_Chain);
         L_SLOC_Start := L_First;
         R_SLOC_Start := R_First;

         L_SLOC_End := Index (L.Text.all, " ") - 1;
         R_SLOC_End := Index (R.Text.all, " ") - 1;

         if L.Text (L_SLOC_Start .. L_SLOC_End) /=
            R.Text (R_SLOC_Start .. R_SLOC_End)
         then
            if L.Text (L_SLOC_Start .. L_SLOC_End) /=
               R.Text (R_SLOC_Start .. R_SLOC_End)
            then
               return SLOC_Less_Than (L.Text (L_SLOC_Start .. L_SLOC_End),
                                      R.Text (R_SLOC_Start .. R_SLOC_End));
            end if;
         end if;
      end if;

      --  And if we are here, we have equal SLOCs (both with instantiation
      --  chains or both - without chains), so

      return L.Num < R.Num;
   end Diag_Is_Less_Than;

   function Diag_Is_Less_Than_Old (L, R : Diag_Message) return Boolean is
      L_SLOC_Start : Natural;
      R_SLOC_Start : Natural;

      L_SLOC_End   : Natural := Index (L.Text.all, ": ") - 1;
      R_SLOC_End   : Natural := Index (R.Text.all, ": ") - 1;

      L_Has_Chain : Boolean;
      R_Has_Chain : Boolean;

      Result : Boolean;
   begin
      if Diag_Is_Equal (L, R) then
         return False;
      end if;

      L_Has_Chain := L.Text (L_SLOC_End) = ']';
      R_Has_Chain := R.Text (R_SLOC_End) = ']';

      if L_Has_Chain then
         while L.Text (L_SLOC_End) = ']' loop
            L_SLOC_End := L_SLOC_End - 1;
         end loop;

         L_SLOC_Start := L_SLOC_End;

         while L.Text (L_SLOC_Start) /= '[' loop
            L_SLOC_Start := L_SLOC_Start - 1;
         end loop;

         L_SLOC_Start := L_SLOC_Start + 1;
      else
         L_SLOC_Start := L.Text'First;
      end if;

      if R_Has_Chain then
         while R.Text (R_SLOC_End) = ']' loop
            R_SLOC_End := R_SLOC_End - 1;
         end loop;

         R_SLOC_Start := R_SLOC_End;

         while R.Text (R_SLOC_Start) /= '[' loop
            R_SLOC_Start := R_SLOC_Start - 1;
         end loop;

         R_SLOC_Start := R_SLOC_Start + 1;
      else
         R_SLOC_Start := R.Text'First;
      end if;

      Result := SLOC_Less_Than (L.Text (L_SLOC_Start .. L_SLOC_End),
                                R.Text (R_SLOC_Start .. R_SLOC_End));

      if L.Text (L_SLOC_Start .. L_SLOC_End) /=
         R.Text (R_SLOC_Start .. R_SLOC_End)
      then
         return Result;
      else
         --  If we are here, SLOCs are the same
         if L_Has_Chain and then not R_Has_Chain then
            return False;
         elsif not L_Has_Chain and then R_Has_Chain then
            return True;
         end if;
      end if;

      --  If we are here then either both SLOC have chains or none of them has
      --  a chain. If we have chains, we have to compare SLOCs in templates:

      if L_Has_Chain then
         pragma Assert (R_Has_Chain);
         L_SLOC_Start := L.Text'First;
         R_SLOC_Start := R.Text'First;

         L_SLOC_End := Index (L.Text.all, "[") - 1;
         R_SLOC_End := Index (R.Text.all, "[") - 1;

         if L.Text (L_SLOC_Start .. L_SLOC_End) /=
            R.Text (R_SLOC_Start .. R_SLOC_End)
         then
            if L.Text (L_SLOC_Start .. L_SLOC_End) /=
               R.Text (R_SLOC_Start .. R_SLOC_End)
            then
               return SLOC_Less_Than (L.Text (L_SLOC_Start .. L_SLOC_End),
                                      R.Text (R_SLOC_Start .. R_SLOC_End));
            end if;
         end if;
      end if;

      --  And if we are here, we have equal SLOCs (both with instantiation
      --  chains or both - without chains), so

      return L.Num < R.Num;
   end Diag_Is_Less_Than_Old;

   -----------------------------
   -- Exemption_Justification --
   -----------------------------

   function Exemption_Justification (Rule : Rule_Id) return String_Access is
   begin
      return Exemption_Sections (Rule).Justification;
   end Exemption_Justification;

   ------------------------------
   -- Diag_Srorage_Debug_Image --
   ------------------------------

   procedure Diag_Srorage_Debug_Image is
      procedure Debug_Image (Position : Error_Messages_Storage.Cursor);

      procedure Debug_Image (Position : Error_Messages_Storage.Cursor) is
         Tmp : constant Diag_Message :=
           Error_Messages_Storage.Element (Position);
      begin
         Info ("Text");
         Info (Tmp.Text.all);

         Info ("Justification");

         if Tmp.Justification = null then
            Info ("<no justification>");
         else
            Info (Tmp.Justification.all);
         end if;

         Info ("Diagnosis_Kind: " & Tmp.Diagnosis_Kind'Img);

         if Tmp.Diagnosis_Kind = Rule_Violation then
            Info ("Rule          :"  & Tmp.Rule'Img &
                  '(' & Rule_Name (Tmp.Rule) & ')');
         end if;

         Info ("SF            :" & Tmp.SF'Img);
         Info ("Num           :" & Tmp.Num'Img);
         Info ("");
      end Debug_Image;

   begin
      Info ("***Diagnoses storage debug image start");

      Error_Messages_Storage.Iterate
        (Container => All_Error_Messages,
         Process   => Debug_Image'Access);

      Info ("***Diagnoses storage debug image end");
   end Diag_Srorage_Debug_Image;

   -----------------------------------
   -- Generate_Qualification_Report --
   -----------------------------------

   procedure Generate_Qualification_Report is
   begin
      Number := new String'(Get_Number);

      Ignored_Sources := Exempted_Sources;

      Process_Postponed_Exemptions;
      Compute_Statisctics;

      if XML_Report_ON then
         XML_Report ("<?xml version=""1.0""?>");
         XML_Report_No_EOL ("<gnatcheck-report");

         if Gnatcheck_Prj.Is_Specified then
            XML_Report (" project=""" &
              (if Aggregated_Project then
                  Get_Aggregated_Project
               else
                  Gnatcheck_Prj.Source_Prj) & """>");
         else
            XML_Report (">");
         end if;
      end if;

      --  OVERVIEW
      if not Short_Report then

         Print_Report_Header;
         Print_Active_Rules_File;
         Print_File_List_File;
         Print_Ignored_File_List_File;
         Print_Argument_Files_Summary;

         if Text_Report_ON then
            Report_EOL;
         end if;

         Print_Violation_Summary;
--         Print_Failure_Info;

         --  2. DETECTED EXEMPTED RULE VIOLATIONS
         if Text_Report_ON then
            Report_EOL;
            Report ("2. Exempted Coding Standard Violations");
            Report_EOL;
         end if;

         if XML_Report_ON then
            XML_Report ("<violations>");
         end if;
      end if;

      if Detected_Exempted_Violations > 0 then
         Diagnoses_To_Print := (Rule_Violation    => True,
                                Exemption_Warning => False,
                                Compiler_Error    => False);
         Print_Exempted_Violations := True;

         Print_Diagnoses;
      else
         if Text_Report_ON then
            Report ("no exempted violations detected", 1);
         end if;

         if not Short_Report and then XML_Report_ON then
            XML_Report ("no exempted violations detected", 1);
         end if;
      end if;

      if not Short_Report then

         if Text_Report_ON then
            Report_EOL;
            Report ("3. Non-exempted Coding Standard Violations");
            Report_EOL;
         end if;

      end if;

      if Detected_Non_Exempted_Violations > 0 then
         Diagnoses_To_Print := (Rule_Violation    => True,
                                Exemption_Warning => False,
                                Compiler_Error    => False);
         Print_Exempted_Violations := False;

         Print_Diagnoses;
      else
         if Text_Report_ON then
            Report ("no non-exempted violations detected", 1);
         end if;

         if not Short_Report and then XML_Report_ON then
            XML_Report ("no non-exempted violations detected", 1);
         end if;
      end if;

      if not Short_Report then

         if Text_Report_ON then
            Report_EOL;
            Report ("4. Rule exemption problems");
            Report_EOL;
         end if;

      end if;

      if Detected_Exemption_Warning > 0 then
         Diagnoses_To_Print := (Rule_Violation    => False,
                                Exemption_Warning => True,
                                Compiler_Error    => False);

         Print_Diagnoses;
      else
         if Text_Report_ON then
            Report ("no rule exemption problems detected", 1);
         end if;

         if not Short_Report and then XML_Report_ON then
            XML_Report ("no rule exemption problems detected", 1);
         end if;

      end if;

      if not Short_Report then

         if Text_Report_ON then
            Report_EOL;
            Report ("5. Language violations");
            Report_EOL;
         end if;

      end if;

      if Detected_Compiler_Error > 0 then
         Diagnoses_To_Print := (Rule_Violation    => False,
                                Exemption_Warning => False,
                                Compiler_Error    => True);

         Print_Diagnoses;
      else
         if Text_Report_ON then
            Report ("no language violations detected", 1);
         end if;

         if not Short_Report and then XML_Report_ON then
            XML_Report ("no language violations detected", 1);
         end if;
      end if;

         --  User-defined part

      if not Short_Report then

         if XML_Report_ON then
            XML_Report ("</violations>");
         end if;

         if Text_Report_ON then
            Report_EOL;
         end if;

         if User_Info_File /= null then

            if Text_Report_ON then
               Report ("6. Additional Information");
               Report_EOL;
            end if;

            if XML_Report_ON then
               XML_Report ("<additional-information>");
            end if;

            Copy_User_Info;

            if Text_Report_ON then
               Report_EOL;
            end if;

            if XML_Report_ON then
               XML_Report ("</additional-information>");
            end if;

         end if;

      end if;

      if XML_Report_ON then
         XML_Report ("</gnatcheck-report>");
      end if;

      --  Sending the diagnoses into Stderr.
      if not Quiet_Mode then
         Print_Out_Diagnoses;
      end if;

      if Debug_Diagnoses_Storage then
         Diag_Srorage_Debug_Image;
         Postponed_Exemptions_Debug_Image;
      end if;

   end Generate_Qualification_Report;

   ----------------------
   -- Get_Exem_Section --
   ----------------------

   function Get_Exem_Section
     (Exem_Sections              : Parametrized_Exemption_Sections.Set;
      Param                      : String;
      Line                       : Natural := 0;
      Col                        : Natural := 0;
      Check_Postponed_Exemptions : Boolean := False)
      return          Parametrized_Exemption_Sections.Cursor
   is
      use Parametrized_Exemption_Sections;
      Result               : Cursor  := No_Element;
      Next_Section         : Cursor  := First (Exem_Sections);

      Diag_In_Section      : Boolean := True;
      Diag_Before_Sections : Boolean := False;
      --  These two Boolean flags are used only if Check_Postponed_Exemptions
      --  is ON to control iterating through all the parametric exemption
      --  sections for the source that are stored in Exem_Sections

      Next_Section_El : Parametrized_Exemption_Info;
   begin
      while Has_Element (Next_Section) loop

         Next_Section_El :=
           Parametrized_Exemption_Sections.Element (Next_Section);

         if Check_Postponed_Exemptions then
            Diag_Before_Sections :=
              Line < Next_Section_El.Exempt_Info.Line_Start
             or else
              (Line = Next_Section_El.Exempt_Info.Line_Start
              and then
               Col < Next_Section_El.Exempt_Info.Col_Start);

            exit when Diag_Before_Sections;

            Diag_In_Section :=
              (Line > Next_Section_El.Exempt_Info.Line_Start
             or else
              (Line = Next_Section_El.Exempt_Info.Line_Start
              and then
               Col > Next_Section_El.Exempt_Info.Col_Start))
             and then
              (Line < Next_Section_El.Exempt_Info.Line_End
             or else
              (Line = Next_Section_El.Exempt_Info.Line_End
              and then
               Col < Next_Section_El.Exempt_Info.Col_End));
         end if;

         if Diag_In_Section
           and then
            Belongs (Param, Next_Section_El.Params)
         then
            Result := Next_Section;
            exit;
         end if;

         Next_Section := Next (Next_Section);
      end loop;

      return Result;
   end Get_Exem_Section;

   ------------------------
   -- Get_Exemption_Kind --
   ------------------------

   function Get_Exemption_Kind (Image : Wide_String) return Exemption_Kinds is
      Result : Exemption_Kinds;
   begin

      if Image (Image'First) = '"' then
         Result :=
           Exemption_Kinds'Wide_Value
             (Image (Image'First + 1 .. Image'Last - 1));
      --  Old format of Annotate pragma. We have to cut out quotation marks
      else
         Result :=
           Exemption_Kinds'Wide_Value (Image);
      end if;

      return Result;
   exception
      when Constraint_Error =>
         return Not_An_Exemption;
   end Get_Exemption_Kind;

   -----------------------
   -- Get_Justification --
   -----------------------

   function Get_Justification (For_Check : Rule_Id) return String_Access is
   begin
      return Current_Postponed_Exemption_Sections (For_Check).
                Exemption_Section.Justification;
   end Get_Justification;

   ---------------------
   -- Get_Original_SF --
   ---------------------

   function Get_Original_SF (Diag : Diag_Message) return SF_Id is
      Result    : SF_Id := Diag.SF;
      Start_Idx : Natural;
      End_Idx   : Natural := Index (Diag.Text.all, Instance_SLOC_Txt);
   begin
      if End_Idx > 0 then
         End_Idx   := Index (Diag.Text.all, ":") - 1;
         Start_Idx := Diag.Text'First;
         Result :=
           File_Find (Diag.Text (Start_Idx .. End_Idx),
                      Use_Short_Name => True);
      end if;

      return Result;
   end Get_Original_SF;

   -----------------------------
   -- Get_Param_Justification --
   -----------------------------

   function Get_Param_Justification
     (Rule                       : Rule_Id;
      Diag                       : String;
      SF                         : SF_Id;
      Line                       : Natural := 0;
      Col                        : Natural := 0;
      Check_Postponed_Exemptions : Boolean := False)
      return                       String_Access
   is
      Result : String_Access;

      use Parametrized_Exemption_Sections;
      Exem_Sections  : access Parametrized_Exemption_Sections.Set;
      --  You should never assign containers!!!
      Fit_In_Section : Cursor;
   begin
      if Needs_Postponed_Exemption_Processing (Rule)
        and then
         not Check_Postponed_Exemptions
      then
         return null;
      end if;

      declare
         Param : constant String := Rule_Parameter (Diag, Rule);
         pragma Assert (Param /= "" or else Rule = Warnings_Id);

      begin

         if Check_Postponed_Exemptions then
            Exem_Sections :=
              Postponed_Param_Exempt_Sections (Rule) (SF)'Unrestricted_Access;
         else
            Exem_Sections :=
              Rule_Param_Exempt_Sections (Rule)'Unrestricted_Access;
         end if;

         Fit_In_Section := Get_Exem_Section
           (Exem_Sections.all,
            Param,
            Line,
            Col,
            Check_Postponed_Exemptions);

         if Has_Element (Fit_In_Section) then
            Result :=
              Parametrized_Exemption_Sections.Element (Fit_In_Section).
                Exempt_Info.Justification;
            Icrease_Diag_Counter (Exem_Sections.all, Fit_In_Section);
         end if;
      end;

      return Result;
   end Get_Param_Justification;

   --------------------------
   -- Icrease_Diag_Counter --
   --------------------------

   procedure Icrease_Diag_Counter
     (Exem_Sections : in out Parametrized_Exemption_Sections.Set;
      Section       :        Parametrized_Exemption_Sections.Cursor)
   is
      procedure Add_One (Exem_Section : in out Parametrized_Exemption_Info);

      procedure Add_One (Exem_Section : in out Parametrized_Exemption_Info) is
      begin
         Exem_Section.Exempt_Info.Detected :=
           Exem_Section.Exempt_Info.Detected + 1;
      end Add_One;
   begin
      Exem_Section_Keys.Update_Element_Preserving_Key
        (Container => Exem_Sections,
         Position  => Section,
         Process   => Add_One'Access);
   end Icrease_Diag_Counter;

   -----------------------------------
   -- Ignored_Source_List_File_Name --
   -----------------------------------

   function Ignored_Source_List_File_Name return String is
      Dot_Idx : constant Natural :=
        Index (Ignored_Source_List_File_Name_Str, ".", Forward);
   begin
      if not Aggregated_Project then
         return Ignored_Source_List_File_Name_Str;
      end if;

      pragma Assert (Dot_Idx > 0);

      return Ignored_Source_List_File_Name_Str
               (Ignored_Source_List_File_Name_Str'First .. Dot_Idx - 1) &
             Number.all                                         &
             Ignored_Source_List_File_Name_Str
               (Dot_Idx .. Ignored_Source_List_File_Name_Str'Last);

   end Ignored_Source_List_File_Name;

   ---------------------
   -- Init_Exemptions --
   ---------------------

   procedure Init_Exemptions is
   begin

      Exemption_Sections :=
        new Exemption_Sections_Array (First_Compiler_Check .. All_Rules.Last);

      Exemption_Sections.all := (others =>
        (Line_Start    => 0,
         Col_Start     => 0,
         Line_End      => 0,
         Col_End       => 0,
         Justification => null,
         Detected      => 0));

      Postponed_Exemption_Sections :=
        new Postponed_Exemption_Sections_Array
          (First_Compiler_Check .. All_Rules.Last);

      Current_Postponed_Exemption_Sections :=
        new Current_Postponed_Exemption_Sections_Array
          (First_Compiler_Check .. All_Rules.Last);

      for Rule in Postponed_Exemption_Sections'Range loop

         if Needs_Postponed_Exemption_Processing (Rule) then
            Postponed_Exemption_Sections (Rule) :=
              new Postponed_Check_Exemption_Sections_Array
                (First_SF_Id .. Last_Argument_Source);
            Postponed_Exemption_Sections (Rule).all := (others => null);
         end if;

      end loop;

      --  Parametric exemptions:

      Rule_Param_Exempt_Sections :=
        new Rule_Param_Exempt_Sections_Type
          (First_Compiler_Check .. All_Rules.Last);

      Postponed_Param_Exempt_Sections :=
        new Per_Rule_Postponed_Param_Exemp
          (First_Compiler_Check .. All_Rules.Last);

      for Rule in Postponed_Exemption_Sections'Range loop

         if Needs_Postponed_Exemption_Processing (Rule)
           and then
            Allows_Parametrized_Exemption (Rule)
         then
            Postponed_Param_Exempt_Sections (Rule) :=
              new Per_Source_Postponed_Param_Exemp
                (First_SF_Id .. Last_Argument_Source);
         end if;
      end loop;

   end Init_Exemptions;

   -------------------------------------
   -- Init_Postponed_Check_Exemptions --
   -------------------------------------

   procedure Init_Postponed_Check_Exemptions is
   begin
      --  !!!??? FREE THE MEMORY!!!

      for Rule in Current_Postponed_Exemption_Sections'Range loop

         if Needs_Postponed_Exemption_Processing (Rule) then
            Current_Postponed_Exemption_Sections (Rule) := null;
         end if;

      end loop;
   end Init_Postponed_Check_Exemptions;

   --------------
   -- Is_Equal --
   --------------

   function Is_Equal (L, R : String_List_Access) return Boolean is
      Result : Boolean := (L = null) and (R = null);
      R_Idx  : Natural;
   begin
      if L /= null and then
         R /= null and then
         L'Length = R'Length
      then
         R_Idx  := R'First;
         Result := True;

         for J in L'Range loop
            if L (J) = null or else R (R_Idx) = null then
               --  Even if both elements are null we return False because this
               --  cannot be considered as the same sequence of parameters
               Result := False;
               exit;
            elsif L (J).all /= R (R_Idx).all then
               Result := False;
               exit;
            end if;

            R_Idx := R_Idx + 1;
         end loop;
      end if;

      return Result;
   end Is_Equal;

   function Is_Equal
     (L    : Exemption_Parameters.Set;
      R    : String_List_Access)
      return Boolean
   is
      use Exemption_Parameters;
      Result : Boolean := not Is_Empty (L) and then
                          R /= null        and then
                          Natural (Length (L)) = R'Length;
   begin
      if Result then
         declare
            L_Arr : String_List_Access := To_Pars_List (L);
         begin
            Result := Is_Equal (L_Arr, R);
            Free (L_Arr);
         end;
      end if;

      return Result;
   end Is_Equal;

   -----------------
   -- Is_Exempted --
   -----------------

   function Is_Exempted (Rule : Rule_Id) return Boolean is
   begin
      return Exemption_Sections (Rule).Line_Start > 0;
   end Is_Exempted;

   ---------------------------
   -- Is_Exempted_With_Pars --
   ---------------------------

   procedure Is_Exempted_With_Pars
     (Rule        :     Rule_Id;
      Exempted_At : out Parametrized_Exemption_Sections.Cursor)
   is
      use Parametrized_Exemption_Sections;
      Next_Section : Cursor;
   begin
      Exempted_At := No_Element;

      if not Is_Empty (Rule_Param_Exempt_Sections (Rule)) then
         Next_Section := First (Rule_Param_Exempt_Sections (Rule));

         while Has_Element (Next_Section) loop
            if Is_Equal
                 (Rule_Exemption_Parameters,
                  Parametrized_Exemption_Sections.Element
                    (Next_Section).Params)
            then
               Exempted_At := Next_Section;
               exit;
            end if;

            Next_Section := Next (Next_Section);
         end loop;

      end if;
   end Is_Exempted_With_Pars;

   -------------------------
   -- Is_Exemption_Pragma --
   -------------------------

   function Is_Exemption_Pragma (El : Asis.Element) return Boolean is
      Result : Boolean := False;
   begin

      if Pragma_Kind (El) = An_Implementation_Defined_Pragma
        and then
         To_Lower (To_String (Pragma_Name_Image (El))) = "annotate"
      then

         declare
            Pragma_Args : constant Asis.Element_List :=
              Pragma_Argument_Associations (El);
            --  Always non-empty for Annotate pragma!
            First_Par : Asis.Element;
         begin
            First_Par := Pragma_Args (Pragma_Args'First);
            First_Par := Actual_Parameter (First_Par);

            if To_Lower (To_String (Name_Image (First_Par))) = "gnatcheck" then
               Result := True;
            end if;
         end;

      end if;

      return Result;
   end Is_Exemption_Pragma;

   ---------------------------------------
   -- Map_On_Postponed_Check_Exemption --
   ---------------------------------------

   procedure Map_On_Postponed_Check_Exemption
     (In_File     :     SF_Id;
      For_Check   :     Rule_Id;
      For_Line    :     Positive;
      Is_Exempted : out Boolean)
   is
   begin

      Is_Exempted := False;

      if not Is_Argument_Source (In_File) then
         --  Exemption sections are processed in argument files only!
         return;
      end if;

      if Postponed_Exemption_Sections (For_Check) (In_File) = null then
         return;
      end if;

      if Current_Postponed_Exemption_Sections (For_Check) = null
        or else
         Current_Postponed_Exemption_Sections (For_Check).SF /= In_File
      then
         Current_Postponed_Exemption_Sections (For_Check) :=
          Postponed_Exemption_Sections (For_Check) (In_File);
      end if;

      --  Traverse exemption section chain:
      while Current_Postponed_Exemption_Sections (For_Check) /= null loop
         if For_Line in
            Current_Postponed_Exemption_Sections (For_Check).Exemption_Section.
              Line_Start
                ..
            Current_Postponed_Exemption_Sections (For_Check).Exemption_Section.
              Line_End
         then
            Is_Exempted := True;
            exit;
         end if;

         if For_Line <
            Current_Postponed_Exemption_Sections (For_Check).Exemption_Section.
              Line_Start
         then
            exit;
         end if;

         Current_Postponed_Exemption_Sections (For_Check) :=
           Current_Postponed_Exemption_Sections (For_Check).
             Next_Exemption_Section;
      end loop;

   end Map_On_Postponed_Check_Exemption;

   ------------------------------------------
   -- Needs_Postponed_Exemption_Processing --
   ------------------------------------------

   function Needs_Postponed_Exemption_Processing
     (Rule : Rule_Id)
      return Boolean
   is
      Result : Boolean := Rule in Compiler_Checks;
   begin

      if not Result then
         Result :=
           Is_Global (Rule)
          or else
           Checked_On_Expanded_Code (All_Rules.Table (Rule).all);
      end if;

      return Result;
   end Needs_Postponed_Exemption_Processing;

   ----------------------
   -- Next_Message_Num --
   ----------------------

   Next_Message_Num_Value : Natural := 0;

   function Next_Message_Num return Positive is
   begin
      Next_Message_Num_Value := Next_Message_Num_Value + 1;
      return Next_Message_Num_Value;
   end Next_Message_Num;

   -------------------------------------------
   -- Parametrized_Exem_Section_Debug_Image --
   -------------------------------------------

   procedure Parametrized_Exem_Section_Debug_Image
     (S : Parametrized_Exemption_Sections.Cursor)
   is
      ES_Info : Parametrized_Exemption_Info;
   begin
      if Parametrized_Exemption_Sections.Has_Element (S) then
         ES_Info := Parametrized_Exemption_Sections.Element (S);

         Info ("+++++++++++++++++++++++++++++++++++++++++++++++");
         Info_No_EOL ("parametric exemption section for rule");
         Info (ES_Info.Rule'Img & "(" & Rule_Name (ES_Info.Rule) & ")");
         Info ("in file SF=" & ES_Info.SF'Img &
               "(" & Short_Source_Name (ES_Info.SF) & ")");
         Info ("Parameters:");

         if ES_Info.Params = null then
            Info ("   Null parameter list!!!");
         elsif ES_Info.Params'Length = 0 then
            Info ("   Empty parameter list!!!");
         else
            for J in ES_Info.Params'Range loop
               Info (">>" & ES_Info.Params (J).all & "<<");
            end loop;
         end if;

         Info ("Span: " & ES_Info.Exempt_Info.Line_Start'Img & ":" &
                ES_Info.Exempt_Info.Col_Start'Img & " -"  &
                ES_Info.Exempt_Info.Line_End'Img & ":" &
                ES_Info.Exempt_Info.Col_End'Img);
         Info ("Detected :" & ES_Info.Exempt_Info.Detected'Img);
         Info_No_EOL ("Justification: ");

         if ES_Info.Exempt_Info.Justification = null then
            Info ("IS NOT SET!!!");
         else
            Info (">>" & ES_Info.Exempt_Info.Justification.all & "<<");
         end if;

      else
         Info ("No parametric exemption section");
      end if;

   end Parametrized_Exem_Section_Debug_Image;

   ----------------
   -- Params_Img --
   ----------------

   function Params_Img
     (Params : String_List_Access;
      Rule   : Rule_Id)
      return   String
   is
      Res_Len : Natural := 0;
   begin
      pragma Assert (Params /= null);

      for J in Params'Range loop
         Res_Len := Res_Len + Params (J)'Length;
      end loop;

      Res_Len := Res_Len + (Params'Length - 1) * 2;  --  for ", "

      declare
         Result      : String (1 .. Res_Len);
         Append_From : Natural := Result'First;
      begin
         for J in Params'Range loop
            if J > Params'First then
               Result (Append_From .. Append_From + 1) := ", ";
               Append_From := Append_From + 2;
            end if;

            Result (Append_From .. Append_From + Params (J)'Length - 1) :=
             (if Rule = Warnings_Id then
                 Params (J).all
              else
                 Proper_Case (Params (J).all));

            Append_From := Append_From + Params (J)'Length;

         end loop;

         return Result;
      end;

   end Params_Img;

   --------------------------------------
   -- Postponed_Exemptions_Debug_Image --
   --------------------------------------

   procedure Postponed_Exemptions_Debug_Image is
      procedure Sections_Debug_Image
        (S : Postponed_Rule_Exemption_Info_Access);

      procedure Sections_Debug_Image
        (S : Postponed_Rule_Exemption_Info_Access)
      is
         Tmp : Postponed_Rule_Exemption_Info_Access := S;
      begin
         if Tmp = null then
            Info ("*no exemption section");
         else
            while Tmp /= null loop
               Info ("Section " & Tmp.Exemption_Section.Line_Start'Img &
                     ':' & Tmp.Exemption_Section.Col_Start'Img & " - " &
                     Tmp.Exemption_Section.Line_End'Img &
                     ':' & Tmp.Exemption_Section.Col_End'Img);
               Info ("SF: " & Tmp.SF'Img);
               Info ("Detected: " & Tmp.Exemption_Section.Detected'Img);
               Info ("Justification: " &
                     Tmp.Exemption_Section.Justification.all);

               Tmp := Tmp.Next_Exemption_Section;
            end loop;
         end if;
      end Sections_Debug_Image;
   begin
      Info ("***Postponed non-parametric exemptions debug image start");

      for SF in First_SF_Id .. Last_Argument_Source loop
         Info ("***Exemption sections for " & Short_Source_Name (SF));

         for Rule in Postponed_Exemption_Sections'Range loop

            if Needs_Postponed_Exemption_Processing (Rule) then
               Info ("** " & Rule_Name (Rule));
               Sections_Debug_Image (Postponed_Exemption_Sections (Rule) (SF));
            end if;

         end loop;

      end loop;

      Info ("***Postponed non-parametric exemptions debug image end");
      Info ("");
      Info ("***Postponed parametric exemptions debug image start");

      for SF in First_SF_Id .. Last_Argument_Source loop
         Info ("***PARAMETRIC Exemption sections for " &
               Short_Source_Name (SF));

         for Rule in Postponed_Param_Exempt_Sections'Range loop

            if Allows_Parametrized_Exemption (Rule)
              and then
               Needs_Postponed_Exemption_Processing (Rule)
            then
               Info ("** Rule " & Rule_Name (Rule));

               if Parametrized_Exemption_Sections.Is_Empty
                 (Postponed_Param_Exempt_Sections (Rule) (SF))
               then
                  Info ("   No parametric exemption sections");
               else
                  Parametrized_Exemption_Sections.Iterate
                    (Container => Postponed_Param_Exempt_Sections (Rule) (SF),
                     Process => Parametrized_Exem_Section_Debug_Image'Access);
               end if;

            end if;

         end loop;

      end loop;

      Info ("***Postponed parametric exemptions debug image end");

   end Postponed_Exemptions_Debug_Image;

   -----------------------------
   -- Print_Active_Rules_File --
   -----------------------------

   procedure Print_Active_Rules_File is
      Rule_List_File : Ada.Text_IO.File_Type;
   begin
      if Text_Report_ON then
         Report_No_EOL ("coding standard   : ");
      end if;

      if XML_Report_ON then
         XML_Report_No_EOL
           ("<coding-standard from-file=""", Indent_Level => 1);
      end if;

      if not Individual_Rules_Set
        and then
         Rule_File_Name /= null
      then
         if Text_Report_ON then
            Report (Rule_File_Name.all);
         end if;

         if XML_Report_ON then
            XML_Report (Rule_File_Name.all & """>");
         end if;

      else
         --  Creating the list of active rules in Rule_List_File_Name

         declare
            Full_Rule_List_File_Name : constant String :=
              (if Get_Report_File_Name /= "" then
                  GNAT.Directory_Operations.Dir_Name (Get_Report_File_Name)
               else
                  GNAT.Directory_Operations.Dir_Name
                    (Get_XML_Report_File_Name)) & Rule_List_File_Name;
         begin

            if Is_Regular_File (Full_Rule_List_File_Name) then
               Open
                 (Rule_List_File,
                  Out_File,
                  Full_Rule_List_File_Name);
            else
               Create
                 (Rule_List_File,
                  Out_File,
                  Full_Rule_List_File_Name);
            end if;

            for Rule in All_Rules.First .. All_Rules.Last loop

               if All_Rules.Table (Rule).Diagnosis /= null
                and then
                  Is_Enable (All_Rules.Table (Rule).all)
               then
                  --  Note, that if a rule does not have its own diagnoses,
                  --  this means that it is implemented by some other rules,
                  --  so it should not go into the report

                  Print_Rule_To_File
                    (All_Rules.Table (Rule).all, Rule_List_File);
                  New_Line (Rule_List_File);
               end if;
            end loop;

            New_Line (Rule_List_File);

            --  Compiler-made checks:

            if Use_gnaty_Option then
               New_Line (Rule_List_File);
               Put_Line (Rule_List_File, "-- Compiler style checks:");
               Put      (Rule_List_File, "+RStyle_Checks : ");
               Put_Line (Rule_List_File,
                         Get_Style_Option
                           (Get_Style_Option'First + 6 ..
                              Get_Style_Option'Last));
            end if;

            if Use_gnatw_Option then
               New_Line (Rule_List_File);
               Put_Line (Rule_List_File, "--  Compiler warnings:");
               Put      (Rule_List_File, "+RWarnings : ");
               Put_Line (Rule_List_File,
                         Get_Specified_Warning_Option
                           (Get_Specified_Warning_Option'First + 6 ..
                            Get_Specified_Warning_Option'Last));
            end if;

            if Check_Restrictions then
               New_Line (Rule_List_File);
               Put_Line (Rule_List_File, "--  Compiler restrictions:");
               Print_Active_Restrictions_To_File (Rule_List_File);
            end if;

            Close (Rule_List_File);

            if Text_Report_ON then
               Report (Rule_List_File_Name);
            end if;

            if XML_Report_ON then
               XML_Report (Rule_List_File_Name & """>");
            end if;

         end;
      end if;

      if XML_Report_ON then
         for Rule in All_Rules.First .. All_Rules.Last loop

            if All_Rules.Table (Rule).Diagnosis /= null
             and then
               Is_Enable (All_Rules.Table (Rule).all)
            then
               XML_Print_Rule (All_Rules.Table (Rule).all, Indent_Level => 2);
            end if;

         end loop;

         if Use_gnaty_Option then
            XML_Report
              ("<rule id=""Style_Checks"">",
               Indent_Level => 2);

            XML_Report
              ("<parameter>" &
                 Get_Style_Option
                   (Get_Style_Option'First + 6 ..
                      Get_Style_Option'Last) & "</parameter>",
               Indent_Level => 3);

            XML_Report ("</rule>", Indent_Level => 2);
         end if;

         if Use_gnatw_Option then
            XML_Report
              ("<rule id=""Warnings"">",
               Indent_Level => 2);

            XML_Report
              ("<parameter>" &
                 Get_Specified_Warning_Option
                           (Get_Specified_Warning_Option'First + 6 ..
                            Get_Specified_Warning_Option'Last) &
                 "</parameter>",
               Indent_Level => 3);

            XML_Report ("</rule>", Indent_Level => 2);
         end if;

         if Check_Restrictions then
            XML_Print_Active_Restrictions (Indent_Level => 1);
         end if;

         XML_Report ("</coding-standard>", Indent_Level => 1);
      end if;
   end Print_Active_Rules_File;

   ----------------------------------
   -- Print_Argument_Files_Summary --
   ----------------------------------

   procedure Print_Argument_Files_Summary is
   begin

      if Text_Report_ON then
         Report ("1. Summary");
         Report_EOL;

         Report ("fully compliant sources               :" &
                 Fully_Compliant_Sources'Img, 1);
         Report ("sources with exempted violations only :" &
                 Sources_With_Exempted_Violations'Img, 1);
         Report ("sources with non-exempted violations  :" &
                 Sources_With_Violations'Img, 1);
         Report ("unverified sources                    :" &
                 Unverified_Sources'Img, 1);
         Report ("total sources                         :" &
                 Last_Argument_Source'Img, 1);
         Report ("ignored sources                       :" &
                 Ignored_Sources'Img, 1);
      end if;

      if XML_Report_ON then
         XML_Report ("<summary>");

         XML_Report ("<fully-compliant-sources>"     &
                     Image (Fully_Compliant_Sources) &
                     "</fully-compliant-sources>",
                     Indent_Level => 1);

         XML_Report ("<sources-with-exempted-violations-only>"     &
                     Image (Sources_With_Exempted_Violations) &
                     "</sources-with-exempted-violations-only>",
                     Indent_Level => 1);

         XML_Report ("<sources-with-non-exempted-violations>"     &
                     Image (Sources_With_Violations) &
                     "</sources-with-non-exempted-violations>",
                     Indent_Level => 1);

         XML_Report ("<unverified-sources>"     &
                     Image (Unverified_Sources) &
                     "</unverified-sources>",
                     Indent_Level => 1);

         XML_Report ("<total-sources>"     &
                     Image (Integer (Last_Argument_Source)) &
                     "</total-sources>",
                     Indent_Level => 1);

      end if;

      pragma Assert (Checked_Sources =
                       Fully_Compliant_Sources +
                       Sources_With_Violations +
                       Sources_With_Exempted_Violations +
                       Ignored_Sources);
      pragma Assert (Natural (Last_Argument_Source) =
                       Checked_Sources + Unverified_Sources);
   end Print_Argument_Files_Summary;

   ---------------------
   -- Print_Diagnoses --
   ---------------------

   procedure Print_Diagnoses is

      procedure Print_Specified_Diagnoses
        (Position : Error_Messages_Storage.Cursor);

      procedure Print_Specified_Diagnoses
        (Position : Error_Messages_Storage.Cursor)
      is
         Diag : constant Diag_Message :=
           Error_Messages_Storage.Element (Position);
      begin
         if Diagnoses_To_Print (Diag.Diagnosis_Kind) then

            if Diag.Diagnosis_Kind = Rule_Violation
              and then
                Print_Exempted_Violations =
                (Diag.Justification = null)
            then
               return;
            end if;

            if Text_Report_ON then
               Report (Diag.Text.all);

               if Diag.Justification /= null then
                  Report ("(" & Diag.Justification.all & ")", 1);
               end if;
            end if;

            if XML_Report_ON then
               XML_Report_Diagnosis (Diag, Short_Report);
            end if;

         end if;
      end Print_Specified_Diagnoses;

   begin
      Error_Messages_Storage.Iterate
        (Container => All_Error_Messages,
         Process   => Print_Specified_Diagnoses'Access);
   end Print_Diagnoses;

   ------------------------
   -- Print_Failure_Info --
   ------------------------

   procedure Print_Failure_Info is
   begin

      if Tool_Failures > 0 then
         Report ("Total gnatcheck failures:" & Tool_Failures'Img);
         Report_EOL;
      end if;

   end Print_Failure_Info;

   --------------------------
   -- Print_File_List_File --
   --------------------------

   procedure Print_File_List_File is
      Source_List_File : Ada.Text_IO.File_Type;
   begin
      if Text_Report_ON then
         Report_No_EOL ("list of sources   : ");

      end if;

      if XML_Report_ON then
         XML_Report_No_EOL ("<sources from-file=""", Indent_Level => 1);
      end if;

      if All_Sources_In_One_File then

         if Text_Report_ON then
            Report (Arg_Source_File_Name);
         end if;

         if XML_Report_ON then
            XML_Report (Arg_Source_File_Name & """>");
         end if;

      else
         --  Creating the list of processed sources in Source_List_File_Name

         declare
            Full_Source_List_File_Name : constant String :=
              (if Get_Report_File_Name /= "" then
                  GNAT.Directory_Operations.Dir_Name (Get_Report_File_Name)
               else
                  GNAT.Directory_Operations.Dir_Name
                    (Get_XML_Report_File_Name)) & Source_List_File_Name;
         begin
            if XML_Report_ON then
               XML_Report (Source_List_File_Name & """>");
            end if;

            if Is_Regular_File (Full_Source_List_File_Name) then
               Open
                 (Source_List_File,
                  Out_File,
                  Full_Source_List_File_Name);
            else
               Create
                 (Source_List_File,
                  Out_File,
                  Full_Source_List_File_Name);
            end if;

            for SF in First_SF_Id .. Last_Argument_Source loop
               if Source_Info (SF) /= Ignore_Unit then
                  Put_Line
                    (Source_List_File,
                     (if Full_Source_Locations then
                         Source_Name (SF)
                      else
                         Short_Source_Name (SF)));
               end if;
            end loop;

            Close (Source_List_File);

            if Text_Report_ON then
               Report (Source_List_File_Name);
            end if;

         end;
      end if;

      if Text_Report_ON then
         Report_EOL;
      end if;

      if XML_Report_ON then

         for SF in First_SF_Id .. Last_Argument_Source loop

            if XML_Report_ON
              and then
               Source_Info (SF) /= Ignore_Unit
            then
               XML_Report
                 ("<source>" & Source_Name (SF) & "</source>",
                  Indent_Level => 2);
            end if;
         end loop;

         XML_Report ("</sources>", Indent_Level => 1);
      end if;
   end Print_File_List_File;

   ----------------------------------
   -- Print_Gnatcheck_Command_Line --
   ----------------------------------

   procedure Print_Gnatcheck_Command_Line (XML : Boolean := False) is
      GNAT_Driver_Call : constant String_Access :=
       Getenv ("GNAT_DRIVER_COMMAND_LINE");

   begin
      if GNAT_Driver_Call /= null
       and then
         GNAT_Driver_Call.all /= ""
      then
         if XML then
            XML_Report (GNAT_Driver_Call.all);
         else
            Report (GNAT_Driver_Call.all);
         end if;
      else

         if XML then
            XML_Report_No_EOL (Ada.Command_Line.Command_Name);
         else
            Report_No_EOL (Ada.Command_Line.Command_Name);
         end if;

         for Arg in 1 .. Ada.Command_Line.Argument_Count loop
            if XML then
               XML_Report_No_EOL (" " & Ada.Command_Line.Argument (Arg));
            else
               Report_No_EOL (" " & Ada.Command_Line.Argument (Arg));
            end if;
         end loop;

         if not XML then
            Report_EOL;
         end if;
      end if;

   end Print_Gnatcheck_Command_Line;

   ----------------------------------
   -- Print_Ignored_File_List_File --
   ----------------------------------

   procedure Print_Ignored_File_List_File is
      Ignored_Source_List_File : Ada.Text_IO.File_Type;
   begin
      if Ignored_Sources = 0 then
         return;
      end if;

      if Text_Report_ON then
         Report_No_EOL ("list of ignored sources   : ");

      end if;

      if XML_Report_ON then
         XML_Report_No_EOL ("<ignored-sources from-file=""",
                            Indent_Level => 1);
      end if;

      declare
         Full_Ignored_Source_List_File_Name : constant String :=
           (if Get_Report_File_Name /= "" then
                  GNAT.Directory_Operations.Dir_Name (Get_Report_File_Name)
               else
                  GNAT.Directory_Operations.Dir_Name
                    (Get_XML_Report_File_Name)) &
           Ignored_Source_List_File_Name;
      begin
         if XML_Report_ON then
            XML_Report (Ignored_Source_List_File_Name & """>");
         end if;

         if Is_Regular_File (Full_Ignored_Source_List_File_Name) then
            Open
              (Ignored_Source_List_File,
               Out_File,
               Full_Ignored_Source_List_File_Name);
         else
            Create
              (Ignored_Source_List_File,
               Out_File,
               Full_Ignored_Source_List_File_Name);
         end if;

         for SF in First_SF_Id .. Last_Argument_Source loop
            if Source_Info (SF) = Ignore_Unit then
               Put_Line
                 (Ignored_Source_List_File,
                  (if Full_Source_Locations then
                      Source_Name (SF)
                   else
                      Short_Source_Name (SF)));
            end if;
         end loop;

         Close (Ignored_Source_List_File);

         if Text_Report_ON then
            Report (Ignored_Source_List_File_Name);
         end if;

      end;

      if Text_Report_ON then
         Report_EOL;
      end if;

      if XML_Report_ON then

         for SF in First_SF_Id .. Last_Argument_Source loop

            if XML_Report_ON
              and then
               Source_Info (SF) = Ignore_Unit
            then
               XML_Report
                 ("<source>" & Source_Name (SF) & "</source>",
                  Indent_Level => 2);
            end if;
         end loop;

         XML_Report ("</ignored-sources>", Indent_Level => 1);
      end if;
   end Print_Ignored_File_List_File;

   -------------------------
   -- Print_Out_Diagnoses --
   -------------------------

   procedure Print_Out_Diagnoses is
      Diagnoses_Reported :           Natural := 0;
      Limit_Exceeded     :           Boolean := False;
      GPS_Prefix         : constant String  := "check:";

      function Add_GPS_Prefix (Diag : String) return String;
      --  If the corresponding option is set (at the moment it is '-dd' option
      --  that activates progress indicator, but this may be changed) adds
      --  GPS_Prefix to the diagnosis that is supposed to be passed as an
      --  actual. Otherwise returns the argument unchanged.

      procedure Counted_Print_Diagnosis
        (Position : Error_Messages_Storage.Cursor);

      function Add_GPS_Prefix (Diag : String) return String is
         Idx        : Natural;
         Inst_Count : Natural;
      begin
         if ASIS_UL.Debug.Debug_Flag_D then
            Idx        := Index (Diag, ": ");
            Inst_Count := Ada.Strings.Fixed.Count (Diag, Instance_SLOC_Txt);

            if Inst_Count = 0 then
               return Diag (Diag'First .. Idx + 1) & GPS_Prefix & ' ' &
                            Diag (Idx + 2 .. Diag'Last);
            else
               declare
                  Result : String
                    (1 .. Diag'Length + Inst_Count + GPS_Prefix'Length);
                  Res_Idx    :          Natural := Result'First;
                  Diag_Start :          Natural := Diag'First;
                  Diag_End   :          Natural;
                  Diag_Last  : constant Natural := Diag'Last;
               begin
                  while  Inst_Count > 0 loop
                     Diag_End := Index (Diag (Diag_Start .. Diag_Last),
                                        Instance_SLOC_Txt);

                     Result (Res_Idx ..
                             Res_Idx + (Diag_End - Diag_Start - 1)) :=
                               Diag (Diag_Start .. Diag_End - 1);

                     Res_Idx := Res_Idx + (Diag_End - Diag_Start);
                     Result (Res_Idx) := ':';
                     Res_Idx := Res_Idx + 1;

                     Result (Res_Idx ..
                             Res_Idx + Instance_SLOC_Txt'Length - 1) :=
                               Instance_SLOC_Txt;

                     Res_Idx := Res_Idx + Instance_SLOC_Txt'Length;

                     Diag_Start := Diag_End + Instance_SLOC_Txt'Length;

                     Inst_Count := Inst_Count - 1;
                  end loop;

                  Diag_End := Index (Diag (Diag_Start .. Diag_Last), ": ");

                  return Result (Result'First .. Res_Idx - 1) &
                         Diag (Diag_Start .. Diag_End)        &
                         ' ' & GPS_Prefix                     &
                         Diag (Diag_End + 1 .. Diag_Last);
               end;
            end if;
         else
            return Diag;
         end if;
      end Add_GPS_Prefix;

      procedure Counted_Print_Diagnosis
        (Position : Error_Messages_Storage.Cursor)
      is
      begin
         if not Limit_Exceeded then
            if Max_Diagnoses > 0 and then
               Diagnoses_Reported > Max_Diagnoses
            then
               Limit_Exceeded := True;
               Info ("Maximum diagnoses reached, " &
                     "see the report file for full details");
            else
               if Error_Messages_Storage.Element (Position).Justification =
                  null
               then
                  Diagnoses_Reported := Diagnoses_Reported + 1;
                  Info
                    (Add_GPS_Prefix
                      (Error_Messages_Storage.Element (Position).Text.all));
               end if;
            end if;
         end if;
      end Counted_Print_Diagnosis;

   begin
      Error_Messages_Storage.Iterate
        (Container => All_Error_Messages,
         Process   => Counted_Print_Diagnosis'Access);
   end Print_Out_Diagnoses;

   -------------------------
   -- Print_Report_Header --
   -------------------------

   procedure Print_Report_Header is
      Time_Of_Check   : constant Time := Clock;
      Month_Of_Check  : constant Month_Number := Month (Time_Of_Check);
      Day_Of_Check    : constant Day_Number   := Day (Time_Of_Check);
      Sec_Of_Check    : constant Day_Duration := Seconds (Time_Of_Check);

      Hour_Of_Chech   :          Integer range 0 .. 23;
      Minute_Of_Check :          Integer range 0 .. 59;
      Seconds_In_Hour : constant Integer := 60 * 60;

   begin
      Hour_Of_Chech   := Integer (Sec_Of_Check) / Seconds_In_Hour;
      Minute_Of_Check := (Integer (Sec_Of_Check) rem Seconds_In_Hour) / 60;

      if Text_Report_ON then
         Report ("GNATCheck report");
         Report_EOL;

         Report_No_EOL ("date              : ");
         Report_No_EOL (Trim (Year (Time_Of_Check)'Img, Left) & '-');

         if Month_Of_Check < 10 then
            Report_No_EOL ("0");
         end if;

         Report_No_EOL (Trim (Month_Of_Check'Img, Left) & '-');

         if Day_Of_Check < 10 then
            Report_No_EOL ("0");
         end if;

         Report_No_EOL (Trim (Day_Of_Check'Img, Left) & ' ');

         if Hour_Of_Chech < 10 then
            Report_No_EOL ("0");
         end if;

         Report_No_EOL (Trim (Hour_Of_Chech'Img, Left) & ':');

         if Minute_Of_Check < 10 then
            Report_No_EOL ("0");
         end if;

         Report        (Trim (Minute_Of_Check'Img, Left));

         Report_No_EOL ("gnatcheck version : ");
         Report_No_EOL (Tool_Name.all &  ' ');
         Report        (Gnat_Version_String);

         Report_No_EOL ("command line      : ");
         Print_Gnatcheck_Command_Line;

         Report_No_EOL ("runtime           : ");
         Print_Runtime;
      end if;

      if XML_Report_ON then
         XML_Report_No_EOL ("<date>" &
                            Trim (Year (Time_Of_Check)'Img, Left) & '-',
                            Indent_Level => 1);
         XML_Report_No_EOL (if Month_Of_Check < 10 then "0" else "");
         XML_Report_No_EOL (Trim (Month_Of_Check'Img, Left) & '-');
         XML_Report_No_EOL (if Day_Of_Check < 10 then "0" else "");
         XML_Report_No_EOL (Trim (Day_Of_Check'Img, Left) & ' ');
         XML_Report_No_EOL (if Hour_Of_Chech < 10 then "0" else "");
         XML_Report_No_EOL (Trim (Hour_Of_Chech'Img, Left) & ':');
         XML_Report_No_EOL (if Minute_Of_Check < 10 then "0" else "");
         XML_Report        (Trim (Minute_Of_Check'Img, Left) & "</date>");

         XML_Report ("<version>" & Tool_Name.all & ' ' &  Gnat_Version_String &
                     "</version>",
                     Indent_Level => 1);

         XML_Report_No_EOL ("<command-line>",
                            Indent_Level => 1);
         Print_Gnatcheck_Command_Line (XML => True);
         XML_Report        ("</command-line>");

         XML_Report_No_EOL ("<runtime>",
                            Indent_Level => 1);
         Print_Runtime (XML => True);
         XML_Report        ("</runtime>");
      end if;
   end Print_Report_Header;

   -------------------
   -- Print_Runtime --
   -------------------

   procedure Print_Runtime (XML : Boolean := False) is
   begin
      if ASIS_UL.Compiler_Options.Custom_RTS /= null then
         if XML then
            XML_Report (ASIS_UL.Compiler_Options.Custom_RTS.all);
         else
            Report (ASIS_UL.Compiler_Options.Custom_RTS.all);
         end if;
      else
         if XML then
            XML_Report_No_EOL ("default");
         else
            Report ("<default>");
         end if;
      end if;
   end Print_Runtime;

   -----------------------------
   -- Print_Violation_Summary --
   -----------------------------

   procedure Print_Violation_Summary is
   begin
      if Text_Report_ON then
         Report
           ("non-exempted violations               :" &
            Detected_Non_Exempted_Violations'Img, 1);

         Report
           ("rule exemption warnings               :" &
            Detected_Exemption_Warning'Img, 1);

         Report
           ("compilation errors                    :" &
           Detected_Compiler_Error'Img, 1);

         Report
           ("exempted violations                   :" &
            Detected_Exempted_Violations'Img, 1);
      end if;

      if XML_Report_ON then
         XML_Report ("<non-exempted-violations>"              &
                     Image (Detected_Non_Exempted_Violations) &
                     "</non-exempted-violations>",
                     Indent_Level => 1);

         XML_Report ("<rule-exemption-warnings>"     &
                     Image (Detected_Exemption_Warning) &
                     "</rule-exemption-warnings>",
                     Indent_Level => 1);

         XML_Report ("<compilation-errors>"     &
                     Image (Detected_Compiler_Error) &
                     "</compilation-errors>",
                     Indent_Level => 1);

         XML_Report ("<exempted-violations>"     &
                     Image (Detected_Exempted_Violations) &
                     "</exempted-violations>",
                     Indent_Level => 1);

         XML_Report ("</summary>");
      end if;

   end Print_Violation_Summary;

   ------------------------------
   -- Process_Exemption_Pragma --
   ------------------------------

   procedure Process_Exemption_Pragma (El : Asis.Element) is
      Pragma_Args : constant Asis.Element_List :=
        Pragma_Argument_Associations (El);

      First_Idx     : constant Natural := Pragma_Args'First;
      Next_Arg      :          Asis.Element;
      Tmp_Str       :          String_Access;
      First_Par_Idx :          Natural;
      Pars_Start    :          Natural;
      pragma Unreferenced (Pars_Start);
      Pars_End      :          Natural;
      Last_Par_Idx  :          Natural;
      Has_Param     :          Boolean := False;
      Exem_Span     :          Asis.Text.Span := Nil_Span;
      SF            : constant SF_Id := File_Find (El);

      Rule           : Rule_Id;
      Exemption_Kind : Exemption_Kinds;

      Exempted_At    : Parametrized_Exemption_Sections.Cursor;
      --  Used to check if the same rule with the same parameters is already
      --  exempted - set to No_Element if it is not the case or points to the
      --  record corresponding to the exemption section that exempts the same
      --  rule with the same parameters

      Par : String_Access;

      use Gnatcheck.Rules.Exemption_Parameters;

   begin
      --  We do not analyze exemption pragmas in instantiations - at the moment
      --  it is not clear how to define reasonable exemption policy for nested
      --  instantiations

      if Is_Part_Of_Instance (El) then
         return;
      end if;

      --  First, analyze the pragma format:
      --
      --  1. Check that we have at least three parameters

      if Pragma_Args'Length < 3 then
         Store_Diagnosis
           (Text           => Build_GNAT_Location (El) &
                              ": too few parameters for exemption, ignored",
            Diagnosis_Kind     => Exemption_Warning,
            SF                 => SF);

         return;
      end if;

      --  2. Second parameter should be either "Exempt_On" or "Exempt_Off"

      Next_Arg := Pragma_Args (First_Idx + 1);
      Next_Arg := Actual_Parameter (Next_Arg);

      if Expression_Kind (Next_Arg) = A_String_Literal then
         Exemption_Kind := Get_Exemption_Kind (Value_Image (Next_Arg));
      elsif Expression_Kind (Next_Arg) = An_Identifier then
         Exemption_Kind := Get_Exemption_Kind (Name_Image (Next_Arg));
      end if;

      if Exemption_Kind = Not_An_Exemption then
         Store_Diagnosis
           (Text               => Build_GNAT_Location (Next_Arg) &
                                  ": wrong exemption kind, ignored",
            Diagnosis_Kind     => Exemption_Warning,
            SF                 => SF);

         return;
      end if;

      --  3. Third parameter should be the name of some existing rule,
      --     may be, with parameter names, but the latter is allowed only if
      --     fine-tined exemptions is allowed for the rule, and if parameter
      --     names make sense for the given rule:

      Next_Arg := Pragma_Args (First_Idx + 2);
      Next_Arg := Actual_Parameter (Next_Arg);

      if Expression_Kind (Next_Arg) = A_String_Literal then
         Tmp_Str   := new String'(To_String (Value_Image (Next_Arg)));
         First_Par_Idx := Tmp_Str'First + 1;  --  skip '"'
         Last_Par_Idx  := Index (Tmp_Str.all, ":");

         if Last_Par_Idx = 0 then
            Last_Par_Idx := Tmp_Str'Last - 1; --  skip '"'
         else
            Last_Par_Idx := Last_Par_Idx - 1; --  skip ':'

            Has_Param    := True;
         end if;

         Rule := Get_Rule (Trim (Tmp_Str (First_Par_Idx .. Last_Par_Idx),
                                 Both));
      else
         Rule := No_Rule;
      end if;

      --  3.1 Check if we have an existing rule
      if not Present (Rule) then
         Store_Diagnosis
           (Text               => Build_GNAT_Location (Next_Arg) &
                                  ": wrong rule name in exemption, ignored",
            Diagnosis_Kind     => Exemption_Warning,
            SF                 => SF);

         Free (Tmp_Str);
         return;
      end if;

      --  3.2 Check if we have parameter(s) specified then parameter(s) make(s)
      --      sense for the rule.

      if Has_Param then

         if not Allows_Parametrized_Exemption (Rule) then
            Store_Diagnosis
              (Text               => Build_GNAT_Location (Next_Arg) &
                                     ": rule " & Rule_Name (Rule)   &
                                     " cannot have parametric "   &
                                     "exemption, ignored",
               Diagnosis_Kind     => Exemption_Warning,
               SF                 => SF);

            Free (Tmp_Str);
            return;
         end if;

         First_Par_Idx := Last_Par_Idx + 2;

         Pars_End := Tmp_Str'Last - 1;

         while First_Par_Idx < Pars_End
             and then
               Tmp_Str (First_Par_Idx) = ' '
         loop
            First_Par_Idx := First_Par_Idx + 1;
         end loop;

         Set_Rule_Exempt_Pars
           (Rule,
            Tmp_Str (First_Par_Idx .. Pars_End),
            SF,
            Build_GNAT_Location (Next_Arg));

         if Is_Empty (Rule_Exemption_Parameters) then
            Free (Tmp_Str);
            return;
         end if;

      end if;

      --  4. Fourth parameter, if present, should be a string.

      if Pragma_Args'Length >= 4 then
         Next_Arg := Pragma_Args (First_Idx + 3);
         Next_Arg := Actual_Parameter (Next_Arg);

         if Expression_Kind (Next_Arg) = A_String_Literal then
            Tmp_Str := new String'(To_String (Value_Image (Next_Arg)));
         end if;

         if Tmp_Str = null then
            Store_Diagnosis
              (Text               => Build_GNAT_Location (Next_Arg) &
                                     ": exemption justification "   &
                                     "should be a string",
               Diagnosis_Kind     => Exemption_Warning,
               SF                 => SF);
         end if;

         --  5. Fourth parameter is ignored if exemption is turned OFF

         if Exemption_Kind = Exempt_Off then
            Store_Diagnosis
              (Text               => Build_GNAT_Location (Next_Arg) &
                                     ": turning exemption OFF "     &
                                     "does not need justification",
               Diagnosis_Kind     => Exemption_Warning,
               SF                 => SF);
         end if;

      end if;

      --  6. If exemption is turned ON, justification is expected

      if Exemption_Kind = Exempt_On
        and then
         Pragma_Args'Length = 3
      then
         Store_Diagnosis
           (Text           => Build_GNAT_Location (El) &
                              ": turning exemption ON expects justification",
            Diagnosis_Kind     => Exemption_Warning,
            SF                 => SF);
      end if;

      if Pragma_Args'Length >= 5 then
         Next_Arg := Pragma_Args (First_Idx + 4);

         Store_Diagnosis
           (Text               => Build_GNAT_Location (Next_Arg) &
                                  ": rule exemption may have "   &
                                  " at most four parameters",
            Diagnosis_Kind     => Exemption_Warning,
            SF                 => SF);
      end if;

      --  If Rule does not denote the enabled rule - nothing to do

      if not (Is_Enabled (Rule)
             or else
              (Rule = Warnings_Id
               and then
               Is_Enabled (Restrictions_Id)))
      then
         --  In case when a Restriction rule is enabled, we may want to use
         --  exemptions section for Warnings rule to suppress default warnings.
         --  We may get rid of this if and when we get a possibility to turn
         --  off all the warnings except related to restrictions only.
         return;
      end if;

      --  Now - processing of the exemption pragma. If we are here, we are
      --  sure, that:
      --  - Rule denotes and existing and enabled rule;
      --  - if we are in an expanded instance, this rule should be checked on
      --    the expanded code
      --
      --  Exemptions for global rules are not implemented yet!
      --  Exemptions for local rules that should be checked on expanded
      --  instantiations are not fully implemented!

      Exem_Span := Element_Span (El);

      case Exemption_Kind is
         when Exempt_On =>

            if Tmp_Str = null then
               Tmp_Str := new String'("""unjustified""");
            end if;

            if Is_Exempted (Rule) then
               Store_Diagnosis
                 (Text => Build_GNAT_Location (El)       &
                          ": rule " & Rule_Name (Rule)   &
                          " is already exempted at line" &
                          Exemption_Sections (Rule).Line_Start'Img,
                  Diagnosis_Kind     => Exemption_Warning,
                  SF                 => SF);

               Free (Tmp_Str);
               return;
            end if;

            if not Has_Param
              and then
               Allows_Parametrized_Exemption (Rule)
            then
               --  Is Rule already exempted with parameters?
               if not Parametrized_Exemption_Sections.Is_Empty
                 (Rule_Param_Exempt_Sections (Rule))
               then
                  Store_Diagnosis
                    (Text => Build_GNAT_Location (El)                 &
                             ": rule " & Rule_Name (Rule)             &
                             " is already exempted with parameter(s)" &
                             " at line"                               &
                             Parametrized_Exemption_Sections.Element
                               (Parametrized_Exemption_Sections.First
                                 (Rule_Param_Exempt_Sections (Rule))).
                                   Exempt_Info.Line_Start'Img,
                     Diagnosis_Kind     => Exemption_Warning,
                     SF                 => SF);

                  Free (Tmp_Str);
                  return;
               end if;
            end if;

            if Has_Param then
               Is_Exempted_With_Pars (Rule, Exempted_At);

               if Parametrized_Exemption_Sections.Has_Element
                    (Exempted_At)
               then
                  Store_Diagnosis
                    (Text => Build_GNAT_Location (El)                        &
                             ": rule " & Rule_Name (Rule)                    &
                             " is already exempted with the same parameters" &
                             " at line" &
                             Parametrized_Exemption_Sections.Element
                               (Exempted_At).Exempt_Info.Line_Start'Img,
                     Diagnosis_Kind     => Exemption_Warning,
                     SF                 => SF);

                  Free (Tmp_Str);
                  return;
               else
                  --  We have to check the following:
                  --
                  --     Exempt_On, Rule:Par1, Par3
                  --     ...
                  --     Exempt_On, Rule:Par1, Par2

                  Same_Parameter_Exempted (Rule, Exempted_At, Par);

                  if Parametrized_Exemption_Sections.Has_Element
                       (Exempted_At)
                  then
                     Store_Diagnosis
                       (Text => Build_GNAT_Location (El)                &
                                ": rule " & Rule_Name (Rule)            &
                                " is already exempted with parameter '" &
                                Par.all & "' at line"                   &
                                Parametrized_Exemption_Sections.Element
                                  (Exempted_At).Exempt_Info.Line_Start'Img,
                        Diagnosis_Kind     => Exemption_Warning,
                        SF                 => SF);

                     Free (Tmp_Str);
                     Free (Par);
                     return;
                  end if;

                  --  If we are here then we know for sure that the
                  --  parametric exemption is correct, and there is no
                  --  open exemption section for this rule and this
                  --  parameter(s). So we can just add the corresponding record
                  --  to Rule_Param_Exempt_Sections:

                  Parametrized_Exemption_Sections.Insert
                    (Rule_Param_Exempt_Sections (Rule),
                    (Exempt_Info =>
                       (Line_Start    => Exem_Span.First_Line,
                        Col_Start     => Exem_Span.First_Column,
                        Line_End      => 0,
                        Col_End       => 0,
                        Justification => new
                          String'((Tmp_Str
                                    (Tmp_Str'First + 1 .. Tmp_Str'Last - 1))),
                        Detected      => 0),
                     Rule        => Rule,
                     SF          => SF,
                     Params      => To_Pars_List (Rule_Exemption_Parameters)));
               end if;
            else
               Exemption_Sections (Rule) :=
                 (Line_Start    => Exem_Span.First_Line,
                  Col_Start     => Exem_Span.First_Column,
                  Line_End      => 0,
                  Col_End       => 0,
                  Justification => new String'((Tmp_Str
                                     (Tmp_Str'First + 1 .. Tmp_Str'Last - 1))),
                  Detected      => 0);
            end if;

            Free (Tmp_Str);

         when Exempt_Off =>

            if Has_Param then
               Is_Exempted_With_Pars (Rule, Exempted_At);

               if not Parametrized_Exemption_Sections.Has_Element
                    (Exempted_At)
               then
                  Store_Diagnosis
                    (Text           => Build_GNAT_Location (El)     &
                                       ": rule " & Rule_Name (Rule) &
                                       " is not in exempted state",
                     Diagnosis_Kind => Exemption_Warning,
                     SF             => SF);

                  return;
               else
                  Turn_Off_Parametrized_Exemption
                    (Rule, Exempted_At, Exem_Span, SF);
               end if;
            else
               if not Is_Exempted (Rule) then
                  Store_Diagnosis
                    (Text           => Build_GNAT_Location (El)     &
                                       ": rule " & Rule_Name (Rule) &
                                       " is not in exempted state",
                     Diagnosis_Kind => Exemption_Warning,
                     SF             => SF);

                  return;
               end if;

               Turn_Off_Exemption (Rule, Exem_Span, SF);
            end if;

         when Not_An_Exemption =>
            pragma Assert (False);
            null;
      end case;

   end Process_Exemption_Pragma;

   ----------------------------------
   -- Process_Postponed_Exemptions --
   ----------------------------------

   procedure Process_Postponed_Exemptions is

      Next_Postponed_Section  : Postponed_Rule_Exemption_Info_Access;
      Next_Post_Param_Section : Parametrized_Exemption_Sections.Cursor;
      use Parametrized_Exemption_Sections;

      Next_Par_S_Info : Parametrized_Exemption_Info;

      procedure Map_Diagnosis (Position : Error_Messages_Storage.Cursor);
      --  Maps the diagnosis pointed by the argument onto stored information
      --  about exemption sections. If the diagnosis points to some place
      --  inside some exemption section, and the diagnosis is not exempted,
      --  then the diagnosis is exempted by adding the justification from the
      --  exemption section, and the corresponding exemption violation is
      --  counted for the given exemption section
      --
      --  At the moment this kind of diagnoses post-processing is implemented
      --  for compiler checks only.

      procedure Map_Diagnosis (Position : Error_Messages_Storage.Cursor) is
         Diag : Diag_Message := Error_Messages_Storage.Element (Position);
         Diag_Line   : Positive;
         Diag_Column : Positive;
         SF          : SF_Id;
         Is_Exempted : Boolean;
      begin
         if Diag.Diagnosis_Kind = Rule_Violation
           and then
             Needs_Postponed_Exemption_Processing (Diag.Rule)
         then
            if Diag.Justification /= null then
               --  some diagnoses may be already exempted as a part of the
               --  regular processing
               return;
            end if;

            SF := Get_Original_SF (Diag);

            if not Present (SF) then
               --  This is the case when the diagnosis is generated for
               --  expanded generic, and the generic itself is not the
               --  gnatcheck argument
               return;
            end if;

            --  First, check for non-parametric exemptions:

            Diag_Line := Select_Line (Diag.Text);

            Map_On_Postponed_Check_Exemption
              (In_File     => SF,
               For_Check   => Diag.Rule,
               For_Line    => Diag_Line,
               Is_Exempted => Is_Exempted);

            if Is_Exempted then
               Add_Exempted_Violation (Diag.Rule);
               Diag.Justification := Get_Justification (Diag.Rule);
            end if;

            --  Check for parametric exemptions

            if not Is_Exempted
              and then
               Allows_Parametrized_Exemption (Diag.Rule)
              and then
               not Parametrized_Exemption_Sections.Is_Empty
                     (Postponed_Param_Exempt_Sections (Diag.Rule) (SF))
            then
               Diag_Column := Select_Column (Diag.Text);
               Diag.Justification :=
                 Get_Param_Justification
                   (Rule                       => Diag.Rule,
                    Diag                       => Diag.Text.all,
                    SF                         => SF,
                    Line                       => Diag_Line,
                    Col                        => Diag_Column,
                    Check_Postponed_Exemptions => True);
            end if;

            if Diag.Justification /= null then
               Error_Messages_Storage.Replace_Element
                 (Container => All_Error_Messages,
                  Position  => Position,
                  New_Item  => Diag);
            end if;
         end if;
      end Map_Diagnosis;

   --  Start of processing for Process_Postponed_Exemptions

   begin
      --  !!!??? Still does not work on rules checked on expanded generic!!!

      Error_Messages_Storage.Iterate
        (Container => All_Error_Messages,
         Process   => Map_Diagnosis'Access);

      --  Now, iterate through the stored exemption and generate exemption
      --  warnings for those of them for which no exempted diagnoses are found.

      for Rule in First_Compiler_Check .. All_Rules.Last loop
         if Needs_Postponed_Exemption_Processing (Rule) then
            for SF in First_SF_Id .. Last_Argument_Source loop

               --  Non-parametric exemptions:
               Next_Postponed_Section :=
                 Postponed_Exemption_Sections (Rule) (SF);

               while Next_Postponed_Section /= null loop

                  if Next_Postponed_Section.Exemption_Section.Detected = 0 then
                     Store_Diagnosis
                       (Text => Short_Source_Name (SF) & ':' &
                                Image
                                  (Next_Postponed_Section.Exemption_Section.
                                     Line_End)
                                & ':' &
                                Image
                                  (Next_Postponed_Section.Exemption_Section.
                                     Col_End) &
                                ": no detection for "                         &
                                Rule_Name (Rule)                              &
                                " rule in exemption section starting at line" &
                                Next_Postponed_Section.Exemption_Section.
                                  Line_Start'Img,

                        Diagnosis_Kind => Exemption_Warning,
                        SF             => SF);
                  end if;

                  Next_Postponed_Section :=
                    Next_Postponed_Section.Next_Exemption_Section;
               end loop;

            --  Parametric exemptions:
               if Allows_Parametrized_Exemption (Rule)
                 and then
                  not Is_Empty (Postponed_Param_Exempt_Sections (Rule) (SF))
               then
                  Next_Post_Param_Section :=
                    First (Postponed_Param_Exempt_Sections (Rule) (SF));

                  while Has_Element (Next_Post_Param_Section) loop
                     Next_Par_S_Info :=
                       Parametrized_Exemption_Sections.Element
                         (Next_Post_Param_Section);

                     if Next_Par_S_Info.Exempt_Info.Detected = 0 then

                        Store_Diagnosis
                          (Text => Short_Source_Name (SF) & ':'              &
                                   Image (Next_Par_S_Info.Exempt_Info.Line_End)
                                   & ':' &
                                   Image (Next_Par_S_Info.Exempt_Info.Col_End)
                                   & ": no detection for '"                  &
                                   Rule_Name (Rule) & ": "                   &
                                   Params_Img (Next_Par_S_Info.Params, Rule) &
                                   "' rule in exemption section starting "   &
                                   "at line"                                 &
                                   Next_Par_S_Info.Exempt_Info.Line_Start'Img,
                           Diagnosis_Kind => Exemption_Warning,
                           SF             => SF);

                     end if;

                     Next_Post_Param_Section := Next (Next_Post_Param_Section);
                  end loop;
               end if;

            end loop;
         end if;

      end loop;

      --  Parametric exemptions:

   end Process_Postponed_Exemptions;

   -------------------------
   -- Rule_List_File_Name --
   -------------------------

   function Rule_List_File_Name return String is
      Dot_Idx : constant Natural :=
        Index (Rule_List_File_Name_Str, ".", Forward);
   begin
      if not Aggregated_Project then
         return Rule_List_File_Name_Str;
      end if;

      pragma Assert (Dot_Idx > 0);

      return Rule_List_File_Name_Str
               (Rule_List_File_Name_Str'First .. Dot_Idx - 1) &
             Number.all                                         &
             Rule_List_File_Name_Str
               (Dot_Idx .. Rule_List_File_Name_Str'Last);

   end Rule_List_File_Name;

   --------------------
   -- Rule_Parameter --
   --------------------

   function Rule_Parameter (Diag : String; Rule : Rule_Id) return String is
   begin
      case Rule is
         when Restrictions_Id =>
            return Restriction_Rule_Parameter (Diag);
         when Style_Checks_Id =>
            return "";   --  ??? NOT IMPLEMENTED YET!!!!!!!
         when Warnings_Id     =>
            return Warning_Rule_Parameter (Diag);
         when others =>
            return Rule_Parameter (All_Rules.Table (Rule).all, Diag);
      end case;
   end Rule_Parameter;

   -------------------------------------------------
   -- Rule_Parametrized_Exem_Sections_Debug_Image --
   -------------------------------------------------

   procedure Rule_Parametrized_Exem_Sections_Debug_Image (Rule : Rule_Id) is
   begin
      Info ("** PARAMETRIC EXEMPTION SECTIONS FOR RULE" & Rule'Img &
            " (" & Rule_Name (Rule) & ") **");

      if Parametrized_Exemption_Sections.Is_Empty
        (Rule_Param_Exempt_Sections (Rule))
      then
         Info ("   No parametric exemption sections");
      else
         Parametrized_Exemption_Sections.Iterate
           (Container => Rule_Param_Exempt_Sections (Rule),
            Process   => Parametrized_Exem_Section_Debug_Image'Access);
      end if;

   end Rule_Parametrized_Exem_Sections_Debug_Image;

   -----------------------------
   -- Same_Parameter_Exempted --
   -----------------------------

   procedure Same_Parameter_Exempted
     (Rule        :     Rule_Id;
      Exempted_At : out Parametrized_Exemption_Sections.Cursor;
      Par         : out String_Access)
   is
      Next_Par : Exemption_Parameters.Cursor :=
        Exemption_Parameters.First (Rule_Exemption_Parameters);

      use Parametrized_Exemption_Sections;
      Next_Section : Cursor;
   begin
      Free (Par);

      Exempted_At := No_Element;

      if not Is_Empty (Rule_Param_Exempt_Sections (Rule)) then

         while Exemption_Parameters.Has_Element (Next_Par) loop

            Next_Section := First (Rule_Param_Exempt_Sections (Rule));

            while Has_Element (Next_Section) loop
               if Belongs
                    (Exemption_Parameters.Element (Next_Par),
                     Parametrized_Exemption_Sections.Element
                       (Next_Section).Params)
               then
                  Exempted_At := Next_Section;
                  Par         :=
                    new String'(Exemption_Parameters.Element (Next_Par));
                  return;
               end if;

               Next_Section := Next (Next_Section);
            end loop;

            Next_Par := Exemption_Parameters.Next (Next_Par);
         end loop;
      end if;
   end Same_Parameter_Exempted;

   -------------------
   -- Select_Column --
   -------------------

   function Select_Column (Diag : String_Access) return Positive is
      Start_Idx : Natural;
      End_Idx   : Natural;
   begin
      Start_Idx := Index (Diag.all, ":") + 1;
      Start_Idx := Index (Diag (Start_Idx .. Diag'Last), ":") + 1;
      End_Idx   := Index (Diag (Start_Idx .. Diag'Last), ":") - 1;

      return Positive'Value (Diag (Start_Idx .. End_Idx));
   end Select_Column;

   -----------------
   -- Select_Line --
   -----------------

   function Select_Line (Diag : String_Access) return Positive is
      Start_Idx : Natural;
      End_Idx   : Natural;
   begin

      Start_Idx := Index (Diag.all, ":");

      if Start_Idx = Diag'First + 1
        and then
         Diag (Start_Idx + 1) = '\'
      then
         Start_Idx := Index (Diag (Start_Idx + 2 .. Diag'Last), ":");
      end if;

      Start_Idx := Start_Idx + 1;

      End_Idx   := Index (Diag (Start_Idx .. Diag'Last), ":") - 1;

      return Positive'Value (Diag (Start_Idx .. End_Idx));
   end Select_Line;

   --------------------------
   -- Set_Rule_Exempt_Pars --
   --------------------------

   procedure Set_Rule_Exempt_Pars
     (Rule : Rule_Id;
      Pars : String;
      SF   : SF_Id;
      SLOC : String)
   is
      use Gnatcheck.Rules.Exemption_Parameters;

      Par_Start   :          Natural := Pars'First;
      Par_End     :          Natural;
      Last_Idx    : constant Natural    := Pars'Last;
      Position    :          Cursor;
      Success     :          Boolean;
      Warning_Par : constant Boolean := Rule = Warnings_Id;
      --  In case of Warnings rule, we consider parameters one by one. That is,
      --  for "ad.c.d.fg" as Pars string we separately store 'a', 'd', '.c',
      --  '.d', '.f' and 'g'

   begin
      Clear (Rule_Exemption_Parameters);

      loop
         while Par_Start <= Last_Idx and then Pars (Par_Start) = ' ' loop
            Par_Start := Par_Start + 1;
         end loop;

         exit when Par_Start > Last_Idx;

         if Warning_Par then
            Par_End := (if Pars (Par_Start) = '.' then
                           Par_Start + 1
                        else
                           Par_Start);
         else
            Par_End := Index (Pars (Par_Start + 1 .. Last_Idx), (1 => ','));
         end if;

         if Par_End = 0 then
            Par_End := Last_Idx;
         elsif not Warning_Par then
            Par_End := Par_End - 1;
         end if;

         while Pars (Par_End) = ' ' loop
            Par_End := Par_End - 1;
         end loop;

         if Allowed_As_Exemption_Parameter
              (Rule,
               (if Rule = Warnings_Id then
                   Remove_Spaces (Pars (Par_Start .. Par_End))
                else
                   To_Lower (Remove_Spaces (Pars (Par_Start .. Par_End)))))
         then
            Insert (Container => Rule_Exemption_Parameters,
                    New_Item  =>
                      (if Rule = Warnings_Id then
                          Remove_Spaces (Pars (Par_Start .. Par_End))
                       else
                          To_Lower (Remove_Spaces
                                      (Pars (Par_Start .. Par_End)))),
                    Position  => Position,
                    Inserted  => Success);

            if not Success then
               Store_Diagnosis
                 (Text               => SLOC &
                                        ": parameter "   &
                                        Pars (Par_Start .. Par_End) &
                                        " duplicated in rule exemption",
                  Diagnosis_Kind     => Exemption_Warning,
                  SF                 => SF);
            end if;

         else
            Store_Diagnosis
              (Text               => SLOC &
                                     ": parameter "   &
                                     Pars (Par_Start .. Par_End) &
                                     " is not allowed in exemption for rule " &
                                     Rule_Name (Rule),
               Diagnosis_Kind     => Exemption_Warning,
               SF                 => SF);

            Clear (Rule_Exemption_Parameters);
         end if;

         if Warning_Par then
            Par_Start := Par_Start + 1;
            exit when Par_Start > Last_Idx;
         else
            Par_Start := Index (Pars (Par_End + 1 .. Last_Idx), ",");

            if Par_Start = 0 then
               exit;
            else
               Par_Start := Par_Start + 1;
            end if;

         end if;

      end loop;

   end Set_Rule_Exempt_Pars;

   ---------------------
   -- Store_Diagnosis --
   ---------------------

   procedure Store_Diagnosis
     (Text           : String;
      Diagnosis_Kind : Diagnosis_Kinds;
      SF             : SF_Id;
      Rule           : Rule_Id        := No_Rule;
      Justification  : String_Access  := null)
   is
      Tmp : Diag_Message :=
        (Text           => new String'(Text),
         Justification  => Justification,
         Diagnosis_Kind => Diagnosis_Kind,
         Rule           => Rule,
         SF             => SF,
         Num            => Next_Message_Num);

      use Parametrized_Exemption_Sections;
   begin
      --  We need this check to avoid diagnoses duplication. Our set container
      --  has broken "<" relation, so Insert may add diagnoses that are already
      --  stored in the container (see the documentation for "<" for more
      --  details.

      if not Error_Messages_Storage.Contains
               (Container => All_Error_Messages,
                Item      => Tmp)
      then
         if Justification /= null then
            --  Here we count detections for non-parametric exemption
            --  sections only
            Exemption_Sections (Rule).Detected :=
               Exemption_Sections (Rule).Detected + 1;
         end if;

         if Rule /= No_Rule
         --  To skip compiler errors and exemption warnings
           and then
            Justification = null
           and then
            Allows_Parametrized_Exemption (Rule)
           and then
            not Needs_Postponed_Exemption_Processing (Rule)
           and then
            not Is_Empty (Rule_Param_Exempt_Sections (Rule))
         then
            Tmp.Justification :=
              Get_Param_Justification (Rule, Text, SF,
                                       Check_Postponed_Exemptions => False);
            --  This call counts detections for parametric exemption sections
         end if;

         Error_Messages_Storage.Insert
           (Container => All_Error_Messages,
            New_Item  => Tmp,
            Position  => Unused_Position,
            Inserted  => Unused_Inserted);
      else
         Free (Tmp.Text);
      end if;

   end Store_Diagnosis;

   --------------------------
   -- Store_Error_Messages --
   --------------------------

   procedure Store_Error_Messages
     (Compiler_Out_Fname : String;
      SF                 : SF_Id)
   is
      Comp_Out_File : Ada.Text_IO.File_Type;
      Line_Buffer   : String (1 .. 16 * 1024);
      Line_Len      : Natural := 0;

      Is_Error_Message    : Boolean;
      Error_Message_Found : Boolean := False;
   begin
      pragma Assert (Source_Status (SF) = Not_A_Legal_Source);

      if not Is_Regular_File (Compiler_Out_Fname) then
         Error ("no compiler message file found for " & Source_Name (SF));
         return;
      end if;

      Open
        (File => Comp_Out_File,
         Mode => In_File,
         Name => Compiler_Out_Fname);

      while not End_Of_File (Comp_Out_File) loop
         Get_Line
           (File => Comp_Out_File,
            Item => Line_Buffer,
            Last => Line_Len);

         Is_Error_Message :=
           Index
             (Source  => Line_Buffer (1 .. Line_Len),
              Pattern => "(style)") = 0;

         if Is_Error_Message then
            Is_Error_Message :=
              Index
                (Source  => Line_Buffer (1 .. Line_Len),
                 Pattern => ": warning:") = 0;
         end if;

         if Is_Error_Message then
            Error_Message_Found := True;

            if Index (Source  => Line_Buffer (1 .. Line_Len),
                      Pattern => "BUG DETECTED") /= 0
            then
               --  If there is a bug box, we should skip the rest of
               --  processing to avoid storing some completely unmanageable
               --  (for diagnoses storage) diagnoses
               exit;
            end if;

            if Index (Source  => Line_Buffer (1 .. Line_Len),
                      Pattern => ":") /= 0
            then
               --  We should not store very general messages that do not
               --  contain SLOCs (such as "compilation abandoned", otherwise
               --  we get problems when messages are sorted.
               Store_Diagnosis
                 (Text               => Line_Buffer (1 .. Line_Len),
                  Diagnosis_Kind     => Compiler_Error,
                  SF                 => SF);
            end if;
         end if;

      end loop;

      if not Error_Message_Found then
         Store_Diagnosis
           (Text               => Short_Source_Name (SF) &
                                  ":1:1: cannot be compiled by unknown reason",
            Diagnosis_Kind     => Compiler_Error,
            SF                 => SF);
      end if;

      Close (Comp_Out_File);
   end Store_Error_Messages;

   ------------------
   -- To_Pars_List --
   ------------------

   function To_Pars_List
     (Pars : Exemption_Parameters.Set)
      return String_List_Access
   is
      use Exemption_Parameters;

      Result : constant String_List_Access := new
        String_List (1 .. Natural (Length (Pars)));
      Next_Par : Cursor := First (Pars);
   begin
      for J in Result'Range loop
         Result (J) :=
           new String'(Exemption_Parameters.Element (Next_Par));
         Next_Par := Next (Next_Par);
      end loop;

      return Result;
   end To_Pars_List;

   ---------------------------
   -- Source_List_File_Name --
   ---------------------------

   function Source_List_File_Name return String is
      Dot_Idx : constant Natural :=
        Index (Source_List_File_Name_Str, ".", Forward);
   begin
      if not Aggregated_Project then
         return Source_List_File_Name_Str;
      end if;

      pragma Assert (Dot_Idx > 0);

      return Source_List_File_Name_Str
               (Source_List_File_Name_Str'First .. Dot_Idx - 1) &
             Number.all                                         &
             Source_List_File_Name_Str
               (Dot_Idx .. Source_List_File_Name_Str'Last);

   end Source_List_File_Name;

   ------------------------
   -- Turn_Off_Exemption --
   ------------------------

   procedure Turn_Off_Exemption
     (Rule         : Rule_Id;
      Closing_Span : Asis.Text.Span;
      SF           : SF_Id)
   is
      Tmp : Postponed_Rule_Exemption_Info_Access;
   begin
      --  Special processing for global rules, rules checked on expanded
      --  generics and compiler checks is not implemented yet

      if Needs_Postponed_Exemption_Processing (Rule) then
         --  Store compiler check exemption section
         Exemption_Sections (Rule).Line_End := Closing_Span.Last_Line;
         Exemption_Sections (Rule).Col_End  := Closing_Span.Last_Column;

         Tmp := new Postponed_Rule_Exemption_Info'
                      (Exemption_Section      => Exemption_Sections (Rule),
                       SF                     => SF,
                       Next_Exemption_Section => null);

         if Postponed_Exemption_Sections (Rule) (SF) = null then
            Postponed_Exemption_Sections (Rule) (SF) := Tmp;
         else
            Current_Postponed_Exemption_Sections (Rule).
              Next_Exemption_Section := Tmp;
         end if;

         Current_Postponed_Exemption_Sections (Rule) := Tmp;
      end if;

      if Exemption_Sections (Rule).Detected = 0
        and then
         not (Needs_Postponed_Exemption_Processing (Rule))
      then
         --  No one needs Justification
         Free (Exemption_Sections (Rule).Justification);

         Store_Diagnosis
           (Text           => Short_Source_Name (SF) & ':'                  &
                              Image (Closing_Span.Last_Line) & ':'          &
                              Image (Closing_Span.Last_Column)              &
                              ": no detection for "                         &
                              Rule_Name (Rule)                              &
                              " rule in exemption section starting at line" &
                               Exemption_Sections (Rule).Line_Start'Img,
            Diagnosis_Kind     => Exemption_Warning,
            SF                 => SF);

      end if;

      Exemption_Sections (Rule).Line_Start    := 0;
      Exemption_Sections (Rule).Col_Start     := 0;
      Exemption_Sections (Rule).Line_End      := 0;
      Exemption_Sections (Rule).Col_End       := 0;
      Exemption_Sections (Rule).Justification := null;
      Exemption_Sections (Rule).Detected      := 0;

   end Turn_Off_Exemption;

   -------------------------------------
   -- Turn_Off_Parametrized_Exemption --
   -------------------------------------

   procedure Turn_Off_Parametrized_Exemption
     (Rule         :         Rule_Id;
      Exempted_At  : in out Parametrized_Exemption_Sections.Cursor;
      Closing_Span :         Asis.Text.Span;
      SF           :         SF_Id)
   is
      Tmp         : String_Access;
      New_Section : Parametrized_Exemption_Info;
   begin
      if Needs_Postponed_Exemption_Processing (Rule) then
         New_Section := Parametrized_Exemption_Sections.Element (Exempted_At);

         New_Section.Exempt_Info.Line_End := Closing_Span.Last_Line;
         New_Section.Exempt_Info.Col_End  := Closing_Span.Last_Column;

         Parametrized_Exemption_Sections.Insert
           (Container => Postponed_Param_Exempt_Sections (Rule) (SF),
            New_Item  => New_Section);
         null;
         --  !!!NOT IMPLEMENTED YET!!! @@  ???
      else
         if Parametrized_Exemption_Sections.Element
                 (Exempted_At).Exempt_Info.Detected = 0
         then
            --  No one needs Justification
            Tmp := Parametrized_Exemption_Sections.Element
                    (Exempted_At).Exempt_Info.Justification;

            Free (Tmp); --  ???
            --  Does it really free the memory?

            Store_Diagnosis
              (Text => Short_Source_Name (SF) & ':'                   &
                       Image (Closing_Span.Last_Line) & ':'           &
                       Image (Closing_Span.Last_Column)               &
                       ": no detection for '"                         &
                       Rule_Name (Rule) & ": "                        &
                       Params_Img
                         (Parametrized_Exemption_Sections.Element
                           (Exempted_At).Params, Rule)                &
                       "' rule in exemption section starting at line" &
                       Parametrized_Exemption_Sections.Element
                         (Exempted_At).Exempt_Info.Line_Start'Img,
               Diagnosis_Kind => Exemption_Warning,
               SF             => SF);

         end if;

      end if;

      Parametrized_Exemption_Sections.Delete
        (Rule_Param_Exempt_Sections (Rule), Exempted_At);
   end Turn_Off_Parametrized_Exemption;

   --------------------------
   -- XML_Report_Diagnosis --
   --------------------------

   procedure XML_Report_Diagnosis
     (Diag         : Diag_Message;
      Short_Report : Boolean)
   is
      Indentation : constant Natural := (if Short_Report then 1 else 2);
      Exempted    : constant Boolean :=
        Diag.Diagnosis_Kind = Rule_Violation and then
        Diag.Justification /= null;
      Message     : constant String  := Diag.Text.all;
      L_Idx       :          Natural := Message'First;
      R_Idx       :          Natural := Index (Message, ":");
      Last_Idx    : constant Natural := Message'Last;
   begin
      XML_Report_No_EOL
        ((if Diag.Diagnosis_Kind = Exemption_Warning then
             "<exemption-problem"
          elsif Diag.Diagnosis_Kind = Compiler_Error then
             "<compiler-error"
          elsif Exempted then
             "<exempted-violation"
          else "<violation"),
         Indent_Level => Indentation);

      XML_Report_No_EOL
        (" file=" & """" & Message (L_Idx .. R_Idx - 1) & """ ");

      L_Idx := R_Idx + 1;
      R_Idx := Index (Message (L_Idx .. Last_Idx), ":");

      XML_Report_No_EOL
        ("line=" & """" & Message (L_Idx .. R_Idx - 1) & """ ");

      L_Idx := R_Idx + 1;
      R_Idx := Index (Message (L_Idx .. Last_Idx), ":");

      while Message (L_Idx) = '0' loop
         L_Idx := L_Idx + 1;
      end loop;

      XML_Report_No_EOL
        ("column=" & """" & Message (L_Idx .. R_Idx - 1) & """");

      if Diag.Diagnosis_Kind = Rule_Violation then
         XML_Report_No_EOL (" rule-id=""" & Rule_Name (Diag.Rule) & '"');
      end if;

      XML_Report (">");

      XML_Report
        ("<message>" & Message (R_Idx + 2 .. Last_Idx) & "</message>",
         Indent_Level => Indentation + 1);

      if Exempted then
         XML_Report
           ("<justification>" & Diag.Justification.all & "</justification>",
            Indent_Level => Indentation + 1);
      end if;

      XML_Report
        ((if Diag.Diagnosis_Kind = Exemption_Warning then
             "</exemption-problem>"
          elsif Diag.Diagnosis_Kind = Compiler_Error then
             "</compiler-error>"
          elsif Exempted then
             "</exempted-violation>"
          else "</violation>"),
         Indent_Level => Indentation);

   end XML_Report_Diagnosis;

end Gnatcheck.Diagnoses;