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 | ------------------------------------------------------------------------------
-- --
-- GNATCHECK COMPONENTS --
-- --
-- G N A T C H E C K . R U L E S . C U S T O M _ 3 --
-- --
-- B o d y --
-- --
-- Copyright (C) 2016-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.Characters.Handling; use Ada.Characters.Handling;
with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Wide_Fixed; use Ada.Strings.Wide_Fixed;
with Ada.Wide_Characters.Handling; use Ada.Wide_Characters.Handling;
with Asis; use Asis;
with Asis.Clauses; use Asis.Clauses;
with Asis.Compilation_Units; use Asis.Compilation_Units;
with Asis.Declarations; use Asis.Declarations;
with Asis.Definitions; use Asis.Definitions;
with Asis.Elements; use Asis.Elements;
with Asis.Expressions; use Asis.Expressions;
with Asis.Extensions; use Asis.Extensions;
with Asis.Extensions.Flat_Kinds; use Asis.Extensions.Flat_Kinds;
with Asis.Statements; use Asis.Statements;
with Asis.Text; use Asis.Text;
with ASIS_UL.Misc;
with ASIS_UL.Utilities; use ASIS_UL.Utilities;
with Gnatcheck.ASIS_Utilities; use Gnatcheck.ASIS_Utilities;
with Gnatcheck.Rules.Traversing; use Gnatcheck.Rules.Traversing;
with Gnatcheck.Traversal_Stack; use Gnatcheck.Traversal_Stack;
package body Gnatcheck.Rules.Custom_3 is
----------------------------
-- Binary_Case_Statements --
----------------------------
----------------------------------------
-- Init_Rule (Binary_Case_Statements) --
----------------------------------------
procedure Init_Rule (Rule : in out Binary_Case_Statements_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Binary_Case_Statements");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("CASE statements that can be " &
"replaced with IF statements");
Rule.Diagnosis := new String'("CASE statement can be replaced with " &
"IF statement");
end Init_Rule;
------------------------------------------------
-- Rule_Check_Pre_Op (Binary_Case_Statements) --
------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Binary_Case_Statements_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Statement_Kind (Element) = A_Case_Statement then
declare
Case_Paths : constant Asis.Element_List :=
Statement_Paths (Element);
First_Idx : constant List_Index := Case_Paths'First;
begin
if Case_Paths'Length = 2
and then
Case_Path_Alternative_Choices
(Case_Paths (First_Idx))'Length = 1
and then
Case_Path_Alternative_Choices
(Case_Paths (First_Idx + 1))'Length = 1
then
State.Detected := True;
end if;
end;
end if;
end Rule_Check_Pre_Op;
------------------
-- Constructors --
------------------
------------------------------
-- Init_Rule (Constructors) --
------------------------------
procedure Init_Rule (Rule : in out Constructors_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Constructors");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("constructors");
Rule.Diagnosis := new String'("declaration of constructor function");
end Init_Rule;
--------------------------------------
-- Rule_Check_Pre_Op (Constructors) --
--------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Constructors_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
State.Detected := Is_Constructor (Element);
end Rule_Check_Pre_Op;
----------------------------
-- Deep_Library_Hierarchy --
----------------------------
----------------------------------------
-- Init_Rule (Deep_Library_Hierarchy) --
----------------------------------------
procedure Init_Rule (Rule : in out Deep_Library_Hierarchy_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Deep_Library_Hierarchy");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("deep library hierarchy");
Rule.Diagnosis := new String'("unit has too many ancestors (%1%)");
end Init_Rule;
------------------------------------------------
-- Rule_Check_Pre_Op (Deep_Library_Hierarchy) --
------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Deep_Library_Hierarchy_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Def_Name : Asis.Element;
Ansestors : Natural := 0;
pragma Unreferenced (Control);
begin
if Declaration_Kind (Element) in
A_Package_Declaration |
A_Generic_Package_Declaration |
A_Package_Instantiation
and then
Is_Nil (Get_Enclosing_Element)
then
Def_Name := First_Name (Element);
declare
Def_Name_Img : constant Program_Text :=
Defining_Name_Image (Def_Name);
begin
for J in Def_Name_Img'Range loop
if Def_Name_Img (J) = '.' then
Ansestors := Ansestors + 1;
end if;
end loop;
end;
if Ansestors > Rule.Rule_Limit then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" & ASIS_UL.Misc.Image (Ansestors));
end if;
end if;
end Rule_Check_Pre_Op;
------------------------------------------
-- Default_Values_For_Record_Components --
------------------------------------------
------------------------------------------------------
-- Init_Rule (Default_Values_For_Record_Components) --
------------------------------------------------------
procedure
Init_Rule (Rule : in out Default_Values_For_Record_Components_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Default_Values_For_Record_Components");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("default values for record components");
Rule.Diagnosis := new String'("default value for record component");
end Init_Rule;
--------------------------------------------------------------
-- Rule_Check_Pre_Op (Default_Values_For_Record_Components) --
--------------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Default_Values_For_Record_Components_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Declaration_Kind (Element) = A_Component_Declaration
and then
Definition_Kind (Get_Enclosing_Element) /= A_Protected_Definition
and then
not Is_Nil (Initialization_Expression (Element))
then
State.Detected := True;
end if;
end Rule_Check_Pre_Op;
-----------------------------------
-- Deriving_From_Predefined_Type --
-----------------------------------
-----------------------------------------------
-- Init_Rule (Deriving_From_Predefined_Type) --
-----------------------------------------------
procedure
Init_Rule (Rule : in out Deriving_From_Predefined_Type_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Deriving_From_Predefined_Type");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("types derived from predefined types");
Rule.Diagnosis := new String'("deriving from predefined type");
Rule.Check_In_Expanded_Generics := True;
end Init_Rule;
-------------------------------------------------------
-- Rule_Check_Pre_Op (Deriving_From_Predefined_Type) --
-------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Deriving_From_Predefined_Type_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
Ancestor : Asis.Element;
begin
if Declaration_Kind (Element) = An_Ordinary_Type_Declaration then
Ancestor := Type_Declaration_View (Element);
if Type_Kind (Ancestor) = A_Derived_Type_Definition then
Ancestor := Corresponding_Root_Type (Ancestor);
Ancestor := Unwind_Type (Ancestor);
pragma Assert (not Is_Nil (Ancestor));
if Unit_Origin (Enclosing_Compilation_Unit (Ancestor)) =
A_Predefined_Unit
then
State.Detected := True;
end if;
end if;
end if;
end Rule_Check_Pre_Op;
-------------------------------
-- Downward_View_Conversions --
-------------------------------
-------------------------------------------
-- Init_Rule (Downward_View_Conversions) --
-------------------------------------------
procedure Init_Rule (Rule : in out Downward_View_Conversions_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Downward_View_Conversions");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("downward view conversions");
Rule.Diagnosis := new String'("downward view conversion");
end Init_Rule;
---------------------------------------------------
-- Rule_Check_Pre_Op (Downward_View_Conversions) --
---------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Downward_View_Conversions_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
State.Detected := Is_Downward_View_Conversion (Element);
end Rule_Check_Pre_Op;
----------------------------------------
-- Enumeration_Representation_Clauses --
----------------------------------------
----------------------------------------------------
-- Init_Rule (Enumeration_Representation_Clauses) --
----------------------------------------------------
procedure Init_Rule
(Rule : in out Enumeration_Representation_Clauses_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Enumeration_Representation_Clauses");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("enumeration representation clauses");
Rule.Diagnosis := new String'("enumeration representation clause");
end Init_Rule;
------------------------------------------------------------
-- Rule_Check_Pre_Op (Enumeration_Representation_Clauses) --
------------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Enumeration_Representation_Clauses_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Representation_Clause_Kind (Element) =
An_Enumeration_Representation_Clause
then
State.Detected := True;
end if;
end Rule_Check_Pre_Op;
--------------------------
-- Expression_Functions --
--------------------------
--------------------------------------
-- Init_Rule (Expression_Functions) --
--------------------------------------
procedure Init_Rule (Rule : in out Expression_Functions_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Expression_Functions");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("expression function in package " &
"specifications");
Rule.Diagnosis := new String'("expression function");
end Init_Rule;
----------------------------------------------
-- Rule_Check_Pre_Op (Expression_Functions) --
----------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Expression_Functions_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
EE : Asis.Element;
Step_Up : Elmt_Idx := 0;
pragma Unreferenced (Rule, Control);
begin
if Declaration_Kind (Element) = An_Expression_Function_Declaration then
loop
EE := Get_Enclosing_Element (Step_Up);
exit when Is_Program_Unit (EE);
Step_Up := Step_Up + 1;
end loop;
State.Detected :=
Declaration_Kind (EE) in A_Package_Declaration |
A_Generic_Package_Declaration;
end if;
end Rule_Check_Pre_Op;
---------------------------
-- Fixed_Equality_Checks --
---------------------------
---------------------------------------
-- Init_Rule (Fixed_Equality_Checks) --
---------------------------------------
procedure Init_Rule (Rule : in out Fixed_Equality_Checks_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Fixed_Equality_Checks");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("equality for fixed values");
Rule.Diagnosis := new String'("use of equality operation " &
"for fixed values");
end Init_Rule;
-----------------------------------------------
-- Rule_Check_Pre_Op (Fixed_Equality_Checks) --
-----------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Fixed_Equality_Checks_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule);
pragma Unreferenced (Control);
Call_Element : Asis.Element;
begin
if Operator_Kind (Element) in
An_Equal_Operator .. A_Not_Equal_Operator
then
Call_Element := Get_Call_Element;
if Expression_Kind (Call_Element) = A_Function_Call
and then
Is_Predefined (Element)
and then
Is_Fixed
(Actual_Parameter (Function_Call_Parameters (Call_Element) (1)))
then
State.Detected := True;
end if;
end if;
end Rule_Check_Pre_Op;
------------------------
-- Nested_Subprograms --
------------------------
------------------------------------
-- Init_Rule (Nested_Subprograms) --
------------------------------------
procedure Init_Rule (Rule : in out Nested_Subprograms_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Nested_Subprograms");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("nested subprograms");
Rule.Diagnosis := new String'("subprogram declared in " &
"executable body");
end Init_Rule;
--------------------------------------------
-- Rule_Check_Pre_Op (Nested_Subprograms) --
--------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Nested_Subprograms_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule);
pragma Unreferenced (Control);
EE : Asis.Element;
begin
if (Declaration_Kind (Element) = A_Function_Declaration
and then
Definition_Kind (Enclosing_Element (Element)) /=
A_Protected_Definition)
or else
(Declaration_Kind (Element) = A_Procedure_Declaration
and then
Definition_Kind (Enclosing_Element (Element)) /=
A_Protected_Definition
and then
Declaration_Kind (Corresponding_Body (Element)) /=
A_Null_Procedure_Declaration)
or else
Declaration_Kind (Element) in
A_Procedure_Instantiation |
A_Function_Instantiation
or else
(Declaration_Kind (Element) in
A_Procedure_Body_Declaration |
A_Function_Body_Declaration |
An_Expression_Function_Declaration |
A_Procedure_Body_Stub |
A_Function_Body_Stub
and then
Acts_As_Spec (Element))
then
EE := Enclosing_Element (Element);
while not Is_Nil (EE) loop
if Declaration_Kind (EE) in
A_Procedure_Body_Declaration |
A_Function_Body_Declaration |
A_Task_Body_Declaration |
An_Entry_Body_Declaration
then
State.Detected := True;
exit;
end if;
if Is_Subunit (EE) then
EE := Corresponding_Body_Stub (EE);
else
EE := Enclosing_Element (EE);
end if;
end loop;
end if;
end Rule_Check_Pre_Op;
--------------------------------
-- No_Inherited_Classwide_Pre --
--------------------------------
--------------------------------------------
-- Init_Rule (No_Inherited_Classwide_Pre) --
--------------------------------------------
procedure Init_Rule (Rule : in out No_Inherited_Classwide_Pre_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("No_Inherited_Classwide_Pre");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("overridden operation has no Pre'Class");
Rule.Diagnosis := new String'("overriding operation that does not " &
"inherit Pre'Class (%1%)");
end Init_Rule;
----------------------------------------------------
-- Rule_Check_Pre_Op (No_Inherited_Classwide_Pre) --
----------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out No_Inherited_Classwide_Pre_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule);
pragma Unreferenced (Control);
LSP_Violation_Detected : Boolean;
LSP_Violation_SLOC : String_Loc;
begin
if Is_Dispatching_Operation (Element)
and then
not Is_Not_Overriding_Declaration (Element)
and then
(Is_Overriding_Declaration (Element)
or else
Is_Overriding_Operation (Element))
then
-- All the processing in the implementation of this rule is based on
-- direct tree traversing and analysis, so it is encapsulated in
-- an utility procedure
Check_Classwide_Pre_Vioaltion
(Element,
LSP_Violation_Detected,
LSP_Violation_SLOC);
if LSP_Violation_Detected then
State.Detected := True;
State.Diag_Params := LSP_Violation_SLOC;
end if;
end if;
end Rule_Check_Pre_Op;
----------------
-- Null_Paths --
----------------
----------------------------
-- Init_Rule (Null_Paths) --
----------------------------
procedure Init_Rule (Rule : in out Null_Paths_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Null_Paths");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("null paths in control statements");
Rule.Diagnosis := new String'("null path");
end Init_Rule;
------------------------------------
-- Rule_Check_Pre_Op (Null_Paths) --
------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Null_Paths_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule);
pragma Unreferenced (Control);
begin
if Statement_Kind (Element) in
A_Loop_Statement |
A_While_Loop_Statement |
A_For_Loop_Statement
or else
Path_Kind (Element) in
An_If_Path |
An_Elsif_Path |
An_Else_Path |
A_Case_Path
then
declare
Stmts : constant Asis.Element_List :=
(if Statement_Kind (Element) in
A_Loop_Statement |
A_While_Loop_Statement |
A_For_Loop_Statement
then
Loop_Statements (Element, Include_Pragmas => True)
else
Sequence_Of_Statements (Element, Include_Pragmas => True));
First_Stmt_Span : Span;
begin
State.Detected := True;
for J in Stmts'Range loop
if Statement_Kind (Stmts (J)) /= A_Null_Statement then
Reset_State (State);
exit;
end if;
end loop;
if State.Detected then
First_Stmt_Span := Element_Span (Stmts (Stmts'First));
State.Line := First_Stmt_Span.First_Line;
State.Column := First_Stmt_Span.First_Column;
end if;
end;
end if;
end Rule_Check_Pre_Op;
--------------------------------
-- Objects_Of_Anonymous_Types --
--------------------------------
--------------------------------------------
-- Init_Rule (Objects_Of_Anonymous_Types) --
--------------------------------------------
procedure Init_Rule
(Rule : in out Objects_Of_Anonymous_Types_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Objects_Of_Anonymous_Types");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("data objects of anonymous types");
Rule.Diagnosis := new String'("object of anonymous type");
end Init_Rule;
----------------------------------------------------
-- Rule_Check_Pre_Op (Objects_Of_Anonymous_Types) --
----------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Objects_Of_Anonymous_Types_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
Def : Asis.Element;
begin
if (Declaration_Kind (Element) in
A_Variable_Declaration | A_Constant_Declaration
and then
Declaration_Kind (Get_Enclosing_Element) in
A_Package_Declaration | A_Package_Body_Declaration)
or else
Declaration_Kind (Element) = A_Formal_Object_Declaration
then
Def := Object_Declaration_View (Element);
if Type_Kind (Def) = A_Constrained_Array_Definition
or else
Definition_Kind (Def) = An_Access_Definition
then
State.Detected := True;
end if;
end if;
end Rule_Check_Pre_Op;
------------------------------
-- POS_On_Enumeration_Types --
------------------------------
------------------------------------------
-- Init_Rule (POS_On_Enumeration_Types) --
------------------------------------------
procedure Init_Rule
(Rule : in out POS_On_Enumeration_Types_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("POS_On_Enumeration_Types");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("attribute 'POS applied to " &
"enumeration types");
Rule.Diagnosis := new String'("prefix of 'POS attribute has " &
"enumeration type");
end Init_Rule;
--------------------------------------------------
-- Rule_Check_Pre_Op (POS_On_Enumeration_Types) --
--------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out POS_On_Enumeration_Types_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
Pref : Asis.Element;
begin
if Attribute_Kind (Element) = A_Pos_Attribute then
Pref := Prefix (Element);
if Has_Enumeration_Type (Pref) then
State.Detected := True;
end if;
end if;
end Rule_Check_Pre_Op;
-----------------------------------
-- Representation_Specifications --
-----------------------------------
-----------------------------------------------
-- Init_Rule (Representation_Specifications) --
-----------------------------------------------
procedure Init_Rule
(Rule : in out Representation_Specifications_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Representation_Specifications");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("representation specification " &
"(clause, aspect or pragma)");
Rule.Diagnosis := new String'("representation specification");
end Init_Rule;
-------------------------------------------------------
-- Rule_Check_Pre_Op (Representation_Specifications) --
-------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Representation_Specifications_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
A_Mark : Asis.Element;
pragma Unreferenced (Rule, Control);
begin
if Clause_Kind (Element) = A_Representation_Clause then
State.Detected := True;
elsif Definition_Kind (Element) = An_Aspect_Specification then
A_Mark := Aspect_Mark (Element);
if Expression_Kind (A_Mark) = An_Identifier then
declare
A_Mark_Img : constant String :=
To_Lower (To_String (Name_Image (A_Mark)));
begin
-- The following aspects are checked as representation aspects
-- (see ARM 13.3):
--
-- Address
-- Alignment
-- Size
-- Component_Size
-- External_Tag
--
-- We have also flag aspects that corresponds to representation
-- pragmas:
-- Asynchronous J.15.13(1/3)
-- Atomic J.15.8(9/3)
-- Atomic_Components J.15.8(9/3)
-- Convention J.15.5(1/3)
-- Discard_Names C.5(6)
-- Export J.15.5(1/3)
-- Import J.15.5(1/3)
-- Independent J.15.8(9/3)
-- Independent_Components J.15.8(9/3)
-- No_Return J.15.2(1/3)
-- Pack J.15.3(1/3)
-- Unchecked_Union J.15.6(1/3)
-- Volatile J.15.8(9/3)
-- Volatile_Components J.15.8(9/3)
if A_Mark_Img = "address"
or else
A_Mark_Img = "alignment"
or else
A_Mark_Img = "size"
or else
A_Mark_Img = "component_size"
or else
A_Mark_Img = "external_tag"
or else
A_Mark_Img = "asynchronous"
or else
A_Mark_Img = "atomic"
or else
A_Mark_Img = "atomic_components"
or else
A_Mark_Img = "convention"
or else
A_Mark_Img = "import"
or else
A_Mark_Img = "discard_names"
or else
A_Mark_Img = "export"
or else
A_Mark_Img = "independent"
or else
A_Mark_Img = "independent_components"
or else
A_Mark_Img = "no_return"
or else
A_Mark_Img = "pack"
or else
A_Mark_Img = "unchecked_union"
or else
A_Mark_Img = "volatile"
or else
A_Mark_Img = "volatile_components"
then
State.Detected := True;
end if;
end;
end if;
elsif Element_Kind (Element) = A_Pragma then
declare
Pragme_Name_Img : constant String :=
To_Lower (To_String (Pragma_Name_Image (Element)));
begin
if Pragme_Name_Img = "asynchronous"
or else
Pragme_Name_Img = "atomic"
or else
Pragme_Name_Img = "atomic_components"
or else
Pragme_Name_Img = "convention"
or else
Pragme_Name_Img = "import"
or else
Pragme_Name_Img = "discard_names"
or else
Pragme_Name_Img = "export"
or else
Pragme_Name_Img = "independent"
or else
Pragme_Name_Img = "independent_components"
or else
Pragme_Name_Img = "no_return"
or else
Pragme_Name_Img = "pack"
or else
Pragme_Name_Img = "unchecked_union"
or else
Pragme_Name_Img = "volatile"
or else
Pragme_Name_Img = "volatile_components"
then
State.Detected := True;
end if;
end;
end if;
end Rule_Check_Pre_Op;
------------------------------------
-- Specific_Parent_Type_Invariant --
------------------------------------
function Has_Specific_Type_Invariant
(Type_Decl : Asis.Element)
return Boolean;
-- Checks if Type_Decl has a specification of Type_Invariant aspect.
-- Returns False for any unexpected element.
--
-- Expected Declaration_Kinds
-- A_Private_Type_Declaration
-- A_Private_Extension_Declaration
-- An_Ordinary_Type_Declaration
------------------------------------------------------------------
-- Has_Specific_Type_Invariant (Specific_Parent_Type_Invariant) --
------------------------------------------------------------------
function Has_Specific_Type_Invariant
(Type_Decl : Asis.Element)
return Boolean
is
Result : Boolean := False;
begin
if Declaration_Kind (Type_Decl) in
A_Private_Type_Declaration |
A_Private_Extension_Declaration |
An_Ordinary_Type_Declaration
then
declare
Asp_Defs : constant Asis.Element_List :=
Aspect_Specifications (Type_Decl);
Asp_Mark : Asis.Element;
begin
for J in Asp_Defs'Range loop
Asp_Mark := Aspect_Mark (Asp_Defs (J));
if Expression_Kind (Asp_Mark) = An_Identifier
and then
To_Lower (Name_Image (Asp_Mark)) = "type_invariant"
then
Result := True;
exit;
end if;
end loop;
end;
end if;
return Result;
end Has_Specific_Type_Invariant;
------------------------------------------------
-- Init_Rule (Specific_Parent_Type_Invariant) --
------------------------------------------------
procedure Init_Rule
(Rule : in out Specific_Parent_Type_Invariant_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Specific_Parent_Type_Invariant");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("derivation from a type with " &
"specific Type_Invariant aspect");
Rule.Diagnosis := new String'("parent type has specific " &
"Type_Invariant aspect (%1%)");
end Init_Rule;
--------------------------------------------------------
-- Rule_Check_Pre_Op (Specific_Parent_Type_Invariant) --
--------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Specific_Parent_Type_Invariant_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
Parent : Asis.Element;
begin
if Definition_Kind (Element) = A_Private_Extension_Definition then
Parent := Ancestor_Subtype_Indication (Element);
elsif Type_Kind (Element) = A_Derived_Record_Extension_Definition
and then
Declaration_Kind
(Corresponding_Type_Partial_View (Get_Enclosing_Element)) /=
A_Private_Extension_Declaration
then
Parent := Parent_Subtype_Indication (Element);
else
return;
end if;
Parent := Asis.Definitions.Subtype_Mark (Parent);
Parent := Normalize_Reference (Parent);
Parent := Corresponding_Name_Declaration (Parent);
Parent := Corresponding_First_Subtype (Parent);
if Has_Specific_Type_Invariant (Parent) then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" & Build_GNAT_Location (Parent));
else
if Declaration_Kind (Parent) in
A_Private_Type_Declaration |
A_Private_Extension_Declaration
then
Parent := Corresponding_Type_Completion (Parent);
elsif Declaration_Kind (Parent) = An_Ordinary_Type_Declaration then
Parent := Corresponding_Type_Partial_View (Parent);
else
return;
end if;
if Declaration_Kind (Parent) not in
An_Incomplete_Type_Declaration |
A_Tagged_Incomplete_Type_Declaration
and then
Has_Specific_Type_Invariant (Parent)
then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" & Build_GNAT_Location (Parent));
end if;
end if;
end Rule_Check_Pre_Op;
-----------------------
-- Specific_Pre_Post --
-----------------------
-----------------------------------
-- Init_Rule (Specific_Pre_Post) --
-----------------------------------
procedure Init_Rule (Rule : in out Specific_Pre_Post_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Specific_Pre_Post");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("definition of non class-wide Pre " &
"and Post aspects for tagged type " &
"primitives");
Rule.Diagnosis := new String'("#1#definition of non class-wide " &
"Pre aspect" &
"#2#definition of non class-wide " &
"Post aspect" &
"#3#definition of non class-wide " &
"Pre and Post aspects");
end Init_Rule;
-------------------------------------------
-- Rule_Check_Pre_Op (Specific_Pre_Post) --
-------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Specific_Pre_Post_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Is_Dispatching_Operation (Element) then
declare
Asp_Defs : constant Asis.Element_List :=
Aspect_Specifications (Element);
Pre_Detected : Boolean := False;
Post_Detected : Boolean := False;
Asp_Mark : Asis.Element;
begin
for J in Asp_Defs'Range loop
Asp_Mark := Aspect_Mark (Asp_Defs (J));
if Expression_Kind (Asp_Mark) = An_Identifier then
if To_Lower (Name_Image (Asp_Mark)) = "pre" then
Pre_Detected := True;
elsif To_Lower (Name_Image (Asp_Mark)) = "post" then
Post_Detected := True;
end if;
end if;
exit when Pre_Detected and then Post_Detected;
end loop;
if Pre_Detected or else Post_Detected then
State.Detected := True;
State.Diagnosis :=
(if Pre_Detected and then Post_Detected then 3
elsif Pre_Detected then 1
else 2);
end if;
end;
end if;
end Rule_Check_Pre_Op;
------------------------------------------
-- Init_Rule (Specific_Type_Invariants) --
------------------------------------------
procedure Init_Rule (Rule : in out Specific_Type_Invariants_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Specific_Type_Invariants");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("non class-wide Type_Invariant aspect");
Rule.Diagnosis := new String'("definition of non class-wide " &
"Type_Invariant aspect");
end Init_Rule;
--------------------------------------------------
-- Rule_Check_Pre_Op (Specific_Type_Invariants) --
--------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Specific_Type_Invariants_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
A_Mark : Asis.Element;
pragma Unreferenced (Rule, Control);
begin
if Definition_Kind (Element) = An_Aspect_Specification then
if Declaration_Kind (Get_Enclosing_Element) =
A_Private_Extension_Declaration
or else
(Declaration_Kind (Get_Enclosing_Element) =
A_Private_Type_Declaration
and then
Flat_Element_Kind (Type_Declaration_View
(Get_Enclosing_Element)) =
A_Tagged_Private_Type_Definition)
or else
(Declaration_Kind (Get_Enclosing_Element) =
An_Ordinary_Type_Declaration
and then
Flat_Element_Kind (Type_Declaration_View
(Get_Enclosing_Element)) in
A_Tagged_Record_Type_Definition |
A_Derived_Record_Extension_Definition)
then
A_Mark := Aspect_Mark (Element);
if Expression_Kind (A_Mark) = An_Identifier then
declare
A_Mark_Img : constant String :=
To_Lower (To_String (Name_Image (A_Mark)));
begin
State.Detected := A_Mark_Img = "type_invariant";
end;
end if;
end if;
end if;
end Rule_Check_Pre_Op;
-----------------------
-- Subprogram_Access --
-----------------------
-----------------------------------
-- Init_Rule (Subprogram_Access) --
-----------------------------------
procedure Init_Rule (Rule : in out Subprogram_Access_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Subprogram_Access");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("access to subprogram definition");
Rule.Diagnosis := new String'("access to subprogram definition");
end Init_Rule;
-------------------------------------------
-- Rule_Check_Pre_Op (Subprogram_Access) --
-------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Subprogram_Access_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Access_Type_Kind (Element) in Access_To_Subprogram_Definition
or else
Access_Definition_Kind (Element) in
An_Anonymous_Access_To_Procedure |
An_Anonymous_Access_To_Protected_Procedure |
An_Anonymous_Access_To_Function |
An_Anonymous_Access_To_Protected_Function
then
State.Detected := True;
end if;
end Rule_Check_Pre_Op;
---------------------------
-- Too_Many_Dependencies --
---------------------------
package Dependency_Sets is new
Ada.Containers.Indefinite_Ordered_Sets (Element_Type => Program_Text);
use Dependency_Sets;
Dependencies : Dependency_Sets.Set;
procedure Collect_Dependencies (CU : Asis.Compilation_Unit);
-- Collect all the dependencies for CU in a way as it is required by the
-- rule. That is, in case of a body, the dependencies on the corresponding
-- spec unit is not collected, but the dependencies introduced by the with
-- clauses applied to the spec are collected. In case of a subunit, the
-- dependencies on proper body (proper bodies in case of nested subunits)
-- and the corresponding spec (if any) are not collected, but the
-- dependencies introduced by the with clauses applied to the proper body
-- (bodies) and spec are collected.
procedure Store_Dependencies (CU : Asis.Compilation_Unit);
-- Stores in Dependencies the names of the rule (folded to lower case) the
-- unit under test depends upon it a way needed for this rule. That is, if
-- a child unit is stored that all its parent and all the grandparent
-- units are removed, if any
--------------------------------------------------
-- Collect_Dependencies (Too_Many_Dependencies) --
--------------------------------------------------
procedure Collect_Dependencies (CU : Asis.Compilation_Unit) is
Next_CU : Asis.Compilation_Unit;
begin
Store_Dependencies (CU);
case Unit_Kind (CU) is
when A_Library_Unit_Body =>
Next_CU := Corresponding_Declaration (CU);
if not Is_Nil (Next_CU) then
Collect_Dependencies (Next_CU);
end if;
when A_Subunit =>
Next_CU := Corresponding_Subunit_Parent_Body (CU);
Collect_Dependencies (Next_CU);
when others =>
null;
end case;
end Collect_Dependencies;
--------------------------------------------------
-- Init_Rule (Too_Many_Dependencies) --
--------------------------------------------------
procedure Init_Rule (Rule : in out Too_Many_Dependencies_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Too_Many_Dependencies");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("compilation unit has too many " &
"dependencies");
Rule.Diagnosis := new String'("unit has too many dependencies (%1%)");
end Init_Rule;
-----------------------------------------------
-- Rule_Check_Pre_Op (Too_Many_Dependencies) --
-----------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Too_Many_Dependencies_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
CU : Asis.Compilation_Unit;
N_Of_Dependencies : Natural;
pragma Unreferenced (Control);
begin
if Element_Kind (Element) = A_Declaration
and then
Is_Nil (Get_Enclosing_Element)
then
CU := Enclosing_Compilation_Unit (Element);
else
return;
end if;
Dependencies.Clear;
Collect_Dependencies (CU);
N_Of_Dependencies := Natural (Dependencies.Length);
if N_Of_Dependencies > Rule.Rule_Limit then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" & ASIS_UL.Misc.Image (N_Of_Dependencies));
end if;
end Rule_Check_Pre_Op;
------------------------------------------------
-- Store_Dependencies (Too_Many_Dependencies) --
------------------------------------------------
procedure Store_Dependencies (CU : Asis.Compilation_Unit) is
Cont_Clauses : constant Asis.Element_List :=
Context_Clause_Elements (CU);
Name_Img : Program_Text_Access;
First_Idx : Positive;
Dot_Idx : Natural;
Inserted : Boolean;
C : Cursor;
begin
for J in Cont_Clauses'Range loop
if Clause_Kind (Cont_Clauses (J)) = A_With_Clause then
declare
Nms : constant Asis.Element_List :=
Clause_Names (Cont_Clauses (J));
begin
for K in Nms'Range loop
Name_Img := new Program_Text'
(To_Lower (Full_Name_Image (Nms (K))));
-- Check if we have already collected some child of this
-- unit:
Inserted := False;
C := Dependencies.First;
while Has_Element (C) loop
if Index (Dependency_Sets.Element (C), Name_Img.all) > 0
then
Inserted := True;
exit;
end if;
C := Next (C);
end loop;
if Inserted then
goto Next_Iteration;
end if;
Insert (Container => Dependencies,
New_Item => Name_Img.all,
Position => C,
Inserted => Inserted);
if Inserted then
First_Idx := Name_Img'First;
Dot_Idx := Index (Name_Img.all, ".", Backward);
while Dot_Idx > 0 loop
C := Find
(Dependencies, Name_Img (First_Idx .. Dot_Idx - 1));
if Has_Element (C) then
Delete (Dependencies, C);
end if;
Dot_Idx := Index (Name_Img (First_Idx .. Dot_Idx - 1),
".",
Backward);
end loop;
end if;
<<Next_Iteration>> null;
end loop;
end;
end if;
end loop;
Free (Name_Img);
end Store_Dependencies;
-------------------------
-- Too_Many_Primitives --
-------------------------
-------------------------------------
-- Init_Rule (Too_Many_Primitives) --
-------------------------------------
procedure Init_Rule (Rule : in out Too_Many_Primitives_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Too_Many_Primitives");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("tagged type has too many primitives");
Rule.Diagnosis := new String'("tagged type has too many " &
"primitives (%1%)");
end Init_Rule;
---------------------------------------------
-- Rule_Check_Pre_Op (Too_Many_Primitives) --
---------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Too_Many_Primitives_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Arg_Kind : constant Declaration_Kinds := Declaration_Kind (Element);
Type_Def : Asis.Element;
EE : Asis.Element;
Num_Of_Primitives : Natural := 0;
pragma Unreferenced (Control);
begin
if Arg_Kind in
An_Ordinary_Type_Declaration |
A_Private_Type_Declaration |
A_Private_Extension_Declaration
then
Type_Def := Type_Declaration_View (Element);
-- First, check if we have a tagged type
if Arg_Kind = A_Private_Type_Declaration
and then
Definition_Kind (Type_Def) /= A_Tagged_Private_Type_Definition
then
return;
end if;
if Arg_Kind = An_Ordinary_Type_Declaration
and then
Type_Kind (Type_Def) not in
A_Derived_Record_Extension_Definition |
A_Tagged_Record_Type_Definition |
An_Interface_Type_Definition
then
return;
end if;
-- Then, skip full views of private types.
if Arg_Kind = An_Ordinary_Type_Declaration
and then
Declaration_Kind (Corresponding_Type_Partial_View (Element)) in
A_Private_Type_Declaration |
A_Private_Extension_Declaration
then
return;
end if;
if Arg_Kind = A_Private_Extension_Declaration
or else
Type_Kind (Type_Def) in
A_Derived_Record_Extension_Definition |
An_Interface_Type_Definition
then
Num_Of_Primitives :=
Implicit_Inherited_Subprograms (Type_Def)'Length;
end if;
-- Now, counting explicit primitives
EE := Get_Enclosing_Element;
if Flat_Element_Kind (EE) in
A_Procedure_Body_Declaration |
A_Function_Body_Declaration |
A_Package_Body_Declaration |
A_Task_Body_Declaration |
An_Entry_Body_Declaration |
A_Block_Statement
then
-- New explicit primitives may be added to a type in
-- a package spec only, here we can only override an existing
-- primitive
return;
end if;
declare
Dcls : constant Asis.Element_List :=
(case Flat_Element_Kind (EE) is
when A_Package_Declaration |
A_Generic_Package_Declaration =>
Visible_Part_Declarative_Items (EE),
when others => Nil_Element_List);
Idx : Natural := 0;
Last_Idx : constant Natural := Dcls'Last;
begin
if Is_Nil (Dcls) then
-- A visible part may be empty
return;
end if;
for J in Dcls'Range loop
if Is_Equal (Element, Dcls (J)) then
Idx := J;
exit;
end if;
end loop;
if Idx = 0 then
-- Type is declared in a private part
return;
end if;
for J in Idx + 1 .. Last_Idx loop
if Is_Dispatching_Operation (Dcls (J))
and then
Is_Equal (Primitive_Owner (Dcls (J)), Type_Def)
then
Num_Of_Primitives := Num_Of_Primitives + 1;
end if;
end loop;
end;
if Num_Of_Primitives > Rule.Rule_Limit then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" & ASIS_UL.Misc.Image (Num_Of_Primitives));
end if;
end if;
end Rule_Check_Pre_Op;
------------------------------------
-- Unchecked_Address_Conversions --
------------------------------------
------------------------------------------------
-- Init_Rule (Unchecked_Address_Conversions) --
------------------------------------------------
procedure Init_Rule
(Rule : in out Unchecked_Address_Conversions_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Unchecked_Address_Conversions");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("unchecked conversions from " &
"System.Address into access type");
Rule.Diagnosis := new String'("address-to-access conversion");
Rule.Check_In_Expanded_Generics := True;
end Init_Rule;
-------------------------------------------------------
-- Rule_Check_Pre_Op (Unchecked_Address_Conversions) --
-------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Unchecked_Address_Conversions_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
Is_UC_Instance : Boolean := False;
Is_Address_Source : Boolean := False;
Is_Access_Target : Boolean := False;
Tmp : Asis.Element;
Encl_CU : Asis.Compilation_Unit;
Act_Pars : Asis.Element_List (1 .. 2);
Reverse_Order : Boolean := False;
-- Set to True in case if the instantiation of UC uses named
-- associations and they are in reverse order:
--
-- ... is new Unchecked_Conversion (Target => T1, Source => T2)
--
-- very unlikely but possible.
begin
-- Check that we have an instantiation of Unchecked_Conversion
if Declaration_Kind (Element) = A_Function_Instantiation then
Tmp := Generic_Unit_Name (Element);
if Expression_Kind (Tmp) = A_Selected_Component then
Tmp := Selector (Tmp);
end if;
Tmp := Corresponding_Name_Declaration (Tmp);
if Declaration_Kind (Tmp) =
A_Generic_Function_Renaming_Declaration
then
Tmp := Corresponding_Base_Entity (Tmp);
if Expression_Kind (Tmp) = A_Selected_Component then
Tmp := Selector (Tmp);
end if;
if Expression_Kind (Tmp) = An_Identifier then
Tmp := Corresponding_Name_Declaration (Tmp);
end if;
end if;
Encl_CU := Enclosing_Compilation_Unit (Tmp);
if Unit_Origin (Encl_CU) = A_Predefined_Unit
and then
(To_Lower (Unit_Full_Name (Encl_CU)) =
"ada.unchecked_conversion"
or else
-- GNAT does not literally follow section J.1 of ARM 12
To_Lower (Unit_Full_Name (Encl_CU)) =
"unchecked_conversion")
then
Is_UC_Instance := True;
else
goto Fin;
end if;
else
return;
end if;
Act_Pars := Generic_Actual_Part (Element);
Tmp := Formal_Parameter (Act_Pars (1));
if not Is_Nil (Tmp)
and then
To_Lower (Name_Image (Tmp)) = "target"
then
Reverse_Order := True;
Tmp := Actual_Parameter (Act_Pars (2));
else
Tmp := Actual_Parameter (Act_Pars (1));
end if;
-- Check that the actual for Source is System.Address
Tmp := Get_Underlying_Type (Tmp);
Encl_CU := Enclosing_Compilation_Unit (Tmp);
if Unit_Origin (Encl_CU) = A_Predefined_Unit
and then
To_Lower (Unit_Full_Name (Encl_CU)) = "system"
then
Tmp := First_Name (Tmp);
if To_Lower (Defining_Name_Image (Tmp)) = "address" then
Is_Address_Source := True;
else
goto Fin;
end if;
else
goto Fin;
end if;
-- Check that the actual for Target is an access type
if Reverse_Order then
Tmp := Actual_Parameter (Act_Pars (1));
else
Tmp := Actual_Parameter (Act_Pars (2));
end if;
Tmp := Get_Underlying_Type (Tmp);
case Declaration_Kind (Tmp) is
when A_Formal_Type_Declaration =>
Is_Access_Target :=
Formal_Type_Kind (Type_Declaration_View (Tmp)) =
A_Formal_Access_Type_Definition;
when An_Ordinary_Type_Declaration =>
Is_Access_Target :=
Type_Kind (Type_Declaration_View (Tmp)) =
An_Access_Type_Definition;
when others =>
null;
end case;
<<Fin>> State.Detected :=
Is_UC_Instance and then Is_Address_Source and then Is_Access_Target;
end Rule_Check_Pre_Op;
--------------------------------------
-- Unchecked_Conversions_As_Actuals --
--------------------------------------
--------------------------------------------------
-- Init_Rule (Unchecked_Conversions_As_Actuals) --
--------------------------------------------------
procedure Init_Rule
(Rule : in out Unchecked_Conversions_As_Actuals_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Unchecked_Conversions_As_Actuals");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("passing instance of " &
"Unchecked_Conversion as actual");
Rule.Diagnosis := new String'("#1#instance of Unchecked_Conversion " &
" as actual parameter" &
"#2#instance of Unchecked_Conversion " &
" as default parameter value");
end Init_Rule;
----------------------------------------------------------
-- Rule_Check_Pre_Op (Unchecked_Conversions_As_Actuals) --
----------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Unchecked_Conversions_As_Actuals_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
Called_Fun : Asis.Element;
Encl : Asis.Element;
Template : Asis.Element;
Template_CU : Asis.Compilation_Unit;
begin
if Expression_Kind (Element) = A_Function_Call then
Encl := Get_Enclosing_Element;
if Association_Kind (Encl) = A_Parameter_Association
or else
Declaration_Kind (Encl) = A_Parameter_Specification
then
Called_Fun := Corresponding_Called_Function (Element);
if Declaration_Kind (Called_Fun) =
A_Function_Renaming_Declaration
then
Called_Fun := Corresponding_Base_Entity (Called_Fun);
if Expression_Kind (Called_Fun) = A_Selected_Component then
Called_Fun := Selector (Called_Fun);
end if;
if Expression_Kind (Called_Fun) = An_Identifier then
Called_Fun := Corresponding_Name_Declaration (Called_Fun);
end if;
end if;
if Declaration_Kind (Called_Fun) = A_Function_Instantiation then
Template := Generic_Unit_Name (Called_Fun);
if Expression_Kind (Template) = A_Selected_Component then
Template := Selector (Template);
end if;
Template := Corresponding_Name_Declaration (Template);
if Declaration_Kind (Template) =
A_Generic_Function_Renaming_Declaration
then
Template := Corresponding_Base_Entity (Template);
if Expression_Kind (Template) = A_Selected_Component then
Template := Selector (Template);
end if;
if Expression_Kind (Template) = An_Identifier then
Template := Corresponding_Name_Declaration (Template);
end if;
end if;
Template_CU := Enclosing_Compilation_Unit (Template);
if Unit_Origin (Template_CU) = A_Predefined_Unit
and then
(To_Lower (Unit_Full_Name (Template_CU)) =
"ada.unchecked_conversion"
or else
-- GNAT does not literally follow section J.1 of ARM 12
To_Lower (Unit_Full_Name (Template_CU)) =
"unchecked_conversion")
then
State.Detected := True;
State.Diagnosis :=
(if Association_Kind (Encl) = A_Parameter_Association then
1
else
2);
end if;
end if;
end if;
end if;
end Rule_Check_Pre_Op;
------------------------------------
-- Uninitialized_Global_Variables --
------------------------------------
------------------------------------------------
-- Init_Rule (Uninitialized_Global_Variables) --
------------------------------------------------
procedure Init_Rule
(Rule : in out Uninitialized_Global_Variables_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Uninitialized_Global_Variables");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("uninitialized variables in packages");
Rule.Diagnosis := new String'("uninitialized global variable");
end Init_Rule;
--------------------------------------------------------
-- Rule_Check_Pre_Op (Uninitialized_Global_Variables) --
--------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Uninitialized_Global_Variables_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
SM : Asis.Element;
begin
if Declaration_Kind (Element) = A_Variable_Declaration
and then
Is_Nil (Initialization_Expression (Element))
and then
Declaration_Kind (Get_Enclosing_Element) in
A_Package_Declaration |
A_Package_Body_Declaration |
A_Generic_Package_Declaration
then
SM := Object_Declaration_View (Element);
if Definition_Kind (SM) = A_Type_Definition then
-- Variable declared with constrained array definition
State.Detected := True;
elsif Definition_Kind (SM) = A_Subtype_Indication then
SM := Asis.Definitions.Subtype_Mark (SM);
if not Is_Limited (SM) then
State.Detected := True;
end if;
else
State.Detected := True;
end if;
end if;
end Rule_Check_Pre_Op;
end Gnatcheck.Rules.Custom_3;
------------------------------------------------------------------------------
|